Huáng Jùnliàng 21d7ee2610 String import/export specifier (#12091)
* feat: parse moduleExportName
* feat: add validators
* Support string specifier name in commonjs transform
* Support string specifier name in export-ns-from
* test: add loose testcases
* test: add testcases for amd and umd
* feat: support systemjs
* test: update fixtures fixed in #12110
* add plugin name typings
* test: rename test layout
* feat: implement under moduleStringNames flag
* chore: add plugin syntax module string names
* feat: support ModuleExportName as ModuleExportName
* test: update test fixtures
* fix flow errors
* docs: update AST spec
* feat: support { "some imports" as "some exports" }
* feat: support { "some imports" as "some exports" } in systemjs
* test: add test on `import { "foo" }`
* Address review comments
* add moduleStringNames to missing plugin helpers
* Apply suggestions from code review
* update test fixtures
* Update packages/babel-parser/src/parser/error-message.js
* update test fixtures

Co-Authored-By: Kai Cataldo <kai@kaicataldo.com>
Co-authored-by: Brian Ng <bng412@gmail.com>
2020-10-14 20:01:37 +02:00

54 lines
1.5 KiB
JavaScript

import { declare } from "@babel/helper-plugin-utils";
import syntaxExportNamespaceFrom from "@babel/plugin-syntax-export-namespace-from";
import { types as t } from "@babel/core";
export default declare(api => {
api.assertVersion(7);
return {
name: "proposal-export-namespace-from",
inherits: syntaxExportNamespaceFrom,
visitor: {
ExportNamedDeclaration(path) {
const { node, scope } = path;
const { specifiers } = node;
const index = t.isExportDefaultSpecifier(specifiers[0]) ? 1 : 0;
if (!t.isExportNamespaceSpecifier(specifiers[index])) return;
const nodes = [];
if (index === 1) {
nodes.push(
t.exportNamedDeclaration(null, [specifiers.shift()], node.source),
);
}
const specifier = specifiers.shift();
const { exported } = specifier;
const uid = scope.generateUidIdentifier(
exported.name ?? exported.value,
);
nodes.push(
t.importDeclaration(
[t.importNamespaceSpecifier(uid)],
t.cloneNode(node.source),
),
t.exportNamedDeclaration(null, [
t.exportSpecifier(t.cloneNode(uid), exported),
]),
);
if (node.specifiers.length >= 1) {
nodes.push(node);
}
const [importDeclaration] = path.replaceWithMultiple(nodes);
path.scope.registerDeclaration(importDeclaration);
},
},
};
});