Skip to content
← Back to rules

eslint/no-compare-neg-zero 正确性

This rule is turned on by default.
🛠️💡 An auto-fix and a suggestion are available for this rule for some violations.

它做了什么

禁止与 -0 进行比较

为什么这是不好的?

该规则应警告尝试与 -0 进行比较的代码,因为这不会按预期工作。也就是说,代码如 x === -0 对于 +0-0 都会通过。作者很可能本意是使用 Object.is(x, -0)

示例

此规则的错误代码示例:

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

此规则的正确代码示例:

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

如何使用

要通过配置文件或 CLI 启用此规则,可以使用:

json
{
  "rules": {
    "no-compare-neg-zero": "error"
  }
}
bash
oxlint --deny no-compare-neg-zero

参考资料