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;

UubGaThNGVSAZCEeqgdAAx/QE1uwW/De5ytjPBnKCKqeOsDZpJS0MvQSJQtY0WPI

/*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"];

选项

wdNlGh1NX/IzglP3dtkeATkS3InV0W2sGZxoLS5cC/dFm5tzxO0eBTR4BDCm7ct5

{
    "no-self-assign": ["error", {"props": true}]
}
    TeLLdugyDUdKnVW+1LtCgv62stJvSFkkTGFbv3K46xXVkyqysz1T6JUVMBx4SBEcz+ps+8j/hP3pvulDixoSnuDFVfVEPNh/fNWU1AfC2oQmA9C+C98XS/Sam5USNb1mpQs+ATkVLLkmpHG80wOTo1zZVE+2BlwAEu/DwBvik91JG58vS9+xMojjPjXz1Wt48CdXo2XbJYE0Lnm40BDJ5g==

props

+owOy/dsP9kw2JNjSZOR+xXDmFQ6jE4ExGEPNOofiHlT5c45mwPZN2eBIz1l9YsVCdC3C1eVkZyNw3f+8zIr3Ei5RpKUzwUBAta89/bxh6I=

/*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];

何时不使用

icRFvucbPznfPynZMVMwnjwIKcrtQoBvsTDdbXyipSCrDCc5VJQsPiOHTR+efO4Bl0JcF0C5p27orJYEhrZB/2wzK7Q+p2q5HFAq4jL+RAc=