Skip to content

prefer-string-slice bad autofix from .substr() #2291

@alex-kinokon

Description

@alex-kinokon
let string = "::";

// Prefer `String#slice()` over `String#substr()`.
// eslint(unicorn/prefer-string-slice)
let eslintError = string.substr(-2, 2);
                  ^^^^^^^^^^^^^^^^^^^^
// After applying fix:
let fixed = string.slice(-2, -2 + 2);

eslintError // "::"
fixed // ""

// Uncaught AssertionError [ERR_ASSERTION]: '::' == ''
assert.equal(eslintError, fixed)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions