forked from babel/babel
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
normalize-and-load-metadata.js
519 lines (452 loc) · 14.9 KB
/
normalize-and-load-metadata.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
import { basename, extname } from "path";
import { isIdentifierName } from "@babel/helper-validator-identifier";
import splitExportDeclaration from "@babel/helper-split-export-declaration";
export type ModuleMetadata = {
exportName: string,
// The name of the variable that will reference an object containing export names.
exportNameListName: null | string,
hasExports: boolean,
// Lookup from local binding to export information.
local: Map<string, LocalExportMetadata>,
// Lookup of source file to source file metadata.
source: Map<string, SourceModuleMetadata>,
// List of names that should only be printed as string literals.
// i.e. `import { "any unicode" as foo } from "some-module"`
// `stringSpecifiers` is Set(1) ["any unicode"]
// In most cases `stringSpecifiers` is an empty Set
stringSpecifiers: Set<string>,
};
export type InteropType = "default" | "namespace" | "none";
export type SourceModuleMetadata = {
// A unique variable name to use for this namespace object. Centralized for simplicity.
name: string,
loc: ?BabelNodeSourceLocation,
interop: InteropType,
// Local binding to reference from this source namespace. Key: Local name, value: Import name
imports: Map<string, string>,
// Local names that reference namespace object.
importsNamespace: Set<string>,
// Reexports to create for namespace. Key: Export name, value: Import name
reexports: Map<string, string>,
// List of names to re-export namespace as.
reexportNamespace: Set<string>,
// Tracks if the source should be re-exported.
reexportAll: null | {
loc: ?BabelNodeSourceLocation,
},
};
export type LocalExportMetadata = {
name: Array<string>, // names of exports
kind: "import" | "hoisted" | "block" | "var",
};
/**
* Check if the module has any exports that need handling.
*/
export function hasExports(metadata: ModuleMetadata) {
return metadata.hasExports;
}
/**
* Check if a given source is an anonymous import, e.g. "import 'foo';"
*/
export function isSideEffectImport(source: SourceModuleMetadata) {
return (
source.imports.size === 0 &&
source.importsNamespace.size === 0 &&
source.reexports.size === 0 &&
source.reexportNamespace.size === 0 &&
!source.reexportAll
);
}
/**
* Remove all imports and exports from the file, and return all metadata
* needed to reconstruct the module's behavior.
*/
export default function normalizeModuleAndLoadMetadata(
programPath: NodePath,
exportName?: string,
{
noInterop = false,
initializeReexports = false,
lazy = false,
esNamespaceOnly = false,
} = {},
): ModuleMetadata {
if (!exportName) {
exportName = programPath.scope.generateUidIdentifier("exports").name;
}
const stringSpecifiers = new Set();
nameAnonymousExports(programPath);
const { local, source, hasExports } = getModuleMetadata(
programPath,
{ initializeReexports, lazy },
stringSpecifiers,
);
removeModuleDeclarations(programPath);
// Reuse the imported namespace name if there is one.
for (const [, metadata] of source) {
if (metadata.importsNamespace.size > 0) {
// This is kind of gross. If we stop using `loose: true` we should
// just make this destructuring assignment.
metadata.name = metadata.importsNamespace.values().next().value;
}
if (noInterop) metadata.interop = "none";
else if (esNamespaceOnly) {
// Both the default and namespace interops pass through __esModule
// objects, but the namespace interop is used to enable Babel's
// destructuring-like interop behavior for normal CommonJS.
// Since some tooling has started to remove that behavior, we expose
// it as the `esNamespace` option.
if (metadata.interop === "namespace") {
metadata.interop = "default";
}
}
}
return {
exportName,
exportNameListName: null,
hasExports,
local,
source,
stringSpecifiers,
};
}
function getExportSpecifierName(
path: NodePath,
stringSpecifiers: Set<string>,
): string {
if (path.isIdentifier()) {
return path.node.name;
} else if (path.isStringLiteral()) {
const stringValue = path.node.value;
// add specifier value to `stringSpecifiers` only when it can not be converted to an identifier name
// i.e In `import { "foo" as bar }`
// we do not consider `"foo"` to be a `stringSpecifier` because we can treat it as
// `import { foo as bar }`
// This helps minimize the size of `stringSpecifiers` and reduce overhead of checking valid identifier names
// when building transpiled code from metadata
if (!isIdentifierName(stringValue)) {
stringSpecifiers.add(stringValue);
}
return stringValue;
} else {
throw new Error(
`Expected export specifier to be either Identifier or StringLiteral, got ${path.node.type}`,
);
}
}
/**
* Get metadata about the imports and exports present in this module.
*/
function getModuleMetadata(
programPath: NodePath,
{
lazy,
initializeReexports,
}: { lazy: boolean, initializeReexports: boolean },
stringSpecifiers: Set<string>,
) {
const localData = getLocalExportMetadata(
programPath,
initializeReexports,
stringSpecifiers,
);
const sourceData = new Map();
const getData = sourceNode => {
const source = sourceNode.value;
let data = sourceData.get(source);
if (!data) {
data = {
name: programPath.scope.generateUidIdentifier(
basename(source, extname(source)),
).name,
interop: "none",
loc: null,
// Data about the requested sources and names.
imports: new Map(),
importsNamespace: new Set(),
// Metadata about data that is passed directly from source to export.
reexports: new Map(),
reexportNamespace: new Set(),
reexportAll: null,
lazy: false,
};
sourceData.set(source, data);
}
return data;
};
let hasExports = false;
programPath.get("body").forEach(child => {
if (child.isImportDeclaration()) {
const data = getData(child.node.source);
if (!data.loc) data.loc = child.node.loc;
child.get("specifiers").forEach(spec => {
if (spec.isImportDefaultSpecifier()) {
const localName = spec.get("local").node.name;
data.imports.set(localName, "default");
const reexport = localData.get(localName);
if (reexport) {
localData.delete(localName);
reexport.names.forEach(name => {
data.reexports.set(name, "default");
});
}
} else if (spec.isImportNamespaceSpecifier()) {
const localName = spec.get("local").node.name;
data.importsNamespace.add(localName);
const reexport = localData.get(localName);
if (reexport) {
localData.delete(localName);
reexport.names.forEach(name => {
data.reexportNamespace.add(name);
});
}
} else if (spec.isImportSpecifier()) {
const importName = getExportSpecifierName(
spec.get("imported"),
stringSpecifiers,
);
const localName = spec.get("local").node.name;
data.imports.set(localName, importName);
const reexport = localData.get(localName);
if (reexport) {
localData.delete(localName);
reexport.names.forEach(name => {
data.reexports.set(name, importName);
});
}
}
});
} else if (child.isExportAllDeclaration()) {
hasExports = true;
const data = getData(child.node.source);
if (!data.loc) data.loc = child.node.loc;
data.reexportAll = {
loc: child.node.loc,
};
} else if (child.isExportNamedDeclaration() && child.node.source) {
hasExports = true;
const data = getData(child.node.source);
if (!data.loc) data.loc = child.node.loc;
child.get("specifiers").forEach(spec => {
if (!spec.isExportSpecifier()) {
throw spec.buildCodeFrameError("Unexpected export specifier type");
}
const importName = getExportSpecifierName(
spec.get("local"),
stringSpecifiers,
);
const exportName = getExportSpecifierName(
spec.get("exported"),
stringSpecifiers,
);
data.reexports.set(exportName, importName);
if (exportName === "__esModule") {
throw exportName.buildCodeFrameError('Illegal export "__esModule".');
}
});
} else if (
child.isExportNamedDeclaration() ||
child.isExportDefaultDeclaration()
) {
hasExports = true;
}
});
for (const metadata of sourceData.values()) {
let needsDefault = false;
let needsNamed = false;
if (metadata.importsNamespace.size > 0) {
needsDefault = true;
needsNamed = true;
}
if (metadata.reexportAll) {
needsNamed = true;
}
for (const importName of metadata.imports.values()) {
if (importName === "default") needsDefault = true;
else needsNamed = true;
}
for (const importName of metadata.reexports.values()) {
if (importName === "default") needsDefault = true;
else needsNamed = true;
}
if (needsDefault && needsNamed) {
// TODO(logan): Using the namespace interop here is unfortunate. Revisit.
metadata.interop = "namespace";
} else if (needsDefault) {
metadata.interop = "default";
}
}
for (const [source, metadata] of sourceData) {
if (
lazy !== false &&
!(isSideEffectImport(metadata) || metadata.reexportAll)
) {
if (lazy === true) {
// 'true' means that local relative files are eagerly loaded and
// dependency modules are loaded lazily.
metadata.lazy = !/\./.test(source);
} else if (Array.isArray(lazy)) {
metadata.lazy = lazy.indexOf(source) !== -1;
} else if (typeof lazy === "function") {
metadata.lazy = lazy(source);
} else {
throw new Error(`.lazy must be a boolean, string array, or function`);
}
}
}
return {
hasExports,
local: localData,
source: sourceData,
};
}
/**
* Get metadata about local variables that are exported.
*/
function getLocalExportMetadata(
programPath: NodePath,
initializeReexports: boolean,
stringSpecifiers: Set<string>,
): Map<string, LocalExportMetadata> {
const bindingKindLookup = new Map();
programPath.get("body").forEach(child => {
let kind;
if (child.isImportDeclaration()) {
kind = "import";
} else {
if (child.isExportDefaultDeclaration()) child = child.get("declaration");
if (child.isExportNamedDeclaration()) {
if (child.node.declaration) {
child = child.get("declaration");
} else if (
initializeReexports &&
child.node.source &&
child.get("source").isStringLiteral()
) {
child.node.specifiers.forEach(specifier => {
bindingKindLookup.set(specifier.local.name, "block");
});
return;
}
}
if (child.isFunctionDeclaration()) {
kind = "hoisted";
} else if (child.isClassDeclaration()) {
kind = "block";
} else if (child.isVariableDeclaration({ kind: "var" })) {
kind = "var";
} else if (child.isVariableDeclaration()) {
kind = "block";
} else {
return;
}
}
Object.keys(child.getOuterBindingIdentifiers()).forEach(name => {
bindingKindLookup.set(name, kind);
});
});
const localMetadata = new Map();
const getLocalMetadata = idPath => {
const localName = idPath.node.name;
let metadata = localMetadata.get(localName);
if (!metadata) {
const kind = bindingKindLookup.get(localName);
if (kind === undefined) {
throw idPath.buildCodeFrameError(
`Exporting local "${localName}", which is not declared.`,
);
}
metadata = {
names: [],
kind,
};
localMetadata.set(localName, metadata);
}
return metadata;
};
programPath.get("body").forEach(child => {
if (
child.isExportNamedDeclaration() &&
(initializeReexports || !child.node.source)
) {
if (child.node.declaration) {
const declaration = child.get("declaration");
const ids = declaration.getOuterBindingIdentifierPaths();
Object.keys(ids).forEach(name => {
if (name === "__esModule") {
throw declaration.buildCodeFrameError(
'Illegal export "__esModule".',
);
}
getLocalMetadata(ids[name]).names.push(name);
});
} else {
child.get("specifiers").forEach(spec => {
const local = spec.get("local");
const exported = spec.get("exported");
const localMetadata = getLocalMetadata(local);
const exportName = getExportSpecifierName(exported, stringSpecifiers);
if (exportName === "__esModule") {
throw exported.buildCodeFrameError('Illegal export "__esModule".');
}
localMetadata.names.push(exportName);
});
}
} else if (child.isExportDefaultDeclaration()) {
const declaration = child.get("declaration");
if (
declaration.isFunctionDeclaration() ||
declaration.isClassDeclaration()
) {
getLocalMetadata(declaration.get("id")).names.push("default");
} else {
// These should have been removed by the nameAnonymousExports() call.
throw declaration.buildCodeFrameError(
"Unexpected default expression export.",
);
}
}
});
return localMetadata;
}
/**
* Ensure that all exported values have local binding names.
*/
function nameAnonymousExports(programPath: NodePath) {
// Name anonymous exported locals.
programPath.get("body").forEach(child => {
if (!child.isExportDefaultDeclaration()) return;
splitExportDeclaration(child);
});
}
function removeModuleDeclarations(programPath: NodePath) {
programPath.get("body").forEach(child => {
if (child.isImportDeclaration()) {
child.remove();
} else if (child.isExportNamedDeclaration()) {
if (child.node.declaration) {
child.node.declaration._blockHoist = child.node._blockHoist;
child.replaceWith(child.node.declaration);
} else {
child.remove();
}
} else if (child.isExportDefaultDeclaration()) {
// export default foo;
const declaration = child.get("declaration");
if (
declaration.isFunctionDeclaration() ||
declaration.isClassDeclaration()
) {
declaration._blockHoist = child.node._blockHoist;
child.replaceWith(declaration);
} else {
// These should have been removed by the nameAnonymousExports() call.
throw declaration.buildCodeFrameError(
"Unexpected default expression export.",
);
}
} else if (child.isExportAllDeclaration()) {
child.remove();
}
});
}