Skip to content

Commit a80f60c

Browse files
authored
fix export * as default syntax (microsoft#39803)
* fix export * as default syntax * update comments
1 parent 1ec71f0 commit a80f60c

24 files changed

+1137
-4
lines changed

src/compiler/factory/utilities.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ namespace ts {
490490
*/
491491
export function getLocalNameForExternalImport(factory: NodeFactory, node: ImportDeclaration | ExportDeclaration | ImportEqualsDeclaration, sourceFile: SourceFile): Identifier | undefined {
492492
const namespaceDeclaration = getNamespaceDeclarationNode(node);
493-
if (namespaceDeclaration && !isDefaultImport(node)) {
493+
if (namespaceDeclaration && !isDefaultImport(node) && !isExportNamespaceAsDefaultDeclaration(node)) {
494494
const name = namespaceDeclaration.name;
495495
return isGeneratedIdentifier(name) ? name : factory.createIdentifier(getSourceTextOfNodeFromSourceFile(sourceFile, name) || idText(name));
496496
}

src/compiler/parser.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6996,7 +6996,7 @@ namespace ts {
69966996
}
69976997

69986998
function parseNamespaceExport(pos: number): NamespaceExport {
6999-
return finishNode(factory.createNamespaceExport(parseIdentifier()), pos);
6999+
return finishNode(factory.createNamespaceExport(parseIdentifierName()), pos);
70007000
}
70017001

70027002
function parseExportDeclaration(pos: number, hasJSDoc: boolean, decorators: NodeArray<Decorator> | undefined, modifiers: NodeArray<Modifier> | undefined): ExportDeclaration {

src/compiler/transformers/module/esnextAnd2015.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ namespace ts {
9999
);
100100
setOriginalNode(importDecl, node.exportClause);
101101

102-
const exportDecl = factory.createExportDeclaration(
102+
const exportDecl = isExportNamespaceAsDefaultDeclaration(node) ? factory.createExportDefault(synthName) : factory.createExportDeclaration(
103103
/*decorators*/ undefined,
104104
/*modifiers*/ undefined,
105105
/*isTypeOnly*/ false,

src/compiler/transformers/module/module.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,7 @@ namespace ts {
10431043
else if (node.exportClause) {
10441044
const statements: Statement[] = [];
10451045
// export * as ns from "mod";
1046+
// export * as default from "mod";
10461047
statements.push(
10471048
setOriginalNode(
10481049
setTextRange(
@@ -1051,7 +1052,8 @@ namespace ts {
10511052
factory.cloneNode(node.exportClause.name),
10521053
getHelperExpressionForExport(node, moduleKind !== ModuleKind.AMD ?
10531054
createRequireCall(node) :
1054-
factory.createIdentifier(idText(node.exportClause.name)))
1055+
isExportNamespaceAsDefaultDeclaration(node) ? generatedName :
1056+
factory.createIdentifier(idText(node.exportClause.name)))
10551057
)
10561058
),
10571059
node

src/compiler/utilities.ts

+4
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,10 @@ namespace ts {
519519
return !!findAncestor(node, isJSDocTypeExpression);
520520
}
521521

522+
export function isExportNamespaceAsDefaultDeclaration(node: Node): boolean {
523+
return !!(isExportDeclaration(node) && node.exportClause && isNamespaceExport(node.exportClause) && node.exportClause.name.escapedText === "default");
524+
}
525+
522526
export function getTextOfNodeFromSourceText(sourceText: string, node: Node, includeTrivia = false): string {
523527
if (nodeIsMissing(node)) {
524528
return "";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
//// [tests/cases/conformance/es2020/modules/exportAsNamespace4.ts] ////
2+
3+
//// [0.ts]
4+
export const a = 1;
5+
export const b = 2;
6+
7+
//// [1.ts]
8+
export * as default from './0';
9+
10+
//// [11.ts]
11+
import * as ns from './0';
12+
export default ns;
13+
14+
//// [2.ts]
15+
import foo from './1'
16+
import foo1 from './11'
17+
18+
foo.a;
19+
foo1.a;
20+
21+
foo.b;
22+
foo1.b;
23+
24+
//// [0.js]
25+
define(["require", "exports"], function (require, exports) {
26+
"use strict";
27+
exports.__esModule = true;
28+
exports.b = exports.a = void 0;
29+
exports.a = 1;
30+
exports.b = 2;
31+
});
32+
//// [1.js]
33+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
34+
if (k2 === undefined) k2 = k;
35+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
36+
}) : (function(o, m, k, k2) {
37+
if (k2 === undefined) k2 = k;
38+
o[k2] = m[k];
39+
}));
40+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
41+
Object.defineProperty(o, "default", { enumerable: true, value: v });
42+
}) : function(o, v) {
43+
o["default"] = v;
44+
});
45+
var __importStar = (this && this.__importStar) || function (mod) {
46+
if (mod && mod.__esModule) return mod;
47+
var result = {};
48+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
49+
__setModuleDefault(result, mod);
50+
return result;
51+
};
52+
define(["require", "exports", "./0"], function (require, exports, _0_1) {
53+
"use strict";
54+
exports.__esModule = true;
55+
exports["default"] = void 0;
56+
exports["default"] = __importStar(_0_1);
57+
});
58+
//// [11.js]
59+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
60+
if (k2 === undefined) k2 = k;
61+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
62+
}) : (function(o, m, k, k2) {
63+
if (k2 === undefined) k2 = k;
64+
o[k2] = m[k];
65+
}));
66+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
67+
Object.defineProperty(o, "default", { enumerable: true, value: v });
68+
}) : function(o, v) {
69+
o["default"] = v;
70+
});
71+
var __importStar = (this && this.__importStar) || function (mod) {
72+
if (mod && mod.__esModule) return mod;
73+
var result = {};
74+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
75+
__setModuleDefault(result, mod);
76+
return result;
77+
};
78+
define(["require", "exports", "./0"], function (require, exports, ns) {
79+
"use strict";
80+
exports.__esModule = true;
81+
ns = __importStar(ns);
82+
exports["default"] = ns;
83+
});
84+
//// [2.js]
85+
var __importDefault = (this && this.__importDefault) || function (mod) {
86+
return (mod && mod.__esModule) ? mod : { "default": mod };
87+
};
88+
define(["require", "exports", "./1", "./11"], function (require, exports, _1_1, _11_1) {
89+
"use strict";
90+
exports.__esModule = true;
91+
_1_1 = __importDefault(_1_1);
92+
_11_1 = __importDefault(_11_1);
93+
_1_1["default"].a;
94+
_11_1["default"].a;
95+
_1_1["default"].b;
96+
_11_1["default"].b;
97+
});
98+
99+
100+
//// [0.d.ts]
101+
export declare const a = 1;
102+
export declare const b = 2;
103+
//// [1.d.ts]
104+
export * as default from './0';
105+
//// [11.d.ts]
106+
import * as ns from './0';
107+
export default ns;
108+
//// [2.d.ts]
109+
export {};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
=== tests/cases/conformance/es2020/modules/0.ts ===
2+
export const a = 1;
3+
>a : Symbol(a, Decl(0.ts, 0, 12))
4+
5+
export const b = 2;
6+
>b : Symbol(b, Decl(0.ts, 1, 12))
7+
8+
=== tests/cases/conformance/es2020/modules/1.ts ===
9+
export * as default from './0';
10+
>default : Symbol(default, Decl(1.ts, 0, 6))
11+
12+
=== tests/cases/conformance/es2020/modules/11.ts ===
13+
import * as ns from './0';
14+
>ns : Symbol(ns, Decl(11.ts, 0, 6))
15+
16+
export default ns;
17+
>ns : Symbol(ns, Decl(11.ts, 0, 6))
18+
19+
=== tests/cases/conformance/es2020/modules/2.ts ===
20+
import foo from './1'
21+
>foo : Symbol(foo, Decl(2.ts, 0, 6))
22+
23+
import foo1 from './11'
24+
>foo1 : Symbol(foo1, Decl(2.ts, 1, 6))
25+
26+
foo.a;
27+
>foo.a : Symbol(foo.a, Decl(0.ts, 0, 12))
28+
>foo : Symbol(foo, Decl(2.ts, 0, 6))
29+
>a : Symbol(foo.a, Decl(0.ts, 0, 12))
30+
31+
foo1.a;
32+
>foo1.a : Symbol(foo.a, Decl(0.ts, 0, 12))
33+
>foo1 : Symbol(foo1, Decl(2.ts, 1, 6))
34+
>a : Symbol(foo.a, Decl(0.ts, 0, 12))
35+
36+
foo.b;
37+
>foo.b : Symbol(foo.b, Decl(0.ts, 1, 12))
38+
>foo : Symbol(foo, Decl(2.ts, 0, 6))
39+
>b : Symbol(foo.b, Decl(0.ts, 1, 12))
40+
41+
foo1.b;
42+
>foo1.b : Symbol(foo.b, Decl(0.ts, 1, 12))
43+
>foo1 : Symbol(foo1, Decl(2.ts, 1, 6))
44+
>b : Symbol(foo.b, Decl(0.ts, 1, 12))
45+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
=== tests/cases/conformance/es2020/modules/0.ts ===
2+
export const a = 1;
3+
>a : 1
4+
>1 : 1
5+
6+
export const b = 2;
7+
>b : 2
8+
>2 : 2
9+
10+
=== tests/cases/conformance/es2020/modules/1.ts ===
11+
export * as default from './0';
12+
>default : typeof import("tests/cases/conformance/es2020/modules/0")
13+
14+
=== tests/cases/conformance/es2020/modules/11.ts ===
15+
import * as ns from './0';
16+
>ns : typeof ns
17+
18+
export default ns;
19+
>ns : typeof ns
20+
21+
=== tests/cases/conformance/es2020/modules/2.ts ===
22+
import foo from './1'
23+
>foo : typeof foo
24+
25+
import foo1 from './11'
26+
>foo1 : typeof foo
27+
28+
foo.a;
29+
>foo.a : 1
30+
>foo : typeof foo
31+
>a : 1
32+
33+
foo1.a;
34+
>foo1.a : 1
35+
>foo1 : typeof foo
36+
>a : 1
37+
38+
foo.b;
39+
>foo.b : 2
40+
>foo : typeof foo
41+
>b : 2
42+
43+
foo1.b;
44+
>foo1.b : 2
45+
>foo1 : typeof foo
46+
>b : 2
47+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
//// [tests/cases/conformance/es2020/modules/exportAsNamespace4.ts] ////
2+
3+
//// [0.ts]
4+
export const a = 1;
5+
export const b = 2;
6+
7+
//// [1.ts]
8+
export * as default from './0';
9+
10+
//// [11.ts]
11+
import * as ns from './0';
12+
export default ns;
13+
14+
//// [2.ts]
15+
import foo from './1'
16+
import foo1 from './11'
17+
18+
foo.a;
19+
foo1.a;
20+
21+
foo.b;
22+
foo1.b;
23+
24+
//// [0.js]
25+
"use strict";
26+
exports.__esModule = true;
27+
exports.b = exports.a = void 0;
28+
exports.a = 1;
29+
exports.b = 2;
30+
//// [1.js]
31+
"use strict";
32+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
33+
if (k2 === undefined) k2 = k;
34+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
35+
}) : (function(o, m, k, k2) {
36+
if (k2 === undefined) k2 = k;
37+
o[k2] = m[k];
38+
}));
39+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
40+
Object.defineProperty(o, "default", { enumerable: true, value: v });
41+
}) : function(o, v) {
42+
o["default"] = v;
43+
});
44+
var __importStar = (this && this.__importStar) || function (mod) {
45+
if (mod && mod.__esModule) return mod;
46+
var result = {};
47+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
48+
__setModuleDefault(result, mod);
49+
return result;
50+
};
51+
exports.__esModule = true;
52+
exports["default"] = void 0;
53+
exports["default"] = __importStar(require("./0"));
54+
//// [11.js]
55+
"use strict";
56+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
57+
if (k2 === undefined) k2 = k;
58+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
59+
}) : (function(o, m, k, k2) {
60+
if (k2 === undefined) k2 = k;
61+
o[k2] = m[k];
62+
}));
63+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
64+
Object.defineProperty(o, "default", { enumerable: true, value: v });
65+
}) : function(o, v) {
66+
o["default"] = v;
67+
});
68+
var __importStar = (this && this.__importStar) || function (mod) {
69+
if (mod && mod.__esModule) return mod;
70+
var result = {};
71+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
72+
__setModuleDefault(result, mod);
73+
return result;
74+
};
75+
exports.__esModule = true;
76+
var ns = __importStar(require("./0"));
77+
exports["default"] = ns;
78+
//// [2.js]
79+
"use strict";
80+
var __importDefault = (this && this.__importDefault) || function (mod) {
81+
return (mod && mod.__esModule) ? mod : { "default": mod };
82+
};
83+
exports.__esModule = true;
84+
var _1_1 = __importDefault(require("./1"));
85+
var _11_1 = __importDefault(require("./11"));
86+
_1_1["default"].a;
87+
_11_1["default"].a;
88+
_1_1["default"].b;
89+
_11_1["default"].b;
90+
91+
92+
//// [0.d.ts]
93+
export declare const a = 1;
94+
export declare const b = 2;
95+
//// [1.d.ts]
96+
export * as default from './0';
97+
//// [11.d.ts]
98+
import * as ns from './0';
99+
export default ns;
100+
//// [2.d.ts]
101+
export {};

0 commit comments

Comments
 (0)