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:

var 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 语句的初始化或更新部分。

    ¥In the initialization or update portions of a for statement.

  • 默认情况下,如果表达式序列被显式封装在括号中。可以使用 allowInParentheses 选项删除此异常。

    ¥By default, if the expression sequence is explicitly wrapped in parentheses. This exception can be removed with the allowInParentheses option.

此规则的错误代码示例:

¥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": If set to true (default), this rule allows expression sequences that are explicitly wrapped in parentheses.

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 中引入。

资源

ESLint 中文网
粤ICP备13048890号