From 3250fc15a8e83ccde1f3fc725580096cf37f02c2 Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Tue, 26 Sep 2023 20:44:14 +0200 Subject: [PATCH 1/2] Add suggestions for `regexp/optimal-lookaround-quantifier` --- README.md | 2 +- docs/rules/index.md | 2 +- docs/rules/optimal-lookaround-quantifier.md | 2 ++ lib/rules/optimal-lookaround-quantifier.ts | 23 +++++++++++++++++++ .../rules/optimal-lookaround-quantifier.ts | 12 +++++----- 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 561ec723e..3b3887611 100644 --- a/README.md +++ b/README.md @@ -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 | ✅ | | 🔧 | | diff --git a/docs/rules/index.md b/docs/rules/index.md index 31e251aac..7d617d260 100644 --- a/docs/rules/index.md +++ b/docs/rules/index.md @@ -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 | ✅ | | 🔧 | | diff --git a/docs/rules/optimal-lookaround-quantifier.md b/docs/rules/optimal-lookaround-quantifier.md index 83eada8d2..335913374 100644 --- a/docs/rules/optimal-lookaround-quantifier.md +++ b/docs/rules/optimal-lookaround-quantifier.md @@ -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). + > disallow the alternatives of lookarounds that end with a non-constant quantifier diff --git a/lib/rules/optimal-lookaround-quantifier.ts b/lib/rules/optimal-lookaround-quantifier.ts index cdb5ac8bc..a7613085f 100644 --- a/lib/rules/optimal-lookaround-quantifier.ts +++ b/lib/rules/optimal-lookaround-quantifier.ts @@ -66,10 +66,13 @@ 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", }, @@ -77,6 +80,7 @@ export default createRule("optimal-lookaround-quantifier", { function createVisitor({ node, getRegexpLocation, + fixReplaceNode, }: RegExpContext): RegExpVisitor.Handlers { return { onAssertionEnter(aNode) { @@ -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}}` + }), + }, + ], }) } } diff --git a/tests/lib/rules/optimal-lookaround-quantifier.ts b/tests/lib/rules/optimal-lookaround-quantifier.ts index 0d9e6d815..d3fd17301 100644 --- a/tests/lib/rules/optimal-lookaround-quantifier.ts +++ b/tests/lib/rules/optimal-lookaround-quantifier.ts @@ -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)/` }], }, ], }, @@ -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))/` }], }, ], }, @@ -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))/` }], }, ], }, @@ -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}))/` }], }, ], }, @@ -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: `/(?<=)/` }], }, ], }, @@ -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)/` }], }, ], }, From 1ff0531142f15a6d135874b6ac3e576336d7cdc9 Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Tue, 26 Sep 2023 20:45:14 +0200 Subject: [PATCH 2/2] Create silly-bees-wave.md --- .changeset/silly-bees-wave.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/silly-bees-wave.md diff --git a/.changeset/silly-bees-wave.md b/.changeset/silly-bees-wave.md new file mode 100644 index 000000000..683aba264 --- /dev/null +++ b/.changeset/silly-bees-wave.md @@ -0,0 +1,5 @@ +--- +"eslint-plugin-regexp": minor +--- + +Add suggestions for `regexp/optimal-lookaround-quantifier`