This repository was archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 650
feat(rome_js_analyze): noDuplicateCase, no-duplicate-case #3969 #4039
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
74aca10
feat(rome_js_analyze): noDuplicateCase, no-duplicate-case #3969
denbezrukov 44f7070
Merge remote-tracking branch 'upstream/main' into feat/no-duplicate-case
denbezrukov 55f7b79
feat(rome_js_analyze): noDuplicateCase, no-duplicate-case #3969
denbezrukov 6e8e3f2
Merge remote-tracking branch 'upstream/main' into feat/no-duplicate-case
denbezrukov 5330fa3
feat(rome_js_analyze): noDuplicateCase, no-duplicate-case #3969
denbezrukov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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.
132 changes: 132 additions & 0 deletions
132
crates/rome_js_analyze/src/analyzers/nursery/no_duplicate_case.rs
This file contains hidden or 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,132 @@ | ||
use crate::utils::is_node_equal; | ||
use rome_analyze::context::RuleContext; | ||
use rome_analyze::{declare_rule, Ast, Rule, RuleDiagnostic}; | ||
use rome_js_syntax::{AnyJsExpression, AnyJsSwitchClause, JsCaseClause, JsSwitchStatement}; | ||
use rome_rowan::{AstNode, TextRange}; | ||
|
||
declare_rule! { | ||
/// Disallow duplicate case labels. | ||
/// If a switch statement has duplicate test expressions in case clauses, it is likely that a programmer copied a case clause but forgot to change the test expression. | ||
/// | ||
/// Source: https://eslint.org/docs/latest/rules/no-duplicate-case | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// switch (a) { | ||
/// case 1: | ||
/// break; | ||
/// case 1: | ||
/// break; | ||
/// default: | ||
/// break; | ||
/// } | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// switch (a) { | ||
/// case one: | ||
/// break; | ||
/// case one: | ||
/// break; | ||
/// default: | ||
/// break; | ||
/// } | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// switch (a) { | ||
/// case "1": | ||
/// break; | ||
/// case "1": | ||
/// break; | ||
/// default: | ||
/// break; | ||
/// } | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```js | ||
/// switch (a) { | ||
/// case 1: | ||
/// break; | ||
/// case 2: | ||
/// break; | ||
/// default: | ||
/// break; | ||
/// } | ||
/// ``` | ||
/// | ||
/// ```js | ||
/// switch (a) { | ||
/// case one: | ||
/// break; | ||
/// case two: | ||
/// break; | ||
/// default: | ||
/// break; | ||
/// } | ||
/// ``` | ||
/// | ||
/// ```js | ||
/// switch (a) { | ||
/// case "1": | ||
/// break; | ||
/// case "2": | ||
/// break; | ||
/// default: | ||
/// break; | ||
/// } | ||
/// ``` | ||
pub(crate) NoDuplicateCase { | ||
version: "12.0.0", | ||
name: "noDuplicateCase", | ||
recommended: true, | ||
} | ||
} | ||
|
||
impl Rule for NoDuplicateCase { | ||
type Query = Ast<JsSwitchStatement>; | ||
type State = (TextRange, JsCaseClause); | ||
type Signals = Vec<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let node = ctx.query(); | ||
|
||
let mut defined_tests: Vec<AnyJsExpression> = Vec::new(); | ||
let mut signals = Vec::new(); | ||
|
||
for case in node.cases() { | ||
if let AnyJsSwitchClause::JsCaseClause(case) = case { | ||
if let Ok(test) = case.test() { | ||
let define_test = defined_tests | ||
.iter() | ||
.find(|define_test| is_node_equal(define_test.syntax(), test.syntax())); | ||
|
||
match define_test { | ||
Some(define_test) => { | ||
signals.push((define_test.range(), case)); | ||
} | ||
None => { | ||
defined_tests.push(test); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
signals | ||
} | ||
|
||
fn diagnostic(_: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> { | ||
let (first_label_range, case) = state; | ||
case.test().ok().map(|test| { | ||
RuleDiagnostic::new(rule_category!(), test.range(), "Duplicate case label.") | ||
.detail(first_label_range, "The first similar label is here:") | ||
}) | ||
} | ||
denbezrukov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.