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
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use oxc_allocator::Allocator;
use oxc_ast::{
AstKind,
AstBuilder, AstKind,
ast::{CallExpression, Expression, MemberExpression, RegExpFlags, RegExpLiteral},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_regular_expression::ast::{BoundaryAssertionKind, Term};
use oxc_span::{GetSpan, Span};
use oxc_span::{GetSpan, SPAN, Span};

use crate::{
AstNode,
Expand Down Expand Up @@ -116,10 +117,15 @@ fn do_fix<'a>(
}
};
let Some(argument) = argument else { return fixer.noop() };
let fix_text = format!(r#"{}.{}("{}")"#, fixer.source_range(target_span), method, argument);

fixer.replace(call_expr.span, fix_text)
let mut content = fixer.codegen();
let alloc = Allocator::default();
let ast = AstBuilder::new(&alloc);
content.print_str(&format!(r"{}.{}(", fixer.source_range(target_span), method));
content.print_expression(&ast.expression_string_literal(SPAN, argument, None));
content.print_str(r")");
fixer.replace(call_expr.span, content)
}

fn can_replace(call_expr: &CallExpression) -> Option<Span> {
if call_expr.arguments.len() != 1 {
return None;
Expand Down Expand Up @@ -279,16 +285,24 @@ fn test() {
];

let fix = vec![
("/^foo/.test(x)", r#"x.startsWith("foo")"#, None),
("/foo$/.test(x)", r#"x.endsWith("foo")"#, None),
("/^foo/.test(x.y)", r#"x.y.startsWith("foo")"#, None),
("/foo$/.test(x.y)", r#"x.y.endsWith("foo")"#, None),
("/^foo/.test('x')", r#"'x'.startsWith("foo")"#, None),
("/foo$/.test('x')", r#"'x'.endsWith("foo")"#, None),
("/^foo/.test(`x${y}`)", r#"`x${y}`.startsWith("foo")"#, None),
("/foo$/.test(`x${y}`)", r#"`x${y}`.endsWith("foo")"#, None),
("/^foo/.test(String(x))", r#"String(x).startsWith("foo")"#, None),
("/foo$/.test(String(x))", r#"String(x).endsWith("foo")"#, None),
("/^foo/.test(x)", r"x.startsWith('foo')", None),
("/foo$/.test(x)", r"x.endsWith('foo')", None),
("/^foo/.test(x.y)", r"x.y.startsWith('foo')", None),
("/foo$/.test(x.y)", r"x.y.endsWith('foo')", None),
("/^foo/.test('x')", r"'x'.startsWith('foo')", None),
("/foo$/.test('x')", r"'x'.endsWith('foo')", None),
("/^foo/.test(`x${y}`)", r"`x${y}`.startsWith('foo')", None),
("/foo$/.test(`x${y}`)", r"`x${y}`.endsWith('foo')", None),
("/^foo/.test(String(x))", r"String(x).startsWith('foo')", None),
("/foo$/.test(String(x))", r"String(x).endsWith('foo')", None),
// https://github.com/oxc-project/oxc/issues/10523
(
r"const makePosix = str => /^\\\\\?\\/.test(str)",
r"const makePosix = str => str.startsWith('\\\\?\\')",
None,
),
("/^'/.test('foo')", r"'foo'.startsWith('\'')", None),
(r#"/^"/.test('foo')"#, r#"'foo'.startsWith('"')"#, None),
// should not get fixed
("/^foo/.test(new String('bar'))", "/^foo/.test(new String('bar'))", None),
("/^foo/.test(x as string)", "/^foo/.test(x as string)", None),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,91 +6,91 @@ source: crates/oxc_linter/src/tester.rs
1 │ /^foo/.test(bar)
· ───────────
╰────
help: Replace `/^foo/.test(bar)` with `bar.startsWith("foo")`.
help: Replace `/^foo/.test(bar)` with `bar.startsWith('foo')`.

⚠ eslint-plugin-unicorn(prefer-string-starts-ends-with): Prefer String#endsWith over a regex with a dollar sign.
╭─[prefer_string_starts_ends_with.tsx:1:1]
1 │ /foo$/.test(bar)
· ───────────
╰────
help: Replace `/foo$/.test(bar)` with `bar.endsWith("foo")`.
help: Replace `/foo$/.test(bar)` with `bar.endsWith('foo')`.

⚠ eslint-plugin-unicorn(prefer-string-starts-ends-with): Prefer String#endsWith over a regex with a dollar sign.
╭─[prefer_string_starts_ends_with.tsx:1:1]
1 │ /\$$/.test(bar)
· ──────────
╰────
help: Replace `/\$$/.test(bar)` with `bar.endsWith("$")`.
help: Replace `/\$$/.test(bar)` with `bar.endsWith('$')`.

⚠ eslint-plugin-unicorn(prefer-string-starts-ends-with): Prefer String#startsWith over a regex with a caret.
╭─[prefer_string_starts_ends_with.tsx:1:1]
1 │ /^\^/.test(bar)
· ──────────
╰────
help: Replace `/^\^/.test(bar)` with `bar.startsWith("^")`.
help: Replace `/^\^/.test(bar)` with `bar.startsWith('^')`.

⚠ eslint-plugin-unicorn(prefer-string-starts-ends-with): Prefer String#startsWith over a regex with a caret.
╭─[prefer_string_starts_ends_with.tsx:1:1]
1 │ /^!/.test(bar)
· ─────────
╰────
help: Replace `/^!/.test(bar)` with `bar.startsWith("!")`.
help: Replace `/^!/.test(bar)` with `bar.startsWith('!')`.

⚠ eslint-plugin-unicorn(prefer-string-starts-ends-with): Prefer String#endsWith over a regex with a dollar sign.
╭─[prefer_string_starts_ends_with.tsx:1:1]
1 │ /!$/.test(bar)
· ─────────
╰────
help: Replace `/!$/.test(bar)` with `bar.endsWith("!")`.
help: Replace `/!$/.test(bar)` with `bar.endsWith('!')`.

⚠ eslint-plugin-unicorn(prefer-string-starts-ends-with): Prefer String#startsWith over a regex with a caret.
╭─[prefer_string_starts_ends_with.tsx:1:1]
1 │ /^ /.test(bar)
· ─────────
╰────
help: Replace `/^ /.test(bar)` with `bar.startsWith(" ")`.
help: Replace `/^ /.test(bar)` with `bar.startsWith(' ')`.

⚠ eslint-plugin-unicorn(prefer-string-starts-ends-with): Prefer String#endsWith over a regex with a dollar sign.
╭─[prefer_string_starts_ends_with.tsx:1:1]
1 │ / $/.test(bar)
· ─────────
╰────
help: Replace `/ $/.test(bar)` with `bar.endsWith(" ")`.
help: Replace `/ $/.test(bar)` with `bar.endsWith(' ')`.

⚠ eslint-plugin-unicorn(prefer-string-starts-ends-with): Prefer String#startsWith over a regex with a caret.
╭─[prefer_string_starts_ends_with.tsx:1:17]
1 │ const foo = {}; /^abc/.test(foo);
· ───────────
╰────
help: Replace `/^abc/.test(foo)` with `foo.startsWith("abc")`.
help: Replace `/^abc/.test(foo)` with `foo.startsWith('abc')`.

⚠ eslint-plugin-unicorn(prefer-string-starts-ends-with): Prefer String#startsWith over a regex with a caret.
╭─[prefer_string_starts_ends_with.tsx:1:18]
1 │ const foo = 123; /^abc/.test(foo);
· ───────────
╰────
help: Replace `/^abc/.test(foo)` with `foo.startsWith("abc")`.
help: Replace `/^abc/.test(foo)` with `foo.startsWith('abc')`.

⚠ eslint-plugin-unicorn(prefer-string-starts-ends-with): Prefer String#startsWith over a regex with a caret.
╭─[prefer_string_starts_ends_with.tsx:1:22]
1 │ const foo = "hello"; /^abc/.test(foo);
· ───────────
╰────
help: Replace `/^abc/.test(foo)` with `foo.startsWith("abc")`.
help: Replace `/^abc/.test(foo)` with `foo.startsWith('abc')`.

⚠ eslint-plugin-unicorn(prefer-string-starts-ends-with): Prefer String#startsWith over a regex with a caret.
╭─[prefer_string_starts_ends_with.tsx:1:1]
1 │ /^b/.test((a))
· ─────────
╰────
help: Replace `/^b/.test((a))` with `a.startsWith("b")`.
help: Replace `/^b/.test((a))` with `a.startsWith('b')`.

⚠ eslint-plugin-unicorn(prefer-string-starts-ends-with): Prefer String#startsWith over a regex with a caret.
╭─[prefer_string_starts_ends_with.tsx:1:1]
1 │ (/^b/).test((a))
· ───────────
╰────
help: Replace `(/^b/).test((a))` with `a.startsWith("b")`.
help: Replace `(/^b/).test((a))` with `a.startsWith('b')`.

⚠ eslint-plugin-unicorn(prefer-string-starts-ends-with): Prefer String#startsWith over a regex with a caret.
╭─[prefer_string_starts_ends_with.tsx:1:24]
Expand All @@ -109,7 +109,7 @@ source: crates/oxc_linter/src/tester.rs
1 │ /^a/.test("string")
· ─────────
╰────
help: Replace `/^a/.test("string")` with `"string".startsWith("a")`.
help: Replace `/^a/.test("string")` with `"string".startsWith('a')`.

⚠ eslint-plugin-unicorn(prefer-string-starts-ends-with): Prefer String#startsWith over a regex with a caret.
╭─[prefer_string_starts_ends_with.tsx:1:1]
Expand Down Expand Up @@ -176,14 +176,14 @@ source: crates/oxc_linter/src/tester.rs
1 │ /^a/.test(foo.bar)
· ─────────
╰────
help: Replace `/^a/.test(foo.bar)` with `foo.bar.startsWith("a")`.
help: Replace `/^a/.test(foo.bar)` with `foo.bar.startsWith('a')`.

⚠ eslint-plugin-unicorn(prefer-string-starts-ends-with): Prefer String#startsWith over a regex with a caret.
╭─[prefer_string_starts_ends_with.tsx:1:1]
1 │ /^a/.test(foo.bar())
· ─────────
╰────
help: Replace `/^a/.test(foo.bar())` with `foo.bar().startsWith("a")`.
help: Replace `/^a/.test(foo.bar())` with `foo.bar().startsWith('a')`.

⚠ eslint-plugin-unicorn(prefer-string-starts-ends-with): Prefer String#startsWith over a regex with a caret.
╭─[prefer_string_starts_ends_with.tsx:1:1]
Expand All @@ -202,7 +202,7 @@ source: crates/oxc_linter/src/tester.rs
1 │ /^a/.test(`string`)
· ─────────
╰────
help: Replace `/^a/.test(`string`)` with ``string`.startsWith("a")`.
help: Replace `/^a/.test(`string`)` with ``string`.startsWith('a')`.

⚠ eslint-plugin-unicorn(prefer-string-starts-ends-with): Prefer String#startsWith over a regex with a caret.
╭─[prefer_string_starts_ends_with.tsx:1:1]
Expand Down Expand Up @@ -245,53 +245,53 @@ source: crates/oxc_linter/src/tester.rs
1 │ /^a/u.test("string")
· ──────────
╰────
help: Replace `/^a/u.test("string")` with `"string".startsWith("a")`.
help: Replace `/^a/u.test("string")` with `"string".startsWith('a')`.

⚠ eslint-plugin-unicorn(prefer-string-starts-ends-with): Prefer String#startsWith over a regex with a caret.
╭─[prefer_string_starts_ends_with.tsx:1:1]
1 │ /^a/v.test("string")
· ──────────
╰────
help: Replace `/^a/v.test("string")` with `"string".startsWith("a")`.
help: Replace `/^a/v.test("string")` with `"string".startsWith('a')`.

⚠ eslint-plugin-unicorn(prefer-string-starts-ends-with): Prefer String#endsWith over a regex with a dollar sign.
╭─[prefer_string_starts_ends_with.tsx:1:1]
1 │ /a$/.test(`${unknown}`)
· ─────────
╰────
help: Replace `/a$/.test(`${unknown}`)` with ``${unknown}`.endsWith("a")`.
help: Replace `/a$/.test(`${unknown}`)` with ``${unknown}`.endsWith('a')`.

⚠ eslint-plugin-unicorn(prefer-string-starts-ends-with): Prefer String#endsWith over a regex with a dollar sign.
╭─[prefer_string_starts_ends_with.tsx:1:1]
1 │ /a$/.test(String(unknown))
· ─────────
╰────
help: Replace `/a$/.test(String(unknown))` with `String(unknown).endsWith("a")`.
help: Replace `/a$/.test(String(unknown))` with `String(unknown).endsWith('a')`.

⚠ eslint-plugin-unicorn(prefer-string-starts-ends-with): Prefer String#endsWith over a regex with a dollar sign.
╭─[prefer_string_starts_ends_with.tsx:1:11]
1 │ const a = /你$/.test('a');
· ──────────
╰────
help: Replace `/你$/.test('a')` with `'a'.endsWith("你")`.
help: Replace `/你$/.test('a')` with `'a'.endsWith('你')`.

⚠ eslint-plugin-unicorn(prefer-string-starts-ends-with): Prefer String#startsWith over a regex with a caret.
╭─[prefer_string_starts_ends_with.tsx:1:11]
1 │ const a = /^你/.test('a');
· ──────────
╰────
help: Replace `/^你/.test('a')` with `'a'.startsWith("你")`.
help: Replace `/^你/.test('a')` with `'a'.startsWith('你')`.

⚠ eslint-plugin-unicorn(prefer-string-starts-ends-with): Prefer String#startsWith over a regex with a caret.
╭─[prefer_string_starts_ends_with.tsx:1:5]
1 │ if (/^#/i.test(hex)) {}
· ──────────
╰────
help: Replace `/^#/i.test(hex)` with `hex.startsWith("#")`.
help: Replace `/^#/i.test(hex)` with `hex.startsWith('#')`.

⚠ eslint-plugin-unicorn(prefer-string-starts-ends-with): Prefer String#endsWith over a regex with a dollar sign.
╭─[prefer_string_starts_ends_with.tsx:1:5]
1 │ if (/#$/i.test(hex)) {}
· ──────────
╰────
help: Replace `/#$/i.test(hex)` with `hex.endsWith("#")`.
help: Replace `/#$/i.test(hex)` with `hex.endsWith('#')`.
Loading