Skip to content

Commit 40a2312

Browse files
committed
Modified ts.ts to handle export defaults enum/namespace correctly.
1 parent eb9025e commit 40a2312

File tree

1,278 files changed

+7973
-7959
lines changed

Some content is hidden

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

1,278 files changed

+7973
-7959
lines changed

src/compiler/transformers/ts.ts

Lines changed: 57 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -2653,34 +2653,12 @@ namespace ts {
26532653
// `containerName` is the expression used inside of the enum for assignments.
26542654
const containerName = getNamespaceContainerName(node);
26552655

2656-
// `exportName` is the expression used within this node's container for any exported references.
2657-
const exportName = hasModifier(node, ModifierFlags.Export)
2658-
? getExternalModuleOrNamespaceExportName(currentNamespaceContainerName, node, /*allowComments*/ false, /*allowSourceMaps*/ true)
2659-
: getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true);
2660-
2661-
// x || (x = {})
2662-
// exports.x || (exports.x = {})
2663-
let moduleArg =
2664-
createLogicalOr(
2665-
exportName,
2666-
createAssignment(
2667-
exportName,
2668-
createObjectLiteral()
2669-
)
2670-
);
2671-
2672-
if (hasNamespaceQualifiedExportName(node)) {
2673-
// `localName` is the expression used within this node's containing scope for any local references.
2674-
const localName = getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true);
2675-
2676-
// x = (exports.x || (exports.x = {}))
2677-
moduleArg = createAssignment(localName, moduleArg);
2678-
}
2656+
const localName = getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true);
26792657

26802658
// (function (x) {
26812659
// x[x["y"] = 0] = "y";
26822660
// ...
2683-
// })(x || (x = {}));
2661+
// })(x);
26842662
const enumStatement = createExpressionStatement(
26852663
createCall(
26862664
createFunctionExpression(
@@ -2693,23 +2671,19 @@ namespace ts {
26932671
transformEnumBody(node, containerName)
26942672
),
26952673
/*typeArguments*/ undefined,
2696-
[moduleArg]
2674+
[localName]
26972675
)
26982676
);
26992677

27002678
setOriginalNode(enumStatement, node);
2701-
if (varAdded) {
2702-
// If a variable was added, synthetic comments are emitted on it, not on the moduleStatement.
2703-
setSyntheticLeadingComments(enumStatement, undefined);
2704-
setSyntheticTrailingComments(enumStatement, undefined);
2705-
}
27062679
setTextRange(enumStatement, node);
2680+
setCommentRange(enumStatement, node);
27072681
addEmitFlags(enumStatement, emitFlags);
27082682
statements.push(enumStatement);
27092683

27102684
// Add a DeclarationMarker for the enum to preserve trailing comments and mark
27112685
// the end of the declaration.
2712-
statements.push(createEndOfDeclarationMarker(node));
2686+
// statements.push(createEndOfDeclarationMarker(node));
27132687
return statements;
27142688
}
27152689

@@ -2803,18 +2777,6 @@ namespace ts {
28032777
return isInstantiatedModule(node, !!compilerOptions.preserveConstEnums || !!compilerOptions.isolatedModules);
28042778
}
28052779

2806-
/**
2807-
* Determines whether an exported declaration will have a qualified export name (e.g. `f.x`
2808-
* or `exports.x`).
2809-
*/
2810-
function hasNamespaceQualifiedExportName(node: Node) {
2811-
return isExportOfNamespace(node)
2812-
|| (isExternalModuleExport(node)
2813-
&& moduleKind !== ModuleKind.ES2015
2814-
&& moduleKind !== ModuleKind.ESNext
2815-
&& moduleKind !== ModuleKind.System);
2816-
}
2817-
28182780
/**
28192781
* Records that a declaration was emitted in the current scope, if it was the first
28202782
* declaration for the provided symbol.
@@ -2851,22 +2813,47 @@ namespace ts {
28512813
* Adds a leading VariableStatement for a enum or module declaration.
28522814
*/
28532815
function addVarForEnumOrModuleDeclaration(statements: Statement[], node: ModuleDeclaration | EnumDeclaration) {
2854-
// Emit a variable statement for the module. We emit top-level enums as a `var`
2855-
// declaration to avoid static errors in global scripts scripts due to redeclaration.
2856-
// enums in any other scope are emitted as a `let` declaration.
2857-
const statement = createVariableStatement(
2858-
visitNodes(node.modifiers, modifierVisitor, isModifier),
2859-
createVariableDeclarationList([
2860-
createVariableDeclaration(
2861-
getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true)
2862-
)
2863-
], currentLexicalScope.kind === SyntaxKind.SourceFile ? NodeFlags.None : NodeFlags.Let)
2864-
);
2865-
2866-
setOriginalNode(statement, node);
28672816

