Closed
Description
indexOf
and lastIndexOf
return -1 for "not found"; after autofix, the resulting code return the array length instead for "not found":
const arr = ["a", "b", "c", "d", "e"]; console.log(arr.reverse().indexOf("f"));
becomes
const arr = ["a", "b", "c", "d", "e"]; console.log(arr.length - 1 - arr.lastIndexOf("f"));
printing 5 rather than -1.
If reverse
+ indexOf
is run on an array literal, autofix will give broken output --- though I don't know whether this kind of input should be worth caring about:
console.log(["a", "b", "c", "d", "e"].reverse().indexOf("a"));
becomes
console.log(undefined.length - 1 - ["a", "b", "c", "d", "e"].lastIndexOf("a"));