-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathno-unused-capturing-group.ts
191 lines (177 loc) · 6.31 KB
/
no-unused-capturing-group.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import type { CapturingGroup } from "@eslint-community/regexpp/ast"
import type { RegExpVisitor } from "@eslint-community/regexpp/visitor"
import { getCapturingGroupNumber } from "regexp-ast-analysis"
import type { ObjectOption } from "../types"
import type { RegExpContext } from "../utils"
import { createRule, defineRegexpVisitor } from "../utils"
/**
* Returns an identifier for the given capturing group.
*
* This is either the name of the group or its number.
*/
function getCapturingGroupIdentifier(group: CapturingGroup): string {
if (group.name) {
return `'${group.name}'`
}
return `number ${getCapturingGroupNumber(group)}`
}
export default createRule("no-unused-capturing-group", {
meta: {
docs: {
description: "disallow unused capturing group",
category: "Best Practices",
recommended: true,
},
fixable: "code",
schema: [
{
type: "object",
properties: {
fixable: { type: "boolean" },
allowNamed: { type: "boolean" },
},
additionalProperties: false,
},
],
messages: {
unusedCapturingGroup:
"Capturing group {{identifier}} is defined but never used.",
// suggestions
makeNonCapturing: "Making this a non-capturing group.",
},
type: "suggestion", // "problem",
hasSuggestions: true,
},
create(context) {
const fixable: boolean =
(context.options[0] as ObjectOption)?.fixable ?? false
const allowNamed: boolean =
(context.options[0] as ObjectOption)?.allowNamed ?? false
function reportUnused(
unused: Set<CapturingGroup>,
regexpContext: RegExpContext,
) {
const {
node,
getRegexpLocation,
fixReplaceNode,
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)) {
fixableGroups.add(group)
} else {
break
}
}
for (const cgNode of unused) {
const fix = fixableGroups.has(cgNode)
? fixReplaceNode(
cgNode,
cgNode.raw.replace(/^\((?:\?<[^<>]+>)?/u, "(?:"),
)
: null
context.report({
node,
loc: getRegexpLocation(cgNode),
messageId: "unusedCapturingGroup",
data: { identifier: getCapturingGroupIdentifier(cgNode) },
fix: fixable ? fix : null,
suggest: fix
? [{ messageId: "makeNonCapturing", fix }]
: null,
})
}
}
function getCapturingGroupReferences(regexpContext: RegExpContext) {
const capturingGroupReferences =
regexpContext.getCapturingGroupReferences()
if (!capturingGroupReferences.length) {
// unused regexp
return null
}
const indexRefs: number[] = []
const namedRefs: string[] = []
let hasUnknownName = false
let hasSplit = false
for (const ref of capturingGroupReferences) {
if (ref.type === "UnknownUsage" || ref.type === "UnknownRef") {
return null
}
if (
ref.type === "ArrayRef" ||
ref.type === "ReplacementRef" ||
ref.type === "ReplacerFunctionRef"
) {
if (ref.kind === "index") {
if (ref.ref != null) {
indexRefs.push(ref.ref)
} else {
return null
}
} else {
// named
if (ref.ref) {
namedRefs.push(ref.ref)
} else {
hasUnknownName = true
}
}
} else if (ref.type === "Split") {
hasSplit = true
}
}
return {
unusedIndexRef(index: number): boolean {
if (hasSplit) {
return false
}
return !indexRefs.includes(index)
},
unusedNamedRef(name: string): boolean {
if (hasUnknownName) {
return false
}
return !namedRefs.includes(name)
},
}
}
function createVisitor(
regexpContext: RegExpContext,
): RegExpVisitor.Handlers {
const references = getCapturingGroupReferences(regexpContext)
if (!references) {
// unused regexp or unknown reference
return {}
}
const unused = new Set<CapturingGroup>()
const allCapturingGroups = regexpContext.getAllCapturingGroups()
for (let index = 0; index < allCapturingGroups.length; index++) {
const cgNode = allCapturingGroups[index]
if (
cgNode.references.length ||
!references.unusedIndexRef(index + 1)
) {
continue
}
if (cgNode.name && !references.unusedNamedRef(cgNode.name)) {
continue
}
unused.add(cgNode)
}
reportUnused(unused, regexpContext)
return {}
}
return defineRegexpVisitor(context, {
createVisitor,
})
},
})