Skip to content

Commit fc1528f

Browse files
committed
Dts for export * from "mod" and export { a, b as c,...} [from "mod"]
1 parent 5b6a9a8 commit fc1528f

20 files changed

+651
-5
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,12 +1723,18 @@ module ts {
17231723
}
17241724

17251725
function setDeclarationsOfIdentifierAsVisible(node: Identifier): Node[]{
1726+
var exportSymbol: Symbol;
17261727
if (node.parent && node.parent.kind === SyntaxKind.ExportAssignment) {
1727-
var exportSymbol = resolveName(node.parent, node.text, SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace, Diagnostics.Cannot_find_name_0, node);
1728-
var result: Node[] = [];
1728+
exportSymbol = resolveName(node.parent, node.text, SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace, Diagnostics.Cannot_find_name_0, node);
1729+
}
1730+
else if (node.parent.kind === SyntaxKind.ExportSpecifier) {
1731+
exportSymbol = getTargetOfExportSpecifier(<ExportSpecifier>node.parent);
1732+
}
1733+
var result: Node[] = [];
1734+
if (exportSymbol) {
17291735
buildVisibleNodeList(exportSymbol.declarations);
1730-
return result;
17311736
}
1737+
return result;
17321738

17331739
function buildVisibleNodeList(declarations: Declaration[]) {
17341740
forEach(declarations, declaration => {

src/compiler/emitter.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ module ts {
905905
}
906906
else {
907907
write("{ ");
908-
emitCommaList((<NamedImports>node.importClause.namedBindings).elements, emitImportSpecifier, resolver.isDeclarationVisible);
908+
emitCommaList((<NamedImports>node.importClause.namedBindings).elements, emitImportOrExportSpecifier, resolver.isDeclarationVisible);
909909
write(" }");
910910
}
911911
}
@@ -916,14 +916,43 @@ module ts {
916916
writer.writeLine();
917917
}
918918

919-
function emitImportSpecifier(node: ImportSpecifier) {
919+
function emitImportOrExportSpecifier(node: ImportOrExportSpecifier) {
920920
if (node.propertyName) {
921921
writeTextOfNode(currentSourceFile, node.propertyName);
922922
write(" as ");
923923
}
924924
writeTextOfNode(currentSourceFile, node.name);
925925
}
926926

927+
function emitExportSpecifier(node: ExportSpecifier) {
928+
emitImportOrExportSpecifier(node);
929+
930+
// Make all the declarations visible for the export name
931+
var nodes = resolver.setDeclarationsOfIdentifierAsVisible(node.propertyName || node.name);
932+
933+
// write each of these declarations asynchronously
934+
writeAsynchronousModuleElements(nodes);
935+
}
936+
937+
function emitExportDeclaration(node: ExportDeclaration) {
938+
emitJsDocComments(node);
939+
write("export ");
940+
if (node.exportClause) {
941+
write("{ ");
942+
emitCommaList(node.exportClause.elements, emitExportSpecifier);
943+
write(" }");
944+
}
945+
else {
946+
write("*");
947+
}
948+
if (node.moduleSpecifier) {
949+
write(" from ");
950+
writeTextOfNode(currentSourceFile, node.moduleSpecifier);
951+
}
952+
write(";");
953+
writer.writeLine();
954+
}
955+
927956
function writeModuleDeclaration(node: ModuleDeclaration) {
928957
emitJsDocComments(node);
929958
emitModuleElementDeclarationFlags(node);
@@ -1608,6 +1637,8 @@ module ts {
16081637
case SyntaxKind.ImportDeclaration:
16091638
// Import declaration without import clause is visible, otherwise it is not visible
16101639
return emitModuleElement(node, /*isModuleElementVisible*/!(<ImportDeclaration>node).importClause);
1640+
case SyntaxKind.ExportDeclaration:
1641+
return emitExportDeclaration(<ExportDeclaration>node);
16111642
case SyntaxKind.Constructor:
16121643
case SyntaxKind.MethodDeclaration:
16131644
case SyntaxKind.MethodSignature:
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
tests/cases/compiler/server.ts(2,14): error TS1148: Cannot compile external modules unless the '--module' flag is provided.
2+
3+
4+
==== tests/cases/compiler/server.ts (1 errors) ====
5+
6+
export class c {
7+
~
8+
!!! error TS1148: Cannot compile external modules unless the '--module' flag is provided.
9+
}
10+
export interface i {
11+
}
12+
export module m {
13+
export var x = 10;
14+
}
15+
export var x = 10;
16+
export module uninstantiated {
17+
}
18+
19+
==== tests/cases/compiler/client.ts (0 errors) ====
20+
export * from "server";
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//// [tests/cases/compiler/es6ExportAll.ts] ////
2+
3+
//// [server.ts]
4+
5+
export class c {
6+
}
7+
export interface i {
8+
}
9+
export module m {
10+
export var x = 10;
11+
}
12+
export var x = 10;
13+
export module uninstantiated {
14+
}
15+
16+
//// [client.ts]
17+
export * from "server";
18+
19+
//// [server.js]
20+
var c = (function () {
21+
function c() {
22+
}
23+
return c;
24+
})();
25+
exports.c = c;
26+
var m;
27+
(function (m) {
28+
m.x = 10;
29+
})(m = exports.m || (exports.m = {}));
30+
exports.x = 10;
31+
//// [client.js]
32+
var _server = require("server");
33+
for (var _a in _server) if (!exports.hasOwnProperty(_a)) exports[_a] = _server[_a];
34+
35+
36+
//// [server.d.ts]
37+
export declare class c {
38+
}
39+
export interface i {
40+
}
41+
export declare module m {
42+
var x: number;
43+
}
44+
export declare var x: number;
45+
export declare module uninstantiated {
46+
}
47+
//// [client.d.ts]
48+
export * from "server";
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//// [tests/cases/compiler/es6ExportAllInEs5.ts] ////
2+
3+
//// [server.ts]
4+
5+
export class c {
6+
}
7+
export interface i {
8+
}
9+
export module m {
10+
export var x = 10;
11+
}
12+
export var x = 10;
13+
export module uninstantiated {
14+
}
15+
16+
//// [client.ts]
17+
export * from "server";
18+
19+
//// [server.js]
20+
var c = (function () {
21+
function c() {
22+
}
23+
return c;
24+
})();
25+
exports.c = c;
26+
var m;
27+
(function (m) {
28+
m.x = 10;
29+
})(m = exports.m || (exports.m = {}));
30+
exports.x = 10;
31+
//// [client.js]
32+
var _server = require("server");
33+
for (var _a in _server) if (!exports.hasOwnProperty(_a)) exports[_a] = _server[_a];
34+
35+
36+
//// [server.d.ts]
37+
export declare class c {
38+
}
39+
export interface i {
40+
}
41+
export declare module m {
42+
var x: number;
43+
}
44+
export declare var x: number;
45+
export declare module uninstantiated {
46+
}
47+
//// [client.d.ts]
48+
export * from "server";
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== tests/cases/compiler/server.ts ===
2+
3+
export class c {
4+
>c : c
5+
}
6+
export interface i {
7+
>i : i
8+
}
9+
export module m {
10+
>m : typeof m
11+
12+
export var x = 10;
13+
>x : number
14+
}
15+
export var x = 10;
16+
>x : number
17+
18+
export module uninstantiated {
19+
>uninstantiated : unknown
20+
}
21+
22+
=== tests/cases/compiler/client.ts ===
23+
export * from "server";
24+
No type information for this code.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
tests/cases/compiler/es6ExportClause.ts(12,1): error TS1148: Cannot compile external modules unless the '--module' flag is provided.
2+
3+
4+
==== tests/cases/compiler/es6ExportClause.ts (1 errors) ====
5+
6+
class c {
7+
}
8+
interface i {
9+
}
10+
module m {
11+
export var x = 10;
12+
}
13+
var x = 10;
14+
module uninstantiated {
15+
}
16+
export { c };
17+
~~~~~~~~~~~~~
18+
!!! error TS1148: Cannot compile external modules unless the '--module' flag is provided.
19+
export { c as c2 };
20+
export { i, m as instantiatedModule };
21+
export { uninstantiated };
22+
export { x };
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//// [es6ExportClause.ts]
2+
3+
class c {
4+
}
5+
interface i {
6+
}
7+
module m {
8+
export var x = 10;
9+
}
10+
var x = 10;
11+
module uninstantiated {
12+
}
13+
export { c };
14+
export { c as c2 };
15+
export { i, m as instantiatedModule };
16+
export { uninstantiated };
17+
export { x };
18+
19+
//// [es6ExportClause.js]
20+
var c = (function () {
21+
function c() {
22+
}
23+
return c;
24+
})();
25+
var m;
26+
(function (m) {
27+
m.x = 10;
28+
})(m || (m = {}));
29+
var x = 10;
30+
31+
32+
//// [es6ExportClause.d.ts]
33+
declare class c {
34+
}
35+
interface i {
36+
}
37+
declare module m {
38+
var x: number;
39+
}
40+
declare var x: number;
41+
declare module uninstantiated {
42+
}
43+
export { c };
44+
export { c as c2 };
45+
export { i, m as instantiatedModule };
46+
export { uninstantiated };
47+
export { x };
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//// [es6ExportClauseInEs5.ts]
2+
3+
class c {
4+
}
5+
interface i {
6+
}
7+
module m {
8+
export var x = 10;
9+
}
10+
var x = 10;
11+
module uninstantiated {
12+
}
13+
export { c };
14+
export { c as c2 };
15+
export { i, m as instantiatedModule };
16+
export { uninstantiated };
17+
export { x };
18+
19+
//// [es6ExportClauseInEs5.js]
20+
var c = (function () {
21+
function c() {
22+
}
23+
return c;
24+
})();
25+
exports.c = c;
26+
exports.c2 = c;
27+
var m;
28+
(function (m) {
29+
m.x = 10;
30+
})(m || (m = {}));
31+
exports.instantiatedModule = m;
32+
var x = 10;
33+
exports.x = x;
34+
35+
36+
//// [es6ExportClauseInEs5.d.ts]
37+
declare class c {
38+
}
39+
interface i {
40+
}
41+
declare module m {
42+
var x: number;
43+
}
44+
declare var x: number;
45+
declare module uninstantiated {
46+
}
47+
export { c };
48+
export { c as c2 };
49+
export { i, m as instantiatedModule };
50+
export { uninstantiated };
51+
export { x };
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
=== tests/cases/compiler/es6ExportClauseInEs5.ts ===
2+
3+
class c {
4+
>c : c
5+
}
6+
interface i {
7+
>i : i
8+
}
9+
module m {
10+
>m : typeof m
11+
12+
export var x = 10;
13+
>x : number
14+
}
15+
var x = 10;
16+
>x : number
17+
18+
module uninstantiated {
19+
>uninstantiated : unknown
20+
}
21+
export { c };
22+
>c : typeof c
23+
24+
export { c as c2 };
25+
>c : unknown
26+
>c2 : typeof c
27+
28+
export { i, m as instantiatedModule };
29+
>i : unknown
30+
>m : unknown
31+
>instantiatedModule : typeof m
32+
33+
export { uninstantiated };
34+
>uninstantiated : unknown
35+
36+
export { x };
37+
>x : number
38+

0 commit comments

Comments
 (0)