Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: add option "allowInParentheses" to no-sequences (fixes #14197) #14199

Merged
merged 6 commits into from Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 37 additions & 2 deletions docs/rules/no-sequences.md
Expand Up @@ -17,7 +17,6 @@ while (a = next(), a && a.length);
This rule forbids the use of the comma operator, with the following exceptions:

* In the initialization or update portions of a `for` statement.
* If the expression sequence is explicitly wrapped in parentheses.
danielrentz marked this conversation as resolved.
Show resolved Hide resolved

Examples of **incorrect** code for this rule:

Expand Down Expand Up @@ -46,6 +45,42 @@ Examples of **correct** code for this rule:
```js
/*eslint no-sequences: "error"*/

for (i = 0, j = 10; i < j; i++, j--);
```

## Options

This rule takes one option, an object, with the following properties:

* `"allowInParentheses"`: If set to `true`, this rule allows to use expression sequences that are explicitly wrapped in parentheses.

Examples of **incorrect** code for this rule:

```js
/*eslint no-sequences: ["error", { allowInParentheses: true }]*/

foo = doSomething(), val;

0, eval("doSomething();");

do {} while (doSomething(), !!test);

for (; doSomething(), !!test; );

if (doSomething(), !!test);

switch (val = foo(), val) {}

while (val = foo(), val < 42);

with (doSomething(), val) {}
```

Examples of **correct** code for this rule:

```js
/*eslint no-sequences: ["error", { allowInParentheses: true }]*/

foo = (doSomething(), val);

(0, eval)("doSomething();");
Expand All @@ -66,7 +101,7 @@ with ((doSomething(), val)) {}
## When Not To Use It

Disable this rule if sequence expressions with the comma operator are acceptable.
Another case is where you might want to report all usages of the comma operator, even if they are wrapped in parentheses or in a for loop. You can achieve this using rule `no-restricted-syntax`:
Another case is where you might want to report all usages of the comma operator, even in a for loop. You can achieve this using rule `no-restricted-syntax`:

```js
{
Expand Down
26 changes: 18 additions & 8 deletions lib/rules/no-sequences.js
Expand Up @@ -26,14 +26,22 @@ module.exports = {
url: "https://eslint.org/docs/rules/no-sequences"
},

schema: [],
schema: [{
properties: {
allowInParentheses: {
type: "boolean",
default: false
danielrentz marked this conversation as resolved.
Show resolved Hide resolved
}
}
}],

messages: {
unexpectedCommaExpression: "Unexpected use of comma operator."
}
},

create(context) {
const options = context.options[0] || {};
const sourceCode = context.getSourceCode();

/**
Expand Down Expand Up @@ -99,13 +107,15 @@ module.exports = {
}

// Wrapping a sequence in extra parens indicates intent
if (requiresExtraParens(node)) {
if (isParenthesisedTwice(node)) {
return;
}
} else {
if (isParenthesised(node)) {
return;
if (options.allowInParentheses) {
if (requiresExtraParens(node)) {
if (isParenthesisedTwice(node)) {
return;
}
} else {
if (isParenthesised(node)) {
return;
}
}
}

Expand Down
30 changes: 20 additions & 10 deletions tests/lib/rules/no-sequences.js
Expand Up @@ -39,17 +39,17 @@ ruleTester.run("no-sequences", rule, {
"var arr = [1, 2];",
"var obj = {a: 1, b: 2};",
"var a = 1, b = 2;",
"var foo = (1, 2);",
"(0,eval)(\"foo()\");",
{ code: "var foo = (1, 2);", options: [{ allowInParentheses: true }] },
{ code: "(0,eval)(\"foo()\");", options: [{ allowInParentheses: true }] },
"for (i = 1, j = 2;; i++, j++);",
"foo(a, (b, c), d);",
"do {} while ((doSomething(), !!test));",
"for ((doSomething(), somethingElse()); (doSomething(), !!test); );",
"if ((doSomething(), !!test));",
"switch ((doSomething(), !!test)) {}",
"while ((doSomething(), !!test));",
"with ((doSomething(), val)) {}",
{ code: "a => ((doSomething(), a))", env: { es6: true } }
{ code: "foo(a, (b, c), d);", options: [{ allowInParentheses: true }] },
{ code: "do {} while ((doSomething(), !!test));", options: [{ allowInParentheses: true }] },
{ code: "for ((doSomething(), somethingElse()); (doSomething(), !!test); );", options: [{ allowInParentheses: true }] },
{ code: "if ((doSomething(), !!test));", options: [{ allowInParentheses: true }] },
{ code: "switch ((doSomething(), val)) {}", options: [{ allowInParentheses: true }] },
{ code: "while ((doSomething(), !!test));", options: [{ allowInParentheses: true }] },
{ code: "with ((doSomething(), val)) {}", options: [{ allowInParentheses: true }] },
{ code: "a => ((doSomething(), a))", options: [{ allowInParentheses: true }], env: { es6: true } }
],

// Examples of code that should trigger the rule
Expand All @@ -66,13 +66,23 @@ ruleTester.run("no-sequences", rule, {
}]
},
{ code: "a = 1, 2", errors: errors(6) },
{ code: "var foo = (1, 2);", options: [{ allowInParentheses: false }], errors: errors(13) },
{ code: "(0,eval)(\"foo()\");", errors: errors(3) },
{ code: "foo(a, (b, c), d);", errors: errors(10) },
{ code: "do {} while (doSomething(), !!test);", errors: errors(27) },
{ code: "do {} while ((doSomething(), !!test));", errors: errors(28) },
{ code: "for (; doSomething(), !!test; );", errors: errors(21) },
{ code: "for ((doSomething(), somethingElse()); (doSomething(), !!test); );", errors: errors(54) },
{ code: "if (doSomething(), !!test);", errors: errors(18) },
{ code: "if ((doSomething(), !!test));", errors: errors(19) },
{ code: "switch (doSomething(), val) {}", errors: errors(22) },
{ code: "switch ((doSomething(), val)) {}", errors: errors(23) },
{ code: "while (doSomething(), !!test);", errors: errors(21) },
{ code: "while ((doSomething(), !!test));", errors: errors(22) },
{ code: "with (doSomething(), val) {}", errors: errors(20) },
{ code: "with ((doSomething(), val)) {}", errors: errors(21) },
{ code: "a => (doSomething(), a)", env: { es6: true }, errors: errors(20) },
{ code: "a => ((doSomething(), a))", env: { es6: true }, errors: errors(21) },
{ code: "(1), 2", errors: errors(4) },
{ code: "((1)) , (2)", errors: errors(7) },
{ code: "while((1) , 2);", errors: errors(11) }
Expand Down