Skip to content

Commit bb8a3c4

Browse files
committed
Cleaned up emit of enum declaration
1 parent f025e0c commit bb8a3c4

File tree

65 files changed

+266
-300
lines changed

Some content is hidden

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

65 files changed

+266
-300
lines changed

src/compiler/transformers/module/module.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -792,12 +792,6 @@ namespace ts {
792792
function visitExpressionStatementForEnumOrNamespaceDeclaration(node: ExpressionStatement, original: EnumDeclaration | ModuleDeclaration): VisitResult<Statement> {
793793
const statements: Statement[] = [node];
794794

795-
// Preserve old behavior for enums in which a variable statement is emitted after the body itself.
796-
if (hasModifier(original, ModifierFlags.Export) &&
797-
original.kind === SyntaxKind.EnumDeclaration &&
798-
isFirstDeclarationOfKind(original, SyntaxKind.EnumDeclaration)) {
799-
addVarForExportedEnumOrNamespaceDeclaration(statements, original);
800-
}
801795

802796
addExportMemberAssignments(statements, <Identifier>original.name);
803797

src/compiler/transformers/module/system.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -692,12 +692,6 @@ namespace ts {
692692
const originalNode = getOriginalNode(node);
693693
if ((originalNode.kind === SyntaxKind.ModuleDeclaration || originalNode.kind === SyntaxKind.EnumDeclaration) && hasModifier(originalNode, ModifierFlags.Export)) {
694694
const name = getDeclarationName(<ModuleDeclaration | EnumDeclaration>originalNode);
695-
// We only need to hoistVariableDeclaration for EnumDeclaration
696-
// as ModuleDeclaration is already hoisted when the transformer call visitVariableStatement
697-
// which then call transformsVariable for each declaration in declarationList
698-
if (originalNode.kind === SyntaxKind.EnumDeclaration) {
699-
hoistVariableDeclaration(name);
700-
}
701695
return [
702696
node,
703697
createExportStatement(name, name)
@@ -849,7 +843,7 @@ namespace ts {
849843
visitNode(node.statement, visitNestedNode, isStatement, /*optional*/ false, liftToBlock),
850844
node.expression
851845
);
852-
}
846+
}
853847

854848
/**
855849
* Visits the body of a WhileStatement to hoist declarations.
@@ -862,7 +856,7 @@ namespace ts {
862856
node.expression,
863857
visitNode(node.statement, visitNestedNode, isStatement, /*optional*/ false, liftToBlock)
864858
);
865-
}
859+
}
866860

867861
/**
868862
* Visits the body of a LabeledStatement to hoist declarations.

src/compiler/transformers/ts.ts

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2517,29 +2517,6 @@ namespace ts {
25172517
|| compilerOptions.isolatedModules;
25182518
}
25192519

2520-
function shouldEmitVarForEnumDeclaration(node: EnumDeclaration | ModuleDeclaration) {
2521-
return isFirstEmittedDeclarationInScope(node)
2522-
&& (!hasModifier(node, ModifierFlags.Export)
2523-
|| isES6ExportedDeclaration(node));
2524-
}
2525-
2526-
2527-
/*
2528-
* Adds a trailing VariableStatement for an enum or module declaration.
2529-
*/
2530-
function addVarForEnumExportedFromNamespace(statements: Statement[], node: EnumDeclaration | ModuleDeclaration) {
2531-
const statement = createVariableStatement(
2532-
/*modifiers*/ undefined,
2533-
[createVariableDeclaration(
2534-
getDeclarationName(node),
2535-
/*type*/ undefined,
2536-
getExportName(node)
2537-
)]
2538-
);
2539-
setSourceMapRange(statement, node);
2540-
statements.push(statement);
2541-
}
2542-
25432520
/**
25442521
* Visits an enum declaration.
25452522
*
@@ -2562,7 +2539,7 @@ namespace ts {
25622539
// a leading variable declaration, we should not emit leading comments for the
25632540
// enum body.
25642541
recordEmittedDeclarationInScope(node);
2565-
if (shouldEmitVarForEnumDeclaration(node)) {
2542+
if (isFirstEmittedDeclarationInScope(node)) {
25662543
addVarForEnumOrModuleDeclaration(statements, node);
25672544

25682545
// We should still emit the comments if we are emitting a system module.
@@ -2580,6 +2557,25 @@ namespace ts {
25802557
// `exportName` is the expression used within this node's container for any exported references.
25812558
const exportName = getExportName(node);
25822559

2560+
// x || (x = {})
2561+
// exports.x || (exports.x = {})
2562+
let moduleArg =
2563+
createLogicalOr(
2564+
exportName,
2565+
createAssignment(
2566+
exportName,
2567+
createObjectLiteral()
2568+
)
2569+
);
2570+
2571+
if (hasModifier(node, ModifierFlags.Export) && !isES6ExportedDeclaration(node)) {
2572+
// `localName` is the expression used within this node's containing scope for any local references.
2573+
const localName = getLocalName(node);
2574+
2575+
// x = (exports.x || (exports.x = {}))
2576+
moduleArg = createAssignment(localName, moduleArg);
2577+
}
2578+
25832579
// (function (x) {
25842580
// x[x["y"] = 0] = "y";
25852581
// ...
@@ -2596,25 +2592,14 @@ namespace ts {
25962592
transformEnumBody(node, containerName)
25972593
),
25982594
/*typeArguments*/ undefined,
2599-
[createLogicalOr(
2600-
exportName,
2601-
createAssignment(
2602-
exportName,
2603-
createObjectLiteral()
2604-
)
2605-
)]
2595+
[moduleArg]
26062596
),
26072597
/*location*/ node
26082598
);
26092599

