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

Commit

Permalink
refactor(rome_js_analyze): noFallthroughSwitchClause handle block sta…
Browse files Browse the repository at this point in the history
…tements (#4695)
  • Loading branch information
Conaclos authored Jul 14, 2023
1 parent e6e2b68 commit 3c251f7
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 27 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ if no error diagnostics are emitted.

#### New rules

- Add [`noFallthroughSwitchClause`](https://docs.rome.tools/lint/rules/noFallthroughSwitchClause/)

- Add [`noGlobalIsFinite`](https://docs.rome.tools/lint/rules/noglobalisfinite/)

This rule recommends using `Number.isFinite` instead of the global and unsafe `isFinite` that attempts a type coercion.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use rome_rowan::AstNode;
use rome_rowan::{AstNode, AstNodeList};

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

declare_rule! {
/// Disallow fallthrough of case statements.
/// Disallow fallthrough of `switch` clauses.
///
/// 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.
/// Switch clauses in `switch` statements fall through by default.
/// This can lead to unexpected behavior when forgotten.
///
/// Source: https://eslint.org/docs/latest/rules/no-fallthrough
///
Expand Down Expand Up @@ -41,7 +40,7 @@ declare_rule! {
/// ```
///
pub(crate) NoFallthroughSwitchClause {
version: "12.0.0",
version: "next",
name: "noFallthroughSwitchClause",
recommended: false,
}
Expand Down Expand Up @@ -90,18 +89,30 @@ impl Rule for NoFallthroughSwitchClause {
}

fn case_fell(case: &AnyJsSwitchClause) -> bool {
let mut children = case.consequent().syntax().children();
let statements = case.consequent();
!has_fall_blocker_statement(&statements) && statements.iter().count() != 0
}

if children.clone().count() == 0 {
return false;
fn has_fall_blocker_statement(statements: &JsStatementList) -> bool {
for statement in statements.iter() {
if is_fall_blocker_statement(&statement) {
return true;
}
if let Some(block_statement) = JsBlockStatement::cast_ref(statement.syntax()) {
if has_fall_blocker_statement(&block_statement.statements()) {
return true;
}
}
}
false
}

let has_fall_blocker = children.any(|node| {
JsBreakStatement::can_cast(node.kind())
| JsReturnStatement::can_cast(node.kind())
| JsThrowStatement::can_cast(node.kind())
| JsContinueStatement::can_cast(node.kind())
});

!has_fall_blocker
fn is_fall_blocker_statement(statement: &AnyJsStatement) -> bool {
matches!(
statement,
AnyJsStatement::JsBreakStatement(_)
| AnyJsStatement::JsReturnStatement(_)
| AnyJsStatement::JsThrowStatement(_)
| AnyJsStatement::JsContinueStatement(_)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ function bar(foo) { switch(foo) { case 1: doSomething(); return; case 2: doSomet
switch(foo) { case 1: doSomething(); throw new Error("Boo!"); case 2: doSomething(); }

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

switch(foo) { case 1: { doSomething(); break; } case 2: doSomething(); }
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ switch(foo) { case 1: doSomething(); throw new Error("Boo!"); case 2: doSomethin

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

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

```


2 changes: 1 addition & 1 deletion crates/rome_service/src/configuration/linter/rules.rs

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

2 changes: 1 addition & 1 deletion editors/vscode/configuration_schema.json

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

2 changes: 1 addition & 1 deletion npm/backend-jsonrpc/src/workspace.ts

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

2 changes: 1 addition & 1 deletion npm/rome/configuration_schema.json

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

2 changes: 1 addition & 1 deletion website/src/pages/lint/rules/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ Disallow functions that exceed a given complexity score.
<h3 data-toc-exclude id="noFallthroughSwitchClause">
<a href="/lint/rules/noFallthroughSwitchClause">noFallthroughSwitchClause</a>
</h3>
Disallow fallthrough of case statements.
Disallow fallthrough of <code>switch</code> clauses.
</section>
<section class="rule">
<h3 data-toc-exclude id="noForEach">
Expand Down
8 changes: 4 additions & 4 deletions website/src/pages/lint/rules/noFallthroughSwitchClause.md

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

0 comments on commit 3c251f7

Please sign in to comment.