Skip to content

Commit a0e3747

Browse files
committed
Modified ts.ts to handle export defaults enum/namespace correctly.
1 parent 5113249 commit a0e3747

File tree

1,361 files changed

+8892
-8810
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,361 files changed

+8892
-8810
lines changed

src/compiler/transformers/ts.ts

Lines changed: 67 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ namespace ts {
333333
if (modifierToFlag(node.kind) & ModifierFlags.TypeScriptModifier) {
334334
return undefined;
335335
}
336-
else if (currentNamespace && node.kind === SyntaxKind.ExportKeyword) {
336+
else if (node.kind === SyntaxKind.ExportKeyword || node.kind === SyntaxKind.DefaultKeyword) {
337337
return undefined;
338338
}
339339

@@ -2594,29 +2594,7 @@ namespace ts {
25942594
// `containerName` is the expression used inside of the enum for assignments.
25952595
const containerName = getNamespaceContainerName(node);
25962596

2597-
// `exportName` is the expression used within this node's container for any exported references.
2598-
const exportName = hasModifier(node, ModifierFlags.Export)
2599-
? getExternalModuleOrNamespaceExportName(currentNamespaceContainerName, node, /*allowComments*/ false, /*allowSourceMaps*/ true)
2600-
: getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true);
2601-
2602-
// x || (x = {})
2603-
// exports.x || (exports.x = {})
2604-
let moduleArg =
2605-
createLogicalOr(
2606-
exportName,
2607-
createAssignment(
2608-
exportName,
2609-
createObjectLiteral()
2610-
)
2611-
);
2612-
2613-
if (hasNamespaceQualifiedExportName(node)) {
2614-
// `localName` is the expression used within this node's containing scope for any local references.
2615-
const localName = getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true);
2616-
2617-
// x = (exports.x || (exports.x = {}))
2618-
moduleArg = createAssignment(localName, moduleArg);
2619-
}
2597+
const localName = getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true);
26202598

26212599
// (function (x) {
26222600
// x[x["y"] = 0] = "y";
@@ -2634,18 +2612,19 @@ namespace ts {
26342612
transformEnumBody(node, containerName)
26352613
),
26362614
/*typeArguments*/ undefined,
2637-
[moduleArg]
2615+
[localName]
26382616
)
26392617
);
26402618

26412619
setOriginalNode(enumStatement, node);
26422620
setTextRange(enumStatement, node);
2621+
setCommentRange(enumStatement, node);
26432622
setEmitFlags(enumStatement, emitFlags);
26442623
statements.push(enumStatement);
26452624

26462625
// Add a DeclarationMarker for the enum to preserve trailing comments and mark
26472626
// the end of the declaration.
2648-
statements.push(createEndOfDeclarationMarker(node));
2627+
// statements.push(createEndOfDeclarationMarker(node));
26492628
return statements;
26502629
}
26512630

@@ -2738,18 +2717,6 @@ namespace ts {
27382717
return isInstantiatedModule(node, compilerOptions.preserveConstEnums || compilerOptions.isolatedModules);
27392718
}
27402719

2741-
/**
2742-
* Determines whether an exported declaration will have a qualified export name (e.g. `f.x`
2743-
* or `exports.x`).
2744-
*/
2745-
function hasNamespaceQualifiedExportName(node: Node) {
2746-
return isExportOfNamespace(node)
2747-
|| (isExternalModuleExport(node)
2748-
&& moduleKind !== ModuleKind.ES2015
2749-
&& moduleKind !== ModuleKind.ESNext
2750-
&& moduleKind !== ModuleKind.System);
2751-
}
2752-
27532720
/**
27542721
* Records that a declaration was emitted in the current scope, if it was the first
27552722
* declaration for the provided symbol.
@@ -2786,22 +2753,58 @@ namespace ts {
27862753
* Adds a leading VariableStatement for a enum or module declaration.
27872754
*/
27882755
function addVarForEnumOrModuleDeclaration(statements: Statement[], node: ModuleDeclaration | EnumDeclaration) {
2789-
// Emit a variable statement for the module. We emit top-level enums as a `var`
2790-
// declaration to avoid static errors in global scripts scripts due to redeclaration.
2791-
// enums in any other scope are emitted as a `let` declaration.
2792-
const statement = createVariableStatement(
2793-
visitNodes(node.modifiers, modifierVisitor, isModifier),
2794-
createVariableDeclarationList([
2795-
createVariableDeclaration(
2796-
getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true)
2797-
)
2798-
], currentScope.kind === SyntaxKind.SourceFile ? NodeFlags.None : NodeFlags.Let)
2799-
);
2800-
2801-
setOriginalNode(statement, node);
28022756

