Skip to content

Commit

Permalink
feat(linter/unicorn): add fixer to prefer-string-slice (#5150)
Browse files Browse the repository at this point in the history
  • Loading branch information
camc314 committed Aug 24, 2024
1 parent 34bfaf6 commit 22d57f9
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
20 changes: 17 additions & 3 deletions crates/oxc_linter/src/rules/unicorn/prefer_string_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ declare_oxc_lint!(
/// ```
PreferStringSlice,
pedantic,
pending
fix
);

impl Rule for PreferStringSlice {
Expand All @@ -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")
});
}
}

Expand Down Expand Up @@ -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();
}
50 changes: 50 additions & 0 deletions crates/oxc_linter/src/snapshots/prefer_string_slice.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,297 +6,347 @@ source: crates/oxc_linter/src/tester.rs
1foo.substr()
· ──────
╰────
help: Replace `substr` with `slice`.

eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substr()
╭─[prefer_string_slice.tsx:1:6]
1foo?.substr()
· ──────
╰────
help: Replace `substr` with `slice`.

eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substring()
╭─[prefer_string_slice.tsx:1:10]
1foo.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]
1foo?.[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]
1foo.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]
1foo.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]
1foo.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]
1foo.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]
1foo.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]
1foo.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]
1foo.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]
1foo.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]
1foo.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]
1foo.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]
1foo.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]
1foo.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]
1foo.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]
1foo.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]
1foo.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]
1foo.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]
1foo.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]
1foo.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]
1foo.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]
1foo.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]
1foo.substring((10, bar))
· ─────────
╰────
help: Replace `substring` with `slice`.

0 comments on commit 22d57f9

Please sign in to comment.