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
56 changes: 51 additions & 5 deletions crates/oxc_linter/src/rules/unicorn/prefer_string_slice.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use oxc_ast::{AstKind, ast::MemberExpression};
use oxc_ast::{
AstKind,
ast::{Argument, Expression, MemberExpression},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
Expand Down Expand Up @@ -36,7 +39,7 @@ declare_oxc_lint!(
PreferStringSlice,
unicorn,
pedantic,
fix
conditional_fix
);

impl Rule for PreferStringSlice {
Expand All @@ -53,14 +56,58 @@ impl Rule for PreferStringSlice {
if !matches!(v.property.name.as_str(), "substr" | "substring") {
return;
}
let method_name = v.property.name.as_str();
let has_spread_arguments = call_expr.arguments.iter().any(Argument::is_spread);
let has_unsafe_arguments = match method_name {
"substr" => has_spread_arguments || call_expr.arguments.len() >= 2,
"substring" => {
has_spread_arguments || !is_safe_substring_arguments(&call_expr.arguments)
}
_ => unreachable!(),
};

if has_unsafe_arguments {
ctx.diagnostic(prefer_string_slice_diagnostic(v.property.span, method_name));
return;
}

ctx.diagnostic_with_fix(
prefer_string_slice_diagnostic(v.property.span, v.property.name.as_str()),
prefer_string_slice_diagnostic(v.property.span, method_name),
|fixer| fixer.replace(v.property.span, "slice"),
);
}
}
}

fn is_safe_substring_arguments(arguments: &[Argument<'_>]) -> bool {
match arguments {
[] => true,
[start] => get_non_negative_integer_argument(start).is_some(),
[start, end] => {
let Some(start_value) = get_non_negative_integer_argument(start) else {
return false;
};
let Some(end_value) = get_non_negative_integer_argument(end) else {
return false;
};
start_value <= end_value
}
_ => false,
}
}

fn get_non_negative_integer_argument(argument: &Argument<'_>) -> Option<f64> {
if !argument.is_expression() {
return None;
}

let Expression::NumericLiteral(number) = argument.to_expression().get_inner_expression() else {
return None;
};

if number.value >= 0.0 && number.value.fract() == 0.0 { Some(number.value) } else { None }
}

#[test]
fn test() {
use crate::tester::Tester;
Expand Down Expand Up @@ -90,6 +137,7 @@ fn test() {
r#""foo".substr()"#,
r#""foo".substr(1)"#,
r#""foo".substr(1, 2)"#,
r#""foo".substr(11, 8)"#,
r#""foo".substr(bar.length, Math.min(baz, 100))"#,
r#""foo".substr(1, length)"#,
r#""foo".substr(1, "abc".length)"#,
Expand Down Expand Up @@ -168,7 +216,6 @@ fn test() {
// "foo".slice(0, Math.max(0, length))"#,
// ),
// (r#""foo".substr(0, -1)"#, r#""foo".slice(0, 0)"#),
(r#""foo".substr(0, "foo".length)"#, r#""foo".slice(0, "foo".length)"#),
(
"const uri = 'foo';
((uri || '')).substr(1)",
Expand All @@ -192,7 +239,6 @@ fn test() {
// (r#""foo".substring(-1, -5)"#, r#""foo".slice(0, 0)"#),
// (r#""foo".substring(-1, 2)"#, r#""foo".slice(0, 2)"#),
// (r#""foo".substring(length)"#, r#""foo".slice(Math.max(0, length))"#),
(r#""foo".substring("fo".length)"#, r#""foo".slice("fo".length)"#), // spellchecker:disable-line
// TODO: Get this passing.
// (r#""foo".substring(0, length)"#, r#""foo".slice(0, Math.max(0, length))"#),
// (r#""foo".substring(length, 0)"#, r#""foo".slice(0, Math.max(0, length))"#),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,64 +84,61 @@ source: crates/oxc_linter/src/tester.rs
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(11, 8)
· ──────
╰────

⚠ 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:2:19]
1 │ const length = 123;
2 │ "foo".substr(1, length - 4)
· ──────
╰────
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:2:27]
Expand Down Expand Up @@ -170,28 +167,24 @@ source: crates/oxc_linter/src/tester.rs
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]
Expand Down Expand Up @@ -226,56 +219,48 @@ source: crates/oxc_linter/src/tester.rs
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]
Expand All @@ -289,7 +274,6 @@ source: crates/oxc_linter/src/tester.rs
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]
Expand All @@ -303,7 +287,6 @@ source: crates/oxc_linter/src/tester.rs
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:2:40]
Expand All @@ -330,75 +313,64 @@ source: crates/oxc_linter/src/tester.rs
· ─────────
3 │ /* 9 */ (( /* 10 */ bar /* 11 */ )) /* 12 */,
╰────
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`.

⚠ eslint-plugin-unicorn(prefer-string-slice): Prefer String#slice() over String#substr()
╭─[prefer_string_slice.tsx:2:35]
1 │ const string = "::";
2 │ const output = string.substr(-2, 2);
· ──────
╰────
help: Replace `substr` with `slice`.
Loading