-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
js-analyze.js
311 lines (264 loc) · 8.84 KB
/
js-analyze.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
import { parse } from '@meteorjs/babel';
import { analyze as analyzeScope } from 'escope';
import LRU from "lru-cache";
import Visitor from "@meteorjs/reify/lib/visitor.js";
import { findPossibleIndexes } from "@meteorjs/reify/lib/utils.js";
const hasOwn = Object.prototype.hasOwnProperty;
const objToStr = Object.prototype.toString
function isRegExp(value) {
return value && objToStr.call(value) === "[object RegExp]";
}
var AST_CACHE = new LRU({
max: Math.pow(2, 12),
length(ast) {
return ast.loc.end.line;
}
});
// Like babel.parse, but annotates any thrown error with $ParseError = true.
function tryToParse(source, hash) {
if (hash && AST_CACHE.has(hash)) {
return AST_CACHE.get(hash);
}
let ast;
try {
ast = parse(source);
} catch (e) {
if (typeof e.loc === 'object') {
e.$ParseError = true;
}
throw e;
}
if (hash) {
AST_CACHE.set(hash, ast);
}
return ast;
}
/**
* The `findImportedModuleIdentifiers` function takes a string of module
* source code and returns a map from imported module identifiers to AST
* nodes. The keys of this map are used in ./import-scanner.ts to traverse
* the module dependency graph. The AST nodes are generally ignored.
*
* The implementation uses a regular expression to scan quickly for
* possible locations of certain tokens (`require`, `import`, `export`),
* then uses that location information to steer the AST traversal, so that
* it visits only subtrees that contain interesting tokens, saving a lot
* of time by ignoring the rest of the AST. The AST traversal determines
* if the tokens were actually what we thought they were (a `require`
* function call, or an `import` or `export` statement).
*/
export function findImportedModuleIdentifiers(source, hash) {
const possibleIndexes = findPossibleIndexes(source, [
"require",
"import",
"export",
"dynamicImport",
"link",
]);
if (possibleIndexes.length === 0) {
return {};
}
const ast = tryToParse(source, hash);
importedIdentifierVisitor.visit(ast, source, possibleIndexes);
return importedIdentifierVisitor.identifiers;
}
const importedIdentifierVisitor = new (class extends Visitor {
reset(rootPath, code, possibleIndexes) {
this.requireIsBound = false;
this.identifiers = Object.create(null);
// Defining this.possibleIndexes causes the Visitor to ignore any
// subtrees of the AST that do not contain any indexes of identifiers
// that we care about. Note that findPossibleIndexes uses a RegExp to
// scan for the given identifiers, so there may be false positives,
// but that's fine because it just means scanning more of the AST.
this.possibleIndexes = possibleIndexes;
}
addIdentifier(id, type, dynamic) {
const entry = hasOwn.call(this.identifiers, id)
? this.identifiers[id]
: this.identifiers[id] = {
possiblySpurious: true,
dynamic: !! dynamic
};
if (! dynamic) {
entry.dynamic = false;
}
if (type === "require") {
// If the identifier comes from a require call, but require is not a
// free variable, then this dependency might be spurious.
entry.possiblySpurious =
entry.possiblySpurious && this.requireIsBound;
} else {
// The import keyword can't be shadowed, so any dependencies
// registered by import statements should be trusted absolutely.
entry.possiblySpurious = false;
}
}
visitFunctionExpression(path) {
return this._functionParamRequireHelper(path);
}
visitFunctionDeclaration(path) {
return this._functionParamRequireHelper(path);
}
visitArrowFunctionExpression(path) {
return this._functionParamRequireHelper(path);
}
_functionParamRequireHelper(path) {
const node = path.getValue();
if (node.params.some(param => isIdWithName(param, "require"))) {
const { requireIsBound } = this;
this.requireIsBound = true;
this.visitChildren(path);
this.requireIsBound = requireIsBound;
} else {
this.visitChildren(path);
}
}
visitCallExpression(path) {
const node = path.getValue();
const args = node.arguments;
const argc = args.length;
const firstArg = args[0];
this.visitChildren(path);
if (! isStringLiteral(firstArg)) {
return;
}
if (isIdWithName(node.callee, "require")) {
this.addIdentifier(firstArg.value, "require");
} else if (node.callee.type === "Import" ||
isIdWithName(node.callee, "import")) {
this.addIdentifier(firstArg.value, "import", true);
} else if (node.callee.type === "MemberExpression" &&
// The Reify compiler sometimes renames references to the
// CommonJS module object for hygienic purposes, but it
// always does so by appending additional numbers.
isIdWithName(node.callee.object, /^module\d*$/)) {
const propertyName =
isPropertyWithName(node.callee.property, "link") ||
isPropertyWithName(node.callee.property, "dynamicImport");
if (propertyName) {
this.addIdentifier(
firstArg.value,
"import",
propertyName === "dynamicImport"
);
}
}
}
visitImportDeclaration(path) {
return this._importExportSourceHelper(path);
}
visitExportAllDeclaration(path) {
return this._importExportSourceHelper(path);
}
visitExportNamedDeclaration(path) {
return this._importExportSourceHelper(path);
}
_importExportSourceHelper(path) {
const node = path.getValue();
// The .source of an ImportDeclaration or Export{Named,All}Declaration
// is always a string-valued Literal node, if not null.
if (isStringLiteral(node.source)) {
this.addIdentifier(
node.source.value,
"import",
false
);
}
}
});
function isIdWithName(node, name) {
if (! node ||
node.type !== "Identifier") {
return false;
}
if (typeof name === "string") {
return node.name === name;
}
if (isRegExp(name)) {
return name.test(node.name);
}
return false;
}
function isStringLiteral(node) {
return node && (
node.type === "StringLiteral" ||
(node.type === "Literal" &&
typeof node.value === "string"));
}
function isPropertyWithName(node, name) {
if (isIdWithName(node, name) ||
(isStringLiteral(node) &&
node.value === name)) {
return name;
}
}
// Analyze the JavaScript source code `source` and return a dictionary of all
// globals which are assigned to in the package. The values in the dictionary
// are all `true`.
//
// This is intended for use in detecting package-scope variables in Meteor
// packages, where the linker needs to add a "var" statement to prevent them
// from staying as globals.
//
// It only cares about assignments to variables; an assignment to a field on an
// object (`Foo.Bar = true`) neither causes `Foo` nor `Foo.Bar` to be returned.
const globalsCache = new LRU({
max: Math.pow(2, 12),
length(globals) {
let sum = 0;
Object.keys(globals).forEach(name => sum += name.length);
return sum;
}
});
export function findAssignedGlobals(source, hash) {
if (hash && globalsCache.has(hash)) {
return globalsCache.get(hash);
}
const ast = tryToParse(source, hash);
// We have to pass ignoreEval; otherwise, the existence of a direct eval call
// causes escope to not bother to resolve references in the eval's scope.
// This is because an eval can pull references inward:
//
// function outer() {
// var i = 42;
// function inner() {
// eval('var i = 0');
// i; // 0, not 42
// }
// }
//
// But it can't pull references outward, so for our purposes it is safe to
// ignore.
const scopeManager = analyzeScope(ast, {
ecmaVersion: 6,
sourceType: "module",
ignoreEval: true,
// Ensures we don't treat top-level var declarations as globals.
nodejsScope: true,
});
const program = ast.type === "File" ? ast.program : ast;
const programScope = scopeManager.acquire(program);
const assignedGlobals = {};
// Passing {sourceType: "module"} to analyzeScope leaves this list
// strangely empty, but {sourceType: "script"} forbids ImportDeclaration
// nodes (because they are only legal in modules.
programScope.implicit.variables.forEach(variable => {
assignedGlobals[variable.name] = true;
});
// Fortunately, even with {sourceType: "module"}, the .implicit.left
// array still has all the information we need, as long as we ignore
// global variable references that are not assignments.
programScope.implicit.left.forEach(entry => {
if (entry.identifier &&
entry.identifier.type === "Identifier" &&
// Only consider identifers that are assigned a value.
entry.writeExpr) {
assignedGlobals[entry.identifier.name] = true;
}
});
if (hash) {
globalsCache.set(hash, assignedGlobals);
}
return assignedGlobals;
}