Skip to content

Commit 454cdb8

Browse files
authored
Retain undefined initializations (#36806)
* Emit an export assignment even when the initializer is elided * User a void 0; and elide assignments for enum/namespace-sourced exported variables * HAHA, SIMPLIFY GREATLY
1 parent 56b6d0d commit 454cdb8

File tree

77 files changed

+226
-15
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+226
-15
lines changed

src/compiler/transformers/module/module.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,6 @@ namespace ts {
11801180
let modifiers: NodeArray<Modifier> | undefined;
11811181

11821182
// If we're exporting these variables, then these just become assignments to 'exports.x'.
1183-
// We only want to emit assignments for variables with initializers.
11841183
for (const variable of node.declarationList.declarations) {
11851184
if (isIdentifier(variable.name) && isLocalName(variable.name)) {
11861185
if (!modifiers) {
@@ -1189,7 +1188,7 @@ namespace ts {
11891188

11901189
variables = append(variables, variable);
11911190
}
1192-
else if (variable.initializer) {
1191+
else {
11931192
expressions = append(expressions, transformInitializedVariable(variable));
11941193
}
11951194
}
@@ -1259,7 +1258,7 @@ namespace ts {
12591258
),
12601259
/*location*/ node.name
12611260
),
1262-
visitNode(node.initializer, moduleExpressionElementVisitor)
1261+
node.initializer ? visitNode(node.initializer, moduleExpressionElementVisitor) : createVoidZero()
12631262
);
12641263
}
12651264
}

tests/baselines/reference/aliasUsedAsNameValue.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export var a = function () {
2121
//// [aliasUsedAsNameValue_0.js]
2222
"use strict";
2323
exports.__esModule = true;
24+
exports.id = void 0;
2425
//// [aliasUsedAsNameValue_1.js]
2526
"use strict";
2627
exports.__esModule = true;

tests/baselines/reference/classMemberInitializerWithLamdaScoping4.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export class Test1 {
1818
//// [classMemberInitializerWithLamdaScoping3_0.js]
1919
"use strict";
2020
exports.__esModule = true;
21+
exports.field1 = void 0;
2122
//// [classMemberInitializerWithLamdaScoping3_1.js]
2223
"use strict";
2324
exports.__esModule = true;

tests/baselines/reference/commentsBeforeVariableStatement1.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ export var b: number;
77
define(["require", "exports"], function (require, exports) {
88
"use strict";
99
exports.__esModule = true;
10+
/** b's comment*/
11+
exports.b = void 0;
1012
});

tests/baselines/reference/declFileAmbientExternalModuleWithSingleExportedModule.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export var x: SubModule.m.m3.c;
2121
//// [declFileAmbientExternalModuleWithSingleExportedModule_1.js]
2222
"use strict";
2323
exports.__esModule = true;
24+
exports.x = void 0;
2425

2526

2627
//// [declFileAmbientExternalModuleWithSingleExportedModule_0.d.ts]

tests/baselines/reference/declFileExportAssignmentOfGenericInterface.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ define(["require", "exports"], function (require, exports) {
2020
define(["require", "exports"], function (require, exports) {
2121
"use strict";
2222
exports.__esModule = true;
23+
exports.x = void 0;
2324
exports.x.a;
2425
});
2526

tests/baselines/reference/declFileExportImportChain.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ define(["require", "exports", "declFileExportImportChain_b1"], function (require
6161
define(["require", "exports"], function (require, exports) {
6262
"use strict";
6363
exports.__esModule = true;
64+
exports.x = void 0;
6465
});
6566

6667

tests/baselines/reference/declFileExportImportChain2.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ define(["require", "exports", "declFileExportImportChain2_b"], function (require
5252
define(["require", "exports"], function (require, exports) {
5353
"use strict";
5454
exports.__esModule = true;
55+
exports.x = void 0;
5556
});
5657

5758

tests/baselines/reference/declFileForExportedImport.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var z = b.x;
1414
//// [declFileForExportedImport_0.js]
1515
"use strict";
1616
exports.__esModule = true;
17+
exports.x = void 0;
1718
//// [declFileForExportedImport_1.js]
1819
"use strict";
1920
exports.__esModule = true;

tests/baselines/reference/declFileGenericType.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ var C;
8989
}());
9090
C.D = D;
9191
})(C = exports.C || (exports.C = {}));
92+
exports.a = void 0;
9293
exports.b = C.F;
9394
exports.c = C.F2;
9495
exports.d = C.F3;

tests/baselines/reference/decoratorOnImportEquals2.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ declare function dec<T>(target: T): T;
1212
//// [decoratorOnImportEquals2_0.js]
1313
"use strict";
1414
exports.__esModule = true;
15+
exports.X = void 0;
1516
//// [decoratorOnImportEquals2_1.js]
1617
"use strict";
1718
exports.__esModule = true;

tests/baselines/reference/destructuringAssignmentWithExportedName.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export { exportedFoo as foo, nonexportedFoo as nfoo };
2929
"use strict";
3030
var _a, _b, _c, _d, _e;
3131
Object.defineProperty(exports, "__esModule", { value: true });
32+
exports.exportedFoo = void 0;
3233
exports.foo = exports.exportedFoo;
3334
let nonexportedFoo;
3435
exports.nonexportedFoo = nonexportedFoo;

tests/baselines/reference/errorForConflictingExportEqualsValue.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ import("./a");
66

77
//// [a.js]
88
"use strict";
9+
exports.x = void 0;
910
Promise.resolve().then(function () { return require("./a"); });
1011
module.exports = exports.x;

tests/baselines/reference/excessPropertyChecksWithNestedIntersections.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ var c = { a: { x: 'hello', y: 2 } }; // error - y does not exist in type A
8181
var d = { a: { x: 'hello' }, c: 5 }; // ok
8282
var e = { a: { x: 2 }, c: 5 }; // error - types of property x are incompatible
8383
var f = { a: { x: 'hello', y: 2 }, c: 5 }; // error - y does not exist in type A
84+
exports.obj = void 0;
8485
exports.photo = {
8586
id: 1,
8687
url: '',

tests/baselines/reference/exportNonInitializedVariablesAMD.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,17 @@ define(["require", "exports"], function (require, exports) {
4040
var ;
4141
let;
4242
var ;
43+
exports.a = void 0;
44+
exports.b = void 0;
45+
exports.c = void 0;
46+
exports.d = void 0;
4347
var A = /** @class */ (function () {
4448
function A() {
4549
}
4650
return A;
4751
}());
52+
exports.e = void 0;
53+
exports.f = void 0;
4854
var B;
4955
(function (B) {
5056
B.a = 1, B.c = 2;

tests/baselines/reference/exportNonInitializedVariablesCommonJS.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,17 @@ exports.__esModule = true;
3939
var ;
4040
let;
4141
var ;
42+
exports.a = void 0;
43+
exports.b = void 0;
44+
exports.c = void 0;
45+
exports.d = void 0;
4246
var A = /** @class */ (function () {
4347
function A() {
4448
}
4549
return A;
4650
}());
51+
exports.e = void 0;
52+
exports.f = void 0;
4753
var B;
4854
(function (B) {
4955
B.a = 1, B.c = 2;

tests/baselines/reference/exportNonInitializedVariablesUMD.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,17 @@ export let h1: D = new D;
4848
var ;
4949
let;
5050
var ;
51+
exports.a = void 0;
52+
exports.b = void 0;
53+
exports.c = void 0;
54+
exports.d = void 0;
5155
var A = /** @class */ (function () {
5256
function A() {
5357
}
5458
return A;
5559
}());
60+
exports.e = void 0;
61+
exports.f = void 0;
5662
var B;
5763
(function (B) {
5864
B.a = 1, B.c = 2;

tests/baselines/reference/exportStarForValues5.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ define(["require", "exports"], function (require, exports) {
1616
define(["require", "exports"], function (require, exports) {
1717
"use strict";
1818
exports.__esModule = true;
19+
exports.x = void 0;
1920
});

tests/baselines/reference/giant.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,7 @@ define(["require", "exports"], function (require, exports) {
901901
})(eM = M_1.eM || (M_1.eM = {}));
902902
;
903903
})(M || (M = {}));
904+
exports.eV = void 0;
904905
function eF() { }
905906
exports.eF = eF;
906907
;

tests/baselines/reference/importDecl.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ var d = /** @class */ (function () {
9090
return d;
9191
}());
9292
exports.d = d;
93+
exports.x = void 0;
9394
function foo() { return null; }
9495
exports.foo = foo;
9596
//// [importDecl_require1.js]
@@ -113,6 +114,7 @@ var d = /** @class */ (function () {
113114
return d;
114115
}());
115116
exports.d = d;
117+
exports.x = void 0;
116118
function foo() { return null; }
117119
exports.foo = foo;
118120
//// [importDecl_require3.js]
@@ -124,6 +126,7 @@ var d = /** @class */ (function () {
124126
return d;
125127
}());
126128
exports.d = d;
129+
exports.x = void 0;
127130
function foo() { return null; }
128131
exports.foo = foo;
129132
//// [importDecl_require4.js]

tests/baselines/reference/importDeclarationUsedAsTypeQuery.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ exports.B = B;
2323
//// [importDeclarationUsedAsTypeQuery_1.js]
2424
"use strict";
2525
exports.__esModule = true;
26+
exports.x = void 0;
2627

2728

2829
//// [importDeclarationUsedAsTypeQuery_require.d.ts]

tests/baselines/reference/internalAliasInterfaceInsideTopLevelModuleWithExport.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export var x: b;
1111
//// [internalAliasInterfaceInsideTopLevelModuleWithExport.js]
1212
"use strict";
1313
exports.__esModule = true;
14+
exports.x = void 0;
1415

1516

1617
//// [internalAliasInterfaceInsideTopLevelModuleWithExport.d.ts]

tests/baselines/reference/internalAliasInterfaceInsideTopLevelModuleWithoutExport.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export var x: b;
1212
define(["require", "exports"], function (require, exports) {
1313
"use strict";
1414
exports.__esModule = true;
15+
exports.x = void 0;
1516
});
1617

1718

tests/baselines/reference/internalAliasUninitializedModuleInsideLocalModuleWithoutExportAccessError.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ define(["require", "exports"], function (require, exports) {
2424
(function (c) {
2525
c.x.foo();
2626
})(c = exports.c || (exports.c = {}));
27+
exports.z = void 0;
2728
});

tests/baselines/reference/internalAliasUninitializedModuleInsideTopLevelModuleWithExport.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ x.foo();
1616
define(["require", "exports"], function (require, exports) {
1717
"use strict";
1818
exports.__esModule = true;
19+
exports.x = void 0;
1920
exports.x.foo();
2021
});
2122

tests/baselines/reference/internalAliasUninitializedModuleInsideTopLevelModuleWithoutExport.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ x.foo();
1515
//// [internalAliasUninitializedModuleInsideTopLevelModuleWithoutExport.js]
1616
"use strict";
1717
exports.__esModule = true;
18+
exports.x = void 0;
1819
exports.x.foo();
1920

2021

tests/baselines/reference/isolatedModulesSpecifiedModule.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export var x;
44
//// [file1.js]
55
"use strict";
66
exports.__esModule = true;
7+
exports.x = void 0;

tests/baselines/reference/isolatedModulesUnspecifiedModule.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export var x;
44
//// [file1.js]
55
"use strict";
66
exports.__esModule = true;
7+
exports.x = void 0;

tests/baselines/reference/moduleAliasAsFunctionArgument.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ fn(a); // Error: property 'x' is missing from 'a'
1818
define(["require", "exports"], function (require, exports) {
1919
"use strict";
2020
exports.__esModule = true;
21+
exports.x = void 0;
2122
});
2223
//// [moduleAliasAsFunctionArgument_1.js]
2324
define(["require", "exports", "moduleAliasAsFunctionArgument_0"], function (require, exports, a) {

tests/baselines/reference/nodeResolution4.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ var x = 1;
1515
//// [a.js]
1616
"use strict";
1717
exports.__esModule = true;
18+
/// <reference path="ref.ts"/>
19+
exports.y = void 0;
1820
//// [b.js]
1921
"use strict";
2022
exports.__esModule = true;

tests/baselines/reference/pathMappingBasedModuleResolution7_classic.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export let y: number;
3232
define(["require", "exports"], function (require, exports) {
3333
"use strict";
3434
exports.__esModule = true;
35+
exports.b = void 0;
3536
});
3637
//// [file2.js]
3738
define(["require", "exports", "module1", "templates/module2", "../file3"], function (require, exports, module1_1, module2_1, file3_1) {

tests/baselines/reference/pathMappingBasedModuleResolution7_node.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export let y: number;
3131
//// [module2.js]
3232
"use strict";
3333
exports.__esModule = true;
34+
exports.b = void 0;
3435
//// [file2.js]
3536
"use strict";
3637
exports.__esModule = true;

tests/baselines/reference/privacyCheckTypeOfFunction.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export var b = foo;
1010
exports.__esModule = true;
1111
function foo() {
1212
}
13+
exports.x = void 0;
1314
exports.b = foo;
1415

1516

tests/baselines/reference/privacyTopLevelInternalReferenceImportWithExport.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,11 @@ define(["require", "exports"], function (require, exports) {
177177
var privateUse_im_public_v_private = exports.im_public_v_private;
178178
exports.publicUse_im_public_v_private = exports.im_public_v_private;
179179
var privateUse_im_public_i_private;
180+
exports.publicUse_im_public_i_private = void 0;
180181
var privateUse_im_public_mi_private = new exports.im_public_mi_private.c();
181182
exports.publicUse_im_public_mi_private = new exports.im_public_mi_private.c();
182183
var privateUse_im_public_mu_private;
184+
exports.publicUse_im_public_mu_private = void 0;
183185
// No Privacy errors - importing public elements
184186
exports.im_public_c_public = m_public.c_public;
185187
exports.im_public_e_public = m_public.e_public;
@@ -196,9 +198,11 @@ define(["require", "exports"], function (require, exports) {
196198
var privateUse_im_public_v_public = exports.im_public_v_public;
197199
exports.publicUse_im_public_v_public = exports.im_public_v_public;
198200
var privateUse_im_public_i_public;
201+
exports.publicUse_im_public_i_public = void 0;
199202
var privateUse_im_public_mi_public = new exports.im_public_mi_public.c();
200203
exports.publicUse_im_public_mi_public = new exports.im_public_mi_public.c();
201204
var privateUse_im_public_mu_public;
205+
exports.publicUse_im_public_mu_public = void 0;
202206
});
203207

204208

tests/baselines/reference/privacyTopLevelInternalReferenceImportWithoutExport.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,11 @@ define(["require", "exports"], function (require, exports) {
177177
var privateUse_im_private_v_private = im_private_v_private;
178178
exports.publicUse_im_private_v_private = im_private_v_private;
179179
var privateUse_im_private_i_private;
180+
exports.publicUse_im_private_i_private = void 0;
180181
var privateUse_im_private_mi_private = new im_private_mi_private.c();
181182
exports.publicUse_im_private_mi_private = new im_private_mi_private.c();
182183
var privateUse_im_private_mu_private;
184+
exports.publicUse_im_private_mu_private = void 0;
183185
// No Privacy errors - importing public elements
184186
var im_private_c_public = m_public.c_public;
185187
var im_private_e_public = m_public.e_public;
@@ -196,9 +198,11 @@ define(["require", "exports"], function (require, exports) {
196198
var privateUse_im_private_v_public = im_private_v_public;
197199
exports.publicUse_im_private_v_public = im_private_v_public;
198200
var privateUse_im_private_i_public;
201+
exports.publicUse_im_private_i_public = void 0;
199202
var privateUse_im_private_mi_public = new im_private_mi_public.c();
200203
exports.publicUse_im_private_mi_public = new im_private_mi_public.c();
201204
var privateUse_im_private_mu_public;
205+
exports.publicUse_im_private_mu_public = void 0;
202206
});
203207

204208

tests/baselines/reference/privacyVar.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,9 @@ var glo_C4_public = /** @class */ (function () {
324324
return glo_C4_public;
325325
}());
326326
var glo_v1_private;
327+
exports.glo_v2_public = void 0;
327328
var glo_v3_private;
329+
exports.glo_v4_public = void 0; // error
328330
var glo_v11_private = new glo_C1_public();
329331
exports.glo_v12_public = new glo_C1_public();
330332
var glo_v13_private = new glo_C2_private();

tests/baselines/reference/privacyVarDeclFile.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,8 @@ var privateClassWithWithPublicPropertyTypes = /** @class */ (function () {
460460
}
461461
return privateClassWithWithPublicPropertyTypes;
462462
}());
463+
exports.publicVarWithPrivatePropertyTypes = void 0; // Error
464+
exports.publicVarWithPublicPropertyTypes = void 0;
463465
var privateVarWithPrivatePropertyTypes;
464466
var privateVarWithPublicPropertyTypes;
465467
var publicClassWithPrivateModulePropertyTypes = /** @class */ (function () {
@@ -468,6 +470,7 @@ var publicClassWithPrivateModulePropertyTypes = /** @class */ (function () {
468470
return publicClassWithPrivateModulePropertyTypes;
469471
}());
470472
exports.publicClassWithPrivateModulePropertyTypes = publicClassWithPrivateModulePropertyTypes;
473+
exports.publicVarWithPrivateModulePropertyTypes = void 0; // Error
471474
var privateClassWithPrivateModulePropertyTypes = /** @class */ (function () {
472475
function privateClassWithPrivateModulePropertyTypes() {
473476
}

tests/baselines/reference/project/declarationsCascadingImports/amd/m4.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ define(["require", "exports"], function (require, exports) {
88
}());
99
exports.d = d;
1010
;
11+
exports.x = void 0;
1112
function foo() {
1213
return new d();
1314
}

0 commit comments

Comments
 (0)