26102600
setOriginalNode(enumStatement, node);
26112601
setEmitFlags(enumStatement, emitFlags);
26122602
statements.push(enumStatement);
2613-
2614-
if (isNamespaceExport(node)) {
2615-
addVarForEnumExportedFromNamespace(statements, node);
2616-
}
2617-
26182603
return statements;
26192604
}
26202605

@@ -2739,10 +2724,6 @@ namespace ts {
27392724
return false;
27402725
}
27412726

2742-
function shouldEmitVarForModuleDeclaration(node: ModuleDeclaration) {
2743-
return isFirstEmittedDeclarationInScope(node);
2744-
}
2745-
27462727
/**
27472728
* Adds a leading VariableStatement for a enum or module declaration.
27482729
*/
@@ -2817,7 +2798,7 @@ namespace ts {
28172798
// a leading variable declaration, we should not emit leading comments for the
28182799
// module body.
28192800
recordEmittedDeclarationInScope(node);
2820-
if (shouldEmitVarForModuleDeclaration(node)) {
2801+
if (isFirstEmittedDeclarationInScope(node)) {
28212802
addVarForEnumOrModuleDeclaration(statements, node);
28222803
// We should still emit the comments if we are emitting a system module.
28232804
if (moduleKind !== ModuleKind.System || currentScope !== currentSourceFile) {

tests/baselines/reference/ModuleWithExportedAndNonExportedEnums.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ var b = A.Day.Monday;
1414
//// [ModuleWithExportedAndNonExportedEnums.js]
1515
var A;
1616
(function (A) {
17+
var Color;
1718
(function (Color) {
1819
Color[Color["Red"] = 0] = "Red";
1920
Color[Color["Blue"] = 1] = "Blue";
20-
})(A.Color || (A.Color = {}));
21-
var Color = A.Color;
21+
})(Color = A.Color || (A.Color = {}));
2222
var Day;
2323
(function (Day) {
2424
Day[Day["Monday"] = 0] = "Monday";

tests/baselines/reference/amdImportAsPrimaryExpression.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ if(foo.E1.A === 0){
1515
//// [foo_0.js]
1616
define(["require", "exports"], function (require, exports) {
1717
"use strict";
18+
var E1;
1819
(function (E1) {
1920
E1[E1["A"] = 0] = "A";
2021
E1[E1["B"] = 1] = "B";
2122
E1[E1["C"] = 2] = "C";
22-
})(exports.E1 || (exports.E1 = {}));
23-
var E1 = exports.E1;
23+
})(E1 = exports.E1 || (exports.E1 = {}));
2424
});
2525
//// [foo_1.js]
2626
define(["require", "exports", "./foo_0"], function (require, exports, foo) {

tests/baselines/reference/amdImportNotAsPrimaryExpression.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ define(["require", "exports"], function (require, exports) {
4242
}());
4343
exports.C1 = C1;
4444
C1.s1 = true;
45+
var E1;
4546
(function (E1) {
4647
E1[E1["A"] = 0] = "A";
4748
E1[E1["B"] = 1] = "B";
4849
E1[E1["C"] = 2] = "C";
49-
})(exports.E1 || (exports.E1 = {}));
50-
var E1 = exports.E1;
50+
})(E1 = exports.E1 || (exports.E1 = {}));
5151
});
5252
//// [foo_1.js]
5353
define(["require", "exports"], function (require, exports) {

tests/baselines/reference/collisionExportsRequireAndEnum.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,16 @@ module m4 {
6363
//// [collisionExportsRequireAndEnum_externalmodule.js]
6464
define(["require", "exports"], function (require, exports) {
6565
"use strict";
66+
var require;
6667
(function (require) {
6768
require[require["_thisVal1"] = 0] = "_thisVal1";
6869
require[require["_thisVal2"] = 1] = "_thisVal2";
69-
})(exports.require || (exports.require = {}));
70-
var require = exports.require;
70+
})(require = exports.require || (exports.require = {}));
71+
var exports;
7172
(function (exports) {
7273
exports[exports["_thisVal1"] = 0] = "_thisVal1";
7374
exports[exports["_thisVal2"] = 1] = "_thisVal2";
74-
})(exports.exports || (exports.exports = {}));
75-
var exports = exports.exports;
75+
})(exports = exports.exports || (exports.exports = {}));
7676
var m1;
7777
(function (m1) {
7878
var require;
@@ -88,16 +88,16 @@ define(["require", "exports"], function (require, exports) {
8888
})(m1 || (m1 = {}));
8989
var m2;
9090
(function (m2) {
91+
var require;
9192
(function (require) {
9293
require[require["_thisVal1"] = 0] = "_thisVal1";
9394
require[require["_thisVal2"] = 1] = "_thisVal2";
94-
})(m2.require || (m2.require = {}));
95-
var require = m2.require;
95+
})(require = m2.require || (m2.require = {}));
96+
var exports;
9697
(function (exports) {
9798
exports[exports["_thisVal1"] = 0] = "_thisVal1";
9899
exports[exports["_thisVal2"] = 1] = "_thisVal2";
99-
})(m2.exports || (m2.exports = {}));
100-
var exports = m2.exports;
100+
})(exports = m2.exports || (m2.exports = {}));
101101
})(m2 || (m2 = {}));
102102
});
103103
//// [collisionExportsRequireAndEnum_globalFile.js]
@@ -126,14 +126,14 @@ var m3;
126126
})(m3 || (m3 = {}));
127127
var m4;
128128
(function (m4) {
129+
var require;
129130
(function (require) {
130131
require[require["_thisVal1"] = 0] = "_thisVal1";
131132
require[require["_thisVal2"] = 1] = "_thisVal2";
132-
})(m4.require || (m4.require = {}));
133-
var require = m4.require;
133+
})(require = m4.require || (m4.require = {}));
134+
var exports;
134135
(function (exports) {
135136
exports[exports["_thisVal1"] = 0] = "_thisVal1";
136137
exports[exports["_thisVal2"] = 1] = "_thisVal2";
137-
})(m4.exports || (m4.exports = {}));
138-
var exports = m4.exports;
138+
})(exports = m4.exports || (m4.exports = {}));
139139
})(m4 || (m4 = {}));

