Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(linter): fix incorrect fixer for prefer-regexp-test #7898

Merged
Merged
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
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
Loading