Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -48,6 +48,7 @@ define_dategories! {
"lint/nursery/noConstEnum": "https://docs.rome.tools/lint/rules/noConstEnum",
"lint/nursery/noConstructorReturn": "https://docs.rome.tools/lint/rules/noConstructorReturn",
"lint/nursery/noDistractingElements": "https://docs.rome.tools/lint/rules/noDistractingElements",
"lint/nursery/noDuplicateCase": "https://docs.rome.tools/lint/rules/noDuplicateCase",
"lint/nursery/noDuplicateObjectKeys":"https://docs.rome.tools/lint/rules/noDuplicateObjectKeys",
"lint/nursery/noEmptyInterface": "https://docs.rome.tools/lint/rules/noEmptyInterface",
"lint/nursery/noExtraNonNullAssertion":"https://docs.rome.tools/lint/rules/noExtraNonNullAssertion",
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.

129 changes: 129 additions & 0 deletions crates/rome_js_analyze/src/analyzers/nursery/no_duplicate_case.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
use rome_analyze::context::RuleContext;
use rome_analyze::{declare_rule, Ast, Rule, RuleDiagnostic};
use rome_js_syntax::{AnyJsSwitchClause, JsCaseClause, JsSwitchStatement};
use rome_rowan::AstNode;
use std::collections::hash_map::Entry;
use std::collections::HashMap;

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
///
/// ```ts,expect_diagnostic
/// switch (a) {
/// case 1:
/// break;
/// case 1:
/// break;
/// default:
/// break;
/// }
/// ```
///
/// ```ts,expect_diagnostic
/// switch (a) {
/// case one:
/// break;
/// case one:
/// break;
/// default:
/// break;
/// }
/// ```
///
/// ```ts,expect_diagnostic
/// switch (a) {
/// case "1":
/// break;
/// case "1":
/// break;
/// default:
/// break;
/// }
/// ```
///
/// ### Valid
///
/// ```ts
/// switch (a) {
/// case 1:
/// break;
/// case 2:
/// break;
/// default:
/// break;
/// }
/// ```
///
/// ```ts
/// switch (a) {
/// case one:
/// break;
/// case two:
/// break;
/// default:
/// break;
/// }
/// ```
///
/// ```ts
/// switch (a) {
/// case "1":
/// break;
/// case "2":
/// break;
/// default:
/// break;
/// }
/// ```
pub(crate) NoDuplicateCase {
version: "11.0.0",
name: "noDuplicateCase",
recommended: true,
}
}

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

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let node = ctx.query();

let mut defined_cases: HashMap<String, JsCaseClause> = HashMap::new();
let mut signals = Vec::new();

for case in node.cases() {
if let AnyJsSwitchClause::JsCaseClause(case) = case {
if let Ok(test) = case.test() {
let text = test.text();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it enough to compare text? Or we need to travers expression tree?
Can we get text without inner comment?
For example:

switch (a) {
	case f(
		a + 1 // comment
	).p1:
		break;
	case f(a + 1).p1:
		break;
	default:
		break;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is similar to the check being performed in #4031, maybe there's an opportunity to move this comparison in a shared utility for these two rules

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to do that?

It seems to me this would be case for another lint rule: to only allow constants as the case.

This will fall into the same cases as the binary: these expression can have side effects (or throw) and we will be changing the application behaviour.

Copy link
Contributor Author

@denbezrukov denbezrukov Dec 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took test cases from the eslint rule.
https://github.com/eslint/eslint/blob/main/tests/lib/rules/no-duplicate-case.js
It has this test case.


match defined_cases.entry(text) {
Entry::Occupied(_) => {
signals.push(case);
}
Entry::Vacant(entry) => {
entry.insert(case);
}
}
}
}
}

signals
}

fn diagnostic(_: &RuleContext<Self>, case: &Self::State) -> Option<RuleDiagnostic> {
case.test().ok().map(|test| {
RuleDiagnostic::new(rule_category!(), test.range(), "Duplicate case label.")
})
}
}
187 changes: 187 additions & 0 deletions crates/rome_js_analyze/tests/specs/nursery/noDuplicateCase/invalid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
var a = 1;
switch (a) {
case 1:
break;
case 1:
break;
case 2:
break;
default:
break;
}
var a = "1";
switch (a) {
case "1":
break;
case "1":
break;
case "2":
break;
default:
break;
}
var a = 1,
one = 1;
switch (a) {
case one:
break;
case one:
break;
case 2:
break;
default:
break;
}
var a = 1,
p = { p: { p1: 1, p2: 1 } };
switch (a) {
case p.p.p1:
break;
case p.p.p1:
break;
default:
break;
}
var a = 1,
f = function (b) {
return b ? { p1: 1 } : { p1: 2 };
};
switch (a) {
case f(true).p1:
break;
case f(true).p1:
break;
default:
break;
}
var a = 1,
f = function (s) {
return { p1: s };
};
switch (a) {
case f(a + 1).p1:
break;
case f(a + 1).p1:
break;
default:
break;
}
var a = 1,
f = function (s) {
return { p1: s };
};
switch (a) {
case f(a === 1 ? 2 : 3).p1:
break;
case f(a === 1 ? 2 : 3).p1:
break;
default:
break;
}
var a = 1,
f1 = function () {
return { p1: 1 };
};
switch (a) {
case f1().p1:
break;
case f1().p1:
break;
default:
break;
}
var a = [1, 2];
switch (a.toString()) {
case [1, 2].toString():
break;
case [1, 2].toString():
break;
default:
break;
}
switch (a) {
case a:
case a:
}
switch (a) {
case a:
break;
case b:
break;
case a:
break;
case c:
break;
case a:
break;
}
var a = 1,
p = { p: { p1: 1, p2: 1 } };
switch (a) {
case p.p.p1:
break;
case p.p.p1: // comment\n
break;
default:
break;
}
var a = 1,
p = { p: { p1: 1, p2: 1 } };
switch (a) {
case /* comment */
p.p.p1:
break;
case p.p.p1:
break;
default:
break;
}
var a = 1,
p = { p: { p1: 1, p2: 1 } };
switch (a) {
case p.p /* comment */.p1:
break;
case p.p.p1: // comment
break;
default:
break;
}
var a = 1,
p = { p: { p1: 1, p2: 1 } };
switch (a) {
case p.p.p1:
break;
case p.p.p1: // comment
break;
case /* comment */
p.p.p1:
break;
default:
break;
}
var a = 1,
f = function (s) {
return { p1: s };
};
switch (a) {
case f(a + 1).p1:
break;
case f(a + 1).p1:
break;
default:
break;
}
var a = 1,
f = function (s) {
return { p1: s };
};
switch (a) {
case f(
a + 1 // comment
).p1:
break;
case f(a + 1).p1:
break;
default:
break;
}
Loading