Skip to content

Commit

Permalink
Add suggestions for regexp/optimal-lookaround-quantifier (#623)
Browse files Browse the repository at this point in the history
* Add suggestions for `regexp/optimal-lookaround-quantifier`

* Create silly-bees-wave.md
  • Loading branch information
RunDevelopment authored Oct 1, 2023
1 parent e576987 commit d0b965f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-bees-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-regexp": minor
---

Add suggestions for `regexp/optimal-lookaround-quantifier`
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ The `plugin:regexp/all` config enables all rules. It's meant for testing, not fo
| [no-useless-range](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-useless-range.html) | disallow unnecessary character ranges || | 🔧 | |
| [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 || | 🔧 | |
| [no-zero-quantifier](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-zero-quantifier.html) | disallow quantifiers with a maximum of zero || | | 💡 |
| [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 | || | |
| [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 | || | 💡 |
| [optimal-quantifier-concatenation](https://ota-meshi.github.io/eslint-plugin-regexp/rules/optimal-quantifier-concatenation.html) | require optimal quantifiers for concatenated quantifiers || | 🔧 | |
| [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 (`$$`). | | | | |
| [prefer-predefined-assertion](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-predefined-assertion.html) | prefer predefined assertion over equivalent lookarounds || | 🔧 | |
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ sidebarDepth: 0
| [no-useless-range](no-useless-range.md) | disallow unnecessary character ranges || | 🔧 | |
| [no-useless-two-nums-quantifier](no-useless-two-nums-quantifier.md) | disallow unnecessary `{n,m}` quantifier || | 🔧 | |
| [no-zero-quantifier](no-zero-quantifier.md) | disallow quantifiers with a maximum of zero || | | 💡 |
| [optimal-lookaround-quantifier](optimal-lookaround-quantifier.md) | disallow the alternatives of lookarounds that end with a non-constant quantifier | || | |
| [optimal-lookaround-quantifier](optimal-lookaround-quantifier.md) | disallow the alternatives of lookarounds that end with a non-constant quantifier | || | 💡 |
| [optimal-quantifier-concatenation](optimal-quantifier-concatenation.md) | require optimal quantifiers for concatenated quantifiers || | 🔧 | |
| [prefer-escape-replacement-dollar-char](prefer-escape-replacement-dollar-char.md) | enforces escape of replacement `$` character (`$$`). | | | | |
| [prefer-predefined-assertion](prefer-predefined-assertion.md) | prefer predefined assertion over equivalent lookarounds || | 🔧 | |
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/optimal-lookaround-quantifier.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ since: "v0.8.0"

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

💡 This rule is manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).

<!-- end auto-generated rule header -->

> disallow the alternatives of lookarounds that end with a non-constant quantifier
Expand Down
23 changes: 23 additions & 0 deletions lib/rules/optimal-lookaround-quantifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,21 @@ export default createRule("optimal-lookaround-quantifier", {
default: "warn",
},
schema: [],
hasSuggestions: true,
messages: {
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.",
replacedWith:
"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.",
suggestRemove: "Remove the expression.",
suggestReplace: "Replace the expression with {{replacer}}.",
},
type: "problem",
},
create(context) {
function createVisitor({
node,
getRegexpLocation,
fixReplaceNode,
}: RegExpContext): RegExpVisitor.Handlers {
return {
onAssertionEnter(aNode) {
Expand Down Expand Up @@ -108,6 +112,25 @@ export default createRule("optimal-lookaround-quantifier", {
endOrStart,
replacer,
},
suggest: [
{
messageId:
q.min === 0
? "suggestRemove"
: "suggestReplace",
data: {
replacer,
},
fix: fixReplaceNode(q, () => {
if (q.min === 0) {
return ""
} else if (q.min === 1) {
return q.element.raw
}
return `${q.element.raw}{${q.min}}`
}),
},
],
})
}
}
Expand Down
12 changes: 6 additions & 6 deletions tests/lib/rules/optimal-lookaround-quantifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ tester.run("optimal-lookaround-quantifier", rule as any, {
{
message:
"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.",
line: 1,
column: 6,
suggestions: [{ output: `/(?=b)/` }],
},
],
},
Expand All @@ -28,8 +28,8 @@ tester.run("optimal-lookaround-quantifier", rule as any, {
{
message:
"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.",
line: 1,
column: 14,
suggestions: [{ output: `/(?=(?:a|b|ab))/` }],
},
],
},
Expand All @@ -39,8 +39,8 @@ tester.run("optimal-lookaround-quantifier", rule as any, {
{
message:
"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.",
line: 1,
column: 14,
suggestions: [{ output: `/(?=(?:a|b|abc))/` }],
},
],
},
Expand All @@ -50,8 +50,8 @@ tester.run("optimal-lookaround-quantifier", rule as any, {
{
message:
"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.",
line: 1,
column: 14,
suggestions: [{ output: `/(?=(?:a|b|abc{4}))/` }],
},
],
},
Expand All @@ -61,8 +61,8 @@ tester.run("optimal-lookaround-quantifier", rule as any, {
{
message:
"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.",
line: 1,
column: 6,
suggestions: [{ output: `/(?<=)/` }],
},
],
},
Expand All @@ -72,8 +72,8 @@ tester.run("optimal-lookaround-quantifier", rule as any, {
{
message:
"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.",
line: 1,
column: 6,
suggestions: [{ output: `/(?<=ab)/` }],
},
],
},
Expand Down

0 comments on commit d0b965f

Please sign in to comment.