no-sequences
禁止逗号运算符
逗号运算符包括多个表达式,而通常只期望一个表达式。它从左到右依次计算每个操作数,并返回最后一个操作数的值。然而,这通常会掩盖副作用,并且它的使用往往是无意的。以下是一些序列的例子:
🌐 The comma operator includes multiple expressions where only one is expected. It evaluates each operand from left to right and returns the value of the last operand. However, this frequently obscures side effects, and its use is often an accident. Here are some examples of sequences:
let a = (3, 5); // a = 5
a = b += 5, a + b;
while (a = next(), a && a.length);
(0, eval)("doSomething();");
规则详情
🌐 Rule Details
此规则禁止使用逗号运算符,但以下情况除外:
🌐 This rule forbids the use of the comma operator, with the following exceptions:
- 在
for语句的初始化或更新部分。 - 默认情况下,如果表达式序列被明确地用括号括起来。可以使用
allowInParentheses选项移除此异常。
此规则的错误代码示例:
🌐 Examples of incorrect code for this rule:
/*eslint no-sequences: "error"*/
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:
/*eslint no-sequences: "error"*/
foo = (doSomething(), val);
(0, eval)("doSomething();");
do {} while ((doSomething(), !!test));
for (i = 0, j = 10; i < j; i++, j--);
if ((doSomething(), !!test));
switch ((val = foo(), val)) {}
while ((val = foo(), val < 42));
with ((doSomething(), val)) {}
关于箭头函数体的注意事项
🌐 Note about arrow function bodies
如果箭头函数体是语句而不是块,并且该语句包含序列,则需要在语句周围使用双括号来表示该序列是有意的。
🌐 If an arrow function body is a statement rather than a block, and that statement contains a sequence, you need to use double parentheses around the statement to indicate that the sequence is intentional.
箭头函数的错误代码示例:
🌐 Examples of incorrect code for arrow functions:
/*eslint no-sequences: "error"*/
const foo = (val) => (console.log('bar'), val);
const baz = () => ((bar = 123), 10);
const qux = () => { return (bar = 123), 10 }
箭头函数的正确代码示例:
🌐 Examples of correct code for arrow functions:
/*eslint no-sequences: "error"*/
const foo = (val) => ((console.log('bar'), val));
const baz = () => (((bar = 123), 10));
const qux = () => { return ((bar = 123), 10) }
选项
🌐 Options
此规则采用一个选项,即对象,具有以下属性:
🌐 This rule takes one option, an object, with the following properties:
"allowInParentheses":如果设置为true(默认),此规则允许明确用括号括起来的表达式序列。
allowInParentheses
使用 { "allowInParentheses": false } 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the { "allowInParentheses": false } option:
/*eslint no-sequences: ["error", { "allowInParentheses": false }]*/
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)) {}
const foo = (val) => ((console.log('bar'), val));
使用 { "allowInParentheses": false } 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "allowInParentheses": false } option:
/*eslint no-sequences: ["error", { "allowInParentheses": false }]*/
for (i = 0, j = 10; i < j; i++, j--);
何时不使用
🌐 When Not To Use It
如果允许使用逗号运算符的序列表达式,请禁用此规则。另一种情况是你可能希望报告逗号运算符的所有用法,即使在 for 循环中。你可以使用规则 no-restricted-syntax 实现这一点:
🌐 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 in a for loop. You can achieve this using rule no-restricted-syntax:
{
"rules": {
"no-restricted-syntax": ["error", "SequenceExpression"]
}
}
版本
此规则是在 ESLint v0.5.1 中引入。