no-unsafe-negation
NOTE: this rule is part of the
推荐 rule set.Enable full set in
deno.json:{
"lint": {
"rules": {
"tags": ["推荐"]
}
}
}Enable full set using the Deno CLI:
deno lint --rules-tags=推荐
This rule can be explictly included to or excluded from the rules present in the current tag by adding it to the
include or exclude array in deno.json:{
"lint": {
"rules": {
"include": ["no-unsafe-negation"],
"exclude": ["no-unsafe-negation"]
}
}
}禁止将否定运算符 ! 作为关系运算符的左操作数。
出现在以下运算符左操作数中的 ! 运算符有时会由于运算符优先级而导致意外行为:
in运算符instanceof运算符
例如,当开发者编写代码 !key in someObject 时,他们最有可能希望它的行为与 !(key in someObject) 一致,但实际上它表现得像 (!key) in someObject。这个 lint 规则会警告这种 ! 运算符的使用,以减少混淆。
无效:
if (!key in object) {}
if (!foo instanceof Foo) {}
有效:
if (!(key in object)) {}
if (!(foo instanceof Foo)) {}
if ((!key) in object) {}
if ((!foo) instanceof Foo) {}