no-sequences
禁止逗号运算符
逗号运算符包含多个表达式,其中只需要一个。它从左到右计算每个操作数并返回最后一个操作数的值。然而,这经常掩盖副作用,并且它的使用通常是一个意外。以下是一些序列示例:
var a = (3, 5); // a = 5
a = b += 5, a + b;
while (a = next(), a && a.length);
(0, eval)("doSomething();");
规则详情
此规则禁止使用逗号运算符,但以下情况除外:
- 在
for
语句的初始化或更新部分。 - 默认情况下,如果表达式序列被显式包裹在括号中。可以使用
allowInParentheses
选项删除此异常。
此规则的错误代码示例:
/*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) {}
f/g6SKVZdhoBiPVtnee0m+uPEv1T4kMgIaFUpxdtFwzAZHGBqJ5dJxRHxHHP/cP8
/*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)) {}
关于箭头函数体的注意事项
LTEDBY8vPJjPo1uq7P8Bl71rg603Hkle2radFWaqpWACC5yMqylM8ceW9nlATIc4VCqcqOhx1YZx/dM/W6VU//AJD1qoLPoOgBsDDVNjajhpzc574y93o+6XLJ9BtoxOmuyaSf2ypnFguxW0Jxw58469T+hOdC85O9G823SSMeifAt4fDil6ZruhB2eyJ1SuwDfqHWoZ/E4jjmNV6FHnsw==
ES+1lG+9vSqqs3Yh9hHLd3Bjr9tEVV+jhLxZ0omZuCOgN9o/UiuTzOUh41sYAcBi
/*eslint no-sequences: "error"*/
const foo = (val) => (console.log('bar'), val);
const foo = () => ((bar = 123), 10);
const foo = () => { return (bar = 123), 10 }
7aNllMhFUSBXw3MxV49MWAkzIGpARsuEOaGYzIhZryHFl51wiDC8zbEcfLRplOnb
/*eslint no-sequences: "error"*/
const foo = (val) => ((console.log('bar'), val));
const foo = () => (((bar = 123), 10));
const foo = () => { return ((bar = 123), 10) }
选项
vWW8bjzeIqHfVD0xuzQnY9v+V5HuvSKBVAmdHhMD0qpm/F8nsPdhdh2TR16cVhTH9u3of38G8hzgRDMq8oc9Lg==
- NWBVOY2Km4YcrxrVhhTHlZARSFirqYkin4SgL8jM5NxOzwZnTOdOU7GT49RV8cVrmgFQ0Ne53tqi8/oCQ09M0njl4PI9qcNczqYQB36TMvTICscyDo1+3G0Lhm61FcFErL9mEVfx3igCHUJ8WOdEG/aEYuYIAaBhlR1op1Rv3qnJVFtkYbBmuqXncXw+a52IC7Smv4xZGwGmZ9q1CQKFHA==
allowInParentheses
gXTXeFewMhKUGG8XamsAXX9FzZtKatkC2q6n9hBCQ86xWRAxllCE47u7VGq4dvHYbQebwXAIK5oGDQtiI1aI1cUHEb85P7yQ5vt/8kntHC3uPv0OeuArUxY5kXOlYDET
/*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));
gXTXeFewMhKUGG8XamsAXX9FzZtKatkC2q6n9hBCQ86xWRAxllCE47u7VGq4dvHYbQebwXAIK5oGDQtiI1aI1d0FVb4NCAD9HcpbsQ2I589kY7UCGrzH3uiJXd7zyhcD
/*eslint no-sequences: ["error", { "allowInParentheses": false }]*/
for (i = 0, j = 10; i < j; i++, j--);
何时不使用
Xaoq0wWkX1seavj5mHlRLdU1onnVzAAZe1KJ8ZGaKLwGlMPsG8XgOIM5DnX80qxB1vF9O2oNVyZumLeSJGW2GrS1zt/qCrO/13rPibiSRTizYtLuKBfRBpCyt4l7Dnen1NZnHj/pgL0Cvn6r0P4ne7r2UQVmyy320K07Q02A0fGp808T2VVH2vZ1Vd3C9jvaocp0FuHpMeyHhuxl5L3Y5tGOnR2Ko4BYk1oXYxouhiFrXrBumbjs1+2a36SuXj+aYEgICvdQbnKUtvbXKLB+QHhC9TT4xsLYhUOmp4m0XeyKVgsm+aE0hKMOHDi/E6NfalKWCBUOxbIs68TrZWfxjztPaKtv5LvoO56clfbKEAs=
{
"rules": {
"no-restricted-syntax": ["error", "SequenceExpression"]
}
}