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

Hoist initial assignment to exported names in cjs to they are not blocked by bindings made by __exportStar #37093

Merged
merged 4 commits into from
Feb 28, 2020
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.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
8 changes: 7 additions & 1 deletion src/compiler/transformers/module/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ namespace ts {
if (shouldEmitUnderscoreUnderscoreESModule()) {
append(statements, createUnderscoreUnderscoreESModule());
}
if (length(currentModuleInfo.exportedNames)) {
append(statements, createExpressionStatement(reduceLeft(currentModuleInfo.exportedNames, (prev, nextId) => createAssignment(createPropertyAccess(createIdentifier("exports"), createIdentifier(idText(nextId))), prev), createVoidZero() as Expression)));
}

append(statements, visitNode(currentModuleInfo.externalHelpersImportDeclaration, sourceElementVisitor, isStatement));
addRange(statements, visitNodes(node.statements, sourceElementVisitor, isStatement, statementOffset));
Expand Down Expand Up @@ -414,6 +417,9 @@ namespace ts {
if (shouldEmitUnderscoreUnderscoreESModule()) {
append(statements, createUnderscoreUnderscoreESModule());
}
if (length(currentModuleInfo.exportedNames)) {
append(statements, createExpressionStatement(reduceLeft(currentModuleInfo.exportedNames, (prev, nextId) => createAssignment(createPropertyAccess(createIdentifier("exports"), createIdentifier(idText(nextId))), prev), createVoidZero() as Expression)));
}

// Visit each statement of the module body.
append(statements, visitNode(currentModuleInfo.externalHelpersImportDeclaration, sourceElementVisitor, isStatement));
Expand Down Expand Up @@ -1188,7 +1194,7 @@ namespace ts {

variables = append(variables, variable);
}
else {
else if (variable.initializer) {
expressions = append(expressions, transformInitializedVariable(variable));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/testRunner/unittests/tsserver/compileOnSave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ namespace ts.projectSystem {

const expectedEmittedFileName = "/a/b/f1.js";
assert.isTrue(host.fileExists(expectedEmittedFileName));
assert.equal(host.readFile(expectedEmittedFileName), `"use strict";\r\nexports.__esModule = true;\r\nfunction Foo() { return 10; }\r\nexports.Foo = Foo;\r\n`);
assert.equal(host.readFile(expectedEmittedFileName), `"use strict";\r\nexports.__esModule = true;\r\nexports.Foo = void 0;\r\nfunction Foo() { return 10; }\r\nexports.Foo = Foo;\r\n`);
});

it("shoud not emit js files in external projects", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ exports.fn3 = fn3;`;
expectedFiles: [{
path: `${usageLocation}/usage.js`,
content: `"use strict";
exports.__esModule = true;
exports.__esModule = true;${appendJsText === changeJs ? "\nexports.fn3 = void 0;" : ""}
var fns_1 = require("../decls/fns");
fns_1.fn1();
fns_1.fn2();
Expand Down Expand Up @@ -286,6 +286,7 @@ ${appendJs}`
path: `${dependecyLocation}/fns.js`,
content: `"use strict";
exports.__esModule = true;
${appendJsText === changeJs ? "exports.fn3 = " : ""}exports.fn2 = exports.fn1 = void 0;
function fn1() { }
exports.fn1 = fn1;
function fn2() { }
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/APISample_compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ compile(process.argv.slice(2), {
* Please log a "breaking change" issue for any API breaking change affecting this issue
*/
exports.__esModule = true;
exports.compile = void 0;
var ts = require("typescript");
function compile(fileNames, options) {
var program = ts.createProgram(fileNames, options);
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/APISample_linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ fileNames.forEach(fileName => {
* Please log a "breaking change" issue for any API breaking change affecting this issue
*/
exports.__esModule = true;
exports.delint = void 0;
var ts = require("typescript");
function delint(sourceFile) {
delintNode(sourceFile);
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/APISample_parseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export function createProgram(rootFiles: string[], compilerOptionsJson: string):
* Please log a "breaking change" issue for any API breaking change affecting this issue
*/
exports.__esModule = true;
exports.createProgram = void 0;
var ts = require("typescript");
function printError(error) {
if (!error) {
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/ExportAssignment7.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export = B;

//// [ExportAssignment7.js]
"use strict";
exports.C = void 0;
var C = /** @class */ (function () {
function C() {
}
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/ExportAssignment8.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export class C {

//// [ExportAssignment8.js]
"use strict";
exports.C = void 0;
var C = /** @class */ (function () {
function C() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export module A {
//// [part1.js]
"use strict";
exports.__esModule = true;
exports.A = void 0;
var A;
(function (A) {
var Utils;
Expand All @@ -46,6 +47,7 @@ var A;
//// [part2.js]
"use strict";
exports.__esModule = true;
exports.A = void 0;
var A;
(function (A) {
// collision with 'Origin' var in other part of merged module
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/aliasAssignments.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ y = moduleA; // should be error
//// [aliasAssignments_moduleA.js]
"use strict";
exports.__esModule = true;
exports.someClass = void 0;
var someClass = /** @class */ (function () {
function someClass() {
}
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/aliasUsageInAccessorsOfClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class C2 {
//// [aliasUsage1_backbone.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Model = void 0;
var Model = /** @class */ (function () {
function Model() {
}
Expand All @@ -52,6 +53,7 @@ var __extends = (this && this.__extends) || (function () {
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.VisualizationModel = void 0;
var Backbone = require("./aliasUsage1_backbone");
var VisualizationModel = /** @class */ (function (_super) {
__extends(VisualizationModel, _super);
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/aliasUsageInArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var xs2: typeof moduleA[] = [moduleA];
//// [aliasUsageInArray_backbone.js]
"use strict";
exports.__esModule = true;
exports.Model = void 0;
var Model = /** @class */ (function () {
function Model() {
}
Expand All @@ -46,6 +47,7 @@ var __extends = (this && this.__extends) || (function () {
};
})();
exports.__esModule = true;
exports.VisualizationModel = void 0;
var Backbone = require("./aliasUsageInArray_backbone");
var VisualizationModel = /** @class */ (function (_super) {
__extends(VisualizationModel, _super);
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/aliasUsageInFunctionExpression.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ f = (x) => moduleA;
//// [aliasUsageInFunctionExpression_backbone.js]
"use strict";
exports.__esModule = true;
exports.Model = void 0;
var Model = /** @class */ (function () {
function Model() {
}
Expand All @@ -45,6 +46,7 @@ var __extends = (this && this.__extends) || (function () {
};
})();
exports.__esModule = true;
exports.VisualizationModel = void 0;
var Backbone = require("./aliasUsageInFunctionExpression_backbone");
var VisualizationModel = /** @class */ (function (_super) {
__extends(VisualizationModel, _super);
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/aliasUsageInGenericFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var r2 = foo({ a: <IHasVisualizationModel>null });
//// [aliasUsageInGenericFunction_backbone.js]
"use strict";
exports.__esModule = true;
exports.Model = void 0;
var Model = /** @class */ (function () {
function Model() {
}
Expand All @@ -49,6 +50,7 @@ var __extends = (this && this.__extends) || (function () {
};
})();
exports.__esModule = true;
exports.VisualizationModel = void 0;
var Backbone = require("./aliasUsageInGenericFunction_backbone");
var VisualizationModel = /** @class */ (function (_super) {
__extends(VisualizationModel, _super);
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/aliasUsageInIndexerOfClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class N2 {
//// [aliasUsageInIndexerOfClass_backbone.js]
"use strict";
exports.__esModule = true;
exports.Model = void 0;
var Model = /** @class */ (function () {
function Model() {
}
Expand All @@ -51,6 +52,7 @@ var __extends = (this && this.__extends) || (function () {
};
})();
exports.__esModule = true;
exports.VisualizationModel = void 0;
var Backbone = require("./aliasUsageInIndexerOfClass_backbone");
var VisualizationModel = /** @class */ (function (_super) {
__extends(VisualizationModel, _super);
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/aliasUsageInObjectLiteral.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var c: { y: { z: IHasVisualizationModel } } = { y: { z: moduleA } };
//// [aliasUsageInObjectLiteral_backbone.js]
"use strict";
exports.__esModule = true;
exports.Model = void 0;
var Model = /** @class */ (function () {
function Model() {
}
Expand All @@ -46,6 +47,7 @@ var __extends = (this && this.__extends) || (function () {
};
})();
exports.__esModule = true;
exports.VisualizationModel = void 0;
var Backbone = require("./aliasUsageInObjectLiteral_backbone");
var VisualizationModel = /** @class */ (function (_super) {
__extends(VisualizationModel, _super);
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/aliasUsageInOrExpression.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var f: { x: IHasVisualizationModel } = <{ x: IHasVisualizationModel }>null ? { x
//// [aliasUsageInOrExpression_backbone.js]
"use strict";
exports.__esModule = true;
exports.Model = void 0;
var Model = /** @class */ (function () {
function Model() {
}
Expand All @@ -49,6 +50,7 @@ var __extends = (this && this.__extends) || (function () {
};
})();
exports.__esModule = true;
exports.VisualizationModel = void 0;
var Backbone = require("./aliasUsageInOrExpression_backbone");
var VisualizationModel = /** @class */ (function (_super) {
__extends(VisualizationModel, _super);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class D extends C<IHasVisualizationModel> {
//// [aliasUsageInTypeArgumentOfExtendsClause_backbone.js]
"use strict";
exports.__esModule = true;
exports.Model = void 0;
var Model = /** @class */ (function () {
function Model() {
}
Expand All @@ -49,6 +50,7 @@ var __extends = (this && this.__extends) || (function () {
};
})();
exports.__esModule = true;
exports.VisualizationModel = void 0;
var Backbone = require("./aliasUsageInTypeArgumentOfExtendsClause_backbone");
var VisualizationModel = /** @class */ (function (_super) {
__extends(VisualizationModel, _super);
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/aliasUsageInVarAssignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var m: typeof moduleA = i;
//// [aliasUsageInVarAssignment_backbone.js]
"use strict";
exports.__esModule = true;
exports.Model = void 0;
var Model = /** @class */ (function () {
function Model() {
}
Expand All @@ -45,6 +46,7 @@ var __extends = (this && this.__extends) || (function () {
};
})();
exports.__esModule = true;
exports.VisualizationModel = void 0;
var Backbone = require("./aliasUsageInVarAssignment_backbone");
var VisualizationModel = /** @class */ (function (_super) {
__extends(VisualizationModel, _super);
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/aliasUsedAsNameValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ exports.id = void 0;
//// [aliasUsedAsNameValue_1.js]
"use strict";
exports.__esModule = true;
exports.b = void 0;
function b(a) { return null; }
exports.b = b;
//// [aliasUsedAsNameValue_2.js]
"use strict";
exports.__esModule = true;
exports.a = void 0;
///<reference path='aliasUsedAsNameValue_0.ts' />
///<reference path='aliasUsedAsNameValue_1.ts' />
var mod = require("./aliasUsedAsNameValue_0");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ const y: originalZZZ = x;
//// [b.js]
"use strict";
exports.__esModule = true;
exports.zzz = void 0;
exports.zzz = 123;
exports["default"] = exports.zzz;
//// [a.js]
"use strict";
exports.__esModule = true;
exports["default"] = void 0;
var b_1 = require("./b");
exports["default"] = b_1["default"];
var x = { x: "" };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ export var a = vextend({
//// [func.js]
"use strict";
exports.__esModule = true;
exports.vextend = void 0;
exports.vextend = extend;
//// [app.js]
"use strict";
exports.__esModule = true;
exports.a = void 0;
var func_1 = require("./func");
// hover on vextend
exports.a = func_1.vextend({
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/allowSyntheticDefaultImports1.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ export class Foo {
//// [a.js]
"use strict";
exports.__esModule = true;
exports.x = void 0;
var b_1 = require("./b");
exports.x = new b_1["default"].Foo();
1 change: 1 addition & 0 deletions tests/baselines/reference/allowSyntheticDefaultImports4.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ export var x = new Foo();
//// [a.js]
"use strict";
exports.__esModule = true;
exports.x = void 0;
var b_1 = require("./b");
exports.x = new b_1["default"]();
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ exports.__esModule = true;
//// [file2.js]
"use strict";
exports.__esModule = true;
exports.A = void 0;
var file1_1 = require("./file1");
exports.A = file1_1.styled();

Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/alwaysStrictModule4.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export const a = 1
//// [alwaysStrictModule4.js]
"use strict";
exports.__esModule = true;
exports.a = void 0;
// Module commonjs
exports.a = 1;
1 change: 1 addition & 0 deletions tests/baselines/reference/alwaysStrictModule6.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export const a = 1;
//// [alwaysStrictModule6.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.a = void 0;
// Targeting ES5
exports.a = 1;
1 change: 1 addition & 0 deletions tests/baselines/reference/ambient.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ declare namespace ns {
//// [a.js]
"use strict";
exports.__esModule = true;
exports.A = void 0;
var A = /** @class */ (function () {
function A() {
}
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/amdDeclarationEmitNoExtraDeclare.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var __extends = (this && this.__extends) || (function () {
define("Configurable", ["require", "exports"], function (require, exports) {
"use strict";
exports.__esModule = true;
exports.Configurable = void 0;
function Configurable(base) {
return /** @class */ (function (_super) {
__extends(class_1, _super);
Expand All @@ -56,6 +57,7 @@ define("Configurable", ["require", "exports"], function (require, exports) {
define("Class", ["require", "exports", "Configurable"], function (require, exports, Configurable_1) {
"use strict";
exports.__esModule = true;
exports.ActualClass = exports.HiddenClass = void 0;
var HiddenClass = /** @class */ (function () {
function HiddenClass() {
}
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/amdImportAsPrimaryExpression.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ if(foo.E1.A === 0){
define(["require", "exports"], function (require, exports) {
"use strict";
exports.__esModule = true;
exports.E1 = void 0;
var E1;
(function (E1) {
E1[E1["A"] = 0] = "A";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var e: number = <foo.E1>0;
define(["require", "exports"], function (require, exports) {
"use strict";
exports.__esModule = true;
exports.E1 = exports.C1 = void 0;
var C1 = /** @class */ (function () {
function C1() {
this.m1 = 42;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class Bar {}
define("mynamespace::SomeModuleA", ["require", "exports"], function (require, exports) {
"use strict";
exports.__esModule = true;
exports.Foo = void 0;
/// <amd-module name="mynamespace::SomeModuleA" />
var Foo = /** @class */ (function () {
function Foo() {
Expand All @@ -22,6 +23,7 @@ define("mynamespace::SomeModuleA", ["require", "exports"], function (require, ex
define("mynamespace::SomeModuleB", ["require", "exports"], function (require, exports) {
"use strict";
exports.__esModule = true;
exports.Bar = void 0;
/// <amd-module name="mynamespace::SomeModuleB" />
var Bar = /** @class */ (function () {
function Bar() {
Expand Down
Loading