no-multi-assign
禁止使用鏈式賦值運算式
鏈式變數賦值可能導致意料之外的結果,並且難以閱讀。
(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.
規則詳細資訊
此規則禁止在單一語句中使用多重賦值。
此規則的錯誤程式碼範例
在遊樂場開啟
/*eslint no-multi-assign: "error"*/
var a = ;
const foo = ;
let d =
;
class Foo {
a = ;
}
a = ;
此規則的正確程式碼範例
在遊樂場開啟
/*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";
選項
此規則有一個物件選項
"ignoreNonDeclaration"
:當設定為true
時,此規則允許不包含在宣告中初始化變數或初始化類別欄位的鏈式賦值。預設值為false
。
ignoreNonDeclaration
{ "ignoreNonDeclaration": true }
選項的正確程式碼範例
在遊樂場開啟
/*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 }
選項的錯誤程式碼範例
在遊樂場開啟
/*eslint no-multi-assign: ["error", { "ignoreNonDeclaration": true }]*/
let a = ;
const foo = ;
class Foo {
a = ;
}
相關規則
版本
此規則在 ESLint v3.14.0 中引入。