source: imaps-frontend/node_modules/@babel/helper-module-transforms/lib/normalize-and-load-metadata.js@ 0c6b92a

main
Last change on this file since 0c6b92a was d565449, checked in by stefan toskovski <stefantoska84@…>, 3 months ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 12.7 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = normalizeModuleAndLoadMetadata;
7exports.hasExports = hasExports;
8exports.isSideEffectImport = isSideEffectImport;
9exports.validateImportInteropOption = validateImportInteropOption;
10var _path = require("path");
11var _helperValidatorIdentifier = require("@babel/helper-validator-identifier");
12function hasExports(metadata) {
13 return metadata.hasExports;
14}
15function isSideEffectImport(source) {
16 return source.imports.size === 0 && source.importsNamespace.size === 0 && source.reexports.size === 0 && source.reexportNamespace.size === 0 && !source.reexportAll;
17}
18function validateImportInteropOption(importInterop) {
19 if (typeof importInterop !== "function" && importInterop !== "none" && importInterop !== "babel" && importInterop !== "node") {
20 throw new Error(`.importInterop must be one of "none", "babel", "node", or a function returning one of those values (received ${importInterop}).`);
21 }
22 return importInterop;
23}
24function resolveImportInterop(importInterop, source, filename) {
25 if (typeof importInterop === "function") {
26 return validateImportInteropOption(importInterop(source, filename));
27 }
28 return importInterop;
29}
30function normalizeModuleAndLoadMetadata(programPath, exportName, {
31 importInterop,
32 initializeReexports = false,
33 getWrapperPayload,
34 esNamespaceOnly = false,
35 filename
36}) {
37 if (!exportName) {
38 exportName = programPath.scope.generateUidIdentifier("exports").name;
39 }
40 const stringSpecifiers = new Set();
41 nameAnonymousExports(programPath);
42 const {
43 local,
44 sources,
45 hasExports
46 } = getModuleMetadata(programPath, {
47 initializeReexports,
48 getWrapperPayload
49 }, stringSpecifiers);
50 removeImportExportDeclarations(programPath);
51 for (const [source, metadata] of sources) {
52 const {
53 importsNamespace,
54 imports
55 } = metadata;
56 if (importsNamespace.size > 0 && imports.size === 0) {
57 const [nameOfnamespace] = importsNamespace;
58 metadata.name = nameOfnamespace;
59 }
60 const resolvedInterop = resolveImportInterop(importInterop, source, filename);
61 if (resolvedInterop === "none") {
62 metadata.interop = "none";
63 } else if (resolvedInterop === "node" && metadata.interop === "namespace") {
64 metadata.interop = "node-namespace";
65 } else if (resolvedInterop === "node" && metadata.interop === "default") {
66 metadata.interop = "node-default";
67 } else if (esNamespaceOnly && metadata.interop === "namespace") {
68 metadata.interop = "default";
69 }
70 }
71 return {
72 exportName,
73 exportNameListName: null,
74 hasExports,
75 local,
76 source: sources,
77 stringSpecifiers
78 };
79}
80function getExportSpecifierName(path, stringSpecifiers) {
81 if (path.isIdentifier()) {
82 return path.node.name;
83 } else if (path.isStringLiteral()) {
84 const stringValue = path.node.value;
85 if (!(0, _helperValidatorIdentifier.isIdentifierName)(stringValue)) {
86 stringSpecifiers.add(stringValue);
87 }
88 return stringValue;
89 } else {
90 throw new Error(`Expected export specifier to be either Identifier or StringLiteral, got ${path.node.type}`);
91 }
92}
93function assertExportSpecifier(path) {
94 if (path.isExportSpecifier()) {
95 return;
96 } else if (path.isExportNamespaceSpecifier()) {
97 throw path.buildCodeFrameError("Export namespace should be first transformed by `@babel/plugin-transform-export-namespace-from`.");
98 } else {
99 throw path.buildCodeFrameError("Unexpected export specifier type");
100 }
101}
102function getModuleMetadata(programPath, {
103 getWrapperPayload,
104 initializeReexports
105}, stringSpecifiers) {
106 const localData = getLocalExportMetadata(programPath, initializeReexports, stringSpecifiers);
107 const importNodes = new Map();
108 const sourceData = new Map();
109 const getData = (sourceNode, node) => {
110 const source = sourceNode.value;
111 let data = sourceData.get(source);
112 if (!data) {
113 data = {
114 name: programPath.scope.generateUidIdentifier((0, _path.basename)(source, (0, _path.extname)(source))).name,
115 interop: "none",
116 loc: null,
117 imports: new Map(),
118 importsNamespace: new Set(),
119 reexports: new Map(),
120 reexportNamespace: new Set(),
121 reexportAll: null,
122 wrap: null,
123 get lazy() {
124 return this.wrap === "lazy";
125 },
126 referenced: false
127 };
128 sourceData.set(source, data);
129 importNodes.set(source, [node]);
130 } else {
131 importNodes.get(source).push(node);
132 }
133 return data;
134 };
135 let hasExports = false;
136 programPath.get("body").forEach(child => {
137 if (child.isImportDeclaration()) {
138 const data = getData(child.node.source, child.node);
139 if (!data.loc) data.loc = child.node.loc;
140 child.get("specifiers").forEach(spec => {
141 if (spec.isImportDefaultSpecifier()) {
142 const localName = spec.get("local").node.name;
143 data.imports.set(localName, "default");
144 const reexport = localData.get(localName);
145 if (reexport) {
146 localData.delete(localName);
147 reexport.names.forEach(name => {
148 data.reexports.set(name, "default");
149 });
150 data.referenced = true;
151 }
152 } else if (spec.isImportNamespaceSpecifier()) {
153 const localName = spec.get("local").node.name;
154 data.importsNamespace.add(localName);
155 const reexport = localData.get(localName);
156 if (reexport) {
157 localData.delete(localName);
158 reexport.names.forEach(name => {
159 data.reexportNamespace.add(name);
160 });
161 data.referenced = true;
162 }
163 } else if (spec.isImportSpecifier()) {
164 const importName = getExportSpecifierName(spec.get("imported"), stringSpecifiers);
165 const localName = spec.get("local").node.name;
166 data.imports.set(localName, importName);
167 const reexport = localData.get(localName);
168 if (reexport) {
169 localData.delete(localName);
170 reexport.names.forEach(name => {
171 data.reexports.set(name, importName);
172 });
173 data.referenced = true;
174 }
175 }
176 });
177 } else if (child.isExportAllDeclaration()) {
178 hasExports = true;
179 const data = getData(child.node.source, child.node);
180 if (!data.loc) data.loc = child.node.loc;
181 data.reexportAll = {
182 loc: child.node.loc
183 };
184 data.referenced = true;
185 } else if (child.isExportNamedDeclaration() && child.node.source) {
186 hasExports = true;
187 const data = getData(child.node.source, child.node);
188 if (!data.loc) data.loc = child.node.loc;
189 child.get("specifiers").forEach(spec => {
190 assertExportSpecifier(spec);
191 const importName = getExportSpecifierName(spec.get("local"), stringSpecifiers);
192 const exportName = getExportSpecifierName(spec.get("exported"), stringSpecifiers);
193 data.reexports.set(exportName, importName);
194 data.referenced = true;
195 if (exportName === "__esModule") {
196 throw spec.get("exported").buildCodeFrameError('Illegal export "__esModule".');
197 }
198 });
199 } else if (child.isExportNamedDeclaration() || child.isExportDefaultDeclaration()) {
200 hasExports = true;
201 }
202 });
203 for (const metadata of sourceData.values()) {
204 let needsDefault = false;
205 let needsNamed = false;
206 if (metadata.importsNamespace.size > 0) {
207 needsDefault = true;
208 needsNamed = true;
209 }
210 if (metadata.reexportAll) {
211 needsNamed = true;
212 }
213 for (const importName of metadata.imports.values()) {
214 if (importName === "default") needsDefault = true;else needsNamed = true;
215 }
216 for (const importName of metadata.reexports.values()) {
217 if (importName === "default") needsDefault = true;else needsNamed = true;
218 }
219 if (needsDefault && needsNamed) {
220 metadata.interop = "namespace";
221 } else if (needsDefault) {
222 metadata.interop = "default";
223 }
224 }
225 if (getWrapperPayload) {
226 for (const [source, metadata] of sourceData) {
227 metadata.wrap = getWrapperPayload(source, metadata, importNodes.get(source));
228 }
229 }
230 return {
231 hasExports,
232 local: localData,
233 sources: sourceData
234 };
235}
236function getLocalExportMetadata(programPath, initializeReexports, stringSpecifiers) {
237 const bindingKindLookup = new Map();
238 programPath.get("body").forEach(child => {
239 let kind;
240 if (child.isImportDeclaration()) {
241 kind = "import";
242 } else {
243 if (child.isExportDefaultDeclaration()) {
244 child = child.get("declaration");
245 }
246 if (child.isExportNamedDeclaration()) {
247 if (child.node.declaration) {
248 child = child.get("declaration");
249 } else if (initializeReexports && child.node.source && child.get("source").isStringLiteral()) {
250 child.get("specifiers").forEach(spec => {
251 assertExportSpecifier(spec);
252 bindingKindLookup.set(spec.get("local").node.name, "block");
253 });
254 return;
255 }
256 }
257 if (child.isFunctionDeclaration()) {
258 kind = "hoisted";
259 } else if (child.isClassDeclaration()) {
260 kind = "block";
261 } else if (child.isVariableDeclaration({
262 kind: "var"
263 })) {
264 kind = "var";
265 } else if (child.isVariableDeclaration()) {
266 kind = "block";
267 } else {
268 return;
269 }
270 }
271 Object.keys(child.getOuterBindingIdentifiers()).forEach(name => {
272 bindingKindLookup.set(name, kind);
273 });
274 });
275 const localMetadata = new Map();
276 const getLocalMetadata = idPath => {
277 const localName = idPath.node.name;
278 let metadata = localMetadata.get(localName);
279 if (!metadata) {
280 const kind = bindingKindLookup.get(localName);
281 if (kind === undefined) {
282 throw idPath.buildCodeFrameError(`Exporting local "${localName}", which is not declared.`);
283 }
284 metadata = {
285 names: [],
286 kind
287 };
288 localMetadata.set(localName, metadata);
289 }
290 return metadata;
291 };
292 programPath.get("body").forEach(child => {
293 if (child.isExportNamedDeclaration() && (initializeReexports || !child.node.source)) {
294 if (child.node.declaration) {
295 const declaration = child.get("declaration");
296 const ids = declaration.getOuterBindingIdentifierPaths();
297 Object.keys(ids).forEach(name => {
298 if (name === "__esModule") {
299 throw declaration.buildCodeFrameError('Illegal export "__esModule".');
300 }
301 getLocalMetadata(ids[name]).names.push(name);
302 });
303 } else {
304 child.get("specifiers").forEach(spec => {
305 const local = spec.get("local");
306 const exported = spec.get("exported");
307 const localMetadata = getLocalMetadata(local);
308 const exportName = getExportSpecifierName(exported, stringSpecifiers);
309 if (exportName === "__esModule") {
310 throw exported.buildCodeFrameError('Illegal export "__esModule".');
311 }
312 localMetadata.names.push(exportName);
313 });
314 }
315 } else if (child.isExportDefaultDeclaration()) {
316 const declaration = child.get("declaration");
317 if (declaration.isFunctionDeclaration() || declaration.isClassDeclaration()) {
318 getLocalMetadata(declaration.get("id")).names.push("default");
319 } else {
320 throw declaration.buildCodeFrameError("Unexpected default expression export.");
321 }
322 }
323 });
324 return localMetadata;
325}
326function nameAnonymousExports(programPath) {
327 programPath.get("body").forEach(child => {
328 if (!child.isExportDefaultDeclaration()) return;
329 {
330 var _child$splitExportDec;
331 (_child$splitExportDec = child.splitExportDeclaration) != null ? _child$splitExportDec : child.splitExportDeclaration = require("@babel/traverse").NodePath.prototype.splitExportDeclaration;
332 }
333 child.splitExportDeclaration();
334 });
335}
336function removeImportExportDeclarations(programPath) {
337 programPath.get("body").forEach(child => {
338 if (child.isImportDeclaration()) {
339 child.remove();
340 } else if (child.isExportNamedDeclaration()) {
341 if (child.node.declaration) {
342 child.node.declaration._blockHoist = child.node._blockHoist;
343 child.replaceWith(child.node.declaration);
344 } else {
345 child.remove();
346 }
347 } else if (child.isExportDefaultDeclaration()) {
348 const declaration = child.get("declaration");
349 if (declaration.isFunctionDeclaration() || declaration.isClassDeclaration()) {
350 declaration._blockHoist = child.node._blockHoist;
351 child.replaceWith(declaration);
352 } else {
353 throw declaration.buildCodeFrameError("Unexpected default expression export.");
354 }
355 } else if (child.isExportAllDeclaration()) {
356 child.remove();
357 }
358 });
359}
360
361//# sourceMappingURL=normalize-and-load-metadata.js.map
Note: See TracBrowser for help on using the repository browser.