-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathprefer-result-array-groups.ts
184 lines (172 loc) · 7.21 KB
/
prefer-result-array-groups.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
import type { RegExpVisitor } from "@eslint-community/regexpp/visitor"
import type { Expression, Super } from "estree"
import type * as TS from "typescript"
import type { ObjectOption } from "../types"
import type { RegExpContext } from "../utils"
import { createRule, defineRegexpVisitor } from "../utils"
import {
getTypeScriptTools,
isAny,
isClassOrInterface,
isUnionOrIntersection,
isNull,
} from "../utils/ts-util"
import { isOpeningBracketToken } from "@eslint-community/eslint-utils"
export default createRule("prefer-result-array-groups", {
meta: {
docs: {
description: "enforce using result array `groups`",
category: "Stylistic Issues",
recommended: false,
},
fixable: "code",
schema: [
{
type: "object",
properties: {
strictTypes: { type: "boolean" },
},
additionalProperties: false,
},
],
messages: {
unexpected:
"Unexpected indexed access for the named capturing group '{{ name }}' from regexp result array.",
},
type: "suggestion",
},
create(context) {
const strictTypes =
(context.options[0] as ObjectOption)?.strictTypes ?? true
const sourceCode = context.sourceCode
function createVisitor(
regexpContext: RegExpContext,
): RegExpVisitor.Handlers {
const { getAllCapturingGroups, getCapturingGroupReferences } =
regexpContext
const capturingGroups = getAllCapturingGroups()
if (!capturingGroups.length) {
return {}
}
for (const ref of getCapturingGroupReferences({ strictTypes })) {
if (
ref.type === "ArrayRef" &&
ref.kind === "index" &&
ref.ref != null
) {
const cgNode = capturingGroups[ref.ref - 1]
if (cgNode && cgNode.name) {
const memberNode =
ref.prop.type === "member" ? ref.prop.node : null
context.report({
node: ref.prop.node,
messageId: "unexpected",
data: {
name: cgNode.name,
},
fix:
memberNode && memberNode.computed
? (fixer) => {
const tokens =
sourceCode.getTokensBetween(
memberNode.object,
memberNode.property,
)
let openingBracket = tokens.pop()
while (
openingBracket &&
!isOpeningBracketToken(
openingBracket,
)
) {
openingBracket = tokens.pop()
}
if (!openingBracket) {
// unknown ast
return null
}
const kind = getRegExpArrayTypeKind(
memberNode.object,
)
if (kind === "unknown") {
// Using TypeScript but I can't identify the type or it's not a RegExpXArray type.
return null
}
const needNonNull =
kind === "RegExpXArray"
return fixer.replaceTextRange(
[
openingBracket.range[0],
memberNode.range![1],
],
`${
memberNode.optional ? "" : "."
}groups${
needNonNull ? "!" : ""
}.${cgNode.name}`,
)
}
: null,
})
}
}
}
return {}
}
return defineRegexpVisitor(context, {
createVisitor,
})
type RegExpArrayTypeKind =
| "RegExpXArray" // RegExpMatchArray or RegExpExecArray
| "any"
| "unknown" // It's cannot autofix
/** Gets the type kind of the given node. */
function getRegExpArrayTypeKind(
node: Expression | Super,
): RegExpArrayTypeKind | null {
const { tsNodeMap, checker, usedTS, hasFullTypeInformation } =
getTypeScriptTools(context)
if (!usedTS) {
// Not using TypeScript.
return null
}
if (!hasFullTypeInformation) {
// The user has not given the type information to ESLint. So we don't know if this can be autofix.
return "unknown"
}
const tsNode = tsNodeMap.get(node)
const tsType =
(tsNode && checker?.getTypeAtLocation(tsNode)) || null
if (!tsType) {
// The node type cannot be determined.
return "unknown"
}
if (isAny(tsType)) {
return "any"
}
if (isRegExpMatchArrayOrRegExpExecArray(tsType)) {
return "RegExpXArray"
}
if (isUnionOrIntersection(tsType)) {
if (
tsType.types.every(
(t) =>
isRegExpMatchArrayOrRegExpExecArray(t) || isNull(t),
)
) {
// e.g. (RegExpExecArray | null)
return "RegExpXArray"
}
}
return "unknown"
}
/** Checks whether given type is RegExpMatchArray or RegExpExecArray or not */
function isRegExpMatchArrayOrRegExpExecArray(tsType: TS.Type) {
if (isClassOrInterface(tsType)) {
const name = tsType.symbol.escapedName
return name === "RegExpMatchArray" || name === "RegExpExecArray"
}
return false
}
},
})