28682817
recordEmittedDeclarationInScope(node);
28692818
if (isFirstEmittedDeclarationInScope(node)) {
2819+
// Emit a local variable statement for the module. We emit top-level enums as a `var`
2820+
// declaration to avoid static errors in global scripts scripts due to redeclaration.
2821+
// enums in any other scope are emitted as a `const` declaration.
2822+
const nodeModifierFlags = getModifierFlags(node);
2823+
const nodeIsExportDefault = (nodeModifierFlags & ModifierFlags.Export) && (nodeModifierFlags & ModifierFlags.Default);
2824+
2825+
const fileIsExternalModule = isExternalModule(currentSourceFile);
2826+
const localName = getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true);
2827+
let varInitializer;
2828+
if (fileIsExternalModule && !currentNamespace) {
2829+
// toplevel declaration in an external module
2830+
// cannot merge with another var
2831+
varInitializer = createObjectLiteral();
2832+
}
2833+
else {
2834+
const exportName = hasModifier(node, ModifierFlags.Export) && !nodeIsExportDefault
2835+
? getExternalModuleOrNamespaceExportName(currentNamespaceContainerName, node, /*allowComments*/ false, /*allowSourceMaps*/ true)
2836+
: getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true);
2837+
varInitializer = createLogicalOr(
2838+
exportName,
2839+
createAssignment(exportName, createObjectLiteral())
2840+
);
2841+
}
2842+
2843+
const statement = createVariableStatement(
2844+
visitNodes(node.modifiers, modifierVisitor, isModifier),
2845+
createVariableDeclarationList([
2846+
createVariableDeclaration(
2847+
localName,
2848+
/* type */ undefined,
2849+
varInitializer
2850+
)
2851+
], currentLexicalScope.kind === SyntaxKind.SourceFile && !fileIsExternalModule ? NodeFlags.None : NodeFlags.Const)
2852+
);
2853+
2854+
setOriginalNode(statement, node);
2855+
2856+
28702857
// Adjust the source map emit to match the old emitter.
28712858
if (node.kind === SyntaxKind.EnumDeclaration) {
28722859
setSourceMapRange(statement.declarationList, node);
@@ -2882,32 +2869,28 @@ namespace ts {
28822869
// module m1 {
28832870
// function foo4Export() {
28842871
// }
2885-
// } // trailing comment module
2872+
// } // trailing module comment
28862873
//
28872874
// Should emit:
28882875
//
28892876
// /** Module comment*/
2890-
// var m1;
2877+
// var m1 = m1 || {};
28912878
// (function (m1) {
28922879
// function foo4Export() {
28932880
// }
2894-
// })(m1 || (m1 = {})); // trailing comment module
2881+
// })(m1); // trailing module comment
28952882
//
28962883
setCommentRange(statement, node);
2897-
addEmitFlags(statement, EmitFlags.NoTrailingComments | EmitFlags.HasEndOfDeclarationMarker);
2884+
addEmitFlags(statement, EmitFlags.NoTrailingComments);
28982885
statements.push(statement);
2886+
if (nodeIsExportDefault) {
2887+
statements.push(createExportAssignment(/* decorators */ undefined, /* modifiers */ undefined, /* isExportEquals */ false, localName));
2888+
}
2889+
else if (!currentNamespace && nodeModifierFlags & ModifierFlags.Export) {
2890+
statements.push(createExternalModuleExport(localName));
2891+
}
28992892
return true;
29002893
}
2901-
else {
2902-
// For an EnumDeclaration or ModuleDeclaration that merges with a preceeding
2903-
// declaration we do not emit a leading variable declaration. To preserve the
2904-
// begin/end semantics of the declararation and to properly handle exports
2905-
// we wrap the leading variable declaration in a `MergeDeclarationMarker`.
2906-
const mergeMarker = createMergeDeclarationMarker(statement);
2907-
setEmitFlags(mergeMarker, EmitFlags.NoComments | EmitFlags.HasEndOfDeclarationMarker);
2908-
statements.push(mergeMarker);
2909-
return false;
2910-
}
29112894
}
29122895

