-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathscanModuleCode.mjs
298 lines (265 loc) · 10.2 KB
/
scanModuleCode.mjs
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
// @ts-check
/** @import { ParserPlugin } from "@babel/parser" */
// Use `@babel/core` instead of `@babel/parser` and `@babel/traverse` directly
// so that project Babel config will be respected when parsing code.
import babel from "@babel/core";
import getVariableDeclarationIdentifierNames from "./getVariableDeclarationIdentifierNames.mjs";
// Babel seems to also support non-standard string literals in place of named
// import and export identifiers, perhaps because CJS can have export names
// containing dashes, etc. such as `exports["a-b-c"]` and they want to support
// these names in ESM that’s transpiled to CJS. Such non-standard names are
// explicitly not supported here, using the assertion below.
/** @type {typeof babel.types.assertIdentifier} */
const assertIdentifier = babel.types.assertIdentifier;
/** @type {typeof babel.types.assertFile} */
const assertFile = babel.types.assertFile;
/**
* Scans a JavaScript module’s code for ECMAScript module imports and exports.
* An ECMAScript module may contain all kinds of imports and exports. A CommonJS
* module may only contain dynamic imports, but because it might be source code
* to be bundled or transpiled, regular imports and exports are still analyzed.
* @param {string} code JavaScript code.
* @param {string} [path] Path to the file the code is from, for more useful
* Babel parse errors.
* @returns {Promise<ModuleScan>} Resolves an analysis of the module’s imports
* and exports.
*/
export default async function scanModuleCode(code, path) {
if (typeof code !== "string")
throw new TypeError("Argument 1 `code` must be a string.");
if (path !== undefined && typeof path !== "string")
throw new TypeError("Argument 2 `path` must be a string.");
/** @type {ModuleScan} */
const analysis = {
imports: {},
exports: new Set(),
};
/** @type {Array<ParserPlugin>} */
const plugins = [
// Allow parsing code containing modern syntax even if a project doesn’t
// have Babel config to handle it.
"classProperties",
["decorators", { decoratorsBeforeExport: false }],
];
if (path) {
// Path is a TypeScript module.
if (
path.endsWith(".mts") ||
path.endsWith(".cts") ||
path.endsWith(".ts") ||
path.endsWith(".tsx")
)
// Allow parsing code containing TypeScript syntax.
plugins.push("typescript");
// Allow parsing code containing JSX syntax.
if (path.endsWith(".tsx") || path.endsWith(".jsx")) plugins.push("jsx");
}
const ast = await babel.parseAsync(code, {
// Provide the code file path for more useful Babel parse errors.
filename: path,
parserOpts: {
plugins,
},
});
// Todo: Clarify what might cause `ast` to not be a `File`.
assertFile(ast);
babel.traverse(ast, {
ImportDeclaration(path) {
// There may be multiple statements for the same specifier.
if (!analysis.imports[path.node.source.value])
analysis.imports[path.node.source.value] = new Set();
for (const specifier of path.node.specifiers)
switch (specifier.type) {
case "ImportDefaultSpecifier":
// E.g. `import a from "a"`
// ^
analysis.imports[path.node.source.value].add("default");
break;
case "ImportNamespaceSpecifier":
// E.g. `import * as a from "a"`
// ^^^^^^
analysis.imports[path.node.source.value].add("*");
break;
case "ImportSpecifier": {
// E.g. `import { a as b } from "a"`
// ^^^^^^
// Guard against Babel support for a non-standard string literal:
// E.g. `import { "a-b-c" as a } from "a"`
// ^^^^^^^
assertIdentifier(specifier.imported);
analysis.imports[path.node.source.value].add(
specifier.imported.name,
);
break;
}
}
path.skip();
},
Import(path) {
// E.g. `import("a")`
// ^^^^^^
const [specifier] = /** @type {babel.types.CallExpression} */ (
path.parent
).arguments;
if (specifier && specifier.type === "StringLiteral") {
// There may be multiple statements for the same specifier.
if (!analysis.imports[specifier.value])
analysis.imports[specifier.value] = new Set();
// A dynamic import pulls in everything. It’s not feasible for this tool
// to figure out exactly the default or named imports used at runtime.
analysis.imports[specifier.value].add("default");
analysis.imports[specifier.value].add("*");
}
},
ExportDefaultDeclaration(path) {
// E.g. `export default 1`
// ^^^^^^^^^^^^^^^^
analysis.exports.add("default");
path.skip();
},
ExportAllDeclaration(path) {
// E.g. `export * from "a"`
// ^^^^^^^^^^^^^^^^^
// This is a special case as the export names are unknown.
// There may be multiple statements for the same specifier.
if (!analysis.imports[path.node.source.value])
analysis.imports[path.node.source.value] = new Set(["*"]);
else analysis.imports[path.node.source.value].add("*");
path.skip();
},
ExportNamedDeclaration(path) {
// Determine if the export is a declaration and export in one, or if it is
// either exporting an existing declaration or is “export from” syntax.
if (path.node.declaration)
switch (path.node.declaration.type) {
case "FunctionDeclaration":
// E.g. `export function a() {}`
// ^^^^^^^^^^^^^^^
analysis.exports.add(
// @ts-ignore `id` must exist in export declarations.
path.node.declaration.id.name,
);
break;
case "VariableDeclaration": {
// E.g. `export const a = 1`
// ^^^^^^^^^^^
for (const name of getVariableDeclarationIdentifierNames(
path.node.declaration,
))
analysis.exports.add(name);
}
}
else if (path.node.source) {
// E.g. `export { default } from "a"`
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^
// E.g. `export { default as a, a as b } from "a"`
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// E.g. `export * as a, { a as b } from "a"`
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// There may be multiple statements for the same specifier.
if (!analysis.imports[path.node.source.value])
analysis.imports[path.node.source.value] = new Set();
for (const specifier of path.node.specifiers) {
// Process the import.
switch (specifier.type) {
case "ExportNamespaceSpecifier":
// E.g. `export * as a from "a"`
// ^^^^^^
analysis.imports[path.node.source.value].add("*");
break;
case "ExportSpecifier": {
if (specifier.local.name === "default")
// E.g. `export { default as a } from "a"`
// ^^^^^^^
analysis.imports[path.node.source.value].add("default");
// E.g. `export { a as b } from "a"`
// ^
else
analysis.imports[path.node.source.value].add(
specifier.local.name,
);
break;
}
}
// Guard against Babel support for a non-standard string literal:
// E.g. `export { "a-b-c" as a } from "a"`
// ^^^^^^^
assertIdentifier(specifier.exported);
// Process the export.
if (specifier.exported.name === "default") {
// E.g. `export { a as default } from "a"`
// ^^^^^^^
analysis.exports.add("default");
} else {
// E.g. `export { a as b } from "a"`
// ^
analysis.exports.add(specifier.exported.name);
}
}
} else {
// E.g. `const a = 1; export { a }`
// ^^^^^^^^^^^^
for (const { exported } of path.node.specifiers) {
// Guard against Babel support for a non-standard string literal:
// E.g. `const a = 1; export { a as "a-b-c" }`
// ^^^^^^^
assertIdentifier(exported);
analysis.exports.add(exported.name);
}
}
path.skip();
},
});
// Scan comments to determine the ignored exports.
if (ast.comments)
for (const { value } of ast.comments) {
const comment = value.trim();
// Check if the comment matches an ignore exports comment.
const match = comment.match(/^ignore unused exports *(.*)?$/iu);
if (match) {
const [, exportNameList] = match;
if (exportNameList) {
// Check the list of export names matches the required format (words
// separated by a comma and optional spaces).
if (exportNameList.match(/^\w+(?:, *\w+)*$/u))
// Ignore all of the export names listed in the comment.
for (const name of exportNameList.split(","))
analysis.exports.delete(name.trim());
}
// No export names were provided, so ignore all the exports.
else analysis.exports.clear();
}
}
return analysis;
}
/**
* Scan of an ECMAScript module’s imports and exports.
* @typedef {object} ModuleScan
* @property {{ [importSpecifier: string]: ModuleImports }} imports Map of
* import specifiers and the imports used.
* @property {ModuleExports} exports Declared exports.
*/
/**
* List of ECMAScript module import names, including `default` if one is a
* default export or `*` for a namespace import.
* @typedef {Set<string>} ModuleImports
*/
/**
* List of ECMAScript module export names, including `default` if one is a
* default export.
* @typedef {Set<string>} ModuleExports
* @example
* These export statements:
*
* ```js
* export const a = 1;
* export const b = 2;
* export default 3;
* ```
*
* Translate to:
*
* ```js
* new Set(["a", "b", "default"]);
* ```
*/