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 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 27 additions & 16 deletions src/compiler/transformers/utilities.ts
Original file line number Diff line number Diff line change
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);
}
}
addExportedFunctionDeclarartion(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) {
addExportedFunctionDeclarartion(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 addExportedFunctionDeclarartion(node: FunctionDeclaration, name: Identifier | undefined, isDefault: boolean) {
exportedFunctions = append(exportedFunctions, 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
4 changes: 3 additions & 1 deletion tests/baselines/reference/exportsAndImports3-amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ 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.f = f;
exports.f1 = f;
exports.v = 1;
Expand Down
4 changes: 3 additions & 1 deletion tests/baselines/reference/exportsAndImports3-es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ 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.f = f;
exports.f1 = f;
exports.v = 1;
Expand Down
4 changes: 3 additions & 1 deletion tests/baselines/reference/exportsAndImports3.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ 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.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 });
4 changes: 3 additions & 1 deletion tests/baselines/reference/jsDeclarationsDefault.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ 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;
exports.default = foo;
exports.bar = foo;
jakebailey marked this conversation as resolved.
Show resolved Hide resolved
function foo() {
Expand Down
7 changes: 6 additions & 1 deletion tests/baselines/reference/jsDeclarationsFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,20 @@ 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.i = i;
exports.ii = i;
exports.j = j;
exports.jj = j;
exports.j = j;
exports.jj = j;
function a() { }
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
3 changes: 2 additions & 1 deletion tests/baselines/reference/reExportDefaultExport.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ foo();
//// [m1.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.f = void 0;
exports.default = f;
exports.f = f;
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