Skip to content

Commit 29a8fad

Browse files
committed
Change addVarForEnumOrModuleDeclaration to init the var and output an export statement if necessary.
visitEnumDeclaration and visitModuleDeclaration now just pass the local var to the generating function.
1 parent 39b9cc3 commit 29a8fad

7 files changed

+67
-72
lines changed

src/compiler/transformers/ts.ts

Lines changed: 46 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2595,28 +2595,7 @@ namespace ts {
25952595
const containerName = getNamespaceContainerName(node);
25962596

25972597
// `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-
}
2598+
const localName = getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true);
26202599

26212600
// (function (x) {
26222601
// x[x["y"] = 0] = "y";
@@ -2634,7 +2613,7 @@ namespace ts {
26342613
transformEnumBody(node, containerName)
26352614
),
26362615
/*typeArguments*/ undefined,
2637-
[moduleArg]
2616+
[localName]
26382617
)
26392618
);
26402619

@@ -2742,13 +2721,13 @@ namespace ts {
27422721
* Determines whether an exported declaration will have a qualified export name (e.g. `f.x`
27432722
* or `exports.x`).
27442723
*/
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-
}
2724+
// function hasNamespaceQualifiedExportName(node: Node) {
2725+
// return isExportOfNamespace(node)
2726+
// || (isExternalModuleExport(node)
2727+
// && moduleKind !== ModuleKind.ES2015
2728+
// && moduleKind !== ModuleKind.ESNext
2729+
// && moduleKind !== ModuleKind.System);
2730+
// }
27522731

27532732
/**
27542733
* Records that a declaration was emitted in the current scope, if it was the first
@@ -2789,11 +2768,35 @@ namespace ts {
27892768
// Emit a variable statement for the module. We emit top-level enums as a `var`
27902769
// declaration to avoid static errors in global scripts scripts due to redeclaration.
27912770
// enums in any other scope are emitted as a `let` declaration.
2771+
const nodeModifierFlags = getModifierFlags(node);
2772+
const nodeIsExportDefault = (nodeModifierFlags & ModifierFlags.Export) && (nodeModifierFlags & ModifierFlags.Default);
2773+
function modifierVisitor(node: Node): VisitResult<Node> {
2774+
if (modifierToFlag(node.kind) & ModifierFlags.TypeScriptModifier) {
2775+
return undefined;
2776+
}
2777+
else if ((currentNamespace || nodeIsExportDefault) && node.kind === SyntaxKind.ExportKeyword) {
2778+
return undefined;
2779+
}
2780+
if (node.kind == SyntaxKind.DefaultKeyword) {
2781+
return undefined;
2782+
}
2783+
return node;
2784+
}
2785+
const exportName = hasModifier(node, ModifierFlags.Export)
2786+
? getExternalModuleOrNamespaceExportName(currentNamespaceContainerName, node, /*allowComments*/ false, /*allowSourceMaps*/ true)
2787+
: getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true);
2788+
2789+
const localName = getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true);
27922790
const statement = createVariableStatement(
27932791
visitNodes(node.modifiers, modifierVisitor, isModifier),
27942792
createVariableDeclarationList([
27952793
createVariableDeclaration(
2796-
getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true)
2794+
localName,
2795+
undefined,
2796+
createLogicalOr(
2797+
exportName,
2798+
createObjectLiteral()
2799+
)
27972800
)
27982801
], currentScope.kind === SyntaxKind.SourceFile ? NodeFlags.None : NodeFlags.Let)
27992802
);
@@ -2831,6 +2834,16 @@ namespace ts {
28312834
setCommentRange(statement, node);
28322835
setEmitFlags(statement, EmitFlags.NoTrailingComments | EmitFlags.HasEndOfDeclarationMarker);
28332836
statements.push(statement);
2837+
if (nodeIsExportDefault) {
2838+
statements.push(createExportAssignment(undefined, undefined, false, localName));
2839+
}
2840+
if (currentNamespace && (nodeModifierFlags & ModifierFlags.Export)) {
2841+
statements.push(
2842+
createStatement(
2843+
createAssignment(createPropertyAccess(currentNamespaceContainerName, localName), localName)
2844+
)
2845+
);
2846+
}
28342847
return true;
28352848
}
28362849
else {
@@ -2883,28 +2896,7 @@ namespace ts {
28832896
const containerName = getNamespaceContainerName(node);
28842897

28852898
// `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-
}
2899+
const localName = getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true);
29082900