29132896
/**
@@ -2948,33 +2931,11 @@ namespace ts {
29482931
// `containerName` is the expression used inside of the namespace for exports.
29492932
const containerName = getNamespaceContainerName(node);
29502933

2951-
// `exportName` is the expression used within this node's container for any exported references.
2952-
const exportName = hasModifier(node, ModifierFlags.Export)
2953-
? getExternalModuleOrNamespaceExportName(currentNamespaceContainerName, node, /*allowComments*/ false, /*allowSourceMaps*/ true)
2954-
: getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true);
2955-
2956-
// x || (x = {})
2957-
// exports.x || (exports.x = {})
2958-
let moduleArg =
2959-
createLogicalOr(
2960-
exportName,
2961-
createAssignment(
2962-
exportName,
2963-
createObjectLiteral()
2964-
)
2965-
);
2966-
2967-
if (hasNamespaceQualifiedExportName(node)) {
2968-
// `localName` is the expression used within this node's containing scope for any local references.
2969-
const localName = getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true);
2970-
2971-
// x = (exports.x || (exports.x = {}))
2972-
moduleArg = createAssignment(localName, moduleArg);
2973-
}
2934+
const localName = getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true);
29742935

29752936
// (function (x_1) {
29762937
// x_1.y = ...;
2977-
// })(x || (x = {}));
2938+
// })(x);
29782939
const moduleStatement = createExpressionStatement(
29792940
createCall(
29802941
createFunctionExpression(
@@ -2987,23 +2948,16 @@ namespace ts {
29872948
transformModuleBody(node, containerName)
29882949
),
29892950
/*typeArguments*/ undefined,
2990-
[moduleArg]
2951+
[localName]
29912952
)
29922953
);
29932954

29942955
setOriginalNode(moduleStatement, node);
2995-
if (varAdded) {
2996-
// If a variable was added, synthetic comments are emitted on it, not on the moduleStatement.
2997-
setSyntheticLeadingComments(moduleStatement, undefined);
2998-
setSyntheticTrailingComments(moduleStatement, undefined);
2999-
}
2956+
setCommentRange(moduleStatement, node);
30002957
setTextRange(moduleStatement, node);
30012958
addEmitFlags(moduleStatement, emitFlags);
30022959
statements.push(moduleStatement);
30032960

3004-
// Add a DeclarationMarker for the namespace to preserve trailing comments and mark
3005-
// the end of the declaration.
3006-
statements.push(createEndOfDeclarationMarker(node));
30072961
return statements;
30082962
}
30092963

tests/baselines/reference/AmbientModuleAndNonAmbientClassWithSameNameAndCommonRoot.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var p = A.Point.Origin;
2323
var p = new A.Point(0, 0); // unexpected error here, bug 840000
2424

2525
//// [classPoint.js]
26-
var A;
26+
var A = A || (A = {});
2727
(function (A) {
2828
var Point = /** @class */ (function () {
2929
function Point(x, y) {
@@ -33,7 +33,7 @@ var A;
3333
return Point;
3434
}());
3535
A.Point = Point;
36-
})(A || (A = {}));
36+
})(A);
3737
//// [test.js]
3838
var p;
3939
var p = A.Point.Origin;

tests/baselines/reference/ClassAndModuleThatMergeWithModuleMemberThatUsesClassTypeParameter.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ var clodule1 = /** @class */ (function () {
5858
}());
5959
(function (clodule1) {
6060
function f(x) { }
61-
})(clodule1 || (clodule1 = {}));
61+
})(clodule1);
6262
var clodule2 = /** @class */ (function () {
6363
function clodule2() {
6464
}
@@ -71,15 +71,15 @@ var clodule2 = /** @class */ (function () {
7171
}
7272
return D;
7373
}());
74-
})(clodule2 || (clodule2 = {}));
74+
})(clodule2);
7575
var clodule3 = /** @class */ (function () {
7676
function clodule3() {
7777
}
7878
return clodule3;
7979
}());
8080
(function (clodule3) {
8181
clodule3.y = { id: T };
82-
})(clodule3 || (clodule3 = {}));
82+
})(clodule3);
8383
var clodule4 = /** @class */ (function () {
8484
function clodule4() {
8585
}
@@ -91,4 +91,4 @@ var clodule4 = /** @class */ (function () {
9191
}
9292
return D;
9393
}());
94-
})(clodule4 || (clodule4 = {}));
94+
})(clodule4);

