no-self-assign
禁止自我分配(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}
此规则的正确
代码示例:
/*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];
选项
此规则也可以选择检查属性。
{
"no-self-assign": ["error", {"props": false}]
}
props
- 如果是true
,no-self-assign
规则会警告属性的自我分配。默认是false
。
配件
{ "props": true }
选项的错误
代码示例:
/*eslint no-self-assign: [error, {props: true}]*/
// self-assignments with properties.
obj.a = obj.a;
obj.a.b = obj.a.b;
obj["a"] = obj["a"];
obj[a] = obj[a];
{ "props": true }
选项的正确
代码示例:
/*eslint no-self-assign: [error, {props: true}]*/
// 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
// 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"];
何时不使用它?
如果您不想通知自我分配,那么禁用此规则是安全的。
版本
该规则在ESLint 2.0.0-rc.0中引入。