eslint/no-unsafe-negation 正确性
它的作用
禁止对关系运算符的左操作数取反,以防止由于误解运算符优先级或意外使用取反而导致的逻辑错误。
对于 TypeScript 代码,可以禁用此规则,因为 TypeScript 编译器会强制执行此检查。
为什么这很糟糕?
对关系运算符的左操作数取反,可能会由于运算符优先级而导致意外行为,从而引发逻辑错误。例如,!a in b 可能会被解释为 (!a) in b,而不是 !(a in b),这并不是预期的逻辑。
示例
以下是此规则的错误代码示例:
javascript
if (!key in object) {}
if (!obj instanceof Ctor) {}以下是此规则的正确代码示例:
javascript
if (!(key in object)) {}
if (!(obj instanceof Ctor)) {}配置
此规则接受一个包含以下属性的配置对象:
enforceForOrderingRelations
type: boolean
default: false
enforceForOrderingRelations 选项决定是否允许在排序关系运算符(<、>、<=、>=)左侧使用取反。
其目的是避免出现诸如 !a < b(它等价于 (a ? 0 : 1) < b)这样的表达式,而真正想要的是 !(a < b)。
如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"rules": {
"no-unsafe-negation": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"no-unsafe-negation": "error",
},
});bash
oxlint --deny no-unsafe-negation版本
此规则在 v0.0.3 中添加。
