Skip to content

Commit 3250fc1

Browse files
Add suggestions for regexp/optimal-lookaround-quantifier
1 parent 7a38486 commit 3250fc1

File tree

5 files changed

+33
-8
lines changed

5 files changed

+33
-8
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ The `plugin:regexp/all` config enables all rules. It's meant for testing, not fo
157157
| [no-useless-range](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-useless-range.html) | disallow unnecessary character ranges || | 🔧 | |
158158
| [no-useless-two-nums-quantifier](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-useless-two-nums-quantifier.html) | disallow unnecessary `{n,m}` quantifier || | 🔧 | |
159159
| [no-zero-quantifier](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-zero-quantifier.html) | disallow quantifiers with a maximum of zero || | | 💡 |
160-
| [optimal-lookaround-quantifier](https://ota-meshi.github.io/eslint-plugin-regexp/rules/optimal-lookaround-quantifier.html) | disallow the alternatives of lookarounds that end with a non-constant quantifier | || | |
160+
| [optimal-lookaround-quantifier](https://ota-meshi.github.io/eslint-plugin-regexp/rules/optimal-lookaround-quantifier.html) | disallow the alternatives of lookarounds that end with a non-constant quantifier | || | 💡 |
161161
| [optimal-quantifier-concatenation](https://ota-meshi.github.io/eslint-plugin-regexp/rules/optimal-quantifier-concatenation.html) | require optimal quantifiers for concatenated quantifiers || | 🔧 | |
162162
| [prefer-escape-replacement-dollar-char](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-escape-replacement-dollar-char.html) | enforces escape of replacement `$` character (`$$`). | | | | |
163163
| [prefer-predefined-assertion](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-predefined-assertion.html) | prefer predefined assertion over equivalent lookarounds || | 🔧 | |

docs/rules/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ sidebarDepth: 0
6464
| [no-useless-range](no-useless-range.md) | disallow unnecessary character ranges || | 🔧 | |
6565
| [no-useless-two-nums-quantifier](no-useless-two-nums-quantifier.md) | disallow unnecessary `{n,m}` quantifier || | 🔧 | |
6666
| [no-zero-quantifier](no-zero-quantifier.md) | disallow quantifiers with a maximum of zero || | | 💡 |
67-
| [optimal-lookaround-quantifier](optimal-lookaround-quantifier.md) | disallow the alternatives of lookarounds that end with a non-constant quantifier | || | |
67+
| [optimal-lookaround-quantifier](optimal-lookaround-quantifier.md) | disallow the alternatives of lookarounds that end with a non-constant quantifier | || | 💡 |
6868
| [optimal-quantifier-concatenation](optimal-quantifier-concatenation.md) | require optimal quantifiers for concatenated quantifiers || | 🔧 | |
6969
| [prefer-escape-replacement-dollar-char](prefer-escape-replacement-dollar-char.md) | enforces escape of replacement `$` character (`$$`). | | | | |
7070
| [prefer-predefined-assertion](prefer-predefined-assertion.md) | prefer predefined assertion over equivalent lookarounds || | 🔧 | |

docs/rules/optimal-lookaround-quantifier.md

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ since: "v0.8.0"
99

1010
⚠️ This rule _warns_ in the ✅ `plugin:regexp/recommended` config.
1111

12+
💡 This rule is manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).
13+
1214
<!-- end auto-generated rule header -->
1315

1416
> disallow the alternatives of lookarounds that end with a non-constant quantifier

lib/rules/optimal-lookaround-quantifier.ts

+23
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,21 @@ export default createRule("optimal-lookaround-quantifier", {
6666
default: "warn",
6767
},
6868
schema: [],
69+
hasSuggestions: true,
6970
messages: {
7071
remove: "The quantified expression {{expr}} at the {{endOrStart}} of the expression tree should only be matched a constant number of times. The expression can be removed without affecting the lookaround.",
7172
replacedWith:
7273
"The quantified expression {{expr}} at the {{endOrStart}} of the expression tree should only be matched a constant number of times. The expression can be replaced with {{replacer}} without affecting the lookaround.",
74+
suggestRemove: "Remove the expression.",
75+
suggestReplace: "Replace the expression with {{replacer}}.",
7376
},
7477
type: "problem",
7578
},
7679
create(context) {
7780
function createVisitor({
7881
node,
7982
getRegexpLocation,
83+
fixReplaceNode,
8084
}: RegExpContext): RegExpVisitor.Handlers {
8185
return {
8286
onAssertionEnter(aNode) {
@@ -108,6 +112,25 @@ export default createRule("optimal-lookaround-quantifier", {
108112
endOrStart,
109113
replacer,
110114
},
115+
suggest: [
116+
{
117+
messageId:
118+
q.min === 0
119+
? "suggestRemove"
120+
: "suggestReplace",
121+
data: {
122+
replacer,
123+
},
124+
fix: fixReplaceNode(q, () => {
125+
if (q.min === 0) {
126+
return ""
127+
} else if (q.min === 1) {
128+
return q.element.raw
129+
}
130+
return `${q.element.raw}{${q.min}}`
131+
}),
132+
},
133+
],
111134
})
112135
}
113136
}

tests/lib/rules/optimal-lookaround-quantifier.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ tester.run("optimal-lookaround-quantifier", rule as any, {
1717
{
1818
message:
1919
"The quantified expression 'a*' at the end of the expression tree should only be matched a constant number of times. The expression can be removed without affecting the lookaround.",
20-
line: 1,
2120
column: 6,
21+
suggestions: [{ output: `/(?=b)/` }],
2222
},
2323
],
2424
},
@@ -28,8 +28,8 @@ tester.run("optimal-lookaround-quantifier", rule as any, {
2828
{
2929
message:
3030
"The quantified expression 'c*' at the end of the expression tree should only be matched a constant number of times. The expression can be removed without affecting the lookaround.",
31-
line: 1,
3231
column: 14,
32+
suggestions: [{ output: `/(?=(?:a|b|ab))/` }],
3333
},
3434
],
3535
},
@@ -39,8 +39,8 @@ tester.run("optimal-lookaround-quantifier", rule as any, {
3939
{
4040
message:
4141
"The quantified expression 'c+' at the end of the expression tree should only be matched a constant number of times. The expression can be replaced with 'c' (no quantifier) without affecting the lookaround.",
42-
line: 1,
4342
column: 14,
43+
suggestions: [{ output: `/(?=(?:a|b|abc))/` }],
4444
},
4545
],
4646
},
@@ -50,8 +50,8 @@ tester.run("optimal-lookaround-quantifier", rule as any, {
5050
{
5151
message:
5252
"The quantified expression 'c{4,9}' at the end of the expression tree should only be matched a constant number of times. The expression can be replaced with 'c{4}' without affecting the lookaround.",
53-
line: 1,
5453
column: 14,
54+
suggestions: [{ output: `/(?=(?:a|b|abc{4}))/` }],
5555
},
5656
],
5757
},
@@ -61,8 +61,8 @@ tester.run("optimal-lookaround-quantifier", rule as any, {
6161
{
6262
message:
6363
"The quantified expression '[a-c]*' at the start of the expression tree should only be matched a constant number of times. The expression can be removed without affecting the lookaround.",
64-
line: 1,
6564
column: 6,
65+
suggestions: [{ output: `/(?<=)/` }],
6666
},
6767
],
6868
},
@@ -72,8 +72,8 @@ tester.run("optimal-lookaround-quantifier", rule as any, {
7272
{
7373
message:
7474
"The quantified expression '(?:d|c)*' at the start of the expression tree should only be matched a constant number of times. The expression can be removed without affecting the lookaround.",
75-
line: 1,
7675
column: 6,
76+
suggestions: [{ output: `/(?<=ab)/` }],
7777
},
7878
],
7979
},

0 commit comments

Comments
 (0)