-
-
Notifications
You must be signed in to change notification settings - Fork 421
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lint/useDefaultSwitchClause): add rule (#2605)
- Loading branch information
1 parent
b3ed181
commit de063b4
Showing
12 changed files
with
242 additions
and
7 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
crates/biome_cli/src/execute/migrate/eslint_any_rule_to_biome.rs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
85 changes: 85 additions & 0 deletions
85
crates/biome_js_analyze/src/lint/nursery/use_default_switch_clause.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use std::ops::Not; | ||
|
||
use biome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic, RuleSource}; | ||
use biome_console::markup; | ||
use biome_js_syntax::JsSwitchStatement; | ||
use biome_rowan::AstNode; | ||
|
||
declare_rule! { | ||
/// Require the default clause in switch statements. | ||
/// | ||
/// Some code conventions require that all switch statements have a default clause. The thinking is that it’s better | ||
/// to always explicitly state what the default behavior should be so that it’s clear whether or not the developer | ||
/// forgot to include the default behavior by mistake. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// switch (a) { | ||
/// case 1: | ||
/// /* code */ | ||
/// break; | ||
/// } | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```js | ||
/// switch (a) { | ||
/// case 1: | ||
/// /* code */ | ||
/// break; | ||
/// | ||
/// default: | ||
/// /* code */ | ||
/// break; | ||
/// } | ||
/// ``` | ||
pub UseDefaultSwitchClause { | ||
version: "next", | ||
name: "useDefaultSwitchClause", | ||
sources: &[RuleSource::Eslint("default-case")], | ||
recommended: false, | ||
} | ||
} | ||
|
||
impl Rule for UseDefaultSwitchClause { | ||
type Query = Ast<JsSwitchStatement>; | ||
type State = (); | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let node = ctx.query(); | ||
|
||
let is_missing_default_case = node | ||
.cases() | ||
.into_iter() | ||
.any(|clause| clause.as_js_default_clause().is_some()) | ||
.not(); | ||
|
||
is_missing_default_case.then_some(()) | ||
} | ||
|
||
fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> { | ||
let node = ctx.query(); | ||
|
||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
node.range(), | ||
markup! { | ||
"Expected a default switch clause." | ||
}, | ||
) | ||
.note(markup! { | ||
"The lack of a default clause can be a possible omission." | ||
}) | ||
.note(markup! { | ||
"Consider adding a default clause." | ||
}), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
crates/biome_js_analyze/tests/specs/nursery/useDefaultSwitchClause/invalid.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
switch (a) { | ||
case 1: | ||
break; | ||
} | ||
|
||
switch (a) { | ||
} |
57 changes: 57 additions & 0 deletions
57
crates/biome_js_analyze/tests/specs/nursery/useDefaultSwitchClause/invalid.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: invalid.js | ||
--- | ||
# Input | ||
```jsx | ||
switch (a) { | ||
case 1: | ||
break; | ||
} | ||
|
||
switch (a) { | ||
} | ||
|
||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid.js:1:1 lint/nursery/useDefaultSwitchClause ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Expected a default switch clause. | ||
> 1 │ switch (a) { | ||
│ ^^^^^^^^^^^^ | ||
> 2 │ case 1: | ||
> 3 │ break; | ||
> 4 │ } | ||
│ ^ | ||
5 │ | ||
6 │ switch (a) { | ||
i The lack of a default clause can be a possible omission. | ||
i Consider adding a default clause. | ||
``` | ||
|
||
``` | ||
invalid.js:6:1 lint/nursery/useDefaultSwitchClause ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Expected a default switch clause. | ||
4 │ } | ||
5 │ | ||
> 6 │ switch (a) { | ||
│ ^^^^^^^^^^^^ | ||
> 7 │ } | ||
│ ^ | ||
8 │ | ||
i The lack of a default clause can be a possible omission. | ||
i Consider adding a default clause. | ||
``` |
14 changes: 14 additions & 0 deletions
14
crates/biome_js_analyze/tests/specs/nursery/useDefaultSwitchClause/valid.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
switch (a) { | ||
case 1: | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
switch (a) { | ||
case 1: | ||
break; | ||
case 2: | ||
default: | ||
break; | ||
} |
22 changes: 22 additions & 0 deletions
22
crates/biome_js_analyze/tests/specs/nursery/useDefaultSwitchClause/valid.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: valid.js | ||
--- | ||
# Input | ||
```jsx | ||
switch (a) { | ||
case 1: | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
switch (a) { | ||
case 1: | ||
break; | ||
case 2: | ||
default: | ||
break; | ||
} | ||
|
||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.