Index

no-negated-in-lhs

禁止对 in 表达式中的左操作数求反

Important

This rule was deprecated in ESLint v3.3.0. It will be removed in v11.0.0. Please replace the rule with no-unsafe-negation.

Learn more

就像开发者在表示一个和的负数时可能会输入 -a + b 而实际上是想输入 -(a + b) 一样,他们也可能错误地输入 !key in object,而实际上几乎可以肯定是想输入 !(key in object) 来测试一个键是否不在对象中。

🌐 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.

规则详情

🌐 Rule Details

此规则不允许在 in 表达式中否定左操作数。

🌐 This rule disallows negating the left operand in in expressions.

此规则的错误代码示例:

🌐 Examples of incorrect code for this rule:

在线运行
/*eslint no-negated-in-lhs: "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
}

符合此规则的正确代码示例:

🌐 Examples of correct code for this rule:

在线运行
/*eslint no-negated-in-lhs: "error"*/

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

if(('' + !key) in object) {
    // make operator precedence and type conversion explicit
    // in a rare situation when that is the intended meaning
}

何时不使用

🌐 When Not To Use It

绝不。

🌐 Never.

版本

此规则是在 ESLint v0.1.2 中引入。

资源