fix(linter) Fix false positives in prefer string start, ends with, port more test cases (#1689)

closes #1687 - uses `intersection` instead of `||`:
 - this improves performance as it combines the flags into a single bitmask instead of doing two seperate checks
 - 

Adds missing test cases from eslint-plugin-unicorn
This commit is contained in:
Cameron 2023-12-15 17:08:42 +00:00 committed by GitHub
parent d101acf833
commit 6268f3f2f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 3 deletions

View file

@ -90,9 +90,7 @@ enum ErrorKind {
}
fn check_regex(regexp_lit: &RegExpLiteral) -> Option<ErrorKind> {
if regexp_lit.regex.flags.contains(RegExpFlags::I)
|| regexp_lit.regex.flags.contains(RegExpFlags::M)
{
if regexp_lit.regex.flags.intersects(RegExpFlags::I | RegExpFlags::M) {
return None;
}
@ -123,6 +121,7 @@ fn test() {
use crate::tester::Tester;
let pass = vec![
// Unicorn Tests
r#"foo.startsWith("bar")"#,
r#"foo.endsWith("bar")"#,
r#"reject(new Error("foo"))"#,
@ -133,10 +132,33 @@ fn test() {
r"foo()()",
r"if (foo.match(/^foo/)) {}",
r"if (/^foo/.exec(foo)) {}",
r"/foo/.test(bar)",
r"/^foo$/.test(bar)",
r"/^foo+/.test(bar)",
r"/foo+$/.test(bar)",
r"/^[,af]/.test(bar)",
r"/[,af]$/.test(bar)",
r"/^\w/.test(bar)",
r"/\w$/.test(bar)",
r"/^foo./.test(bar)",
r"/foo.$/.test(bar)",
r"/\^foo/.test(bar)",
r"/^foo/i.test(bar)",
r"/^foo/m.test(bar)",
r"/^foo/im.test(bar)",
r"/^A|B/.test(bar)",
r"/A|B$/.test(bar)",
// Additional tests
r"/^http/i.test(uri)",
];
let fail = vec![
r"/^foo/.test(bar)",
r"/foo$/.test(bar)",
r"/^!/.test(bar)",
r"/!$/.test(bar)",
r"/^ /.test(bar)",
r"/ $/.test(bar)",
r"const foo = {}; /^abc/.test(foo);",
r"const foo = 123; /^abc/.test(foo);",
r#"const foo = "hello"; /^abc/.test(foo);"#,

View file

@ -2,6 +2,42 @@
source: crates/oxc_linter/src/tester.rs
expression: prefer_string_starts_ends_with
---
⚠ 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 │ /^foo/.test(bar)
· ────────────────
╰────
⚠ 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)
· ────────────────
╰────
⚠ 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)
· ──────────────
╰────
⚠ 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)
· ──────────────
╰────
⚠ 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)
· ──────────────
╰────
⚠ 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)
· ──────────────
╰────
⚠ 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 │ const foo = {}; /^abc/.test(foo);