From 7a8a1ccff63da1c731b5f07ac741102e404e718e Mon Sep 17 00:00:00 2001 From: Victorien Elvinger Date: Wed, 7 Aug 2024 13:40:51 +0200 Subject: [PATCH] fix(noUselessEscapeInRegex): handle \9 backreference (#3610) --- .../nursery/no_useless_escape_in_regex.rs | 2 +- .../nursery/noUselessEscapeInRegex/invalid.js | 5 ++++- .../noUselessEscapeInRegex/invalid.js.snap | 21 +++++++++++++++++++ .../nursery/noUselessEscapeInRegex/valid.js | 15 ++++++------- .../noUselessEscapeInRegex/valid.js.snap | 15 ++++++------- 5 files changed, 42 insertions(+), 16 deletions(-) diff --git a/crates/biome_js_analyze/src/lint/nursery/no_useless_escape_in_regex.rs b/crates/biome_js_analyze/src/lint/nursery/no_useless_escape_in_regex.rs index fb2a0bcf521c..831dab278868 100644 --- a/crates/biome_js_analyze/src/lint/nursery/no_useless_escape_in_regex.rs +++ b/crates/biome_js_analyze/src/lint/nursery/no_useless_escape_in_regex.rs @@ -84,7 +84,7 @@ impl Rule for NoUselessEscapeInRegex { // quantrifiers | b'*' | b'+' | b'?' | b'{' | b'}' // Backreferences - | b'1'..b'9' + | b'1'..=b'9' // Groups | b'(' | b')' // Alternation diff --git a/crates/biome_js_analyze/tests/specs/nursery/noUselessEscapeInRegex/invalid.js b/crates/biome_js_analyze/tests/specs/nursery/noUselessEscapeInRegex/invalid.js index c9373fd3e74c..08d3fc0d57ca 100644 --- a/crates/biome_js_analyze/tests/specs/nursery/noUselessEscapeInRegex/invalid.js +++ b/crates/biome_js_analyze/tests/specs/nursery/noUselessEscapeInRegex/invalid.js @@ -46,4 +46,7 @@ /[[\.&]&&[\.&]]/v; // Unlike ESLint, we report `\k` when it is not in a unicode-aware regex -/(?)\k/; \ No newline at end of file +/(?)\k/; + +// A test with unicode characters that take more than one byte +/😀\😀/ \ No newline at end of file diff --git a/crates/biome_js_analyze/tests/specs/nursery/noUselessEscapeInRegex/invalid.js.snap b/crates/biome_js_analyze/tests/specs/nursery/noUselessEscapeInRegex/invalid.js.snap index d9a5b49f01f4..308ba524c503 100644 --- a/crates/biome_js_analyze/tests/specs/nursery/noUselessEscapeInRegex/invalid.js.snap +++ b/crates/biome_js_analyze/tests/specs/nursery/noUselessEscapeInRegex/invalid.js.snap @@ -53,6 +53,9 @@ expression: invalid.js // Unlike ESLint, we report `\k` when it is not in a unicode-aware regex /(?)\k/; + +// A test with unicode characters that take more than one byte +/😀\😀/ ``` # Diagnostics @@ -991,6 +994,8 @@ invalid.js:49:8 lint/nursery/noUselessEscapeInRegex FIXABLE ━━━━━━ 48 │ // Unlike ESLint, we report `\k` when it is not in a unicode-aware regex > 49 │ /(?)\k/; │ ^^ + 50 │ + 51 │ // A test with unicode characters that take more than one byte i The escape sequence is only useful if the regular expression is unicode-aware. To be unicode-aware, the `u` or `v` flag should be used. @@ -1000,3 +1005,19 @@ invalid.js:49:8 lint/nursery/noUselessEscapeInRegex FIXABLE ━━━━━━ │ - ``` + +``` +invalid.js:52:3 lint/nursery/noUselessEscapeInRegex FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + ! The character doesn't need to be escaped. + + 51 │ // A test with unicode characters that take more than one byte + > 52 │ /😀\😀/ + │ ^^^ + + i Safe fix: Unescape the character. + + 52 │ /😀\😀/ + │ - + +``` diff --git a/crates/biome_js_analyze/tests/specs/nursery/noUselessEscapeInRegex/valid.js b/crates/biome_js_analyze/tests/specs/nursery/noUselessEscapeInRegex/valid.js index 99eb963cec47..e00f7591b5fe 100644 --- a/crates/biome_js_analyze/tests/specs/nursery/noUselessEscapeInRegex/valid.js +++ b/crates/biome_js_analyze/tests/specs/nursery/noUselessEscapeInRegex/valid.js @@ -16,10 +16,11 @@ // https://github.com/eslint/eslint/issues/7472 /\0/; // null character -/\\1/; // \x01 character (octal literal) -/(a)\\1/; // backreference -/(a)\\12/; // backreference -/[\\0]/; // null character in character class +/\1/; // \x01 character (octal literal) +/(a)\1/; // backreference +/(a)\12/; // backreference +/(a)\9/; // backreference +/[\0]/; // null character in character class // https://github.com/eslint/eslint/issues/7789 /]/; @@ -31,7 +32,7 @@ // ES2018 /\]/u; // /(?)\k/; // Unlike ESLint, we report `\k` when it is not in a unicode-aware regex -/(\\?)/; +/(\?)/; /\p{ASCII}/u; /\P{ASCII}/u; /[\p{ASCII}]/u; @@ -67,7 +68,7 @@ /[\>>]/v; /[\??]/v; /[\@@]/v; -/[\\``]/v; +/[\``]/v; /[\~~]/v; /[^\^^]/v; /[_\^^]/v; @@ -87,7 +88,7 @@ /[>\>]/v; /[?\?]/v; /[@\@]/v; -/[`\\`]/v; +/[`\`]/v; /[~\~]/v; /[^^\^]/v; /[_^\^]/v; diff --git a/crates/biome_js_analyze/tests/specs/nursery/noUselessEscapeInRegex/valid.js.snap b/crates/biome_js_analyze/tests/specs/nursery/noUselessEscapeInRegex/valid.js.snap index abf09f4b4eef..0e7168161dcd 100644 --- a/crates/biome_js_analyze/tests/specs/nursery/noUselessEscapeInRegex/valid.js.snap +++ b/crates/biome_js_analyze/tests/specs/nursery/noUselessEscapeInRegex/valid.js.snap @@ -22,10 +22,11 @@ expression: valid.js // https://github.com/eslint/eslint/issues/7472 /\0/; // null character -/\\1/; // \x01 character (octal literal) -/(a)\\1/; // backreference -/(a)\\12/; // backreference -/[\\0]/; // null character in character class +/\1/; // \x01 character (octal literal) +/(a)\1/; // backreference +/(a)\12/; // backreference +/(a)\9/; // backreference +/[\0]/; // null character in character class // https://github.com/eslint/eslint/issues/7789 /]/; @@ -37,7 +38,7 @@ expression: valid.js // ES2018 /\]/u; // /(?)\k/; // Unlike ESLint, we report `\k` when it is not in a unicode-aware regex -/(\\?)/; +/(\?)/; /\p{ASCII}/u; /\P{ASCII}/u; /[\p{ASCII}]/u; @@ -73,7 +74,7 @@ expression: valid.js /[\>>]/v; /[\??]/v; /[\@@]/v; -/[\\``]/v; +/[\``]/v; /[\~~]/v; /[^\^^]/v; /[_\^^]/v; @@ -93,7 +94,7 @@ expression: valid.js /[>\>]/v; /[?\?]/v; /[@\@]/v; -/[`\\`]/v; +/[`\`]/v; /[~\~]/v; /[^^\^]/v; /[_^\^]/v;