no-self-assign
禁止兩邊完全相同的賦值
✅ 建議
在設定檔中使用 @eslint/js
的 recommended
設定會啟用此規則
自我賦值沒有任何效果,因此可能是由於不完整的重構而導致的錯誤。這表示您應該做的事情仍然存在。
foo = foo;
[bar, baz] = [bar, qiz];
規則詳細資訊
此規則旨在消除自我賦值。
此規則的錯誤程式碼範例
在遊樂場中開啟
/*eslint no-self-assign: "error"*/
foo = ;
[a, b] = [, ];
[a, ...b] = [x, ...];
({a, b} = {, x});
foo &&= ;
foo ||= ;
foo ??= ;
此規則的正確程式碼範例
在遊樂場中開啟
/*eslint no-self-assign: "error"*/
foo = bar;
[a, b] = [b, a];
// This pattern is warned by the `no-use-before-define` rule.
let foo = foo;
// The default values have an effect.
[foo = 1] = [foo];
// non-self-assignments with properties.
obj.a = obj.b;
obj.a.b = obj.c.b;
obj.a.b = obj.a.c;
obj[a] = obj["a"];
// This ignores if there is a function call.
obj.a().b = obj.a().b;
a().b = a().b;
// `&=` and `|=` have an effect on non-integers.
foo &= foo;
foo |= foo;
// Known limitation: this does not support computed properties except single literal or single identifier.
obj[a + b] = obj[a + b];
obj["a" + "b"] = obj["a" + "b"];
選項
此規則也可以選擇檢查屬性。
{
"no-self-assign": ["error", {"props": true}]
}
props
- 如果此值為true
,則no-self-assign
規則會警告屬性的自我賦值。預設值為true
。
props
使用 { "props": false }
選項的正確程式碼範例
在遊樂場中開啟
/*eslint no-self-assign: ["error", {"props": false}]*/
// self-assignments with properties.
obj.a = obj.a;
obj.a.b = obj.a.b;
obj["a"] = obj["a"];
obj[a] = obj[a];
何時不該使用
如果您不希望收到關於自我賦值的通知,則可以安全地停用此規則。
版本
此規則是在 ESLint v2.0.0-rc.0 中引入的。