Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(noUselessEscapeInRegex): handle \9 backreference #3610

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading