Eslint
规则 | Rules

no-unsafe-negation

disallow negating the left operand of relational operators (no-unsafe-negation)

"extends": "eslint:recommended"配置文件中的属性启用此规则。

--fix命令行上的选项可以自动修复一些被这条规则反映的问题。

正如开发者可能键入-a + b时,他们的意思是-(a + b)一和的负数,他们可能会输入!key in object错误时,他们几乎肯定意味着!(key in object)要测试的关键是不是一个对象。!obj instanceof Ctor很相似。

规则细节

此规则不允许否定关系运算符的左操作数。

关系运算符是:

  • in operator.

  • instanceof operator.

此规则的错误代码示例:

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

此规则的正确代码示例:

/*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 } if(("" + !key) in object) { // make operator precedence and type conversion explicit // in a rare situation when that is the intended meaning }

选项

没有。

何时不使用它

如果您不想通知不安全的逻辑否定,则可以安全地禁用此规则。

版本

这条规则是在 ESLint 3.3.0中引入的。

资源