Skip to content

Commit

Permalink
fix(linter): fix incorrect fixer for prefer-regexp-test (#7898)
Browse files Browse the repository at this point in the history
closes #7793
  • Loading branch information
camc314 authored Dec 15, 2024
1 parent 8ca8fce commit 9c9b73d
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions crates/oxc_linter/src/rules/unicorn/prefer_regexp_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use oxc_ast::{
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use oxc_span::{GetSpan, Span};

use crate::{ast_util::outermost_paren_parent, context::LintContext, rule::Rule, AstNode};

Expand Down Expand Up @@ -147,11 +147,28 @@ impl Rule for PreferRegexpTest {
return;
}
}
_ => unreachable!("match or test {:?}", name),
_ => unreachable!("expected match or test, got: {:?}", name),
}

ctx.diagnostic_with_fix(prefer_regexp_test_diagnostic(span), |fixer| {
fixer.replace(span, "test")
if name.as_str() == "exec" {
return fixer.replace(span, "test");
}
let mut fix = fixer.new_fix_with_capacity(3);

fix.push(fixer.replace(span, "test"));

fix.push(fixer.replace(
call_expr.arguments[0].span(),
fixer.source_range(member_expr.object().span()),
));

fix.push(fixer.replace(
member_expr.object().span(),
fixer.source_range(call_expr.arguments[0].span()),
));

fix
});
}
}
Expand Down Expand Up @@ -242,34 +259,35 @@ fn test() {
];

let fix = vec![
("const re = /a/; const bar = !foo.match(re)", "const re = /a/; const bar = !foo.test(re)"),
("const re = /a/; const bar = !foo.match(re)", "const re = /a/; const bar = !re.test(foo)"),
(
"const re = /a/; const bar = Boolean(foo.match(re))",
"const re = /a/; const bar = Boolean(foo.test(re))",
"const re = /a/; const bar = Boolean(re.test(foo))",
),
("const re = /a/; if (foo.match(re)) {}", "const re = /a/; if (foo.test(re)) {}"),
("const re = /a/; if (foo.match(re)) {}", "const re = /a/; if (re.test(foo)) {}"),
(
"const re = /a/; const bar = foo.match(re) ? 1 : 2",
"const re = /a/; const bar = foo.test(re) ? 1 : 2",
"const re = /a/; const bar = re.test(foo) ? 1 : 2",
),
(
"const re = /a/; while (foo.match(re)) foo = foo.slice(1);",
"const re = /a/; while (foo.test(re)) foo = foo.slice(1);",
"const re = /a/; while (re.test(foo)) foo = foo.slice(1);",
),
(
"const re = /a/; do {foo = foo.slice(1)} while (foo.match(re));",
"const re = /a/; do {foo = foo.slice(1)} while (foo.test(re));",
"const re = /a/; do {foo = foo.slice(1)} while (re.test(foo));",
),
(
"const re = /a/; for (; foo.match(re); ) foo = foo.slice(1);",
"const re = /a/; for (; foo.test(re); ) foo = foo.slice(1);",
"const re = /a/; for (; re.test(foo); ) foo = foo.slice(1);",
),
("const re = /a/; const bar = !re.exec(foo)", "const re = /a/; const bar = !re.test(foo)"),
(
"const re = /a/; const bar = Boolean(re.exec(foo))",
"const re = /a/; const bar = Boolean(re.test(foo))",
),
("const re = /a/; if (re.exec(foo)) {}", "const re = /a/; if (re.test(foo)) {}"),
("const re = /a/; if (someStr.match(re)) {}", "const re = /a/; if (re.test(someStr)) {}"),
];

Tester::new(PreferRegexpTest::NAME, PreferRegexpTest::CATEGORY, pass, fail)
Expand Down

0 comments on commit 9c9b73d

Please sign in to comment.