Skip to content

Commit

Permalink
feat(linter/unicorn): add fixer to prefer-regexp-test (#5151)
Browse files Browse the repository at this point in the history
  • Loading branch information
camc314 committed Aug 23, 2024
1 parent 27db769 commit d35c6f5
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions crates/oxc_linter/src/rules/unicorn/prefer_regexp_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ declare_oxc_lint!(
/// ```
PreferRegexpTest,
pedantic,
pending
fix
);

impl Rule for PreferRegexpTest {
Expand Down Expand Up @@ -147,7 +147,9 @@ impl Rule for PreferRegexpTest {
_ => unreachable!("match or test {:?}", name),
}

ctx.diagnostic(prefer_regexp_test_diagnostic(span));
ctx.diagnostic_with_fix(prefer_regexp_test_diagnostic(span), |fixer| {
fixer.replace(span, "test")
});
}
}

Expand Down Expand Up @@ -236,5 +238,36 @@ fn test() {
r"!/a/v.exec(foo)",
];

Tester::new(PreferRegexpTest::NAME, pass, fail).test_and_snapshot();
let fix = vec![
("const re = /a/; const bar = !foo.match(re)", "const re = /a/; const bar = !foo.test(re)"),
(
"const re = /a/; const bar = Boolean(foo.match(re))",
"const re = /a/; const bar = Boolean(foo.test(re))",
),
("const re = /a/; if (foo.match(re)) {}", "const re = /a/; if (foo.test(re)) {}"),
(
"const re = /a/; const bar = foo.match(re) ? 1 : 2",
"const re = /a/; const bar = foo.test(re) ? 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/; do {foo = foo.slice(1)} while (foo.match(re));",
"const re = /a/; do {foo = foo.slice(1)} while (foo.test(re));",
),
(
"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/; 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)) {}"),
];

Tester::new(PreferRegexpTest::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
}

0 comments on commit d35c6f5

Please sign in to comment.