Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autofix for avoid-reverse (still) has unsafe edge case #26

Closed
arild-haugstad opened this issue Jan 16, 2019 · 1 comment
Closed

autofix for avoid-reverse (still) has unsafe edge case #26

arild-haugstad opened this issue Jan 16, 2019 · 1 comment
Labels

Comments

@arild-haugstad
Copy link

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"));

@freaktechnik
Copy link
Owner

freaktechnik commented Jan 16, 2019

Ugh, you're right. I guess the real solution is just to avoid lastIndexOf all together.

though I don't know whether this kind of input should be worth caring about

I think there should actually be a rule to not allow "unnecessary" operations on literals, like reversing or empty slices.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants