feat(linter/unicorn): add fixer to prefer-string-slice (#5150)

This commit is contained in:
Cameron 2024-08-24 02:37:35 +01:00 committed by GitHub
parent 34bfaf6792
commit 22d57f9e5e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 67 additions and 3 deletions

View file

@ -34,7 +34,7 @@ declare_oxc_lint!(
/// ```
PreferStringSlice,
pedantic,
pending
fix
);
impl Rule for PreferStringSlice {
@ -57,7 +57,9 @@ impl Rule for PreferStringSlice {
_ => return,
};
ctx.diagnostic(prefer_string_slice_diagnostic(span, name.as_str()));
ctx.diagnostic_with_fix(prefer_string_slice_diagnostic(span, name.as_str()), |fixer| {
fixer.replace(span, "slice")
});
}
}
@ -130,5 +132,17 @@ fn test() {
r"foo.substring((10, bar))",
];
Tester::new(PreferStringSlice::NAME, pass, fail).test_and_snapshot();
let fix = vec![
("foo.substr()", "foo.slice()"),
("foo?.substr()", "foo?.slice()"),
("foo.bar?.substring()", "foo.bar?.slice()"),
("foo?.[0]?.substring()", "foo?.[0]?.slice()"),
("foo.bar.substr?.()", "foo.bar.slice?.()"),
("foo.bar?.substring?.()", "foo.bar?.slice?.()"),
("foo.bar?.baz?.substr()", "foo.bar?.baz?.slice()"),
("foo.bar?.baz.substring()", "foo.bar?.baz.slice()"),
("foo.bar.baz?.substr()", "foo.bar.baz?.slice()"),
];
Tester::new(PreferStringSlice::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
}

View file

