Versions

no-compare-neg-zero

Disallow comparing against -0

Recommended

The "extends": "eslint:recommended" property in a configuration file enables this rule

规则细节

这条规则应该对试图与 -0 比较的代码提出警告,因为这不会像预期的那样工作。也就是说,像 x === -0 这样的代码对于 +0-0 都会通过。作者可能想要的是 Object.is(x, -0)

使用此规则的错误示例:

Open in Playground
/* eslint no-compare-neg-zero: "error" */

if (x === -0) {
    // doSomething()...
}

使用此规则的正确示例:

Open in Playground
/* eslint no-compare-neg-zero: "error" */

if (x === 0) {
    // doSomething()...
}
Open in Playground
/* eslint no-compare-neg-zero: "error" */

if (Object.is(x, -0)) {
    // doSomething()...
}

Version

This rule was introduced in ESLint v3.17.0.

Resources

更改语言