no-self-compare
禁止双方完全相同的比较
将变量与自身进行比较通常是错误的,要么是拼写错误,要么是重构错误。这会让读者感到困惑,并且可能会引入运行时错误。
¥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' && isNaN(x)
或 Number.isNaN ES2015 函数 比让代码的读者来确定自我比较的意图要合适得多。
¥The only time you would compare a variable against itself is when you are testing for NaN
. However, it is far more appropriate to use typeof x === 'number' && isNaN(x)
or the Number.isNaN ES2015 function for that use case rather than leaving the reader of the code to determine the intent of self comparison.
规则详情
¥Rule Details
引发此错误以高亮可能令人困惑且可能毫无意义的代码段。几乎没有情况需要你将某物与自身进行比较。
¥This error is raised to highlight a potentially confusing and potentially pointless piece of code. There are almost no situations in which you would need to compare something to itself.
此规则的错误代码示例:
¥Examples of incorrect code for this rule:
/*eslint no-self-compare: "error"*/
var x = 10;
if (x === x) {
x = 20;
}
版本
此规则是在 ESLint v0.0.9 中引入。