Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions src/compiler/transformers/module/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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.
Expand Down
28 changes: 13 additions & 15 deletions src/compiler/transformers/module/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
47 changes: 29 additions & 18 deletions src/compiler/transformers/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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*
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}

Expand All @@ -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[][]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
};
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/exportsAndImports1-amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() { }
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/exportsAndImports1-es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() { }
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/exportsAndImports1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() { }
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/exportsAndImports3-amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/exportsAndImports3-es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/exportsAndImports3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/importNonExportedMember.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
2 changes: 1 addition & 1 deletion tests/baselines/reference/importNonExportedMember1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
2 changes: 1 addition & 1 deletion tests/baselines/reference/jsDeclarationsDefault.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/jsDeclarationsFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example of this working, in the test case:

/**
 * @param {{x: string}} a
 * @param {{y: typeof b}} b
 */
function hh(a, b) {
    return a.x && b.y();
}
export { hh as h };

exports.i = i;
exports.ii = i;
exports.j = j;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/jsxNamespaceReexports.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down
1 change: 0 additions & 1 deletion tests/baselines/reference/reExportDefaultExport.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/valuesMergingAcrossModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down