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 都会返回 true。作者可能本意是想使用 Object.is(x, -0)

示例

此规则 错误 代码的示例:

javascript
if (x === -0) {
  // 执行某些操作...
}
javascript
if (-0 > x) {
  // 执行某些操作...
}

此规则 正确 代码的示例:

javascript
if (x === 0) {
  // 执行某些操作...
}
javascript
if (Object.is(x, -0)) {
  // 执行某些操作...
}
javascript
if (0 > x) {
  // 执行某些操作...
}

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "no-compare-neg-zero": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "no-compare-neg-zero": "error",
  },
});
bash
oxlint --deny no-compare-neg-zero

版本

此规则于 v0.0.3 中添加。

参考资料