This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 659
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rome_js_analyze):
noCommaOperator
(#4019)
Co-authored-by: Micha Reiser <micha@rome.tools> Closes #3932
- Loading branch information
Showing
15 changed files
with
702 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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.
76 changes: 76 additions & 0 deletions
76
crates/rome_js_analyze/src/analyzers/nursery/no_comma_operator.rs
This file contains 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,76 @@ | ||
use rome_analyze::context::RuleContext; | ||
use rome_analyze::{declare_rule, Ast, Rule, RuleDiagnostic}; | ||
use rome_js_syntax::{JsForStatement, JsSequenceExpression}; | ||
use rome_rowan::AstNode; | ||
|
||
declare_rule! { | ||
/// Disallow comma operator. | ||
/// | ||
/// The comma operator includes multiple expressions where only one is expected. | ||
/// It evaluates every operand from left to right and returns the value of the last operand. | ||
/// It frequently obscures side effects, and its use is often an accident. | ||
/// | ||
/// The use of the comma operator in the initialization and update parts of a `for` is still allowed. | ||
/// | ||
/// Source: https://eslint.org/docs/latest/rules/no-sequences | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// const foo = doSomething(), 0; | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// for (; doSomething(), !!test; ) {} | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// // Use a semicolon instead. | ||
/// let a, b; | ||
/// a = 1, b = 2; | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```js | ||
/// for(a = 0, b = 0; (a + b) < 10; a++, b += 2) {} | ||
/// ``` | ||
/// | ||
pub(crate) NoCommaOperator { | ||
version: "12.0.0", | ||
name: "noCommaOperator", | ||
recommended: true, | ||
} | ||
} | ||
|
||
impl Rule for NoCommaOperator { | ||
type Query = Ast<JsSequenceExpression>; | ||
type State = (); | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let seq = ctx.query(); | ||
if let Some(for_stmt) = seq.parent::<JsForStatement>() { | ||
// Allow comma operator in initializer and update parts of a `for` | ||
if for_stmt.test().map(AstNode::into_syntax).as_ref() != Some(seq.syntax()) { | ||
return None; | ||
} | ||
} | ||
Some(()) | ||
} | ||
|
||
fn diagnostic(ctx: &RuleContext<Self>, _: &Self::State) -> Option<RuleDiagnostic> { | ||
let seq = ctx.query(); | ||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
seq.comma_token().ok()?.text_trimmed_range(), | ||
"The comma operator is disallowed.", | ||
) | ||
.note("Its use is often confusing and obscures side effects."), | ||
) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
crates/rome_js_analyze/tests/specs/nursery/noCommaOperator/invalid.jsonc
This file contains 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,25 @@ | ||
[ | ||
"a = 1, 2", | ||
"do {} while (doSomething(), !!test);", | ||
"for (; doSomething(), !!test; );", | ||
"if (doSomething(), !!test);", | ||
"switch (doSomething(), val) {}", | ||
"while (doSomething(), !!test);", | ||
"with (doSomething(), val) {}", | ||
"a => (doSomething(), a)", | ||
"(1), 2", | ||
"((1)) , (2)", | ||
"while((1) , 2);", | ||
|
||
// Do not allow comma operator in parentheses | ||
"var foo = (1, 2);", | ||
"(0,eval)(\"foo()\");", | ||
"foo(a, (b, c), d);", | ||
"do {} while ((doSomething(), !!test));", | ||
"for (; (doSomething(), !!test); );", | ||
"if ((doSomething(), !!test));", | ||
"switch ((doSomething(), val)) {}", | ||
"while ((doSomething(), !!test));", | ||
"with ((doSomething(), val)) {}", | ||
"a => ((doSomething(), a))" | ||
] |
Oops, something went wrong.