-
Notifications
You must be signed in to change notification settings - Fork 20
/
babel-runtime.ts
377 lines (357 loc) Β· 16.4 KB
/
babel-runtime.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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import { isTopLevel } from '@wakaru/ast-utils'
import { getTopLevelStatements } from '@wakaru/ast-utils/program'
import type { Module } from '../Module'
import type { ArrowFunctionExpression, Collection, FunctionDeclaration, FunctionExpression, JSCodeshift } from 'jscodeshift'
const moduleMatchers: Record<string, Array<string | RegExp | Array<string | RegExp>>> = {
'@babel/runtime/helpers/arrayLikeToArray': [
/for\s?\(var \w+\s?=\s?0,\s?\w+\s?=\s?(new\s)?Array\(\w+\);\s?\w+\s?<\s?\w+;\s?\w+\+\+\)\s?\w+\[\w+\]\s?=\s?\w+\[\w+\]/,
],
'@babel/runtime/helpers/arrayWithHoles': [
/{(\r\n|\r|\n)?(\s+)?if\s?\(Array\.isArray\(\w+\)\)\s?return\s?\w+;?(\r\n|\r|\n)?(\s+)?}/,
],
'@babel/runtime/helpers/classCallCheck': [
'throw new TypeError("Cannot call a class as a function")',
],
'@babel/runtime/helpers/createForOfIteratorHelperLoose': [
'throw new TypeError("Invalid attempt to iterate non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")',
],
'@babel/runtime/helpers/createForOfIteratorHelper': [
[
/if\s?\(!\w+\s?&&\s?\w+\.return\s?!=\s?null\)\s?\w+\.return\(\)/,
'throw new TypeError("Invalid attempt to iterate non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")',
],
],
'@babel/runtime/helpers/extends': [
/\w+\s?=\s?Object\.assign\s?\|\|\s?function\s?\(\w+\)/,
// v7.18.2 added a .bind() call to Object.assign
/\w+\s?=\s?Object\.assign\s?\?\s?Object\.assign\.bind\(\)\s?:\s?function\s?\(\w+\)/,
],
'@babel/runtime/helpers/inherits': [
'throw new TypeError("Super expression must either be null or a function")',
],
'@babel/runtime/helpers/interopRequireDefault': [
/return\s?\w+\s?&&\s?\w+\.__esModule\s?\?\s?\w+\s?:\s?{ default: \w+ }/,
],
'@babel/runtime/helpers/interopRequireWildcard': [
'typeof WeakMap',
// !nodeInterop && obj && obj.__esModule
/!\w+\s?&&\s?\w+\s?&&\s?\w+\.__esModule/,
],
'@babel/runtime/helpers/iterableToArray': [
/if\s?\(typeof\sSymbol\s?!==\s?"undefined"\s?&&\s?\w+\[Symbol\.iterator\]\s?!=\s?null\s?\|\|\s?\w+\["@@iterator"\]\s?!=\s?null\)\s?return\sArray\.from\(\w+\)/,
],
'@babel/runtime/helpers/iterableToArrayLimit': [
/\w+\s?=\s?null\s?==\s?\w+\s?\?\s?null\s?:\s?"undefined"\s?!=\s?typeof Symbol\s?&&\s?\w+\[Symbol\.iterator\]\s?\|\|\s?\w+\["@@iterator"\]/,
],
'@babel/runtime/helpers/iterableToArrayLimitLoose': [
/\w+\s?=\s?\w+\s?&&\s?\("undefined"\s?!=\s?typeof Symbol\s?&&\s?\w+\[Symbol\.iterator]\s?\|\|\s?\w+\["@@iterator"\]\)/,
],
'@babel/runtime/helpers/newArrowCheck': [
'throw new TypeError("Cannot instantiate an arrow function")',
],
'@babel/runtime/helpers/nonIterableRest': [
'Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.',
],
'@babel/runtime/helpers/objectDestructuringEmpty': [
'throw new TypeError("Cannot destructure "',
],
'@babel/runtime/helpers/objectWithoutProperties': [
[
/if\s?\(\w+\.indexOf\(\w+\)\s?>=\s?0\)\s?continue/,
/if\s?\(Object\.getOwnPropertySymbols\)\s?{(\r\n|\r|\n)?(\s+)?var \w+\s?=\s?Object\.getOwnPropertySymbols\(\w+\)/,
/if\s?\(!Object\.prototype\.propertyIsEnumerable\.call\(\w+,\s?\w+\)\)\s?continue/,
],
[
// let's try some keyword based matching
'.indexOf(',
'Object.getOwnPropertySymbols',
'Object.prototype.propertyIsEnumerable.call(',
/\w+\[\w+\]\s?=\s?\w+\[\w+\]/,
],
],
// FIXME: this function's implementation is too generic, we need to find a better way to match it.
'@babel/runtime/helpers/objectWithoutPropertiesLoose': [
[
/if\s?\(\w+\.indexOf\(\w+\)\s?>=\s?0\)\s?continue/,
/var \w+\s?=\s?{};?(\r\n|\r|\n)?(\s+)?var \w+\s?=\s?Object\.keys\(\w+\)/,
/\w+\[\w+\]\s?=\s?\w+\[\w+\]/,
],
],
'@babel/runtime/helpers/taggedTemplateLiteral': [
[
/if\s?\(!(?<raw>\w+)\)\s?\{\s+?\k<raw>\s?=\s?(?<strings>\w+)\.slice\(0\);?\s+?}/,
/return\sObject\.freeze\(Object\.defineProperties\(\w+,\s?\{\s?raw:\s?\{\s?value:\s?Object\.freeze\((?<raw>\w+)\)\s?}\s?}\s?\)\)/,
],
],
'@babel/runtime/helpers/taggedTemplateLiteralLoose ': [
[
/if\s?\(!(?<raw>\w+)\)\s?\{\s+?\k<raw>\s?=\s?(?<strings>\w+)\.slice\(0\);?\s+?}/,
/(?<strings>\w+)\.raw\s?=\s?(?<raw>\w+)/,
],
],
'@babel/runtime/helpers/typeof': [
/\w+\s?=\s?"function"\s?===?\s?typeof Symbol\s?&&\s?"symbol"\s?===?\s?typeof Symbol\.iterator\s?\?/,
/&& \w+\.constructor\s?===?\s?Symbol\s?&&\s?\w+\s?!==?\s?Symbol\.prototype\s?\?\s?"symbol"\s?:\s?typeof\s?\w+/,
],
'@babel/runtime/helpers/unsupportedIterableToArray': [
/\/\^\(\?:Ui\|I\)nt\(\?:8\|16\|32\)\(\?:Clamped\)\?Array\$\/\.test\(\w+\)\)/,
],
'@babel/runtime/helpers/wrapNativeSuper': [
'throw new TypeError("Super expression must either be null or a function")',
],
}
const moduleDeps: Record<string, string[] | undefined> = {
// '@babel/runtime/helpers/arrayLikeToArray': [],
// '@babel/runtime/helpers/arrayWithHoles': [],
'@babel/runtime/helpers/arrayWithoutHoles': [
'@babel/runtime/helpers/arrayLikeToArray',
],
// '@babel/runtime/helpers/classCallCheck': [],
'@babel/runtime/helpers/construct': [
'@babel/runtime/helpers/isNativeReflectConstruct',
'@babel/runtime/helpers/setPrototypeOf',
],
'@babel/runtime/helpers/createForOfIteratorHelper': [
'@babel/runtime/helpers/unsupportedIterableToArray',
],
'@babel/runtime/helpers/createForOfIteratorHelperLoose': [
'@babel/runtime/helpers/unsupportedIterableToArray',
],
'@babel/runtime/helpers/createSuper': [
'@babel/runtime/helpers/getPrototypeOf',
'@babel/runtime/helpers/isNativeReflectConstruct',
'@babel/runtime/helpers/possibleConstructorReturn',
],
'@babel/runtime/helpers/defineProperty': [
'@babel/runtime/helpers/toPropertyKey',
],
// '@babel/runtime/helpers/extends': [],
'@babel/runtime/helpers/get': [
'@babel/runtime/helpers/superPropBase',
],
// '@babel/runtime/helpers/getPrototypeOf': [],
'@babel/runtime/helpers/inherits': [
'@babel/runtime/helpers/setPrototypeOf',
],
'@babel/runtime/helpers/inheritsLoose': [
'@babel/runtime/helpers/setPrototypeOf',
],
// '@babel/runtime/helpers/isNativeFunction': [],
// '@babel/runtime/helpers/isNativeReflectConstruct': [],
// '@babel/runtime/helpers/iterableToArray': [],
// '@babel/runtime/helpers/interopRequireDefault': [],
// '@babel/runtime/helpers/interopRequireWildcard': [],
// '@babel/runtime/helpers/iterableToArrayLimit': [],
// '@babel/runtime/helpers/iterableToArrayLimitLoose': [],
'@babel/runtime/helpers/maybeArrayLike': [
'@babel/runtime/helpers/arrayLikeToArray',
],
// '@babel/runtime/helpers/newArrowCheck': [],
// '@babel/runtime/helpers/nonIterableRest': [],
// '@babel/runtime/helpers/nonIterableSpread': [],
// '@babel/runtime/helpers/objectDestructuringEmpty': [],
'@babel/runtime/helpers/objectSpread': [
'@babel/runtime/helpers/defineProperty',
],
'@babel/runtime/helpers/objectSpread2': [
'@babel/runtime/helpers/defineProperty',
],
'@babel/runtime/helpers/objectWithoutProperties': [
'@babel/runtime/helpers/objectWithoutPropertiesLoose',
],
// '@babel/runtime/helpers/objectWithoutPropertiesLoose': [],
'@babel/runtime/helpers/set': [
'@babel/runtime/helpers/defineProperty',
'@babel/runtime/helpers/superPropBase',
],
// '@babel/runtime/helpers/setPrototypeOf': [],
// '@babel/runtime/helpers/skipFirstGeneratorNext': [],
'@babel/runtime/helpers/slicedToArray': [
'@babel/runtime/helpers/arrayWithHoles',
'@babel/runtime/helpers/iterableToArrayLimit',
'@babel/runtime/helpers/nonIterableRest',
'@babel/runtime/helpers/unsupportedIterableToArray',
],
'@babel/runtime/helpers/slicedToArrayLoose': [
'@babel/runtime/helpers/arrayWithHoles',
'@babel/runtime/helpers/iterableToArrayLimitLoose',
'@babel/runtime/helpers/nonIterableRest',
'@babel/runtime/helpers/unsupportedIterableToArray',
],
'@babel/runtime/helpers/superPropBase': [
'@babel/runtime/helpers/getPrototypeOf',
],
// '@babel/runtime/helpers/taggedTemplateLiteral': [],
// '@babel/runtime/helpers/taggedTemplateLiteralLoose': [],
'@babel/runtime/helpers/toArray': [
'@babel/runtime/helpers/arrayWithoutHoles',
'@babel/runtime/helpers/iterableToArray',
'@babel/runtime/helpers/nonIterableRest',
'@babel/runtime/helpers/unsupportedIterableToArray',
],
'@babel/runtime/helpers/toConsumableArray': [
'@babel/runtime/helpers/arrayWithoutHoles',
'@babel/runtime/helpers/iterableToArray',
'@babel/runtime/helpers/nonIterableSpread',
'@babel/runtime/helpers/unsupportedIterableToArray',
],
// '@babel/runtime/helpers/toPrimitive': [],
'@babel/runtime/helpers/toPropertyKey': [
'@babel/runtime/helpers/toPrimitive',
],
'@babel/runtime/helpers/unsupportedIterableToArray': [
'@babel/runtime/helpers/arrayLikeToArray',
],
'@babel/runtime/helpers/wrapNativeSuper': [
'@babel/runtime/helpers/construct',
'@babel/runtime/helpers/getPrototypeOf',
'@babel/runtime/helpers/isNativeFunction',
'@babel/runtime/helpers/setPrototypeOf',
],
}
/**
* Scan all top level functions and mark tags based on the content of the function.
*/
export function scanBabelRuntime(j: JSCodeshift, module: Module & { root: Collection }) {
const { root } = module
const statements = getTopLevelStatements(root)
const functions = statements.filter((node): node is FunctionExpression | FunctionDeclaration | ArrowFunctionExpression => {
return j.FunctionDeclaration.check(node)
|| j.ArrowFunctionExpression.check(node)
})
functions.forEach((func) => {
const functionName = func.id?.name
if (!functionName || typeof functionName !== 'string') return
const code = j(func).toSource()
const collectedTags = [...new Set(Object.keys(moduleMatchers)
.filter((moduleName) => {
const matchers = moduleMatchers[moduleName]
return matchers.some((matcher) => {
if (typeof matcher === 'string') {
return code.includes(matcher)
}
else if (matcher instanceof RegExp) {
return matcher.test(code)
}
else if (Array.isArray(matcher)) {
return matcher.every((m) => {
if (typeof m === 'string') {
return code.includes(m)
}
else if (m instanceof RegExp) {
return m.test(code)
}
return false
})
}
return false
})
}),
)]
/**
* Module's dependencies might be inlined by compiler.
* So we need to remove scanned tag that are dependent
* of other scanned tags.
*/
const tagsDependencies = collectedTags.flatMap(tag => moduleDeps[tag] ?? [])
const tags = collectedTags.filter(tag => !tagsDependencies.includes(tag))
module.tags[functionName] ??= []
module.tags[functionName].push(...tags)
})
}
/**
* Go through all tagged functions and check for the usage of other tagged functions.
* If a tagged function is used, then we need to add the tag to the function.
* In the end, we will have a complete list of tags for each function, so that
* we have a chance to change the tags to their upper level functions.
*/
export function postScanBabelRuntime(j: JSCodeshift, modules: (Module & { root: Collection })[]) {
modules.forEach((module) => {
const { root, import: imports } = module
const rootScope = root.get().scope
const taggedImportLocals = new Map<string, string[]>()
imports.forEach((imp) => {
if (imp.type === 'bare' || imp.type === 'namespace') return
const targetModule = modules.find(m => m.id.toString() === imp.source.toString())
if (!targetModule || Object.keys(targetModule.tags).length === 0) return
if (imp.type === 'named') {
const targetTags = targetModule.tags[imp.name]
if (!targetTags || targetTags.length === 0) return
taggedImportLocals.set(imp.local, targetTags)
return
}
if (imp.type === 'default') {
const targetTags = targetModule.tags.default
if (targetTags && targetTags.length !== 0) {
taggedImportLocals.set(imp.name, targetTags)
}
Object.entries(targetModule.export).forEach(([exportName, exportLocalName]) => {
const targetTags = targetModule.tags[exportLocalName]
// TODO: Currently we didn't pull in dependent's imported tags.
// We might need to build a module graph and start from the leaf.
if (!targetTags || targetTags.length === 0) return
taggedImportLocals.set(`${imp.name}.${exportName}`, targetTags)
})
}
}, {} as Record<string, string[]>)
const functionPaths = [
...root.find(j.FunctionDeclaration).filter(p => isTopLevel(j, p)).paths(),
...root.find(j.ArrowFunctionExpression).filter(p => isTopLevel(j, p)).paths(),
]
functionPaths.forEach((func) => {
const functionName = func.node.id?.name
if (!functionName || typeof functionName !== 'string') return
taggedImportLocals.forEach((tags, localName) => {
const [importObj, importProp] = localName.split('.')
const isReferenced = localName.includes('.')
? j(func)
.find(j.MemberExpression, {
object: { name: importObj },
property: { name: importProp },
})
.filter((path) => {
const scope = path.scope?.lookup(importObj)
return scope === rootScope
})
.size() > 0
: j(func)
.find(j.Identifier, { name: localName })
.filter((path) => {
const scope = path.scope?.lookup(localName)
return scope === rootScope
})
.size() > 0
if (isReferenced) {
module.tags[functionName] ??= []
module.tags[functionName].push(...tags)
}
})
if (module.tags[functionName]?.length === 0) return
/**
* Try to combine tags based on dependencies.
*/
const moduleTag = module.tags[functionName]!
let score = 0
let matchedTag: string | null = null
Object.entries(moduleDeps).forEach(([tag, deps]) => {
if (!deps) return
const allMatch = deps.every((dep) => {
return moduleTag.includes(dep)
})
// TODO: we can further improve the scoring algorithm.
if (allMatch && deps.length > score) {
score = deps.length
matchedTag = tag
}
})
if (matchedTag) {
const deps = moduleDeps[matchedTag]!
deps.forEach((dep) => {
const index = moduleTag.indexOf(dep)
moduleTag.splice(index, 1)
})
moduleTag.unshift(matchedTag, ...deps.map(dep => `- ${dep}`))
}
})
})
}