@ -6,297 +6,347 @@ source: crates/oxc_linter/src/tester.rs
1 │ foo.substr()
· ──────
╰────
help: Replace `substr` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substr()
╭─[prefer_string_slice.tsx:1:6]
1 │ foo?.substr()
· ──────
╰────
help: Replace `substr` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substring()
╭─[prefer_string_slice.tsx:1:10]
1 │ foo.bar?.substring()
· ─────────
╰────
help: Replace `substring` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substring()
╭─[prefer_string_slice.tsx:1:11]
1 │ foo?.[0]?.substring()
· ─────────
╰────
help: Replace `substring` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substr()
╭─[prefer_string_slice.tsx:1:9]
1 │ foo.bar.substr?.()
· ──────
╰────
help: Replace `substr` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substring()
╭─[prefer_string_slice.tsx:1:10]
1 │ foo.bar?.substring?.()
· ─────────
╰────
help: Replace `substring` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substr()
╭─[prefer_string_slice.tsx:1:15]
1 │ foo.bar?.baz?.substr()
· ──────
╰────
help: Replace `substr` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substring()
╭─[prefer_string_slice.tsx:1:14]
1 │ foo.bar?.baz.substring()
· ─────────
╰────
help: Replace `substring` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substr()
╭─[prefer_string_slice.tsx:1:14]
1 │ foo.bar.baz?.substr()
· ──────
╰────
help: Replace `substr` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substr()
╭─[prefer_string_slice.tsx:1:7]
1 │ "foo".substr()
· ──────
╰────
help: Replace `substr` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substr()
╭─[prefer_string_slice.tsx:1:7]
1 │ "foo".substr(1)
· ──────
╰────
help: Replace `substr` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substr()
╭─[prefer_string_slice.tsx:1:7]
1 │ "foo".substr(1, 2)
· ──────
╰────
help: Replace `substr` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substr()
╭─[prefer_string_slice.tsx:1:7]
1 │ "foo".substr(bar.length, Math.min(baz, 100))
· ──────
╰────
help: Replace `substr` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substr()
╭─[prefer_string_slice.tsx:1:7]
1 │ "foo".substr(1, length)
· ──────
╰────
help: Replace `substr` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substr()
╭─[prefer_string_slice.tsx:1:7]
1 │ "foo".substr(1, "abc".length)
· ──────
╰────
help: Replace `substr` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substr()
╭─[prefer_string_slice.tsx:1:7]
1 │ "foo".substr("1", 2)
· ──────
╰────
help: Replace `substr` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substr()
╭─[prefer_string_slice.tsx:1:7]
1 │ "foo".substr(0, -1)
· ──────
╰────
help: Replace `substr` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substr()
╭─[prefer_string_slice.tsx:1:7]
1 │ "foo".substr(0, "foo".length)
· ──────
╰────
help: Replace `substr` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substr()
╭─[prefer_string_slice.tsx:1:7]
1 │ "foo".substr(1, length)
· ──────
╰────
help: Replace `substr` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substr()
╭─[prefer_string_slice.tsx:1:5]
1 │ foo.substr(start)
· ──────
╰────
help: Replace `substr` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substr()
╭─[prefer_string_slice.tsx:1:7]
1 │ "foo".substr(1)
· ──────
╰────
help: Replace `substr` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substr()
╭─[prefer_string_slice.tsx:1:5]
1 │ foo.substr(start, length)
· ──────
╰────
help: Replace `substr` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substr()
╭─[prefer_string_slice.tsx:1:7]
1 │ "foo".substr(1, 2)
· ──────
╰────
help: Replace `substr` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substr()
╭─[prefer_string_slice.tsx:1:5]
1 │ foo.substr(1, 2, 3)
· ──────
╰────
help: Replace `substr` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substr()
╭─[prefer_string_slice.tsx:1:10]
1 │ "Sample".substr(0, "Sample".lastIndexOf("/"))
· ──────
╰────
help: Replace `substr` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substring()
╭─[prefer_string_slice.tsx:1:5]
1 │ foo.substring()
· ─────────
╰────
help: Replace `substring` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substring()
╭─[prefer_string_slice.tsx:1:7]
1 │ "foo".substring()
· ─────────
╰────
help: Replace `substring` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substring()
╭─[prefer_string_slice.tsx:1:7]
1 │ "foo".substring(1)
· ─────────
╰────
help: Replace `substring` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substring()
╭─[prefer_string_slice.tsx:1:7]
1 │ "foo".substring(1, 2)
· ─────────
╰────
help: Replace `substring` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substring()
╭─[prefer_string_slice.tsx:1:7]
1 │ "foo".substring(2, 1)
· ─────────
╰────
help: Replace `substring` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substring()
╭─[prefer_string_slice.tsx:1:7]
1 │ "foo".substring(-1, -5)
· ─────────
╰────
help: Replace `substring` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substring()
╭─[prefer_string_slice.tsx:1:7]
1 │ "foo".substring(-1, 2)
· ─────────
╰────
help: Replace `substring` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substring()
╭─[prefer_string_slice.tsx:1:7]
1 │ "foo".substring(length)
· ─────────
╰────
help: Replace `substring` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substring()
╭─[prefer_string_slice.tsx:1:10]
1 │ "foobar".substring("foo".length)
· ─────────
╰────
help: Replace `substring` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substring()
╭─[prefer_string_slice.tsx:1:7]
1 │ "foo".substring(0, length)
· ─────────
╰────
help: Replace `substring` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substring()
╭─[prefer_string_slice.tsx:1:7]
1 │ "foo".substring(length, 0)
· ─────────
╰────
help: Replace `substring` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substring()
╭─[prefer_string_slice.tsx:1:5]
1 │ foo.substring(start)
· ─────────
╰────
help: Replace `substring` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substring()
╭─[prefer_string_slice.tsx:1:7]
1 │ "foo".substring(1)
· ─────────
╰────
help: Replace `substring` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substring()
╭─[prefer_string_slice.tsx:1:5]
1 │ foo.substring(start, end)
· ─────────
╰────
help: Replace `substring` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substring()
╭─[prefer_string_slice.tsx:1:7]
1 │ "foo".substring(1, 3)
· ─────────
╰────
help: Replace `substring` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substring()
╭─[prefer_string_slice.tsx:1:5]
1 │ foo.substring(1, 2, 3)
· ─────────
╰────
help: Replace `substring` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substr()
╭─[prefer_string_slice.tsx:1:5]
1 │ foo.substr(0, ...bar)
· ──────
╰────
help: Replace `substr` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substr()
╭─[prefer_string_slice.tsx:1:5]
1 │ foo.substr(...bar)
· ──────
╰────
help: Replace `substr` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substr()
╭─[prefer_string_slice.tsx:1:5]
1 │ foo.substr(0, (100, 1))
· ──────
╰────
help: Replace `substr` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substr()
╭─[prefer_string_slice.tsx:1:5]
1 │ foo.substr(0, 1, extraArgument)
· ──────
╰────
help: Replace `substr` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substr()
╭─[prefer_string_slice.tsx:1:5]
1 │ foo.substr((0, bar.length), (0, baz.length))
· ──────
╰────
help: Replace `substr` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substring()
╭─[prefer_string_slice.tsx:1:5]
1 │ foo.substring((10, 1), 0)
· ─────────
╰────
help: Replace `substring` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substring()
╭─[prefer_string_slice.tsx:1:5]
1 │ foo.substring(0, (10, 1))
· ─────────
╰────
help: Replace `substring` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substring()
╭─[prefer_string_slice.tsx:1:5]
1 │ foo.substring(0, await 1)
· ─────────
╰────
help: Replace `substring` with `slice`.
⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substring()
╭─[prefer_string_slice.tsx:1:5]
1 │ foo.substring((10, bar))
· ─────────
╰────
help: Replace `substring` with `slice`.