Skip to content

Commit

Permalink
Fixed false positives for hasIndices in `regexp/no-unused-capturing-g…
Browse files Browse the repository at this point in the history
…roup` (#676)
  • Loading branch information
ota-meshi authored Dec 4, 2023
1 parent 34ba766 commit 4a13302
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-numbers-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-regexp": patch
---

Fixed false positives for hasIndices in `regexp/no-unused-capturing-group`
31 changes: 26 additions & 5 deletions lib/utils/extract-capturing-group-references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -641,12 +641,12 @@ function* iterateForRegExpMatchArrayReference(
for (const namedRef of ref.extractPropertyReferences()) {
yield getNamedArrayRef(namedRef)
}
} else if (ref.name === "indices") {
for (const indicesRef of ref.extractPropertyReferences()) {
yield* iterateForRegExpIndicesArrayReference(indicesRef)
}
} else {
if (
ref.name === "input" ||
ref.name === "index" ||
ref.name === "indices"
) {
if (ref.name === "input" || ref.name === "index") {
return
}
yield getIndexArrayRef(ref)
Expand All @@ -660,6 +660,27 @@ function* iterateForRegExpMatchArrayReference(
}
}

/** Iterate the capturing group references for RegExpIndicesArray reference. */
function* iterateForRegExpIndicesArrayReference(
ref: PropertyReference,
): Iterable<CapturingGroupReference> {
if (hasNameRef(ref)) {
if (ref.name === "groups") {
for (const namedRef of ref.extractPropertyReferences()) {
yield getNamedArrayRef(namedRef)
}
} else {
yield getIndexArrayRef(ref)
}
} else {
yield {
type: "UnknownRef",
kind: "array",
prop: ref,
}
}
}

/** Iterate the capturing group references for Array method of String.prototype.matchAll(). */
function* iterateForArrayMethodOfStringMatchAll(
node: CallExpression,
Expand Down
31 changes: 31 additions & 0 deletions tests/lib/rules/no-unused-capturing-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ tester.run("no-unused-capturing-group", rule as any, {
const text = 'Lorem ipsum dolor sit amet'
const replaced = text.replace(/([\q{Lorem|ipsum}])/vg, '**$1**')
`,
// hasIndices
String.raw`const re = /(foo)/d
console.log(re.exec('foo').indices[1])
`,
String.raw`const re = /(?<f>foo)/d
console.log(re.exec('foo').indices.groups.f)
`,
String.raw`const re = /(foo)/d
console.log(re.exec('foo').indices[unknown])
`,
],
invalid: [
{
Expand Down Expand Up @@ -546,5 +556,26 @@ tester.run("no-unused-capturing-group", rule as any, {
options: [{ fixable: true }],
errors: ["Capturing group number 1 is defined but never used."],
},
// hasIndices
{
code: String.raw`const re = /(foo)/d
console.log(re.exec('foo').indices[2])
`,
output: String.raw`const re = /(?:foo)/d
console.log(re.exec('foo').indices[2])
`,
options: [{ fixable: true }],
errors: ["Capturing group number 1 is defined but never used."],
},
{
code: String.raw`const re = /(?<f>foo)/d
console.log(re.exec('foo').indices.groups.x)
`,
output: String.raw`const re = /(?:foo)/d
console.log(re.exec('foo').indices.groups.x)
`,
options: [{ fixable: true }],
errors: ["Capturing group 'f' is defined but never used."],
},
],
})

0 comments on commit 4a13302

Please sign in to comment.