no-unsafe-negation

禁止对关系运算符的左操作数求反

配置文件 中的 "extends": "eslint:recommended" 属性启用了该规则

就像开发人员可能会在他们的意思是 -(a + b) 的负数时键入 -a + b 一样,当他们几乎可以肯定的意思是 !(key in object) 以测试某个键不在对象中时,他们可能会错误地键入 !key in object!obj instanceof Ctor 类似。

规则详情

此规则不允许对以下关系运算符的左操作数取反:

r6hi7htUov+vPER817DMpSRTvHu5NMFNJX5PKtsQSh4quBRUaItxaVoNlZcysCNG

/*eslint no-unsafe-negation: "error"*/

if (!key in object) {
    // operator precedence makes it equivalent to (!key) in object
    // and type conversion makes it equivalent to (key ? "false" : "true") in object
}

if (!obj instanceof Ctor) {
    // operator precedence makes it equivalent to (!obj) instanceof Ctor
    // and it equivalent to always false since boolean values are not objects.
}

6RXgRVcYea97/4k5PNW/nItJttC+MHc4vVjxm8VW+ghbeqd3KsyvO/XAfiqXOEJI

/*eslint no-unsafe-negation: "error"*/

if (!(key in object)) {
    // key is not in object
}

if (!(obj instanceof Ctor)) {
    // obj is not an instance of Ctor
}

异常

0TV950dnE8R4LUCNqpyZ7H4dCES+dEVJ5/Mvmn6edTeu9l+Qf73g3qhFaplhDvtH/r7KCrWZYc0AChHKiKSP39N61f96DOjwces2/6pRH4gsLIlB778LPTWjSwAe+TDiAd4wmv2VDOgLrX3j61kLQtRrebqmSVT47spgvyBpBKY60HhDuzQIjd6n++s91VOCxDjFiSH7ANhdpIRpoU/sSg==

6RXgRVcYea97/4k5PNW/nItJttC+MHc4vVjxm8VW+ghbeqd3KsyvO/XAfiqXOEJI

/*eslint no-unsafe-negation: "error"*/

if ((!foo) in object) {
    // allowed, because the negation is explicitly wrapped in parentheses
    // it is equivalent to (foo ? "false" : "true") in object
    // this is allowed as an exception for rare situations when that is the intended meaning
}

if(("" + !foo) in object) {
    // you can also make the intention more explicit, with type conversion
}

r6hi7htUov+vPER817DMpSRTvHu5NMFNJX5PKtsQSh4quBRUaItxaVoNlZcysCNG

/*eslint no-unsafe-negation: "error"*/

if (!(foo) in object) {
    // this is not an allowed exception
}

选项

udkeHjlgR0Ll1wM5CtIul2lnik6PaDZqN/XHqqxqg9G8jIctSRLQTCt3JgRdKnkD

    +ZzUk0S1nWCgCGPzFTqv3/mYQO9zqqYXK3RpkOG85sCov1KQoI7VumV75oMd87ZcePX7Iawy7J/HiD/aFWzs7eVY7P7vCGHTuQnEklj7dx08nZ2sxgtkbBSNEsSuUkIFGGUB7M9wWSJ5uGtitk8teweNUftAo1tzYVvW6PD887e0L6YweHq39ZOZYE4R18kHqGA7Bw2PfMySN3R76DYkAD1oW5Vt2Mt2LesF6yqKpoMDDZY/N1h4lh4a6Oq8czxm1nUoQwLcIAsxhtemcpYFoJLvoWHN9BwiLrAvdC197cjBVweomqYfs8UbcIWpt/obeMvMm/FRTgN2LzWn9Lb9aNkRn2rZgu52+m+WssIn0KdRj5UgWv+I9T86aJuX4wq4lZnmvZNpGel49hcRUygRV5tAPfG4zCN6sh5k3OmQ7vw=

enforceForOrderingRelations

GkL66NxH+1g8w9aBSn3Jk8E80QtLl0O2cHiaflSKNGSOdu1gCPcZ3YJfJZCwMDSabYkASroq1FN0BcIB1Gah3a2Qchhw1BPavRUn4023qB9FHNiAWDWVuROHojjeXnLOx7v0ryOJ6PuY76h6rbRFjg==

    T0fR68zz9gHPFj+JtMlW8185lWbw5wUxK1Ox8rJklbgLjKwoyyof1dPHqtK+zj8WNWbBNRJncbNNu9U9NxN9BkIfGNor8T/p7iDS/2pv9xJqKyK/euDTjjMcqnb85WpDogPRJT/tZ+XpWKlxEzWfRgVMAE9MWvw1znIU+nJPvgo7H1ZT9kHepnfGHJ6PI/02FTbTskM+zDcjBCDDRB/nJR3l+EH3nNtbyMb1fWz9RA4=

ieWLYnRUhJREuGv+nfhe0a0UdN03p8trrBsBGHKMMwAkQ5W1G7sZNJ9WfVqeAvhcDv+TpOUl8MDoB+iKFP9dLJMj1iU/a2ocka8zF3J91psd9NoXOY5Bg2JDDVDcgBor20SKqJuTIywnsDyDKlZYSRJQlNgqBKIeJKVnFGS90YLyVuMMdcnISwCE/vYOdautE6OBizD7mxAGexKHzKcYAGKqAFTaoNcQmpjE4NfsfyU=

F23nqTp9hb4Vd8m/iEN1DMdAB9F5Ju1VqUzYP8yWlWRVel75ZXql455WH84o3a4U233G+Wv0/Bg1Cslqhj7ce7d4gIWWjJUiTowQDZaA6NgU6yIGQ5E2jF1niSBeXHmrND9q8et9codyCjuwVvoUSw==

/*eslint no-unsafe-negation: ["error", { "enforceForOrderingRelations": true }]*/

if (! a < b) {}

while (! a > b) {}

foo = ! a <= b;

foo = ! a >= b;

何时不使用

6q2u/yEXUG+gx/NUPaAX4t9b6OU3tdMEgHOAKg4sSAMdAIdhL6vTyWZW3IudoPBjKiV8oYF00ki3/ub4TmjtNTrb5gfDy01lkaBqUy8Mvo219LjHE1YW5L8lWcnlKoXx