Skip to content

Commit d6e758a

Browse files
Make no-useless-escape fixable (#226)
* Make `no-useless-escape` fixable * Update lib/rules/no-useless-escape.ts Co-authored-by: Yosuke Ota <[email protected]> Co-authored-by: Yosuke Ota <[email protected]>
1 parent 775bbe4 commit d6e758a

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ The rules with the following star :star: are included in the `plugin:regexp/reco
155155
| [regexp/hexadecimal-escape](https://ota-meshi.github.io/eslint-plugin-regexp/rules/hexadecimal-escape.html) | enforce consistent usage of hexadecimal escape | :wrench: |
156156
| [regexp/letter-case](https://ota-meshi.github.io/eslint-plugin-regexp/rules/letter-case.html) | enforce into your favorite case | :wrench: |
157157
| [regexp/match-any](https://ota-meshi.github.io/eslint-plugin-regexp/rules/match-any.html) | enforce match any character style | :star::wrench: |
158-
| [regexp/no-useless-escape](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-useless-escape.html) | disallow unnecessary escape characters in RegExp | |
158+
| [regexp/no-useless-escape](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-useless-escape.html) | disallow unnecessary escape characters in RegExp | :wrench: |
159159
| [regexp/no-useless-non-capturing-group](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-useless-non-capturing-group.html) | disallow unnecessary Non-capturing group | :wrench: |
160160
| [regexp/order-in-character-class](https://ota-meshi.github.io/eslint-plugin-regexp/rules/order-in-character-class.html) | enforces elements order in character class | :wrench: |
161161
| [regexp/prefer-character-class](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-character-class.html) | enforce using character class | :wrench: |

docs/rules/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ The rules with the following star :star: are included in the `plugin:regexp/reco
6969
| [regexp/hexadecimal-escape](./hexadecimal-escape.md) | enforce consistent usage of hexadecimal escape | :wrench: |
7070
| [regexp/letter-case](./letter-case.md) | enforce into your favorite case | :wrench: |
7171
| [regexp/match-any](./match-any.md) | enforce match any character style | :star::wrench: |
72-
| [regexp/no-useless-escape](./no-useless-escape.md) | disallow unnecessary escape characters in RegExp | |
72+
| [regexp/no-useless-escape](./no-useless-escape.md) | disallow unnecessary escape characters in RegExp | :wrench: |
7373
| [regexp/no-useless-non-capturing-group](./no-useless-non-capturing-group.md) | disallow unnecessary Non-capturing group | :wrench: |
7474
| [regexp/order-in-character-class](./order-in-character-class.md) | enforces elements order in character class | :wrench: |
7575
| [regexp/prefer-character-class](./prefer-character-class.md) | enforce using character class | :wrench: |

docs/rules/no-useless-escape.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ since: "v0.4.0"
99

1010
> disallow unnecessary escape characters in RegExp
1111
12+
- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule.
13+
1214
## :book: Rule Details
1315

1416
This rule reports unnecessary escape characters in RegExp.
1517
You may be able to find another mistake by finding unnecessary escapes.
1618

17-
<eslint-code-block>
19+
<eslint-code-block fix>
1820

1921
```js
2022
/* eslint regexp/no-useless-escape: "error" */
@@ -34,7 +36,7 @@ var foo = /\u{[41]}/
3436

3537
This rule checks for unnecessary escapes with deeper regular expression parsing than the ESLint core's [no-useless-escape] rule.
3638

37-
<eslint-code-block>
39+
<eslint-code-block fix>
3840

3941
```js
4042
/* eslint no-useless-escape: "error" */

lib/rules/no-useless-escape.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ const REGEX_ESCAPES = new Set([
4646
CP_CLOSING_PAREN, // )
4747
])
4848

49+
const POTENTIAL_ESCAPE_SEQUENCE = new Set("uxkpP")
50+
4951
export default createRule("no-useless-escape", {
5052
meta: {
5153
docs: {
@@ -55,6 +57,7 @@ export default createRule("no-useless-escape", {
5557
// recommended: true,
5658
recommended: false,
5759
},
60+
fixable: "code",
5861
schema: [],
5962
messages: {
6063
unnecessary: "Unnecessary escape character: \\{{character}}.",
@@ -68,12 +71,14 @@ export default createRule("no-useless-escape", {
6871
function createVisitor({
6972
node,
7073
getRegexpLocation,
74+
fixReplaceNode,
7175
}: RegExpContext): RegExpVisitor.Handlers {
7276
/** Report */
7377
function report(
7478
cNode: Character,
7579
offset: number,
7680
character: string,
81+
fix: boolean,
7782
) {
7883
context.report({
7984
node,
@@ -82,6 +87,7 @@ export default createRule("no-useless-escape", {
8287
data: {
8388
character,
8489
},
90+
fix: fix ? fixReplaceNode(cNode, character) : null,
8591
})
8692
}
8793

@@ -120,7 +126,12 @@ export default createRule("no-useless-escape", {
120126
if (!canUnwrapped(cNode, char)) {
121127
return
122128
}
123-
report(cNode, 0, char)
129+
report(
130+
cNode,
131+
0,
132+
char,
133+
!POTENTIAL_ESCAPE_SEQUENCE.has(char),
134+
)
124135
}
125136
}
126137
},

tests/lib/rules/no-useless-escape.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ tester.run("no-useless-escape", rule as any, {
6969
invalid: [
7070
{
7171
code: String.raw`/\a/`,
72+
output: String.raw`/a/`,
7273
errors: [
7374
{
7475
message: "Unnecessary escape character: \\a.",
@@ -81,18 +82,22 @@ tester.run("no-useless-escape", rule as any, {
8182
},
8283
{
8384
code: `/\\x7/`,
85+
output: null,
8486
errors: ["Unnecessary escape character: \\x."],
8587
},
8688
{
8789
code: `/\\u41/`,
90+
output: null,
8891
errors: ["Unnecessary escape character: \\u."],
8992
},
9093
{
9194
code: `/\\u{[41]}/`,
95+
output: null,
9296
errors: ["Unnecessary escape character: \\u."],
9397
},
9498
{
9599
code: String.raw`/[ \^ \/ \. \$ \* \+ \? \[ \{ \} \| \( \) \k<title> \B \8 \9]/`,
100+
output: String.raw`/[ ^ / . $ * + ? [ { } | ( ) \k<title> B 8 9]/`,
96101
errors: [
97102
"Unnecessary escape character: \\^.",
98103
"Unnecessary escape character: \\/.",
@@ -115,6 +120,7 @@ tester.run("no-useless-escape", rule as any, {
115120
},
116121
{
117122
code: String.raw`/\p{ASCII}/; /[\p{ASCII}]/; /\P{ASCII}/; /[\P{ASCII}]/`, // Missing u flag
123+
output: null,
118124
errors: [
119125
"Unnecessary escape character: \\p.",
120126
"Unnecessary escape character: \\p.",

0 commit comments

Comments
 (0)