Skip to content

Export anonymous functions in 2 steps, declare as variable and then assign to exports. #39820

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

Merged
merged 6 commits into from
Aug 8, 2020
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
33 changes: 31 additions & 2 deletions src/compiler/transformers/module/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,7 @@ namespace ts {

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

// If we're exporting these variables, then these just become assignments to 'exports.x'.
for (const variable of node.declarationList.declarations) {
Expand All @@ -1206,7 +1207,31 @@ namespace ts {
variables = append(variables, variable);
}
else if (variable.initializer) {
expressions = append(expressions, transformInitializedVariable(variable as InitializedVariableDeclaration));
if (!isBindingPattern(variable.name) && (isArrowFunction(variable.initializer) || isFunctionExpression(variable.initializer) || isClassExpression(variable.initializer))) {
const expression = factory.createAssignment(
setTextRange(
factory.createPropertyAccessExpression(
factory.createIdentifier("exports"),
variable.name
),
/*location*/ variable.name
),
factory.createIdentifier(getTextOfIdentifierOrLiteral(variable.name))
);
const updatedVariable = factory.createVariableDeclaration(
variable.name,
variable.exclamationToken,
variable.type,
visitNode(variable.initializer, moduleExpressionElementVisitor)
);

variables = append(variables, updatedVariable);
expressions = append(expressions, expression);
removeCommentsOnExpressions = true;
}
else {
expressions = append(expressions, transformInitializedVariable(variable as InitializedVariableDeclaration));
}
}
}

Expand All @@ -1215,7 +1240,11 @@ namespace ts {
}

if (expressions) {
statements = append(statements, setOriginalNode(setTextRange(factory.createExpressionStatement(factory.inlineExpressions(expressions)), node), node));
const statement = setOriginalNode(setTextRange(factory.createExpressionStatement(factory.inlineExpressions(expressions)), node), node);
if (removeCommentsOnExpressions) {
removeAllComments(statement);
}
statements = append(statements, statement);
}
}
else {
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/aliasUsedAsNameValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ exports.a = void 0;
///<reference path='aliasUsedAsNameValue_1.ts' />
var mod = require("./aliasUsedAsNameValue_0");
var b = require("./aliasUsedAsNameValue_1");
exports.a = function () {
var a = function () {
//var x = mod.id; // TODO needed hack that mod is loaded
b.b(mod);
};
exports.a = a;
3 changes: 2 additions & 1 deletion tests/baselines/reference/declarationEmitAliasExportStar.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ __exportStar(require("./thingB"), exports);
"use strict";
exports.__esModule = true;
exports.thing2 = void 0;
exports.thing2 = function (param) { return null; };
var thing2 = function (param) { return null; };
exports.thing2 = thing2;


//// [thingB.d.ts]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ define("conditional_directive_field", ["require", "exports"], function (require,
"use strict";
exports.__esModule = true;
exports.build = void 0;
exports.build = function () {
var build = function () {
return null;
};
exports.build = build;
});


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ var context_1 = require("./context");
exports.context = (_a = {},
_a[context_1.Key] = 'bar',
_a);
exports.withContext = function (_a) {
var withContext = function (_a) {
var _b = context_1.Key, value = _a[_b];
return value;
};
exports.withContext = withContext;


//// [context.d.ts]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ Point.zero = (): Point => Point(0, 0);
"use strict";
exports.__esModule = true;
exports.Rect = exports.Point = void 0;
exports.Point = function (x, y) { return ({ x: x, y: y }); };
exports.Rect = function (a, b) { return ({ a: a, b: b }); };
var Point = function (x, y) { return ({ x: x, y: y }); };
exports.Point = Point;
var Rect = function (a, b) { return ({ a: a, b: b }); };
exports.Rect = Rect;
exports.Point.zero = function () { return exports.Point(0, 0); };


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ exports["default"] = (function (suit, rank) { return ({ suit: suit, rank: rank }
"use strict";
exports.__esModule = true;
exports.lazyCard = void 0;
exports.lazyCard = function () { return Promise.resolve().then(function () { return require('./Card'); }).then(function (a) { return a["default"]; }); };
var lazyCard = function () { return Promise.resolve().then(function () { return require('./Card'); }).then(function (a) { return a["default"]; }); };
exports.lazyCard = lazyCard;


//// [Types.d.ts]
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/declarationEmitOptionalMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export const Foo = (opts: {
"use strict";
exports.__esModule = true;
exports.Foo = void 0;
exports.Foo = function (opts) { return ({}); };
var Foo = function (opts) { return ({}); };
exports.Foo = Foo;


//// [declarationEmitOptionalMethod.d.ts]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ exports.someMethod = exports.Foo = exports.foo = void 0;
* comment1
* @param p
*/
exports.foo = function (p) {
var foo = function (p) {
return {
/**
* comment2
Expand All @@ -66,6 +66,7 @@ exports.foo = function (p) {
bar2: function (s) { }
};
};
exports.foo = foo;
var Foo = /** @class */ (function () {
function Foo() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export const y = (x: Foo<string>) => 1
"use strict";
exports.__esModule = true;
exports.y = void 0;
exports.y = function (x) { return 1; };
var y = function (x) { return 1; };
exports.y = y;


//// [declarationEmitTypeAliasWithTypeParameters1.d.ts]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export const y = (x: Baa<number>) => 1
"use strict";
exports.__esModule = true;
exports.y = void 0;
exports.y = function (x) { return 1; };
var y = function (x) { return 1; };
exports.y = y;


//// [declarationEmitTypeAliasWithTypeParameters2.d.ts]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ function useRef(current) {
return { current: current };
}
exports.useRef = useRef;
exports.useCsvParser = function () {
var useCsvParser = function () {
var parserRef = useRef(null);
return parserRef;
};
exports.useCsvParser = useCsvParser;


//// [index.d.ts]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var __extends = (this && this.__extends) || (function () {
})();
exports.__esModule = true;
exports.mixin = void 0;
exports.mixin = function (Base) {
var mixin = function (Base) {
return /** @class */ (function (_super) {
__extends(class_1, _super);
function class_1() {
Expand All @@ -49,6 +49,7 @@ exports.mixin = function (Base) {
return class_1;
}(Base));
};
exports.mixin = mixin;


//// [dom.d.ts]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var __assign = (this && this.__assign) || function () {
};
exports.__esModule = true;
exports.testRecFun = exports.updateIfChanged = void 0;
exports.updateIfChanged = function (t) {
var updateIfChanged = function (t) {
var reduce = function (u, update) {
var set = function (newU) { return Object.is(u, newU) ? t : update(newU); };
return Object.assign(function (key) {
Expand All @@ -69,15 +69,17 @@ exports.updateIfChanged = function (t) {
};
return reduce(t, function (t) { return t; });
};
exports.updateIfChanged = updateIfChanged;
// example from https://github.com/microsoft/TypeScript/issues/31605
exports.testRecFun = function (parent) {
var testRecFun = function (parent) {
return {
result: parent,
deeper: function (child) {
return exports.testRecFun(__assign(__assign({}, parent), child));
}
};
};
exports.testRecFun = testRecFun;
var p1 = exports.testRecFun({ one: '1' });
void p1.result.one;
var p2 = p1.deeper({ two: '2' });
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/errorsWithInvokablesInUnions01.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export let ctor: IDirectiveLinkFn<number> | ConstructableA | IDirectivePrePost<n
"use strict";
exports.__esModule = true;
exports.ctor = exports.blah = void 0;
exports.blah = function (x) { };
var blah = function (x) { };
exports.blah = blah;
exports.ctor = /** @class */ (function () {
function class_1() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ sayHello(username());
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.username = void 0;
exports.username = () => 'username';
const username = () => 'username';
exports.username = username;
//// [index.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ export const createService = <T>(
"use strict";
exports.__esModule = true;
exports.createService = void 0;
exports.createService = function (ServiceCtr) {
var createService = function (ServiceCtr) {
Object.keys(ServiceCtr).forEach(function (key) {
var method = (ServiceCtr)[key];
var __$daemonMode = method.__$daemonMode, __$action = method.__$action, id = method.id;
});
};
exports.createService = createService;
3 changes: 2 additions & 1 deletion tests/baselines/reference/importCallExpressionAsyncES3AMD.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ define(["require", "exports"], function (require, exports) {
return cl2;
}());
exports.cl2 = cl2;
exports.l = function () { return __awaiter(void 0, void 0, void 0, function () {
var l = function () { return __awaiter(void 0, void 0, void 0, function () {
var req;
return __generator(this, function (_a) {
switch (_a.label) {
Expand All @@ -151,4 +151,5 @@ define(["require", "exports"], function (require, exports) {
}
});
}); };
exports.l = l;
});
3 changes: 2 additions & 1 deletion tests/baselines/reference/importCallExpressionAsyncES3CJS.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ var cl2 = /** @class */ (function () {
return cl2;
}());
exports.cl2 = cl2;
exports.l = function () { return __awaiter(void 0, void 0, void 0, function () {
var l = function () { return __awaiter(void 0, void 0, void 0, function () {
var req;
return __generator(this, function (_a) {
switch (_a.label) {
Expand All @@ -150,3 +150,4 @@ exports.l = function () { return __awaiter(void 0, void 0, void 0, function () {
}
});
}); };
exports.l = l;
3 changes: 2 additions & 1 deletion tests/baselines/reference/importCallExpressionAsyncES3UMD.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
return cl2;
}());
exports.cl2 = cl2;
exports.l = function () { return __awaiter(void 0, void 0, void 0, function () {
var l = function () { return __awaiter(void 0, void 0, void 0, function () {
var req;
return __generator(this, function (_a) {
switch (_a.label) {
Expand All @@ -160,4 +160,5 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
}
});
}); };
exports.l = l;
});
3 changes: 2 additions & 1 deletion tests/baselines/reference/importCallExpressionAsyncES5AMD.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ define(["require", "exports"], function (require, exports) {
return cl2;
}());
exports.cl2 = cl2;
exports.l = function () { return __awaiter(void 0, void 0, void 0, function () {
var l = function () { return __awaiter(void 0, void 0, void 0, function () {
var req;
return __generator(this, function (_a) {
switch (_a.label) {
Expand All @@ -151,4 +151,5 @@ define(["require", "exports"], function (require, exports) {
}
});
}); };
exports.l = l;
});
3 changes: 2 additions & 1 deletion tests/baselines/reference/importCallExpressionAsyncES5CJS.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ var cl2 = /** @class */ (function () {
return cl2;
}());
exports.cl2 = cl2;
exports.l = function () { return __awaiter(void 0, void 0, void 0, function () {
var l = function () { return __awaiter(void 0, void 0, void 0, function () {
var req;
return __generator(this, function (_a) {
switch (_a.label) {
Expand All @@ -150,3 +150,4 @@ exports.l = function () { return __awaiter(void 0, void 0, void 0, function () {
}
});
}); };
exports.l = l;
3 changes: 2 additions & 1 deletion tests/baselines/reference/importCallExpressionAsyncES5UMD.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
return cl2;
}());
exports.cl2 = cl2;
exports.l = function () { return __awaiter(void 0, void 0, void 0, function () {
var l = function () { return __awaiter(void 0, void 0, void 0, function () {
var req;
return __generator(this, function (_a) {
switch (_a.label) {
Expand All @@ -160,4 +160,5 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
}
});
}); };
exports.l = l;
});
3 changes: 2 additions & 1 deletion tests/baselines/reference/importCallExpressionAsyncES6AMD.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ define(["require", "exports"], function (require, exports) {
}
}
exports.cl2 = cl2;
exports.l = () => __awaiter(void 0, void 0, void 0, function* () {
const l = () => __awaiter(void 0, void 0, void 0, function* () {
const req = yield new Promise((resolve_5, reject_5) => { require(['./test'], resolve_5, reject_5); }); // FIVE
});
exports.l = l;
});
3 changes: 2 additions & 1 deletion tests/baselines/reference/importCallExpressionAsyncES6CJS.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class cl2 {
}
}
exports.cl2 = cl2;
exports.l = () => __awaiter(void 0, void 0, void 0, function* () {
const l = () => __awaiter(void 0, void 0, void 0, function* () {
const req = yield Promise.resolve().then(() => require('./test')); // FIVE
});
exports.l = l;
3 changes: 2 additions & 1 deletion tests/baselines/reference/importCallExpressionAsyncES6UMD.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
}
}
exports.cl2 = cl2;
exports.l = () => __awaiter(void 0, void 0, void 0, function* () {
const l = () => __awaiter(void 0, void 0, void 0, function* () {
const req = yield __syncRequire ? Promise.resolve().then(() => require('./test')) : new Promise((resolve_5, reject_5) => { require(['./test'], resolve_5, reject_5); }); // FIVE
});
exports.l = l;
});
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,14 @@ exports.__esModule = true;
exports.tree = exports.MyClass = exports.MySFC = void 0;
/** @jsx predom */
var renderer2_1 = require("./renderer2");
exports.MySFC = function (props) { return renderer2_1.predom("p", null,
var MySFC = function (props) { return renderer2_1.predom("p", null,
props.x,
" + ",
props.y,
" = ",
props.x + props.y,
_this.props.children); };
exports.MySFC = MySFC;
var MyClass = /** @class */ (function () {
function MyClass(props) {
this.props = props;
Expand Down
Loading