Better error for export * as ns without the correct plugin (#13296)

This commit is contained in:
Huáng Jùnliàng 2021-05-11 18:40:13 -04:00 committed by GitHub
parent cca97d1e78
commit 9e241fc180
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 5 deletions

View File

@ -189,6 +189,20 @@ function getExportSpecifierName(
} }
} }
function assertExportSpecifier(
path: NodePath,
): asserts path is NodePath<t.ExportSpecifier> {
if (path.isExportSpecifier()) {
return;
} else if (path.isExportNamespaceSpecifier()) {
throw path.buildCodeFrameError(
"Export namespace should be first transformed by `@babel/plugin-proposal-export-namespace-from`.",
);
} else {
throw path.buildCodeFrameError("Unexpected export specifier type");
}
}
/** /**
* Get metadata about the imports and exports present in this module. * Get metadata about the imports and exports present in this module.
*/ */
@ -307,9 +321,7 @@ function getModuleMetadata(
if (!data.loc) data.loc = child.node.loc; if (!data.loc) data.loc = child.node.loc;
child.get("specifiers").forEach(spec => { child.get("specifiers").forEach(spec => {
if (!spec.isExportSpecifier()) { assertExportSpecifier(spec);
throw spec.buildCodeFrameError("Unexpected export specifier type");
}
const importName = getExportSpecifierName( const importName = getExportSpecifierName(
spec.get("local"), spec.get("local"),
stringSpecifiers, stringSpecifiers,
@ -415,8 +427,9 @@ function getLocalExportMetadata(
child.node.source && child.node.source &&
child.get("source").isStringLiteral() child.get("source").isStringLiteral()
) { ) {
child.node.specifiers.forEach(specifier => { child.get("specifiers").forEach(spec => {
bindingKindLookup.set(specifier.local.name, "block"); assertExportSpecifier(spec);
bindingKindLookup.set(spec.get("local").node.name, "block");
}); });
return; return;
} }

View File

@ -0,0 +1,2 @@
export * as ns from "./foo";

View File

@ -0,0 +1,3 @@
{
"throws": "Export namespace should be first transformed by `@babel/plugin-proposal-export-namespace-from`."
}

View File

@ -0,0 +1,5 @@
{
"plugins": [
["transform-modules-commonjs", { "noInterop": true, "loose": true }]
]
}