Handle exported functions in ExportDeclarations for CJS/AMD/UMD emit (#58489)

This commit is contained in:
Jake Bailey 2024-05-28 17:22:27 -07:00 committed by GitHub
parent 8f408cc120
commit fa58c615a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
24 changed files with 77 additions and 62 deletions

View File

@ -283,10 +283,8 @@ export function transformModule(context: TransformationContext): (x: SourceFile
);
}
}
if (some(currentModuleInfo.exportedFunctions)) {
for (const f of currentModuleInfo.exportedFunctions) {
appendExportsOfHoistedDeclaration(statements, f);
}
for (const f of currentModuleInfo.exportedFunctions) {
appendExportsOfHoistedDeclaration(statements, f);
}
append(statements, visitNode(currentModuleInfo.externalHelpersImportDeclaration, topLevelVisitor, isStatement));
@ -613,10 +611,8 @@ export function transformModule(context: TransformationContext): (x: SourceFile
if (some(currentModuleInfo.exportedNames)) {
append(statements, factory.createExpressionStatement(reduceLeft(currentModuleInfo.exportedNames, (prev, nextId) => factory.createAssignment(factory.createPropertyAccessExpression(factory.createIdentifier("exports"), factory.createIdentifier(idText(nextId))), prev), factory.createVoidZero() as Expression)));
}
if (some(currentModuleInfo.exportedFunctions)) {
for (const f of currentModuleInfo.exportedFunctions) {
appendExportsOfHoistedDeclaration(statements, f);
}
for (const f of currentModuleInfo.exportedFunctions) {
appendExportsOfHoistedDeclaration(statements, f);
}
// Visit each statement of the module body.

View File

@ -433,7 +433,7 @@ export function transformSystemModule(context: TransformationContext): (x: Sourc
// this set is used to filter names brought by star expors.
// local names set should only be added if we have anything exported
if (!some(moduleInfo.exportedNames) && !some(moduleInfo.exportedFunctions) && moduleInfo.exportSpecifiers.size === 0) {
if (!some(moduleInfo.exportedNames) && moduleInfo.exportedFunctions.size === 0 && moduleInfo.exportSpecifiers.size === 0) {
// no exported declarations (export var ...) or export specifiers (export {x})
// check if we have any non star export declarations.
let hasExportDeclarationWithExportClause = false;
@ -469,21 +469,19 @@ export function transformSystemModule(context: TransformationContext): (x: Sourc
}
}
if (moduleInfo.exportedFunctions) {
for (const f of moduleInfo.exportedFunctions) {
if (hasSyntacticModifier(f, ModifierFlags.Default)) {
continue;
}
Debug.assert(!!f.name);
// write name of exported declaration, i.e 'export var x...'
exportedNames.push(
factory.createPropertyAssignment(
factory.createStringLiteralFromNode(f.name),
factory.createTrue(),
),
);
for (const f of moduleInfo.exportedFunctions) {
if (hasSyntacticModifier(f, ModifierFlags.Default)) {
continue;
}
Debug.assert(!!f.name);
// write name of exported declaration, i.e 'export var x...'
exportedNames.push(
factory.createPropertyAssignment(
factory.createStringLiteralFromNode(f.name),
factory.createTrue(),
),
);
}
const exportedNamesStorageRef = factory.createUniqueName("exportedNames");

View File

@ -105,7 +105,7 @@ export interface ExternalModuleInfo {
exportSpecifiers: IdentifierNameMap<ExportSpecifier[]>; // file-local export specifiers by name (no reexports)
exportedBindings: Identifier[][]; // exported names of local declarations
exportedNames: Identifier[] | undefined; // all exported names in the module, both local and reexported, excluding the names of locally exported function declarations
exportedFunctions: FunctionDeclaration[] | undefined; // all of the top-level exported function declarations
exportedFunctions: Set<FunctionDeclaration>; // all of the top-level exported function declarations
exportEquals: ExportAssignment | undefined; // an export= declaration if one was present
hasExportStarsToExportValues: boolean; // whether this module contains export*
}
@ -174,8 +174,8 @@ export function collectExternalModuleInfo(context: TransformationContext, source
const exportSpecifiers = new IdentifierNameMultiMap<ExportSpecifier>();
const exportedBindings: Identifier[][] = [];
const uniqueExports = new Map<string, boolean>();
const exportedFunctions = new Set<FunctionDeclaration>();
let exportedNames: Identifier[] | undefined;
let exportedFunctions: FunctionDeclaration[] | undefined;
let hasExportDefault = false;
let exportEquals: ExportAssignment | undefined;
let hasExportStarsToExportValues = false;
@ -256,22 +256,7 @@ export function collectExternalModuleInfo(context: TransformationContext, source
case SyntaxKind.FunctionDeclaration:
if (hasSyntacticModifier(node, ModifierFlags.Export)) {
exportedFunctions = append(exportedFunctions, node as FunctionDeclaration);
if (hasSyntacticModifier(node, ModifierFlags.Default)) {
// export default function() { }
if (!hasExportDefault) {
multiMapSparseArrayAdd(exportedBindings, getOriginalNodeId(node), context.factory.getDeclarationName(node as FunctionDeclaration));
hasExportDefault = true;
}
}
else {
// export function x() { }
const name = (node as FunctionDeclaration).name!;
if (!uniqueExports.get(idText(name))) {
multiMapSparseArrayAdd(exportedBindings, getOriginalNodeId(node), name);
uniqueExports.set(idText(name), true);
}
}
addExportedFunctionDeclaration(node as FunctionDeclaration, /*name*/ undefined, hasSyntacticModifier(node, ModifierFlags.Default));
}
break;
@ -317,6 +302,11 @@ export function collectExternalModuleInfo(context: TransformationContext, source
|| resolver.getReferencedValueDeclaration(name);
if (decl) {
if (decl.kind === SyntaxKind.FunctionDeclaration) {
addExportedFunctionDeclaration(decl as FunctionDeclaration, specifier.name, specifier.name.escapedText === InternalSymbolName.Default);
continue;
}
multiMapSparseArrayAdd(exportedBindings, getOriginalNodeId(decl), specifier.name);
}
@ -325,6 +315,27 @@ export function collectExternalModuleInfo(context: TransformationContext, source
}
}
}
function addExportedFunctionDeclaration(node: FunctionDeclaration, name: Identifier | undefined, isDefault: boolean) {
exportedFunctions.add(node);
if (isDefault) {
// export default function() { }
// function x() { } + export { x as default };
if (!hasExportDefault) {
multiMapSparseArrayAdd(exportedBindings, getOriginalNodeId(node), name ?? context.factory.getDeclarationName(node));
hasExportDefault = true;
}
}
else {
// export function x() { }
// function x() { } + export { x }
name ??= node.name!;
if (!uniqueExports.get(idText(name))) {
multiMapSparseArrayAdd(exportedBindings, getOriginalNodeId(node), name);
uniqueExports.set(idText(name), true);
}
}
}
}
function collectExportedVariableInfo(decl: VariableDeclaration | BindingElement, uniqueExports: Map<string, boolean>, exportedNames: Identifier[] | undefined, exportedBindings: Identifier[][]) {

View File

@ -23,7 +23,7 @@ function test(obj: string | null): void {
//// [asserts.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isNonNullable = void 0;
exports.isNonNullable = isNonNullable;
function isNonNullable(obj) {
if (obj === undefined || obj === null) {
throw new Error("Must not be a nullable value");

View File

@ -23,7 +23,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
//// [1.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.bar = exports.v = void 0;
exports.v = void 0;
exports.bar = bar;
var v = "str" || true;
exports.v = v;
function bar() {

View File

@ -39,7 +39,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
//// [a.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.doSome = void 0;
exports.doSome = doSome;
var MAP = {
a: "a"
};

View File

@ -37,7 +37,8 @@ export { v, f, C, I, E, D, M, N, T, a };
define(["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.a = exports.M = exports.E = exports.C = exports.f = exports.v = void 0;
exports.a = exports.M = exports.E = exports.C = exports.v = void 0;
exports.f = f;
var v = 1;
exports.v = v;
function f() { }

View File

@ -36,7 +36,8 @@ export { v, f, C, I, E, D, M, N, T, a };
//// [t1.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.a = exports.M = exports.E = exports.C = exports.f = exports.v = void 0;
exports.a = exports.M = exports.E = exports.C = exports.v = void 0;
exports.f = f;
var v = 1;
exports.v = v;
function f() { }

View File

@ -36,7 +36,8 @@ export { v, f, C, I, E, D, M, N, T, a };
//// [t1.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.a = exports.M = exports.E = exports.C = exports.f = exports.v = void 0;
exports.a = exports.M = exports.E = exports.C = exports.v = void 0;
exports.f = f;
var v = 1;
exports.v = v;
function f() { }

View File

@ -37,7 +37,7 @@ export { v, f, C, I, E, D, M, N, T, a };
define(["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.a1 = exports.M1 = exports.E1 = exports.C1 = exports.f1 = exports.v1 = exports.a = exports.M = exports.E = exports.C = exports.v = void 0;
exports.a1 = exports.M1 = exports.E1 = exports.C1 = exports.v1 = exports.a = exports.M = exports.E = exports.C = exports.v = void 0;
exports.f = f;
exports.f1 = f;
exports.v = 1;

View File

@ -36,7 +36,7 @@ export { v, f, C, I, E, D, M, N, T, a };
//// [t1.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.a1 = exports.M1 = exports.E1 = exports.C1 = exports.f1 = exports.v1 = exports.a = exports.M = exports.E = exports.C = exports.v = void 0;
exports.a1 = exports.M1 = exports.E1 = exports.C1 = exports.v1 = exports.a = exports.M = exports.E = exports.C = exports.v = void 0;
exports.f = f;
exports.f1 = f;
exports.v = 1;

View File

@ -36,7 +36,7 @@ export { v, f, C, I, E, D, M, N, T, a };
//// [t1.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.a1 = exports.M1 = exports.E1 = exports.C1 = exports.f1 = exports.v1 = exports.a = exports.M = exports.E = exports.C = exports.v = void 0;
exports.a1 = exports.M1 = exports.E1 = exports.C1 = exports.v1 = exports.a = exports.M = exports.E = exports.C = exports.v = void 0;
exports.f = f;
exports.f1 = f;
exports.v = 1;

View File

@ -12,7 +12,8 @@ import { foo, bar } from "./a";
//// [a.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.baz = exports.foo = void 0;
exports.foo = foo;
exports.baz = bar;
//// [b.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

View File

@ -12,7 +12,7 @@ import { bar } from "./a";
//// [a.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.foo = void 0;
exports.foo = foo;
//// [b.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

View File

@ -46,7 +46,7 @@ exports.default = 12;
//// [index2.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.bar = exports.x = void 0;
exports.x = void 0;
exports.default = foo;
exports.bar = foo;
function foo() {

View File

@ -63,13 +63,14 @@ export function j() {}
//// [index.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.jj = exports.ii = exports.h = exports.g = void 0;
exports.a = a;
exports.b = b;
exports.c = c;
exports.d = d;
exports.e = e;
exports.f = f;
exports.g = g;
exports.h = hh;
exports.i = i;
exports.ii = i;
exports.j = j;

View File

@ -18,7 +18,7 @@ const content = <my-element/>;
//// [library.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createElement = void 0;
exports.createElement = createElement;
function createElement(element, props) {
var children = [];
for (var _i = 2; _i < arguments.length; _i++) {

View File

@ -28,7 +28,8 @@ export {require, exports, Object};
//// [index.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Object = exports.exports = exports.require = exports.__esModule = void 0;
exports.Object = exports.exports = exports.__esModule = void 0;
exports.require = require;
// cjs format file
function require() { }
const exports = {};

View File

@ -28,7 +28,8 @@ export {require, exports, Object};
//// [index.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Object = exports.exports = exports.require = exports.__esModule = void 0;
exports.Object = exports.exports = exports.__esModule = void 0;
exports.require = require;
// cjs format file
function require() { }
const exports = {};

View File

@ -28,7 +28,8 @@ export {require, exports, Object};
//// [index.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Object = exports.exports = exports.require = exports.__esModule = void 0;
exports.Object = exports.exports = exports.__esModule = void 0;
exports.require = require;
// cjs format file
function require() { }
const exports = {};

View File

@ -28,7 +28,8 @@ export {require, exports, Object};
//// [index.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Object = exports.exports = exports.require = exports.__esModule = void 0;
exports.Object = exports.exports = exports.__esModule = void 0;
exports.require = require;
// cjs format file
function require() { }
const exports = {};

View File

@ -16,7 +16,6 @@ foo();
//// [m1.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.f = void 0;
exports.default = f;
exports.f = f;
function f() {

View File

@ -38,7 +38,8 @@ getStuff().exportedDirectly;
"use strict";
// Repro from #44179 with some modification
Object.defineProperty(exports, "__esModule", { value: true });
exports.obj = exports.klass = exports.func = void 0;
exports.obj = exports.klass = void 0;
exports.func = func;
exports.exportedDirectly = exportedDirectly;
function func() { }
var klass = /** @class */ (function () {

View File

@ -22,7 +22,7 @@ A.displayName;
//// [a.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.A = void 0;
exports.A = A;
function A() { }
//// [b.js]
"use strict";