Skip to content

Commit

Permalink
feat(lint/useDefaultCase): add rule notes
Browse files Browse the repository at this point in the history
  • Loading branch information
michellocana committed Apr 25, 2024
1 parent d142481 commit c0242a6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
28 changes: 20 additions & 8 deletions crates/biome_js_analyze/src/lint/nursery/use_default_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,7 @@ impl Rule for UseDefaultCase {

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let node = ctx.query();
let options = ctx.options();
let comment_pattern = options
.comment_pattern
.clone()
.unwrap_or(String::from("^no default$"))
.replace("\\\\", "\\");
let comment_pattern = get_comment_pattern(ctx);

let has_case_clauses = node.cases().into_iter().len() > 0;
let is_missing_default_case = node
Expand Down Expand Up @@ -145,17 +140,34 @@ impl Rule for UseDefaultCase {

fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> {
let node = ctx.query();
let comment_pattern = get_comment_pattern(ctx);

Some(RuleDiagnostic::new(
Some(
RuleDiagnostic::new(
rule_category!(),
node.range(),
markup! {
"Expected a default case."
},
))
)
.note(markup! {
"The lack of a default clause can result in unexpected behaviors in your code."
}).note(markup! {
"Consider adding a default clause or a comment explicitly stating that the default clause is not required, following the regex /" {comment_pattern} "/"
}),
)
}
}

fn get_comment_pattern(ctx: &RuleContext<UseDefaultCase>) -> String {
return ctx
.options()
.comment_pattern
.clone()
.unwrap_or(String::from("^no default$"))
.replace("\\\\", "\\");
}

fn has_disable_comment(trivia: Option<SyntaxTrivia<JsLanguage>>, comment_pattern: &str) -> bool {
if trivia.is_some() {
let comment_regex = Regex::new(comment_pattern).unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ invalid.js:1:1 lint/nursery/useDefaultCase ━━━━━━━━━━━━
5 │
6 │ switch (a) {
i The lack of a default clause can result in unexpected behaviors in your code.
i Consider adding a default clause or a comment explicitly stating that the default clause is not required, following the regex /^no default$/
```

Expand All @@ -59,6 +63,10 @@ invalid.js:6:1 lint/nursery/useDefaultCase ━━━━━━━━━━━━
11 │
12 │ switch (a) {
i The lack of a default clause can result in unexpected behaviors in your code.
i Consider adding a default clause or a comment explicitly stating that the default clause is not required, following the regex /^no default$/
```

Expand All @@ -79,5 +87,9 @@ invalid.js:12:1 lint/nursery/useDefaultCase ━━━━━━━━━━━━
│ ^
18 │
i The lack of a default clause can result in unexpected behaviors in your code.
i Consider adding a default clause or a comment explicitly stating that the default clause is not required, following the regex /^no default$/
```
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ invalidCommentPattern.js:1:1 lint/nursery/useDefaultCase ━━━━━━━
5 │
6 │ switch (a) {
i The lack of a default clause can result in unexpected behaviors in your code.
i Consider adding a default clause or a comment explicitly stating that the default clause is not required, following the regex /skip default(\s\d)*/
```

Expand All @@ -71,6 +75,10 @@ invalidCommentPattern.js:6:1 lint/nursery/useDefaultCase ━━━━━━━
11 │
12 │ switch (a) {
i The lack of a default clause can result in unexpected behaviors in your code.
i Consider adding a default clause or a comment explicitly stating that the default clause is not required, following the regex /skip default(\s\d)*/
```

Expand All @@ -92,6 +100,10 @@ invalidCommentPattern.js:12:1 lint/nursery/useDefaultCase ━━━━━━━
18 │
19 │ switch (a) {
i The lack of a default clause can result in unexpected behaviors in your code.
i Consider adding a default clause or a comment explicitly stating that the default clause is not required, following the regex /skip default(\s\d)*/
```

Expand All @@ -112,6 +124,10 @@ invalidCommentPattern.js:19:1 lint/nursery/useDefaultCase ━━━━━━━
24 │
25 │ switch (a) {
i The lack of a default clause can result in unexpected behaviors in your code.
i Consider adding a default clause or a comment explicitly stating that the default clause is not required, following the regex /skip default(\s\d)*/
```

Expand All @@ -131,5 +147,9 @@ invalidCommentPattern.js:25:1 lint/nursery/useDefaultCase ━━━━━━━
│ ^
30 │
i The lack of a default clause can result in unexpected behaviors in your code.
i Consider adding a default clause or a comment explicitly stating that the default clause is not required, following the regex /skip default(\s\d)*/
```

0 comments on commit c0242a6

Please sign in to comment.