tests/baselines/reference/ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndGenericClassStaticFunctionOfTheSameName.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ var clodule = /** @class */ (function () {
2828
return x;
2929
}
3030
clodule.fn = fn;
31-
})(clodule || (clodule = {}));
31+
})(clodule);

tests/baselines/reference/ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndNonGenericClassStaticFunctionOfTheSameName.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ var clodule = /** @class */ (function () {
2828
return x;
2929
}
3030
clodule.fn = fn;
31-
})(clodule || (clodule = {}));
31+
})(clodule);

tests/baselines/reference/ClassAndModuleThatMergeWithModulesExportedStaticFunctionUsingClassPrivateStatics.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ var clodule = /** @class */ (function () {
2828
return clodule.sfn('a');
2929
}
3030
clodule.fn = fn;
31-
})(clodule || (clodule = {}));
31+
})(clodule);

tests/baselines/reference/ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ var Point = /** @class */ (function () {
3434
(function (Point) {
3535
function Origin() { return null; } //expected duplicate identifier error
3636
Point.Origin = Origin;
37-
})(Point || (Point = {}));
38-
var A;
37+
})(Point);
38+
var A = A || (A = {});
3939
(function (A) {
4040
var Point = /** @class */ (function () {
4141
function Point(x, y) {
@@ -49,5 +49,5 @@ var A;
4949
(function (Point) {
5050
function Origin() { return ""; } //expected duplicate identifier error
5151
Point.Origin = Origin;
52-
})(Point = A.Point || (A.Point = {}));
53-
})(A || (A = {}));
52+
})(Point);
53+
})(A);

tests/baselines/reference/ClassAndModuleThatMergeWithStaticFunctionAndNonExportedFunctionThatShareAName.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ var Point = /** @class */ (function () {
3333
}());
3434
(function (Point) {
3535
function Origin() { return ""; } // not an error, since not exported
36-
})(Point || (Point = {}));
37-
var A;
36+
})(Point);
37+
var A = A || (A = {});
3838
(function (A) {
3939
var Point = /** @class */ (function () {
4040
function Point(x, y) {
@@ -47,5 +47,5 @@ var A;
4747
A.Point = Point;
4848
(function (Point) {
4949
function Origin() { return ""; } // not an error since not exported
50-
})(Point = A.Point || (A.Point = {}));
51-
})(A || (A = {}));
50+
})(Point);
51+
})(A);

tests/baselines/reference/ClassAndModuleThatMergeWithStaticVariableAndExportedVarThatShareAName.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ var Point = /** @class */ (function () {
3333
}());
3434
(function (Point) {
3535
Point.Origin = ""; //expected duplicate identifier error
36-
})(Point || (Point = {}));
37-
var A;
36+
})(Point);
37+
var A = A || (A = {});
3838
(function (A) {
3939
var Point = /** @class */ (function () {
4040
function Point(x, y) {
@@ -47,5 +47,5 @@ var A;
4747
A.Point = Point;
4848
(function (Point) {
4949
Point.Origin = ""; //expected duplicate identifier error
50-
})(Point = A.Point || (A.Point = {}));
51-
})(A || (A = {}));
50+
})(Point);
51+
})(A);

tests/baselines/reference/ClassAndModuleThatMergeWithStaticVariableAndNonExportedVarThatShareAName.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ var Point = /** @class */ (function () {
3333
}());
3434
(function (Point) {
3535
var Origin = ""; // not an error, since not exported
36-
})(Point || (Point = {}));
37-
var A;
36+
})(Point);
37+
var A = A || (A = {});
3838
(function (A) {
3939
var Point = /** @class */ (function () {
4040
function Point(x, y) {
@@ -47,5 +47,5 @@ var A;
4747
A.Point = Point;
4848
(function (Point) {
4949
var Origin = ""; // not an error since not exported
50-
})(Point = A.Point || (A.Point = {}));
51-
})(A || (A = {}));
50+
})(Point);
51+
})(A);

0 commit comments

Comments
 (0)