fix(linter): no-useless-escape: do not crash on backslash character (#6048)

Fixes https://github.com/oxc-project/oxc/issues/6046

Unlike other escape sequences such as `/\w/`, this is not actually parsed as a single character: `/\c/` so we incorrectly try to get the character before the backslash, which doesn't exist.
This commit is contained in:
camchenry 2024-09-24 23:25:49 +00:00
parent 58d333a533
commit c047d423b1

View file

@ -190,7 +190,8 @@ fn check_character(
unicode_sets: bool,
) -> Option<Span> {
let char_text = character.span.source_text(ctx.source_text());
let is_escaped = char_text.starts_with('\\');
// The character is escaped if it has at least two characters and the first character is a backslash
let is_escaped = char_text.starts_with('\\') && char_text.len() >= 2;
if !is_escaped {
return None;
}
@ -573,6 +574,8 @@ fn test() {
r"/[[\-]\-]/v", // { "ecmaVersion": 2024 },
r"/[\^]/v", // { "ecmaVersion": 2024 }
r"/[\s\-(]/", // https://github.com/oxc-project/oxc/issues/5227
r"/\c/", // https://github.com/oxc-project/oxc/issues/6046
r"/\\c/", // https://github.com/oxc-project/oxc/issues/6046
];
let fail = vec![