no-unsafe-negation

不允许否定关系运算符的左操作数

Recommended

配置文件 中使用来自 @eslint/jsrecommended 配置可以启用此规则

💡 hasSuggestions

此规则报告的一些问题可通过编辑器建议手动修复

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

¥Just as developers might type -a + b when they mean -(a + b) for the negative of a sum, they might type !key in object by mistake when they almost certainly mean !(key in object) to test that a key is not in an object. !obj instanceof Ctor is similar.

规则详情

¥Rule Details

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

¥This rule disallows negating the left operand of the following relational operators:

此规则的错误代码示例:

¥Examples of incorrect code for this rule:

在线运行
/*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.
}

此规则的正确代码示例:

¥Examples of correct code for this rule:

在线运行
/*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
}

异常

¥Exception

对于想要否定左操作数的极少数情况,此规则允许例外。如果整个否定明确地包含在括号中,则规则不会报告问题。

¥For rare situations when negating the left operand is intended, this rule allows an exception. If the whole negation is explicitly wrapped in parentheses, the rule will not report a problem.

此规则的正确代码示例:

¥Examples of correct code for this rule:

在线运行
/*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
}

此规则的错误代码示例:

¥Examples of incorrect code for this rule:

在线运行
/*eslint no-unsafe-negation: "error"*/

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

选项

¥Options

此规则有一个对象选项:

¥This rule has an object option:

  • "enforceForOrderingRelations": false(默认)允许对排序关系运算符左侧进行求反(<><=>=

    ¥"enforceForOrderingRelations": false (default) allows negation of the left-hand side of ordering relational operators (<, >, <=, >=)

  • "enforceForOrderingRelations": true 不允许对排序关系运算符的左侧求反

    ¥"enforceForOrderingRelations": true disallows negation of the left-hand side of ordering relational operators

enforceForOrderingRelations

将此选项设置为 true 时,还会针对以下情况额外强制执行该规则:

¥With this option set to true the rule is additionally enforced for:

  • < 运算符。

    ¥< operator.

  • > 运算符。

    ¥> operator.

  • <= 运算符。

    ¥<= operator.

  • >= 运算符。

    ¥>= operator.

目的是避免当真正的意图是 !(a < b) 时,诸如 ! a < b(相当于 (a ? 0 : 1) < b)之类的表达。

¥The purpose is to avoid expressions such as ! a < b (which is equivalent to (a ? 0 : 1) < b) when what is really intended is !(a < b).

使用 { "enforceForOrderingRelations": true } 选项的此规则的其他错误代码示例:

¥Examples of additional incorrect code for this rule with the { "enforceForOrderingRelations": true } option:

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

if (! a < b) {}

while (! a > b) {}

foo = ! a <= b;

foo = ! a >= b;

何时不使用

¥When Not To Use It

如果你不想通知不安全的逻辑否定,那么禁用此规则是安全的。

¥If you don’t want to notify unsafe logical negations, then it’s safe to disable this rule.

由 TypeScript 处理

使用 TypeScript 时禁用此规则是安全的,因为 TypeScript 的编译器强制执行此检查。

版本

此规则是在 ESLint v3.3.0 中引入。

资源

ESLint 中文网
粤ICP备13048890号