-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathdefine-prototype-method-handler.js
354 lines (321 loc) · 12.2 KB
/
define-prototype-method-handler.js
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
"use strict"
const { getPropertyName } = require("eslint-utils")
const { optionalRequire } = require("./optional-require")
/** @type {import("typescript")} */
const ts = optionalRequire(require, "typescript")
/**
* Define handlers to disallow prototype methods.
* @param {RuleContext} context The rule context.
* @param {Record<string, readonly string[]>} nameMap The method names to disallow. The key is class names and that value is method names.
* @returns {Record<string, (node: ASTNode) => void>} The defined handlers.
*/
function definePrototypeMethodHandler(context, nameMap) {
const aggressive = getAggressiveOption(context)
/** @type {ReadonlyMap<any, import("typescript").Node>} */
const tsNodeMap = context.parserServices.esTreeNodeToTSNodeMap
/** @type {import("typescript").TypeChecker} */
const checker =
context.parserServices.program &&
context.parserServices.program.getTypeChecker()
const isTS = Boolean(ts && tsNodeMap && checker)
const hasFullType =
isTS && context.parserServices.hasFullTypeInformation !== false
/**
* Check if the type of the given node is one of given class or not.
* @param {MemberExpression} memberAccessNode The MemberExpression node.
* @param {string} className The class name to disallow.
* @returns {boolean} `true` if should disallow it.
*/
function checkObjectType(memberAccessNode, className) {
// If it's obvious, shortcut.
if (memberAccessNode.object.type === "ArrayExpression") {
return className === "Array"
}
if (
memberAccessNode.object.type === "Literal" &&
memberAccessNode.object.regex
) {
return className === "RegExp"
}
if (
(memberAccessNode.object.type === "Literal" &&
typeof memberAccessNode.object.value === "string") ||
memberAccessNode.object.type === "TemplateLiteral"
) {
return className === "String"
}
// Test object type.
return isTS
? checkByPropertyDeclaration(memberAccessNode, className) ||
checkByObjectExpressionType(memberAccessNode, className)
: aggressive
}
/**
* Check if the type of the given node by the declaration of `node.property`.
* @param {MemberExpression} memberAccessNode The MemberExpression node.
* @param {string} className The class name to disallow.
* @returns {boolean} `true` if should disallow it.
*/
function checkByPropertyDeclaration(memberAccessNode, className) {
const tsNode = tsNodeMap.get(memberAccessNode.property)
const symbol = tsNode && checker.getSymbolAtLocation(tsNode)
const declarations = symbol && symbol.declarations
if (declarations) {
for (const declaration of declarations) {
const type = checker.getTypeAtLocation(declaration.parent)
if (type && typeEquals(type, className)) {
return true
}
}
}
return false
}
/**
* Check if the type of the given node by the type of `node.object`.
* @param {MemberExpression} memberAccessNode The MemberExpression node.
* @param {string} className The class name to disallow.
* @returns {boolean} `true` if should disallow it.
*/
function checkByObjectExpressionType(memberAccessNode, className) {
const tsNode = tsNodeMap.get(memberAccessNode.object)
const type = checker.getTypeAtLocation(tsNode)
return typeEquals(type, className)
}
/**
* Check if the name of the given type is expected or not.
* @param {import("typescript").Type} type The type to check.
* @param {string} className The expected type name.
* @returns {boolean} `true` if should disallow it.
*/
function typeEquals(type, className) {
// console.log(
// "typeEquals(%o, %o)",
// {
// name: isClassOrInterface(type)
// ? type.symbol.escapedName
// : checker.typeToString(type),
// flags: Object.entries(ts.TypeFlags)
// .filter(
// ([_id, flag]) =>
// typeof flag === "number" &&
// (type.flags & flag) === flag,
// )
// .map(([id]) => id)
// .join("|"),
// objectFlags:
// type.objectFlags == null
// ? undefined
// : Object.entries(ts.ObjectFlags)
// .filter(
// ([_id, flag]) =>
// typeof flag === "number" &&
// (type.objectFlags & flag) === flag,
// )
// .map(([id]) => id)
// .join("|"),
// },
// className,
// )
if (isAny(type) || isUnknown(type)) {
return aggressive
}
if (isAnonymousObject(type)) {
// In non full-type mode, array types (e.g. `any[]`) become anonymous object type.
return hasFullType ? false : aggressive
}
if (isStringLike(type)) {
return className === "String"
}
if (isArrayLikeObject(type)) {
return className === "Array"
}
if (isReferenceObject(type) && type.target !== type) {
return typeEquals(type.target, className)
}
if (isTypeParameter(type)) {
const constraintType = getConstraintType(type)
if (constraintType) {
return typeEquals(constraintType, className)
}
return hasFullType ? false : aggressive
}
if (isUnionOrIntersection(type)) {
return type.types.some(t => typeEquals(t, className))
}
if (isClassOrInterface(type)) {
const name = type.symbol.escapedName
return name === className || name === `Readonly${className}`
}
return checker.typeToString(type) === className
}
/**
* Get the constraint type of a given type parameter type if exists.
*
* `type.getConstraint()` method doesn't return the constraint type of the
* type parameter for some reason. So this gets the constraint type via AST.
*
* @param {import("typescript").TypeParameter} type The type parameter type to get.
* @returns {import("typescript").Type | undefined} The constraint type.
*/
function getConstraintType(type) {
const symbol = type.symbol
const declarations = symbol && symbol.declarations
const declaration = declarations && declarations[0]
if (
ts.isTypeParameterDeclaration(declaration) &&
declaration.constraint != null
) {
return checker.getTypeFromTypeNode(declaration.constraint)
}
return undefined
}
// For performance
const nameMapEntries = Object.entries(nameMap)
if (nameMapEntries.length === 1) {
const [[className, methodNames]] = nameMapEntries
return {
MemberExpression(node) {
const propertyName = getPropertyName(node, context.getScope())
if (
methodNames.includes(propertyName) &&
checkObjectType(node, className)
) {
context.report({
node,
messageId: "forbidden",
data: {
name: `${className}.prototype.${propertyName}`,
},
})
}
},
}
}
return {
MemberExpression(node) {
const propertyName = getPropertyName(node, context.getScope())
for (const [className, methodNames] of nameMapEntries) {
if (
methodNames.includes(propertyName) &&
checkObjectType(node, className)
) {
context.report({
node,
messageId: "forbidden",
data: {
name: `${className}.prototype.${propertyName}`,
},
})
return
}
}
},
}
}
/**
* Get `aggressive` option value.
* @param {RuleContext} context The rule context.
* @returns {boolean} The gotten `aggressive` option value.
*/
function getAggressiveOption(context) {
const options = context.options[0]
const globalOptions = context.settings.es
if (options && typeof options.aggressive === "boolean") {
return options.aggressive
}
if (globalOptions && typeof globalOptions.aggressive === "boolean") {
return globalOptions.aggressive
}
return false
}
/**
* Check if a given type is an anonymous object type or not.
* @param {import("typescript").Type} type The type to check.
* @returns {type is import("typescript").ObjectType} `true` if the type is an anonymous object type.
*/
function isAnonymousObject(type) {
return isObject(type) && (type.objectFlags & ts.ObjectFlags.Anonymous) !== 0
}
/**
* Check if a given type is `any` or not.
* @param {import("typescript").Type} type The type to check.
* @returns {boolean} `true` if the type is `any`.
*/
function isAny(type) {
return (type.flags & ts.TypeFlags.Any) !== 0
}
/**
* Check if a given type is an array-like type or not.
* @param {import("typescript").Type} type The type to check.
* @returns {type is import("typescript").ObjectType} `true` if the type is an array-like type.
*/
function isArrayLikeObject(type) {
return (
isObject(type) &&
(type.objectFlags &
(ts.ObjectFlags.ArrayLiteral |
ts.ObjectFlags.EvolvingArray |
ts.ObjectFlags.Tuple)) !==
0
)
}
/**
* Check if a given type is an interface type or not.
* @param {import("typescript").Type} type The type to check.
* @returns {type is import("typescript").InterfaceType} `true` if the type is an interface type.
*/
function isClassOrInterface(type) {
return (
isObject(type) &&
(type.objectFlags & ts.ObjectFlags.ClassOrInterface) !== 0
)
}
/**
* Check if a given type is an object type or not.
* @param {import("typescript").Type} type The type to check.
* @returns {type is import("typescript").ObjectType} `true` if the type is an object type.
*/
function isObject(type) {
return (type.flags & ts.TypeFlags.Object) !== 0
}
/**
* Check if a given type is a reference type or not.
* @param {import("typescript").Type} type The type to check.
* @returns {type is import("typescript").TypeReference} `true` if the type is a reference type.
*/
function isReferenceObject(type) {
return isObject(type) && (type.objectFlags & ts.ObjectFlags.Reference) !== 0
}
/**
* Check if a given type is a string-like type or not.
* @param {import("typescript").Type} type The type to check.
* @returns {boolean} `true` if the type is a string-like type.
*/
function isStringLike(type) {
return (type.flags & ts.TypeFlags.StringLike) !== 0
}
/**
* Check if a given type is a type parameter type or not.
* @param {import("typescript").Type} type The type to check.
* @returns {boolean} `true` if the type is a type parameter type.
*/
function isTypeParameter(type) {
return (type.flags & ts.TypeFlags.TypeParameter) !== 0
}
/**
* Check if a given type is a union-or-intersection type or not.
* @param {import("typescript").Type} type The type to check.
* @returns {type is import("typescript").UnionOrIntersectionType} `true` if the type is a union-or-intersection type.
*/
function isUnionOrIntersection(type) {
return (type.flags & ts.TypeFlags.UnionOrIntersection) !== 0
}
/**
* Check if a given type is `unknown` or not.
* @param {import("typescript").Type} type The type to check.
* @returns {boolean} `true` if the type is `unknown`.
*/
function isUnknown(type) {
return (type.flags & ts.TypeFlags.Unknown) !== 0
}
module.exports = { definePrototypeMethodHandler }