no-negated-in-lhs

此规则在 ESLint v3.3.0 中已弃用,并被 no-unsafe-negation 规则取代。

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

规则详情

此规则不允许对 in 表达式中的左操作数取反。

此规则的错误代码示例:

/*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
}

此规则的正确代码示例:

/*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
}

何时不使用

wj9obKMgSMM9zuny2kqB2A==