Debian Codegraph @

packages/babel-plugin-proposal-export-namespace-from/src/index.ts

raw
1 import { declare } from "@babel/helper-plugin-utils";
2 import syntaxExportNamespaceFrom from "@babel/plugin-syntax-export-namespace-from";
3 import { types as t } from "@babel/core";
4
5 export default declare(api => {
6 api.assertVersion(7);
7
8 return {
9 name: "proposal-export-namespace-from",
10 inherits: syntaxExportNamespaceFrom.default,
11
12 visitor: {
13 ExportNamedDeclaration(path) {
14 const { node, scope } = path;
15 const { specifiers } = node;
16
17 const index = t.isExportDefaultSpecifier(specifiers[0]) ? 1 : 0;
18 if (!t.isExportNamespaceSpecifier(specifiers[index])) return;
19
20 const nodes = [];
21
22 if (index === 1) {
23 nodes.push(
24 t.exportNamedDeclaration(null, [specifiers.shift()], node.source),
25 );
26 }
27
28 const specifier = specifiers.shift();
29 const { exported } = specifier;
30 const uid = scope.generateUidIdentifier(
31 // @ts-expect-error Identifier ?? StringLiteral
32 exported.name ?? exported.value,
33 );
34
35 nodes.push(
36 t.importDeclaration(
37 [t.importNamespaceSpecifier(uid)],
38 t.cloneNode(node.source),
39 ),
40 t.exportNamedDeclaration(null, [
41 t.exportSpecifier(t.cloneNode(uid), exported),
42 ]),
43 );
44
45 if (node.specifiers.length >= 1) {
46 nodes.push(node);
47 }
48
49 const [importDeclaration] = path.replaceWithMultiple(nodes);
50 path.scope.registerDeclaration(importDeclaration);
51 },
52 },
53 };
54 });