28032757
recordEmittedDeclarationInScope(node);
28042758
if (isFirstEmittedDeclarationInScope(node)) {
2759+
// Emit a local variable statement for the module. We emit top-level enums as a `var`
2760+
// declaration to avoid static errors in global scripts scripts due to redeclaration.
2761+
// enums in any other scope are emitted as a `const` declaration.
2762+
const nodeModifierFlags = getModifierFlags(node);
2763+
const nodeIsExportDefault = (nodeModifierFlags & ModifierFlags.Export) && (nodeModifierFlags & ModifierFlags.Default);
2764+
// function modifierVisitor(node: Node): VisitResult<Node> {
2765+
// if (modifierToFlag(node.kind) & ModifierFlags.TypeScriptModifier) {
2766+
// return undefined;
2767+
// }
2768+
// else if ((currentNamespace || nodeIsExportDefault) && node.kind === SyntaxKind.ExportKeyword) {
2769+
// return undefined;
2770+
// }
2771+
// if (node.kind === SyntaxKind.DefaultKeyword) {
2772+
// return undefined;
2773+
// }
2774+
// return node;
2775+
// }
2776+
2777+
const fileIsExternalModule = isExternalModule(currentSourceFile);
2778+
const localName = getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true);
2779+
let varInitializer;
2780+
if (fileIsExternalModule && !currentNamespace) {
2781+
// toplevel declaration in an external module
2782+
// cannot merge with another var
2783+
varInitializer = createObjectLiteral();
2784+
}
2785+
else {
2786+
const exportName = hasModifier(node, ModifierFlags.Export) && !nodeIsExportDefault
2787+
? getExternalModuleOrNamespaceExportName(currentNamespaceContainerName, node, /*allowComments*/ false, /*allowSourceMaps*/ true)
2788+
: getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true);
2789+
varInitializer = createLogicalOr(
2790+
exportName,
2791+
createAssignment(exportName, createObjectLiteral())
2792+
);
2793+
}
2794+
const statement = createVariableStatement(
2795+
visitNodes(node.modifiers, modifierVisitor, isModifier),
2796+
createVariableDeclarationList([
2797+
createVariableDeclaration(
2798+
localName,
2799+
/* type */ undefined,
2800+
varInitializer
2801+
)
2802+
], currentScope.kind === SyntaxKind.SourceFile && !fileIsExternalModule ? NodeFlags.None : NodeFlags.Const)
2803+
);
2804+
2805+
setOriginalNode(statement, node);
2806+
2807+
28052808
// Adjust the source map emit to match the old emitter.
28062809
if (node.kind === SyntaxKind.EnumDeclaration) {
28072810
setSourceMapRange(statement.declarationList, node);
@@ -2817,32 +2820,28 @@ namespace ts {
28172820
// module m1 {
28182821
// function foo4Export() {
28192822
// }
2820-
// } // trailing comment module
2823+
// } // trailing module comment
28212824
//
28222825
// Should emit:
28232826
//
28242827
// /** Module comment*/
2825-
// var m1;
2828+
// var m1 = m1 || {};
28262829
// (function (m1) {
28272830
// function foo4Export() {
28282831
// }
2829-
// })(m1 || (m1 = {})); // trailing comment module
2832+
// })(m1); // trailing module comment
28302833
//
28312834
setCommentRange(statement, node);
2832-
setEmitFlags(statement, EmitFlags.NoTrailingComments | EmitFlags.HasEndOfDeclarationMarker);
2835+
setEmitFlags(statement, EmitFlags.NoTrailingComments);
28332836
statements.push(statement);
2837+
if (nodeIsExportDefault) {
2838+
statements.push(createExportAssignment(/* decorators */ undefined, /* modifiers */ undefined, /* isExportEquals */ false, localName));
2839+
}
2840+
else if (!currentNamespace && nodeModifierFlags & ModifierFlags.Export) {
2841+
statements.push(createExternalModuleExport(localName));
2842+
}
28342843
return true;
28352844
}
2836-
else {
2837-
// For an EnumDeclaration or ModuleDeclaration that merges with a preceeding
2838-
// declaration we do not emit a leading variable declaration. To preserve the
2839-
// begin/end semantics of the declararation and to properly handle exports
2840-
// we wrap the leading variable declaration in a `MergeDeclarationMarker`.
2841-
const mergeMarker = createMergeDeclarationMarker(statement);
2842-
setEmitFlags(mergeMarker, EmitFlags.NoComments | EmitFlags.HasEndOfDeclarationMarker);
2843-
statements.push(mergeMarker);
2844-
return false;
2845-
}
28462845
}
28472846

