Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed false positives for hasIndices in regexp/no-unused-capturing-group #676

Merged
merged 3 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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."],
},
],
})
Loading