Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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`.