no-compare-neg-zero
禁止与 -0 进行比较
✅ Recommended
在 配置文件 中使用来自 @eslint/js 的 recommended 配置可以启用此规则
规则详情
🌐 Rule Details
该规则应该警告那些尝试与 -0 进行比较的代码,因为那样做不会按预期工作。也就是说,像 x === -0 这样的代码对 +0 和 -0 都会通过。作者可能本意是 Object.is(x, -0)。
🌐 The rule should warn against code that tries to compare against -0, since that will not work as intended. That is, code like x === -0 will pass for both +0 and -0. The author probably intended Object.is(x, -0).
此规则的错误代码示例:
🌐 Examples of incorrect code for this rule:
在线运行
/* eslint no-compare-neg-zero: "error" */
if (x === -0) {
// doSomething()...
}
符合此规则的正确代码示例:
🌐 Examples of correct code for this rule:
在线运行
/* eslint no-compare-neg-zero: "error" */
if (x === 0) {
// doSomething()...
}
在线运行
/* eslint no-compare-neg-zero: "error" */
if (Object.is(x, -0)) {
// doSomething()...
}
选项
🌐 Options
此规则没有选项。
🌐 This rule has no options.
版本
此规则是在 ESLint v3.17.0 中引入。