fix(linter) fix panic in no hex escape (#1540)

🦀🦶🔫

lol https://twitter.com/boshen_c/status/1719033308682870891

closes #1539
This commit is contained in:
Cameron 2023-11-24 15:57:02 +00:00 committed by GitHub
parent 32b19561a8
commit 41bb0060eb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 2 deletions

View file

@ -44,7 +44,7 @@ declare_oxc_lint!(
fn check_escape(value: &str) -> Option<String> {
let mut in_escape = false;
let mut matched = Vec::new();
for (index, c) in value.chars().enumerate() {
for (index, c) in value.char_indices() {
if c == '\\' && !in_escape {
in_escape = true;
} else if c == 'x' && in_escape {
@ -68,6 +68,7 @@ fn check_escape(value: &str) -> Option<String> {
Some(fixed)
}
}
impl Rule for NoHexEscape {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
match node.kind() {
@ -137,7 +138,12 @@ fn test() {
r"const foo = `\\\\xd8\\\\x3d\\\\xdc\\\\xa9`",
r"const foo = `foo\\\\x12foo\\\\x34`",
];
let fail = vec![r#"const foo = "\xb1""#];
let fail = vec![
r#"const foo = "\xb1""#,
r"wrapId(/(^|[<nonId>])(?:алг|арг(?:\x20*рез)?|ввод|ВКЛЮЧИТЬ|вс[её]|выбор|вывод|выход|дано|для|до|дс|если|иначе|исп|использовать|кон(?:(?:\x20+|_)исп)?|кц(?:(?:\x20+|_)при)?|надо|нач|нс|нц|от|пауза|пока|при|раза?|рез|стоп|таб|то|утв|шаг)(?=[<nonId>]|$)/.source)",
];
let fix = vec![
(r"const foo = '\xb1'", r"const foo = '\u00b1'", None),
(r"const foo = '\\\xb1'", r"const foo = '\\\u00b1'", None),

View file

@ -8,4 +8,10 @@ expression: no_hex_escape
· ──────
╰────
⚠ eslint-plugin-unicorn(no-hex-escape): Use Unicode escapes instead of hexadecimal escapes.
╭─[no_hex_escape.tsx:1:1]
1 │ wrapId(/(^|[<nonId>])(?:алг|арг(?:\x20*рез)?|ввод|ВКЛЮЧИТЬ|вс[её]|выбор|вывод|выход|дано|для|до|дс|если|иначе|исп|использовать|кон(?:(?:\x20+|_)исп)?|кц(?:(?:\x20+|_)при)?|надо|нач|нс|нц|от|пауза|пока|при|раза?|рез|стоп|таб|то|утв|шаг)(?=[<nonId>]|$)/.source)
· ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
╰────