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

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 12.9 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 const programScope = programPath.scope;
239 const programChildren = programPath.get("body");
240 programChildren.forEach(child => {
241 let kind;
242 if (child.isImportDeclaration()) {
243 kind = "import";
244 } else {
245 if (child.isExportDefaultDeclaration()) {
246 child = child.get("declaration");
247 }
248 if (child.isExportNamedDeclaration()) {
249 if (child.node.declaration) {
250 child = child.get("declaration");
251 } else if (initializeReexports && child.node.source && child.get("source").isStringLiteral()) {
252 child.get("specifiers").forEach(spec => {
253 assertExportSpecifier(spec);
254 bindingKindLookup.set(spec.get("local").node.name, "block");
255 });
256 return;
257 }
258 }
259 if (child.isFunctionDeclaration()) {
260 kind = "hoisted";
261 } else if (child.isClassDeclaration()) {
262 kind = "block";
263 } else if (child.isVariableDeclaration({
264 kind: "var"
265 })) {
266 kind = "var";
267 } else if (child.isVariableDeclaration()) {
268 kind = "block";
269 } else {
270 return;
271 }
272 }
273 Object.keys(child.getOuterBindingIdentifiers()).forEach(name => {
274 bindingKindLookup.set(name, kind);
275 });
276 });
277 const localMetadata = new Map();
278 const getLocalMetadata = idPath => {
279 const localName = idPath.node.name;
280 let metadata = localMetadata.get(localName);
281 if (!metadata) {
282 var _bindingKindLookup$ge, _programScope$getBind;
283 const kind = (_bindingKindLookup$ge = bindingKindLookup.get(localName)) != null ? _bindingKindLookup$ge : (_programScope$getBind = programScope.getBinding(localName)) == null ? void 0 : _programScope$getBind.kind;
284 if (kind === undefined) {
285 throw idPath.buildCodeFrameError(`Exporting local "${localName}", which is not declared.`);
286 }
287 metadata = {
288 names: [],
289 kind
290 };
291 localMetadata.set(localName, metadata);
292 }
293 return metadata;
294 };
295 programChildren.forEach(child => {
296 if (child.isExportNamedDeclaration() && (initializeReexports || !child.node.source)) {
297 if (child.node.declaration) {
298 const declaration = child.get("declaration");
299 const ids = declaration.getOuterBindingIdentifierPaths();
300 Object.keys(ids).forEach(name => {
301 if (name === "__esModule") {
302 throw declaration.buildCodeFrameError('Illegal export "__esModule".');
303 }
304 getLocalMetadata(ids[name]).names.push(name);
305 });
306 } else {
307 child.get("specifiers").forEach(spec => {
308 const local = spec.get("local");
309 const exported = spec.get("exported");
310 const localMetadata = getLocalMetadata(local);
311 const exportName = getExportSpecifierName(exported, stringSpecifiers);
312 if (exportName === "__esModule") {
313 throw exported.buildCodeFrameError('Illegal export "__esModule".');
314 }
315 localMetadata.names.push(exportName);
316 });
317 }
318 } else if (child.isExportDefaultDeclaration()) {
319 const declaration = child.get("declaration");
320 if (declaration.isFunctionDeclaration() || declaration.isClassDeclaration()) {
321 getLocalMetadata(declaration.get("id")).names.push("default");
322 } else {
323 throw declaration.buildCodeFrameError("Unexpected default expression export.");
324 }
325 }
326 });
327 return localMetadata;
328}
329function nameAnonymousExports(programPath) {
330 programPath.get("body").forEach(child => {
331 var _child$splitExportDec;
332 if (!child.isExportDefaultDeclaration()) return;
333 (_child$splitExportDec = child.splitExportDeclaration) != null ? _child$splitExportDec : child.splitExportDeclaration = require("@babel/traverse").NodePath.prototype.splitExportDeclaration;
334 child.splitExportDeclaration();
335 });
336}
337function removeImportExportDeclarations(programPath) {
338 programPath.get("body").forEach(child => {
339 if (child.isImportDeclaration()) {
340 child.remove();
341 } else if (child.isExportNamedDeclaration()) {
342 if (child.node.declaration) {
343 child.node.declaration._blockHoist = child.node._blockHoist;
344 child.replaceWith(child.node.declaration);
345 } else {
346 child.remove();
347 }
348 } else if (child.isExportDefaultDeclaration()) {
349 const declaration = child.get("declaration");
350 if (declaration.isFunctionDeclaration() || declaration.isClassDeclaration()) {
351 declaration._blockHoist = child.node._blockHoist;
352 child.replaceWith(declaration);
353 } else {
354 throw declaration.buildCodeFrameError("Unexpected default expression export.");
355 }
356 } else if (child.isExportAllDeclaration()) {
357 child.remove();
358 }
359 });
360}
361
362//# sourceMappingURL=normalize-and-load-metadata.js.map
Note: See TracBrowser for help on using the repository browser.