From 0a8746c7513d83040103cc60d2abb1986c3a6c76 Mon Sep 17 00:00:00 2001 From: Cameron Date: Fri, 15 Dec 2023 11:35:01 +0000 Subject: [PATCH] fix(linter): Panic in prefer string starts, ends with (#1684) Closes #1683 --- .../rules/unicorn/prefer_string_starts_ends_with.rs | 4 +++- .../snapshots/prefer_string_starts_ends_with.snap | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/oxc_linter/src/rules/unicorn/prefer_string_starts_ends_with.rs b/crates/oxc_linter/src/rules/unicorn/prefer_string_starts_ends_with.rs index 7e9b678e4..e2bdc900d 100644 --- a/crates/oxc_linter/src/rules/unicorn/prefer_string_starts_ends_with.rs +++ b/crates/oxc_linter/src/rules/unicorn/prefer_string_starts_ends_with.rs @@ -102,7 +102,7 @@ fn check_regex(regexp_lit: &RegExpLiteral) -> Option { if regexp_lit.regex.pattern.ends_with('$') && is_simple_string( - ®exp_lit.regex.pattern.as_str()[0..regexp_lit.regex.pattern.len() - 2], + ®exp_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(); diff --git a/crates/oxc_linter/src/snapshots/prefer_string_starts_ends_with.snap b/crates/oxc_linter/src/snapshots/prefer_string_starts_ends_with.snap index 607abf822..eb506044f 100644 --- a/crates/oxc_linter/src/snapshots/prefer_string_starts_ends_with.snap +++ b/crates/oxc_linter/src/snapshots/prefer_string_starts_ends_with.snap @@ -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'); + · ─────────────── + ╰──── +