noSelfCompare
¥Summary
-
规则生效日期:
v1.0.0¥Rule available since:
v1.0.0 -
诊断类别:
lint/suspicious/noSelfCompare¥Diagnostic Category:
lint/suspicious/noSelfCompare -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则没有修复方案。
¥This rule doesn’t have a fix.
-
此规则的默认严重级别为 error。
¥The default severity of this rule is error.
-
来源:
¥Sources:
-
与
no-self-compare相同¥Same as
no-self-compare -
与
eq_op相同¥Same as
eq_op
-
¥How to configure
{ "linter": { "rules": { "suspicious": { "noSelfCompare": "error" } } }}¥Description
禁止在双方完全相同的情况下进行比较。
¥Disallow comparisons where both sides are exactly the same.
在 CI 环境中使用的命令。它会让读者感到困惑,并可能导致运行时错误。
¥Comparing a variable against itself is usually an error, either a typo or refactoring error. It is confusing to the reader and may potentially introduce a runtime error.
唯一需要将变量与自身进行比较的情况是测试
NaN时。但是,对于该用例,使用typeof x === 'number' && Number.isNaN(x)比让代码的读者确定自我比较的意图更为合适。¥The only time you would compare a variable against itself is when you are testing for
NaN. However, it is far more appropriate to usetypeof x === 'number' && Number.isNaN(x)for that use case rather than leaving the reader of the code to determine the intent of self comparison.
¥Examples
¥Invalid
if (x === x) {}code-block.js:1:5 lint/suspicious/noSelfCompare ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Comparing to itself is potentially pointless.
> 1 │ if (x === x) {}
│ ^^^^^^^
2 │
if (a.b.c() !== a.b .c()) {}code-block.js:1:5 lint/suspicious/noSelfCompare ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Comparing to itself is potentially pointless.
> 1 │ if (a.b.c() !== a.b .c()) {}
│ ^^^^^^^^^^^^^^^^^^^^
2 │
¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号