This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 660
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rome_js_analyze): useDefaultSwitchClauseLast
- Loading branch information
Showing
13 changed files
with
550 additions
and
2 deletions.
There are no files selected for viewing
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.
110 changes: 110 additions & 0 deletions
110
crates/rome_js_analyze/src/analyzers/nursery/use_default_switch_clause_last.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,110 @@ | ||
use rome_analyze::context::RuleContext; | ||
use rome_analyze::{declare_rule, Ast, Rule, RuleDiagnostic}; | ||
use rome_console::markup; | ||
use rome_js_syntax::{JsCaseClause, JsDefaultClause}; | ||
use rome_rowan::{AstNode, Direction}; | ||
|
||
declare_rule! { | ||
/// Enforce default clauses in switch statements to be last | ||
/// | ||
/// A switch statement can optionally have a default clause. | ||
/// | ||
/// If present, it’s usually the last clause, but it doesn’t need to be. It is also allowed to put the default clause before all case clauses, or anywhere between. The behavior is mostly the same as if it was the last clause. The default block will be still executed only if there is no match in the case clauses (including those defined after the default), but there is also the ability to “fall through” from the default clause to the following clause in the list. However, such flow is not common and it would be confusing to the readers. | ||
/// | ||
/// Even if there is no "fall through" logic, it’s still unexpected to see the default clause before or between the case clauses. By convention, it is expected to be the last clause. | ||
/// | ||
/// Source: https://eslint.org/docs/latest/rules/default-case-last | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// switch (foo) { | ||
/// default: | ||
/// break; | ||
/// case 0: | ||
/// break; | ||
/// } | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// switch (foo) { | ||
/// default: | ||
/// f(); | ||
/// case 0: | ||
/// break; | ||
/// } | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// switch (foo) { | ||
/// case 0: | ||
/// break; | ||
/// default: | ||
/// case 1: | ||
/// break; | ||
/// } | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```js | ||
/// switch (foo) { | ||
/// case 0: | ||
/// break; | ||
/// case 1: | ||
/// default: | ||
/// break; | ||
/// } | ||
/// ``` | ||
/// | ||
/// ```js | ||
/// switch (foo) { | ||
/// case 0: | ||
/// break; | ||
/// } | ||
/// ``` | ||
/// | ||
pub(crate) UseDefaultSwitchClauseLast { | ||
version: "11.0.0", | ||
name: "useDefaultSwitchClauseLast", | ||
recommended: false, | ||
} | ||
} | ||
|
||
impl Rule for UseDefaultSwitchClauseLast { | ||
type Query = Ast<JsDefaultClause>; | ||
type State = JsCaseClause; | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let default_clause = ctx.query(); | ||
let next_case = default_clause | ||
.syntax() | ||
.siblings(Direction::Next) | ||
.find_map(JsCaseClause::cast); | ||
next_case | ||
} | ||
|
||
fn diagnostic(ctx: &RuleContext<Self>, next_case: &Self::State) -> Option<RuleDiagnostic> { | ||
let default_clause = ctx.query(); | ||
Some(RuleDiagnostic::new( | ||
rule_category!(), | ||
default_clause.range(), | ||
markup! { | ||
"The "<Emphasis>"default"</Emphasis>" clause should be the last "<Emphasis>"switch"</Emphasis>" clause." | ||
}, | ||
).detail( | ||
next_case.range(), | ||
markup! { | ||
"The following "<Emphasis>"case"</Emphasis>" clause is here:" | ||
}, | ||
).note( | ||
markup! { | ||
"Regardless its position, the "<Emphasis>"default"</Emphasis>" clause is always executed when there is no match. To avoid confusion, the "<Emphasis>"default"</Emphasis>" clause should be the last "<Emphasis>"switch"</Emphasis>" clause." | ||
} | ||
)) | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
crates/rome_js_analyze/tests/specs/nursery/useDefaultSwitchClauseLast/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,30 @@ | ||
switch (foo) { | ||
default: | ||
break; | ||
case 0: | ||
break; | ||
} | ||
|
||
switch (foo) { | ||
case 0: | ||
break; | ||
default: | ||
break; | ||
case 1: | ||
break; | ||
} | ||
|
||
switch (foo) { | ||
default: | ||
f(); | ||
case 0: | ||
break; | ||
} | ||
|
||
switch (foo) { | ||
case 0: | ||
break; | ||
default: | ||
case 1: | ||
break; | ||
} |
156 changes: 156 additions & 0 deletions
156
crates/rome_js_analyze/tests/specs/nursery/useDefaultSwitchClauseLast/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,156 @@ | ||
--- | ||
source: crates/rome_js_analyze/tests/spec_tests.rs | ||
assertion_line: 73 | ||
expression: invalid.js | ||
--- | ||
# Input | ||
```js | ||
switch (foo) { | ||
default: | ||
break; | ||
case 0: | ||
break; | ||
} | ||
|
||
switch (foo) { | ||
case 0: | ||
break; | ||
default: | ||
break; | ||
case 1: | ||
break; | ||
} | ||
|
||
switch (foo) { | ||
default: | ||
f(); | ||
case 0: | ||
break; | ||
} | ||
|
||
switch (foo) { | ||
case 0: | ||
break; | ||
default: | ||
case 1: | ||
break; | ||
} | ||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid.js:2:2 lint/nursery/useDefaultSwitchClauseLast ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! The default clause should be the last switch clause. | ||
1 │ switch (foo) { | ||
> 2 │ default: | ||
│ ^^^^^^^^ | ||
> 3 │ break; | ||
│ ^^^^^^ | ||
4 │ case 0: | ||
5 │ break; | ||
i The following case clause is here: | ||
2 │ default: | ||
3 │ break; | ||
> 4 │ case 0: | ||
│ ^^^^^^^ | ||
> 5 │ break; | ||
│ ^^^^^^ | ||
6 │ } | ||
7 │ | ||
i Regardless its position, the default clause is always executed when there is no match. To avoid confusion, the default clause should be the last switch clause. | ||
``` | ||
|
||
``` | ||
invalid.js:11:2 lint/nursery/useDefaultSwitchClauseLast ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! The default clause should be the last switch clause. | ||
9 │ case 0: | ||
10 │ break; | ||
> 11 │ default: | ||
│ ^^^^^^^^ | ||
> 12 │ break; | ||
│ ^^^^^^ | ||
13 │ case 1: | ||
14 │ break; | ||
i The following case clause is here: | ||
11 │ default: | ||
12 │ break; | ||
> 13 │ case 1: | ||
│ ^^^^^^^ | ||
> 14 │ break; | ||
│ ^^^^^^ | ||
15 │ } | ||
16 │ | ||
i Regardless its position, the default clause is always executed when there is no match. To avoid confusion, the default clause should be the last switch clause. | ||
``` | ||
|
||
``` | ||
invalid.js:18:2 lint/nursery/useDefaultSwitchClauseLast ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! The default clause should be the last switch clause. | ||
17 │ switch (foo) { | ||
> 18 │ default: | ||
│ ^^^^^^^^ | ||
> 19 │ f(); | ||
│ ^^^^ | ||
20 │ case 0: | ||
21 │ break; | ||
i The following case clause is here: | ||
18 │ default: | ||
19 │ f(); | ||
> 20 │ case 0: | ||
│ ^^^^^^^ | ||
> 21 │ break; | ||
│ ^^^^^^ | ||
22 │ } | ||
23 │ | ||
i Regardless its position, the default clause is always executed when there is no match. To avoid confusion, the default clause should be the last switch clause. | ||
``` | ||
|
||
``` | ||
invalid.js:27:2 lint/nursery/useDefaultSwitchClauseLast ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! The default clause should be the last switch clause. | ||
25 │ case 0: | ||
26 │ break; | ||
> 27 │ default: | ||
│ ^^^^^^^^ | ||
28 │ case 1: | ||
29 │ break; | ||
i The following case clause is here: | ||
26 │ break; | ||
27 │ default: | ||
> 28 │ case 1: | ||
│ ^^^^^^^ | ||
> 29 │ break; | ||
│ ^^^^^^ | ||
30 │ } | ||
i Regardless its position, the default clause is always executed when there is no match. To avoid confusion, the default clause should be the last switch clause. | ||
``` | ||
|
||
|
28 changes: 28 additions & 0 deletions
28
crates/rome_js_analyze/tests/specs/nursery/useDefaultSwitchClauseLast/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,28 @@ | ||
switch (foo) { | ||
case 0: | ||
break; | ||
case 1: | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
switch (foo) { | ||
case 0: | ||
break; | ||
case 1: | ||
default: | ||
break; | ||
} | ||
|
||
switch (foo) { | ||
default: | ||
break; | ||
} | ||
|
||
switch (foo) {} | ||
|
||
switch (foo) { | ||
case 0: | ||
break; | ||
} |
38 changes: 38 additions & 0 deletions
38
crates/rome_js_analyze/tests/specs/nursery/useDefaultSwitchClauseLast/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,38 @@ | ||
--- | ||
source: crates/rome_js_analyze/tests/spec_tests.rs | ||
assertion_line: 73 | ||
expression: valid.js | ||
--- | ||
# Input | ||
```js | ||
switch (foo) { | ||
case 0: | ||
break; | ||
case 1: | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
switch (foo) { | ||
case 0: | ||
break; | ||
case 1: | ||
default: | ||
break; | ||
} | ||
|
||
switch (foo) { | ||
default: | ||
break; | ||
} | ||
|
||
switch (foo) {} | ||
|
||
switch (foo) { | ||
case 0: | ||
break; | ||
} | ||
``` | ||
|
||
|
Oops, something went wrong.