Skip to content

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

Closed
@arild-haugstad

Description

@arild-haugstad

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions