Skip to content

Commit 668bbc6

Browse files
authored
Export anonymous functions in 2 steps, declare as variable and then assign to exports. (microsoft#39820)
* Preserve the variable name when exporting an arrow or anonymous function This allows the browser or node to properly name the (arrow) function * Updated tests to reflect previous change * Remove duplicated comment * Transforms variable.initializer using moduleExpressionElementVisitor * PR feedback: rbuckton - Use isArrowFunction and isFunctionExpression * PR feedback: rbuckton - Consider ClassExpresion, they can also be named based on the variable.
1 parent 3328fdb commit 668bbc6

File tree

36 files changed

+111
-42
lines changed

36 files changed

+111
-42
lines changed

src/compiler/transformers/module/module.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,7 @@ namespace ts {
11951195

11961196
if (hasSyntacticModifier(node, ModifierFlags.Export)) {
11971197
let modifiers: NodeArray<Modifier> | undefined;
1198+
let removeCommentsOnExpressions = false;
11981199

11991200
// If we're exporting these variables, then these just become assignments to 'exports.x'.
12001201
for (const variable of node.declarationList.declarations) {
@@ -1206,7 +1207,31 @@ namespace ts {
12061207
variables = append(variables, variable);
12071208
}
12081209
else if (variable.initializer) {
1209-
expressions = append(expressions, transformInitializedVariable(variable as InitializedVariableDeclaration));
1210+
if (!isBindingPattern(variable.name) && (isArrowFunction(variable.initializer) || isFunctionExpression(variable.initializer) || isClassExpression(variable.initializer))) {
1211+
const expression = factory.createAssignment(
1212+
setTextRange(
1213+
factory.createPropertyAccessExpression(
1214+
factory.createIdentifier("exports"),
1215+
variable.name
1216+
),
1217+
/*location*/ variable.name
1218+
),
1219+
factory.createIdentifier(getTextOfIdentifierOrLiteral(variable.name))
1220+
);
1221+
const updatedVariable = factory.createVariableDeclaration(
1222+
variable.name,
1223+
variable.exclamationToken,
1224+
variable.type,
1225+
visitNode(variable.initializer, moduleExpressionElementVisitor)
1226+
);
1227+
1228+
variables = append(variables, updatedVariable);
1229+
expressions = append(expressions, expression);
1230+
removeCommentsOnExpressions = true;
1231+
}
1232+
else {
1233+
expressions = append(expressions, transformInitializedVariable(variable as InitializedVariableDeclaration));
1234+
}
12101235
}
12111236
}
12121237

@@ -1215,7 +1240,11 @@ namespace ts {
12151240
}
12161241

12171242
if (expressions) {
1218-
statements = append(statements, setOriginalNode(setTextRange(factory.createExpressionStatement(factory.inlineExpressions(expressions)), node), node));
1243+
const statement = setOriginalNode(setTextRange(factory.createExpressionStatement(factory.inlineExpressions(expressions)), node), node);
1244+
if (removeCommentsOnExpressions) {
1245+
removeAllComments(statement);
1246+
}
1247+
statements = append(statements, statement);
12191248
}
12201249
}
12211250
else {

tests/baselines/reference/aliasUsedAsNameValue.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ exports.a = void 0;
3636
///<reference path='aliasUsedAsNameValue_1.ts' />
3737
var mod = require("./aliasUsedAsNameValue_0");
3838
var b = require("./aliasUsedAsNameValue_1");
39-
exports.a = function () {
39+
var a = function () {
4040
//var x = mod.id; // TODO needed hack that mod is loaded
4141
b.b(mod);
4242
};
43+
exports.a = a;

tests/baselines/reference/declarationEmitAliasExportStar.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ __exportStar(require("./thingB"), exports);
3030
"use strict";
3131
exports.__esModule = true;
3232
exports.thing2 = void 0;
33-
exports.thing2 = function (param) { return null; };
33+
var thing2 = function (param) { return null; };
34+
exports.thing2 = thing2;
3435

3536

3637
//// [thingB.d.ts]

tests/baselines/reference/declarationEmitBundleWithAmbientReferences.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ define("conditional_directive_field", ["require", "exports"], function (require,
2929
"use strict";
3030
exports.__esModule = true;
3131
exports.build = void 0;
32-
exports.build = function () {
32+
var build = function () {
3333
return null;
3434
};
35+
exports.build = build;
3536
});
3637

3738

tests/baselines/reference/declarationEmitComputedNameCausesImportToBePainted.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ var context_1 = require("./context");
2828
exports.context = (_a = {},
2929
_a[context_1.Key] = 'bar',
3030
_a);
31-
exports.withContext = function (_a) {
31+
var withContext = function (_a) {
3232
var _b = context_1.Key, value = _a[_b];
3333
return value;
3434
};
35+
exports.withContext = withContext;
3536

3637

3738
//// [context.d.ts]

tests/baselines/reference/declarationEmitExpandoWithGenericConstraint.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ Point.zero = (): Point => Point(0, 0);
1818
"use strict";
1919
exports.__esModule = true;
2020
exports.Rect = exports.Point = void 0;
21-
exports.Point = function (x, y) { return ({ x: x, y: y }); };
22-
exports.Rect = function (a, b) { return ({ a: a, b: b }); };
21+
var Point = function (x, y) { return ({ x: x, y: y }); };
22+
exports.Point = Point;
23+
var Rect = function (a, b) { return ({ a: a, b: b }); };
24+
exports.Rect = Rect;
2325
exports.Point.zero = function () { return exports.Point(0, 0); };
2426

2527

tests/baselines/reference/declarationEmitExportAliasVisibiilityMarking.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ exports["default"] = (function (suit, rank) { return ({ suit: suit, rank: rank }
2525
"use strict";
2626
exports.__esModule = true;
2727
exports.lazyCard = void 0;
28-
exports.lazyCard = function () { return Promise.resolve().then(function () { return require('./Card'); }).then(function (a) { return a["default"]; }); };
28+
var lazyCard = function () { return Promise.resolve().then(function () { return require('./Card'); }).then(function (a) { return a["default"]; }); };
29+
exports.lazyCard = lazyCard;
2930

3031

3132
//// [Types.d.ts]

tests/baselines/reference/declarationEmitOptionalMethod.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ export const Foo = (opts: {
1111
"use strict";
1212
exports.__esModule = true;
1313
exports.Foo = void 0;
14-
exports.Foo = function (opts) { return ({}); };
14+
var Foo = function (opts) { return ({}); };
15+
exports.Foo = Foo;
1516

1617

1718
//// [declarationEmitOptionalMethod.d.ts]

tests/baselines/reference/declarationEmitRetainsJsdocyComments.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ exports.someMethod = exports.Foo = exports.foo = void 0;
5252
* comment1
5353
* @param p
5454
*/
55-
exports.foo = function (p) {
55+
var foo = function (p) {
5656
return {
5757
/**
5858
* comment2
@@ -66,6 +66,7 @@ exports.foo = function (p) {
6666
bar2: function (s) { }
6767
};
6868
};
69+
exports.foo = foo;
6970
var Foo = /** @class */ (function () {
7071
function Foo() {
7172
}

tests/baselines/reference/declarationEmitTypeAliasWithTypeParameters1.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export const y = (x: Foo<string>) => 1
77
"use strict";
88
exports.__esModule = true;
99
exports.y = void 0;
10-
exports.y = function (x) { return 1; };
10+
var y = function (x) { return 1; };
11+
exports.y = y;
1112

1213

1314
//// [declarationEmitTypeAliasWithTypeParameters1.d.ts]

0 commit comments

Comments
 (0)