Skip to content

Commit

Permalink
Add allowNamed option to no-unused-capturing-group rule (#689)
Browse files Browse the repository at this point in the history
* Add `allowNamed` option to `no-unused-capturing-group` rule

* Remove weird emphasis

* Create lovely-bees-stare.md
  • Loading branch information
RunDevelopment committed Jan 8, 2024
1 parent f67f1df commit cf7348b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/lovely-bees-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-regexp": minor
---

Add `allowNamed` option to `no-unused-capturing-group` rule to allow for unused named capturing groups.
9 changes: 8 additions & 1 deletion docs/rules/no-unused-capturing-group.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ Using capturing groups only if the captured text is used makes their usage unamb
```json
{
"regexp/no-unused-capturing-group": ["error", {
"fixable": false
"fixable": false,
"allowNamed": false
}]
}
```
Expand All @@ -89,6 +90,12 @@ Using capturing groups only if the captured text is used makes their usage unamb

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.

- `allowNamed: true | false`

Whether unused named capturing groups are allowed. Defaults to `false`.

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.

## :couple: Related rules

- [regexp/no-useless-dollar-replacements]
Expand Down
10 changes: 10 additions & 0 deletions lib/rules/no-unused-capturing-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default createRule("no-unused-capturing-group", {
type: "object",
properties: {
fixable: { type: "boolean" },
allowNamed: { type: "boolean" },
},
additionalProperties: false,
},
Expand All @@ -45,6 +46,7 @@ export default createRule("no-unused-capturing-group", {
},
create(context) {
const fixable: boolean = context.options[0]?.fixable ?? false
const allowNamed: boolean = context.options[0]?.allowNamed ?? false

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

if (allowNamed) {
for (const cgNode of unused) {
if (cgNode.name) {
unused.delete(cgNode)
}
}
}

const fixableGroups = new Set<CapturingGroup>()
for (const group of [...getAllCapturingGroups()].reverse()) {
if (unused.has(group)) {
Expand Down
5 changes: 5 additions & 0 deletions tests/lib/rules/no-unused-capturing-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ tester.run("no-unused-capturing-group", rule as any, {
String.raw`const re = /(foo)/d
console.log(re.exec('foo').indices[unknown])
`,

{
code: String.raw`const REGEX_SEMANTIC_VERSION = /(?<majorVersion>\d+)\.(?<minorVersion>\d+)\.(?<patchVersion>\d+)(?:-(?<suffix>.+))?/;`,
options: [{ allowNamed: true }],
},
],
invalid: [
{
Expand Down

0 comments on commit cf7348b

Please sign in to comment.