tests/baselines/reference/commentOnExportEnumDeclaration.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ export enum Color {
1111
/**
1212
* comment
1313
*/
14+
var Color;
1415
(function (Color) {
1516
Color[Color["r"] = 0] = "r";
1617
Color[Color["g"] = 1] = "g";
1718
Color[Color["b"] = 2] = "b";
18-
})(exports.Color || (exports.Color = {}));
19-
var Color = exports.Color;
19+
})(Color = exports.Color || (exports.Color = {}));

tests/baselines/reference/commonJSImportNotAsPrimaryExpression.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ var C1 = (function () {
4141
}());
4242
exports.C1 = C1;
4343
C1.s1 = true;
44+
var E1;
4445
(function (E1) {
4546
E1[E1["A"] = 0] = "A";
4647
E1[E1["B"] = 1] = "B";
4748
E1[E1["C"] = 2] = "C";
48-
})(exports.E1 || (exports.E1 = {}));
49-
var E1 = exports.E1;
49+
})(E1 = exports.E1 || (exports.E1 = {}));
5050
//// [foo_1.js]
5151
"use strict";
5252
var i;

tests/baselines/reference/declFileTypeofInAnonymousType.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ var m1;
3131
return c;
3232
}());
3333
m1.c = c;
34+
var e;
3435
(function (e) {
3536
e[e["weekday"] = 0] = "weekday";
3637
e[e["weekend"] = 1] = "weekend";
3738
e[e["holiday"] = 2] = "holiday";
38-
})(m1.e || (m1.e = {}));
39-
var e = m1.e;
39+
})(e = m1.e || (m1.e = {}));
4040
})(m1 || (m1 = {}));
4141
var a;
4242
var b = {

0 commit comments

Comments
 (0)