Skip to content

Commit cf7348b

Browse files
Add allowNamed option to no-unused-capturing-group rule (#689)
* Add `allowNamed` option to `no-unused-capturing-group` rule * Remove weird emphasis * Create lovely-bees-stare.md
1 parent f67f1df commit cf7348b

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

.changeset/lovely-bees-stare.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-regexp": minor
3+
---
4+
5+
Add `allowNamed` option to `no-unused-capturing-group` rule to allow for unused named capturing groups.

docs/rules/no-unused-capturing-group.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ Using capturing groups only if the captured text is used makes their usage unamb
7878
```json
7979
{
8080
"regexp/no-unused-capturing-group": ["error", {
81-
"fixable": false
81+
"fixable": false,
82+
"allowNamed": false
8283
}]
8384
}
8485
```
@@ -89,6 +90,12 @@ Using capturing groups only if the captured text is used makes their usage unamb
8990

9091
This rule is not fixable by default. Unused capturing groups can indicate a mistake in the code that uses the regex, so changing the regex might not be the right fix. When enabling this option, be sure to carefully check its changes.
9192

93+
- `allowNamed: true | false`
94+
95+
Whether unused named capturing groups are allowed. Defaults to `false`.
96+
97+
Sometimes named capturing groups can be used to document a regex. In such cases, it can be acceptable to have unused named capturing groups. This option is disabled by default to prevent mistakes.
98+
9299
## :couple: Related rules
93100

94101
- [regexp/no-useless-dollar-replacements]

lib/rules/no-unused-capturing-group.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default createRule("no-unused-capturing-group", {
2929
type: "object",
3030
properties: {
3131
fixable: { type: "boolean" },
32+
allowNamed: { type: "boolean" },
3233
},
3334
additionalProperties: false,
3435
},
@@ -45,6 +46,7 @@ export default createRule("no-unused-capturing-group", {
4546
},
4647
create(context) {
4748
const fixable: boolean = context.options[0]?.fixable ?? false
49+
const allowNamed: boolean = context.options[0]?.allowNamed ?? false
4850

4951
function reportUnused(
5052
unused: Set<CapturingGroup>,
@@ -57,6 +59,14 @@ export default createRule("no-unused-capturing-group", {
5759
getAllCapturingGroups,
5860
} = regexpContext
5961

62+
if (allowNamed) {
63+
for (const cgNode of unused) {
64+
if (cgNode.name) {
65+
unused.delete(cgNode)
66+
}
67+
}
68+
}
69+
6070
const fixableGroups = new Set<CapturingGroup>()
6171
for (const group of [...getAllCapturingGroups()].reverse()) {
6272
if (unused.has(group)) {

tests/lib/rules/no-unused-capturing-group.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ tester.run("no-unused-capturing-group", rule as any, {
162162
String.raw`const re = /(foo)/d
163163
console.log(re.exec('foo').indices[unknown])
164164
`,
165+
166+
{
167+
code: String.raw`const REGEX_SEMANTIC_VERSION = /(?<majorVersion>\d+)\.(?<minorVersion>\d+)\.(?<patchVersion>\d+)(?:-(?<suffix>.+))?/;`,
168+
options: [{ allowNamed: true }],
169+
},
165170
],
166171
invalid: [
167172
{

0 commit comments

Comments
 (0)