fix(linter): Panic in prefer string starts, ends with (#1684)

Closes #1683
This commit is contained in:
Cameron 2023-12-15 11:35:01 +00:00 committed by GitHub
parent 0f77e5dc52
commit 0a8746c751
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View file

@ -102,7 +102,7 @@ fn check_regex(regexp_lit: &RegExpLiteral) -> Option<ErrorKind> {
if regexp_lit.regex.pattern.ends_with('$')
&& is_simple_string(
&regexp_lit.regex.pattern.as_str()[0..regexp_lit.regex.pattern.len() - 2],
&regexp_lit.regex.pattern.as_str()[0..regexp_lit.regex.pattern.len() - 1],
)
{
return Some(ErrorKind::EndsWith);
@ -167,6 +167,8 @@ fn test() {
r#"/^a/v.test("string")"#,
r"/a$/.test(`${unknown}`)",
r"/a$/.test(String(unknown))",
r"const a = /你$/.test('a');",
r"const a = /^你/.test('a');",
];
Tester::new_without_config(PreferStringStartsEndsWith::NAME, pass, fail).test_and_snapshot();

View file

@ -200,4 +200,16 @@ expression: prefer_string_starts_ends_with
· ──────────────────────────
╰────
⚠ 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 │ const a = /你$/.test('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 │ const a = /^你/.test('a');
· ───────────────
╰────