Skip to content

Commit

Permalink
fix(noUselessEscapeInRegex): handle \9 backreference (#3610)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos committed Aug 7, 2024
1 parent f1ceeef commit 7a8a1cc
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@
/[[\.&]&&[\.&]]/v;

// Unlike ESLint, we report `\k` when it is not in a unicode-aware regex
/(?<a>)\k<a>/;
/(?<a>)\k<a>/;

// A test with unicode characters that take more than one byte
/😀\😀/
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ expression: invalid.js

// Unlike ESLint, we report `\k` when it is not in a unicode-aware regex
/(?<a>)\k<a>/;

// A test with unicode characters that take more than one byte
/😀\😀/
```

# Diagnostics
Expand Down Expand Up @@ -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 │ /(?<a>)\k<a>/;
│ ^^
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.
Expand All @@ -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 │ /😀\😀/
│ -
```
Original file line number Diff line number Diff line change
Expand Up @@ -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
/]/;
Expand All @@ -31,7 +32,7 @@
// ES2018
/\]/u;
// /(?<a>)\k<a>/; // Unlike ESLint, we report `\k` when it is not in a unicode-aware regex
/(\\?<a>)/;
/(\?<a>)/;
/\p{ASCII}/u;
/\P{ASCII}/u;
/[\p{ASCII}]/u;
Expand Down Expand Up @@ -67,7 +68,7 @@
/[\>>]/v;
/[\??]/v;
/[\@@]/v;
/[\\``]/v;
/[\``]/v;
/[\~~]/v;
/[^\^^]/v;
/[_\^^]/v;
Expand All @@ -87,7 +88,7 @@
/[>\>]/v;
/[?\?]/v;
/[@\@]/v;
/[`\\`]/v;
/[`\`]/v;
/[~\~]/v;
/[^^\^]/v;
/[_^\^]/v;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
/]/;
Expand All @@ -37,7 +38,7 @@ expression: valid.js
// ES2018
/\]/u;
// /(?<a>)\k<a>/; // Unlike ESLint, we report `\k` when it is not in a unicode-aware regex
/(\\?<a>)/;
/(\?<a>)/;
/\p{ASCII}/u;
/\P{ASCII}/u;
/[\p{ASCII}]/u;
Expand Down Expand Up @@ -73,7 +74,7 @@ expression: valid.js
/[\>>]/v;
/[\??]/v;
/[\@@]/v;
/[\\``]/v;
/[\``]/v;
/[\~~]/v;
/[^\^^]/v;
/[_\^^]/v;
Expand All @@ -93,7 +94,7 @@ expression: valid.js
/[>\>]/v;
/[?\?]/v;
/[@\@]/v;
/[`\\`]/v;
/[`\`]/v;
/[~\~]/v;
/[^^\^]/v;
/[_^\^]/v;
Expand Down

0 comments on commit 7a8a1cc

Please sign in to comment.