29092901
// (function (x_1) {
29102902
// x_1.y = ...;
@@ -2921,7 +2913,7 @@ namespace ts {
29212913
transformModuleBody(node, containerName)
29222914
),
29232915
/*typeArguments*/ undefined,
2924-
[moduleArg]
2916+
[localName]
29252917
)
29262918
);
29272919

tests/baselines/reference/exportDefaultClassInNamespace.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ namespace ns_abstract_class {
99

1010

1111
//// [exportDefaultClassInNamespace.js]
12-
var ns_class;
12+
var ns_class = ns_class || {};
1313
(function (ns_class) {
1414
var default_1 = /** @class */ (function () {
1515
function default_1() {
1616
}
1717
return default_1;
1818
}());
1919
ns_class.default_1 = default_1;
20-
})(ns_class || (ns_class = {}));
21-
var ns_abstract_class;
20+
})(ns_class);
21+
var ns_abstract_class = ns_abstract_class || {};
2222
(function (ns_abstract_class) {
2323
var default_2 = /** @class */ (function () {
2424
function default_2() {
2525
}
2626
return default_2;
2727
}());
2828
ns_abstract_class.default_2 = default_2;
29-
})(ns_abstract_class || (ns_abstract_class = {}));
29+
})(ns_abstract_class);

tests/baselines/reference/exportDefaultEnum.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ const y = A[x];
1414
"use strict";
1515
exports.__esModule = true;
1616
// https://github.com/Microsoft/TypeScript/issues/3792
17-
var A;
17+
var A = exports.A || {};
18+
exports["default"] = A;
1819
(function (A) {
1920
A[A["FOO"] = 0] = "FOO";
20-
})(A = exports.A || (exports.A = {}));
21+
})(A);
2122
//// [b.js]
2223
"use strict";
2324
exports.__esModule = true;

tests/baselines/reference/exportDefaultEnumTargetES6.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ const y = A[x];
1515
//// [a.js]
1616
// https://github.com/Microsoft/TypeScript/issues/3792
1717
// test if multiple export default enums merge correctly with target=es6
18-
export default var A;
18+
var A = A || {};
19+
export default A;
1920
(function (A) {
2021
A[A["FOO"] = 0] = "FOO";
21-
})(A || (A = {}));
22+
})(A);
2223
(function (A) {
2324
A[A["BAR"] = 1] = "BAR";
24-
})(A || (A = {}));
25+
})(A);
2526
//// [b.js]
2627
import A from './a';
2728
const x = A.FOO;

tests/baselines/reference/exportDefaultFunctionInNamespace.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,17 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
4444
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
4545
}
4646
};
47-
var ns_function;
47+
var ns_function = ns_function || {};
4848
(function (ns_function) {
4949
default function () { }
5050
ns_function.default_1 = default_1;
51-
})(ns_function || (ns_function = {}));
52-
var ns_async_function;
51+
})(ns_function);
52+
var ns_async_function = ns_async_function || {};
5353
(function (ns_async_function) {
5454
default function () {
5555
return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) {
5656
return [2 /*return*/];
5757
}); });
5858
}
5959
ns_async_function.default_2 = default_2;
60-
})(ns_async_function || (ns_async_function = {}));
60+
})(ns_async_function);

tests/baselines/reference/exportDefaultNamespace.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ const x = A.foo();
1717
"use strict";
1818
exports.__esModule = true;
1919
// https://github.com/Microsoft/TypeScript/issues/3792
20-
var N;
20+
var N = exports.N || {};
21+
exports["default"] = N;
2122
(function (N) {
2223
function foo() {
2324
return 0xC0FFEE;
2425
}
2526
N.foo = foo;
26-
})(N = exports.N || (exports.N = {}));
27+
})(N);
2728
//// [b.js]
2829
"use strict";
2930
exports.__esModule = true;

tests/baselines/reference/exportDefaultProperty.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fooLength + 1;
4444
//// [a.js]
4545
"use strict";
4646
exports.__esModule = true;
47-
var A;
47+
var A = A || {};
4848
(function (A) {
4949
var B = /** @class */ (function () {
5050
function B(b) {
@@ -54,8 +54,8 @@ var A;
5454
A.B = B;
5555
(function (B) {
5656
B.b = 0;
57-
})(B = A.B || (A.B = {}));
58-
})(A || (A = {}));
57+
})(B);
58+
})(A);
5959
exports["default"] = A.B;
6060
//// [b.js]
6161
"use strict";

0 commit comments

Comments
 (0)