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

feat(rome_js_analyze): noFallthroughSwitchClause #4626

Merged
merged 6 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -105,6 +105,7 @@ define_categories! {
"lint/nursery/noVoid": "https://docs.rome.tools/lint/rules/noVoid",
"lint/nursery/noNonoctalDecimalEscape": "https://docs.rome.tools/lint/rules/noNonoctalDecimalEscape",
"lint/nursery/noExcessiveComplexity": "https://docs.rome.tools/lint/rules/noExcessiveComplexity",
"lint/nursery/noFallthroughSwitchClause": "https://docs.rome.tools/lint/rules/noFallthroughSwitchClause",
// Insert new nursery rule here


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,107 @@
use rome_rowan::AstNode;

use rome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic};
use rome_console::markup;
use rome_js_syntax::{
AnyJsSwitchClause, JsBreakStatement, JsContinueStatement, JsReturnStatement, JsSwitchStatement,
JsThrowStatement,
};

declare_rule! {
/// Disallow fallthrough of case statements.
///
/// Case statements in switch statements fall through by default. This can lead to unexpected behavior when forgotten.
/// This rule disallows the fallthrough of case statements.
///
/// Source: https://eslint.org/docs/latest/rules/no-fallthrough
///
/// ## Examples
///
/// ### Invalid
///
/// ```js,expect_diagnostic
/// switch(bar) {
/// case 0:
/// a();
/// case 1:
/// b()
/// }
/// ```
///
/// ## Valid
///
/// ```js
/// switch(foo) {
/// case 1:
/// doSomething();
/// break;
/// case 2:
/// doSomething();
/// }
/// ```
///
pub(crate) NoFallthroughSwitchClause {
version: "12.0.0",
name: "noFallthroughSwitchClause",
recommended: false,
}
}

impl Rule for NoFallthroughSwitchClause {
type Query = Ast<JsSwitchStatement>;
type State = AnyJsSwitchClause;
type Signals = Vec<Self::State>;
type Options = ();

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let query = ctx.query();
let mut signals: Self::Signals = Vec::new();
let mut cases = query.cases().into_iter().peekable();

while let Some(any_case) = cases.next() {
let is_last = cases.peek().is_none();

if is_last {
continue;
}

if case_fell(&any_case) {
signals.push(any_case);
}
}

signals
}

fn diagnostic(_: &RuleContext<Self>, reference: &Self::State) -> Option<RuleDiagnostic> {
Some(
RuleDiagnostic::new(
rule_category!(),
reference.syntax().text_trimmed_range(),
markup! {
"This case is falling through to the next case."
},
)
.note(markup! {
"Add a `break` or `return` statement to the end of this case to prevent fallthrough."
}),
)
}
}

fn case_fell(case: &AnyJsSwitchClause) -> bool {
let mut children = case.consequent().syntax().children();

if children.clone().count() == 0 {
return false;
}

let has_fall_blocker = children.any(|node| {
JsBreakStatement::can_cast(node.kind())
| JsReturnStatement::can_cast(node.kind())
| JsThrowStatement::can_cast(node.kind())
ddanielsantos marked this conversation as resolved.
Show resolved Hide resolved
| JsContinueStatement::can_cast(node.kind())
});

!has_fall_blocker
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
switch(bar) { case 0: a(); case 1: b() }

switch (bar) { case 0: a(); default: b(); case 1: c() }
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
expression: invalid.js
---
# Input
```js
switch(bar) { case 0: a(); case 1: b() }

switch (bar) { case 0: a(); default: b(); case 1: c() }

```

# Diagnostics
```
invalid.js:1:15 lint/nursery/noFallthroughSwitchClause ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! This case is falling through to the next case.

> 1 │ switch(bar) { case 0: a(); case 1: b() }
│ ^^^^^^^^^^^^
2 │
3 │ switch (bar) { case 0: a(); default: b(); case 1: c() }

i Add a `break` or `return` statement to the end of this case to prevent fallthrough.


```

```
invalid.js:3:16 lint/nursery/noFallthroughSwitchClause ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! This case is falling through to the next case.

1 │ switch(bar) { case 0: a(); case 1: b() }
2 │
> 3 │ switch (bar) { case 0: a(); default: b(); case 1: c() }
│ ^^^^^^^^^^^^
4 │

i Add a `break` or `return` statement to the end of this case to prevent fallthrough.


```

```
invalid.js:3:29 lint/nursery/noFallthroughSwitchClause ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! This case is falling through to the next case.

1 │ switch(bar) { case 0: a(); case 1: b() }
2 │
> 3 │ switch (bar) { case 0: a(); default: b(); case 1: c() }
│ ^^^^^^^^^^^^^
4 │

i Add a `break` or `return` statement to the end of this case to prevent fallthrough.


```


Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
switch(foo) { case 1: doSomething(); break; case 2: doSomething(); }

function bar(foo) { switch(foo) { case 1: doSomething(); return; case 2: doSomething(); } }

switch(foo) { case 1: doSomething(); throw new Error("Boo!"); case 2: doSomething(); }

switch(foo) { case 1: case 2: doSomething(); }
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
expression: valid.js
---
# Input
```js
switch(foo) { case 1: doSomething(); break; case 2: doSomething(); }

function bar(foo) { switch(foo) { case 1: doSomething(); return; case 2: doSomething(); } }

switch(foo) { case 1: doSomething(); throw new Error("Boo!"); case 2: doSomething(); }

switch(foo) { case 1: case 2: doSomething(); }

```


Loading