Skip to content

Commit 07c1a7e

Browse files
authored
feat: add allowRegexCharacters to no-useless-escape (#19705)
* feat: add allowedCharacters to no-useless-escape * rename option * fix examples
1 parent cf36352 commit 07c1a7e

File tree

4 files changed

+385
-3
lines changed

4 files changed

+385
-3
lines changed

docs/src/rules/no-useless-escape.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,42 @@ Examples of **correct** code for this rule:
6767

6868
:::
6969

70+
## Options
71+
72+
This rule has an object option:
73+
74+
* `allowRegexCharacters` - An array of characters that should be allowed to have unnecessary escapes in regular expressions. This is useful for characters like `-` where escaping can prevent accidental character ranges. For example, in `/[0\-]/`, the escape is technically unnecessary but helps prevent the pattern from becoming a range if another character is added later (e.g., `/[0\-9]/` vs `/[0-9]/`).
75+
76+
### allowRegexCharacters
77+
78+
Examples of **incorrect** code for the `{ "allowRegexCharacters": ["-"] }` option:
79+
80+
::: incorrect
81+
82+
```js
83+
/*eslint no-useless-escape: ["error", { "allowRegexCharacters": ["-"] }]*/
84+
85+
/\!/;
86+
/\@/;
87+
/[a-z\^]/;
88+
```
89+
90+
:::
91+
92+
Examples of **correct** code for the `{ "allowRegexCharacters": ["-"] }` option:
93+
94+
::: correct
95+
96+
```js
97+
/*eslint no-useless-escape: ["error", { "allowRegexCharacters": ["-"] }]*/
98+
99+
/[0\-]/;
100+
/[\-9]/;
101+
/a\-b/;
102+
```
103+
104+
:::
105+
70106
## When Not To Use It
71107

72108
If you don't want to be notified about unnecessary escapes, you can safely disable this rule.

lib/rules/no-useless-escape.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ module.exports = {
6060
meta: {
6161
type: "suggestion",
6262

63+
defaultOptions: [
64+
{
65+
allowRegexCharacters: [],
66+
},
67+
],
68+
6369
docs: {
6470
description: "Disallow unnecessary escape characters",
6571
recommended: true,
@@ -78,11 +84,26 @@ module.exports = {
7884
"Replace the `\\` with `\\\\` to include the actual backslash character.",
7985
},
8086

81-
schema: [],
87+
schema: [
88+
{
89+
type: "object",
90+
properties: {
91+
allowRegexCharacters: {
92+
type: "array",
93+
items: {
94+
type: "string",
95+
},
96+
uniqueItems: true,
97+
},
98+
},
99+
additionalProperties: false,
100+
},
101+
],
82102
},
83103

84104
create(context) {
85105
const sourceCode = context.sourceCode;
106+
const [{ allowRegexCharacters }] = context.options;
86107
const parser = new RegExpParser();
87108

88109
/**
@@ -217,7 +238,8 @@ module.exports = {
217238

218239
if (
219240
escapedChar !==
220-
String.fromCodePoint(characterNode.value)
241+
String.fromCodePoint(characterNode.value) ||
242+
allowRegexCharacters.includes(escapedChar)
221243
) {
222244
// It's a valid escape.
223245
return;

lib/types/rules.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4141,7 +4141,13 @@ export interface ESLintRules extends Linter.RulesRecord {
41414141
* @since 2.5.0
41424142
* @see https://eslint.org/docs/latest/rules/no-useless-escape
41434143
*/
4144-
"no-useless-escape": Linter.RuleEntry<[]>;
4144+
"no-useless-escape": Linter.RuleEntry<
4145+
[
4146+
Partial<{
4147+
allowRegexCharacters: string[];
4148+
}>,
4149+
]
4150+
>;
41454151

41464152
/**
41474153
* Rule to disallow renaming import, export, and destructured assignments to the same name.

0 commit comments

Comments
 (0)