28482847
/**
@@ -2882,29 +2881,7 @@ namespace ts {
28822881
// `containerName` is the expression used inside of the namespace for exports.
28832882
const containerName = getNamespaceContainerName(node);
28842883

2885-
// `exportName` is the expression used within this node's container for any exported references.
2886-
const exportName = hasModifier(node, ModifierFlags.Export)
2887-
? getExternalModuleOrNamespaceExportName(currentNamespaceContainerName, node, /*allowComments*/ false, /*allowSourceMaps*/ true)
2888-
: getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true);
2889-
2890-
// x || (x = {})
2891-
// exports.x || (exports.x = {})
2892-
let moduleArg =
2893-
createLogicalOr(
2894-
exportName,
2895-
createAssignment(
2896-
exportName,
2897-
createObjectLiteral()
2898-
)
2899-
);
2900-
2901-
if (hasNamespaceQualifiedExportName(node)) {
2902-
// `localName` is the expression used within this node's containing scope for any local references.
2903-
const localName = getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true);
2904-
2905-
// x = (exports.x || (exports.x = {}))
2906-
moduleArg = createAssignment(localName, moduleArg);
2907-
}
2884+
const localName = getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true);
29082885

29092886
// (function (x_1) {
29102887
// x_1.y = ...;
@@ -2921,18 +2898,16 @@ namespace ts {
29212898
transformModuleBody(node, containerName)
29222899
),
29232900
/*typeArguments*/ undefined,
2924-
[moduleArg]
2901+
[localName]
29252902
)
29262903
);
29272904

29282905
setOriginalNode(moduleStatement, node);
2906+
setCommentRange(moduleStatement, node);
29292907
setTextRange(moduleStatement, node);
29302908
setEmitFlags(moduleStatement, emitFlags);
29312909
statements.push(moduleStatement);
29322910

2933-
// Add a DeclarationMarker for the namespace to preserve trailing comments and mark
2934-
// the end of the declaration.
2935-
statements.push(createEndOfDeclarationMarker(node));
29362911
return statements;
29372912
}
29382913

tests/baselines/reference/APISample_compile.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ function compile(fileNames, options) {
6464
console.log("Process exiting with code '" + exitCode + "'.");
6565
process.exit(exitCode);
6666
}
67-
exports.compile = compile;
6867
compile(process.argv.slice(2), {
6968
noEmitOnError: true, noImplicitAny: true,
7069
target: ts.ScriptTarget.ES5, module: ts.ModuleKind.CommonJS

tests/baselines/reference/APISample_parseConfig.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,3 @@ function createProgram(rootFiles, compilerOptionsJson) {
6767
}
6868
return ts.createProgram(rootFiles, settings.options);
6969
}
70-
exports.createProgram = createProgram;

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);

0 commit comments

Comments
 (0)