no-multi-assign
禁止使用链式赋值表达式
链接变量的分配可能会导致意想不到的结果并且难以阅读。
¥Chaining the assignment of variables can lead to unexpected results and be difficult to read.
(function() {
const foo = bar = 0; // Did you mean `foo = bar == 0`?
bar = 1; // This will not fail since `bar` is not constant.
})();
console.log(bar); // This will output 1 since `bar` is not scoped.
规则详情
¥Rule Details
此规则不允许在单个语句中使用多个赋值。
¥This rule disallows using multiple assignments within a single statement.
此规则的错误代码示例:
¥Examples of incorrect code for this rule:
/*eslint no-multi-assign: "error"*/
var a = b = c = 5;
const foo = bar = "baz";
let d =
e =
f;
class Foo {
a = b = 10;
}
a = b = "quux";
此规则的正确代码示例:
¥Examples of correct code for this rule:
/*eslint no-multi-assign: "error"*/
var a = 5;
var b = 5;
var c = 5;
const foo = "baz";
const bar = "baz";
let d = c;
let e = c;
class Foo {
a = 10;
b = 10;
}
a = "quux";
b = "quux";
选项
¥Options
此规则有一个对象选项:
¥This rule has an object option:
-
"ignoreNonDeclaration"
:当设置为true
时,该规则允许不包括在声明中初始化变量或初始化类字段的链。默认为false
。¥
"ignoreNonDeclaration"
: When set totrue
, the rule allows chains that don’t include initializing a variable in a declaration or initializing a class field. Default isfalse
.
ignoreNonDeclaration
{ "ignoreNonDeclaration": true }
选项的正确代码示例:
¥Examples of correct code for the { "ignoreNonDeclaration": true }
option:
/*eslint no-multi-assign: ["error", { "ignoreNonDeclaration": true }]*/
let a;
let b;
a = b = "baz";
const x = {};
const y = {};
x.one = y.one = 1;
{ "ignoreNonDeclaration": true }
选项的错误代码示例:
¥Examples of incorrect code for the { "ignoreNonDeclaration": true }
option:
/*eslint no-multi-assign: ["error", { "ignoreNonDeclaration": true }]*/
let a = b = "baz";
const foo = bar = 1;
class Foo {
a = b = 10;
}
相关规则
版本
此规则是在 ESLint v3.14.0 中引入。