From 16ee5b599fd4d1542b8dad76c04b582a35250cbc Mon Sep 17 00:00:00 2001 From: Babel Bot <30521560+liuxingbaoyu@users.noreply.github.com> Date: Wed, 27 Dec 2023 10:45:58 +0800 Subject: [PATCH] fix --- .../src/normalize-and-load-metadata.ts | 20 +++++++++++++++++-- .../options.json | 3 +++ .../export-namespace-without-plugin/output.js | 6 ------ 3 files changed, 21 insertions(+), 8 deletions(-) create mode 100644 packages/babel-plugin-transform-modules-commonjs/test/fixtures/noInterop-loose/export-namespace-without-plugin/options.json delete mode 100644 packages/babel-plugin-transform-modules-commonjs/test/fixtures/noInterop-loose/export-namespace-without-plugin/output.js diff --git a/packages/babel-helper-module-transforms/src/normalize-and-load-metadata.ts b/packages/babel-helper-module-transforms/src/normalize-and-load-metadata.ts index c1d56ca90fc4..d21e606338b8 100644 --- a/packages/babel-helper-module-transforms/src/normalize-and-load-metadata.ts +++ b/packages/babel-helper-module-transforms/src/normalize-and-load-metadata.ts @@ -213,6 +213,20 @@ function getExportSpecifierName( } } +function assertExportSpecifier( + path: NodePath, +): asserts path is NodePath { + if (path.isExportSpecifier()) { + return; + } else if (path.isExportNamespaceSpecifier()) { + throw path.buildCodeFrameError( + "Export namespace should be first transformed by `@babel/plugin-transform-export-namespace-from`.", + ); + } else { + throw path.buildCodeFrameError("Unexpected export specifier type"); + } +} + /** * Get metadata about the imports and exports present in this module. */ @@ -350,6 +364,7 @@ function getModuleMetadata( if (!data.loc) data.loc = child.node.loc; child.get("specifiers").forEach(spec => { + assertExportSpecifier(spec); const importName = getExportSpecifierName( spec.get("local"), stringSpecifiers, @@ -450,8 +465,9 @@ function getLocalExportMetadata( child.node.source && child.get("source").isStringLiteral() ) { - (child.node.specifiers as t.ExportSpecifier[]).forEach(spec => { - bindingKindLookup.set(spec.local.name, "block"); + child.get("specifiers").forEach(spec => { + assertExportSpecifier(spec); + bindingKindLookup.set(spec.get("local").node.name, "block"); }); return; } diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/noInterop-loose/export-namespace-without-plugin/options.json b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/noInterop-loose/export-namespace-without-plugin/options.json new file mode 100644 index 000000000000..e12448cdcac8 --- /dev/null +++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/noInterop-loose/export-namespace-without-plugin/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Export namespace should be first transformed by `@babel/plugin-transform-export-namespace-from`." +} diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/noInterop-loose/export-namespace-without-plugin/output.js b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/noInterop-loose/export-namespace-without-plugin/output.js deleted file mode 100644 index f67a2fb975a6..000000000000 --- a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/noInterop-loose/export-namespace-without-plugin/output.js +++ /dev/null @@ -1,6 +0,0 @@ -"use strict"; - -exports.__esModule = true; -exports.ns = void 0; -var _ns = require("./foo"); -exports.ns = _ns;