Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
feat(rome_js_analyze): useDefaultSwitchClauseLast
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos committed Nov 23, 2022
1 parent 9e758af commit 12d4488
Show file tree
Hide file tree
Showing 13 changed files with 550 additions and 2 deletions.
1 change: 1 addition & 0 deletions crates/rome_diagnostics_categories/src/categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ define_dategories! {
"lint/nursery/noVoidTypeReturn": "https://docs.rome.tools/lint/rules/noVoidTypeReturn",
"lint/nursery/useCamelCase": "https://docs.rome.tools/lint/rules/useCamelCase",
"lint/nursery/useConst":"https://docs.rome.tools/lint/rules/useConst",
"lint/nursery/useDefaultSwitchClauseLast":"https://docs.rome.tools/lint/rules/useDefaultSwitchClauseLast",
"lint/nursery/useExhaustiveDependencies": "https://docs.rome.tools/lint/rules/useExhaustiveDependencies",
"lint/nursery/useFlatMap": "https://docs.rome.tools/lint/rules/useFlatMap",
"lint/nursery/useNumericLiterals": "https://docs.rome.tools/lint/rules/useNumericLiterals",
Expand Down
3 changes: 2 additions & 1 deletion crates/rome_js_analyze/src/analyzers/nursery.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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."
}
))
}
}
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;
}
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.
```


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;
}
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;
}
```


Loading

0 comments on commit 12d4488

Please sign in to comment.