# no-self-assign
禁止两边完全相同的赋值
配置文件中的 "extends": "eslint:recommended" 属性启用了该规则
自分配没有效果,因此可能是由于重构不完整而导致的错误。那些表明你应该做的事情仍然存在。
foo = foo;
[bar, baz] = [bar, qiz];
# 规则详情
该规则旨在消除自我分配。
此规则的错误代码示例:
/*eslint no-self-assign: "error"*/
foo = foo;
[a, b] = [a, b];
[a, ...b] = [x, ...b];
({a, b} = {a, x});
foo &&= foo;
foo ||= 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];
# 何时不使用
如果您不想通知自我分配,那么禁用此规则是安全的。