Open andrmoel opened 10 months ago
your code is already invalid
if length = 0 true > 0
if length > 0 false > 0
your code is already invalid
if length = 0
true > 0
if length > 0false > 0
Sorry but I don't get it. My code works as expected. I might give a better example:
const foo = [];
if (!foo.length > 0) {
console.log('foo is empty');
} else {
console.log('foo is not empty');
}
// Result "foo is empty"
Expected outcome after autofix:
const foo = [];
if (foo.length === 0) {
console.log('foo is empty');
} else {
console.log('foo is not empty');
}
// Result "foo is empty"
You can add some items to the foo array and you will get the other console log.
Ok get it!
!foo.length
should be transpilled to foo.length === 0
Make sense. So the >
operator is unnecessary.
The autofix is correct, !length
produces the same result as length === 0
. As mentioned before, your code is wrong; use !foo.length
OR foo.length > 0
, not both.
Strictly speaking, the auto fix isn't correct,
if (foo.length === 0 > 0) ;
means
if (foo.length === (0 > 0)) ;
not
if ((foo.length === 0) > 0) ;
Update: This means the autofix also breaks cases uses operators between !
and ===
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_precedence
Maybe we should keep this open for edge cases.
You're right, I did some tests but did not finish my boolean table
!3 > 0 | 🔴 false |
3 === 0 > 0 | 🔴 false |
!0 > 0 | 🟢 true |
0 === 0 > 0 | 🔴 false |
There is a wrong autofix for the
explicit-length-check
rule.My code:
Code after autofix:
Expected replacement