no-unsafe-negation
不允许否定关系运算符的左操作数
在 配置文件 中使用来自 @eslint/js 的 recommended 配置可以启用此规则
此规则报告的一些问题可通过编辑器 建议 手动修复
就像开发者在想表达某个和的负值时可能会输入 -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": true不允许对排序关系运算符的左侧进行取反
enforceForOrderingRelations
将此选项设置为 true 时,该规则还将强制适用于:
🌐 With this option set to true the rule is additionally enforced for:
<操作符。>操作符。<=操作符。>=操作符。
目的是避免使用诸如 ! a < b(其等同于 (a ? 0 : 1) < b)的表达,而实际上真正想表达的是 !(a < 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 中引入。