From fc1528f3e5eeb3dbb59666bd60ef9f50651540b8 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Tue, 17 Feb 2015 09:28:55 -0800 Subject: [PATCH] Dts for export * from "mod" and export { a, b as c,...} [from "mod"] --- src/compiler/checker.ts | 14 ++-- src/compiler/emitter.ts | 35 +++++++++- .../reference/es6ExportAll.errors.txt | 20 ++++++ tests/baselines/reference/es6ExportAll.js | 48 ++++++++++++++ .../baselines/reference/es6ExportAllInEs5.js | 48 ++++++++++++++ .../reference/es6ExportAllInEs5.types | 24 +++++++ .../reference/es6ExportClause.errors.txt | 22 +++++++ tests/baselines/reference/es6ExportClause.js | 47 ++++++++++++++ .../reference/es6ExportClauseInEs5.js | 51 +++++++++++++++ .../reference/es6ExportClauseInEs5.types | 38 +++++++++++ ...ortClauseWithoutModuleSpecifier.errors.txt | 24 +++++++ .../es6ExportClauseWithoutModuleSpecifier.js | 65 +++++++++++++++++++ ...ExportClauseWithoutModuleSpecifierInEs5.js | 65 +++++++++++++++++++ ...ortClauseWithoutModuleSpecifierInEs5.types | 40 ++++++++++++ tests/cases/compiler/es6ExportAll.ts | 17 +++++ tests/cases/compiler/es6ExportAllInEs5.ts | 18 +++++ tests/cases/compiler/es6ExportClause.ts | 19 ++++++ tests/cases/compiler/es6ExportClauseInEs5.ts | 20 ++++++ .../es6ExportClauseWithoutModuleSpecifier.ts | 21 ++++++ ...ExportClauseWithoutModuleSpecifierInEs5.ts | 22 +++++++ 20 files changed, 652 insertions(+), 6 deletions(-) create mode 100644 tests/baselines/reference/es6ExportAll.errors.txt create mode 100644 tests/baselines/reference/es6ExportAll.js create mode 100644 tests/baselines/reference/es6ExportAllInEs5.js create mode 100644 tests/baselines/reference/es6ExportAllInEs5.types create mode 100644 tests/baselines/reference/es6ExportClause.errors.txt create mode 100644 tests/baselines/reference/es6ExportClause.js create mode 100644 tests/baselines/reference/es6ExportClauseInEs5.js create mode 100644 tests/baselines/reference/es6ExportClauseInEs5.types create mode 100644 tests/baselines/reference/es6ExportClauseWithoutModuleSpecifier.errors.txt create mode 100644 tests/baselines/reference/es6ExportClauseWithoutModuleSpecifier.js create mode 100644 tests/baselines/reference/es6ExportClauseWithoutModuleSpecifierInEs5.js create mode 100644 tests/baselines/reference/es6ExportClauseWithoutModuleSpecifierInEs5.types create mode 100644 tests/cases/compiler/es6ExportAll.ts create mode 100644 tests/cases/compiler/es6ExportAllInEs5.ts create mode 100644 tests/cases/compiler/es6ExportClause.ts create mode 100644 tests/cases/compiler/es6ExportClauseInEs5.ts create mode 100644 tests/cases/compiler/es6ExportClauseWithoutModuleSpecifier.ts create mode 100644 tests/cases/compiler/es6ExportClauseWithoutModuleSpecifierInEs5.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index db8c9fc832d..b07c578c671 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1723,12 +1723,18 @@ module ts { } function setDeclarationsOfIdentifierAsVisible(node: Identifier): Node[]{ + var exportSymbol: Symbol; if (node.parent && node.parent.kind === SyntaxKind.ExportAssignment) { - var exportSymbol = resolveName(node.parent, node.text, SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace, Diagnostics.Cannot_find_name_0, node); - var result: Node[] = []; - buildVisibleNodeList(exportSymbol.declarations); - return result; + exportSymbol = resolveName(node.parent, node.text, SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace, Diagnostics.Cannot_find_name_0, node); } + else if (node.parent.kind === SyntaxKind.ExportSpecifier) { + exportSymbol = getTargetOfExportSpecifier(node.parent); + } + var result: Node[] = []; + if (exportSymbol) { + buildVisibleNodeList(exportSymbol.declarations); + } + return result; function buildVisibleNodeList(declarations: Declaration[]) { forEach(declarations, declaration => { diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 9fc10185a04..e94c40cee91 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -905,7 +905,7 @@ module ts { } else { write("{ "); - emitCommaList((node.importClause.namedBindings).elements, emitImportSpecifier, resolver.isDeclarationVisible); + emitCommaList((node.importClause.namedBindings).elements, emitImportOrExportSpecifier, resolver.isDeclarationVisible); write(" }"); } } @@ -916,7 +916,7 @@ module ts { writer.writeLine(); } - function emitImportSpecifier(node: ImportSpecifier) { + function emitImportOrExportSpecifier(node: ImportOrExportSpecifier) { if (node.propertyName) { writeTextOfNode(currentSourceFile, node.propertyName); write(" as "); @@ -924,6 +924,35 @@ module ts { writeTextOfNode(currentSourceFile, node.name); } + function emitExportSpecifier(node: ExportSpecifier) { + emitImportOrExportSpecifier(node); + + // Make all the declarations visible for the export name + var nodes = resolver.setDeclarationsOfIdentifierAsVisible(node.propertyName || node.name); + + // write each of these declarations asynchronously + writeAsynchronousModuleElements(nodes); + } + + function emitExportDeclaration(node: ExportDeclaration) { + emitJsDocComments(node); + write("export "); + if (node.exportClause) { + write("{ "); + emitCommaList(node.exportClause.elements, emitExportSpecifier); + write(" }"); + } + else { + write("*"); + } + if (node.moduleSpecifier) { + write(" from "); + writeTextOfNode(currentSourceFile, node.moduleSpecifier); + } + write(";"); + writer.writeLine(); + } + function writeModuleDeclaration(node: ModuleDeclaration) { emitJsDocComments(node); emitModuleElementDeclarationFlags(node); @@ -1608,6 +1637,8 @@ module ts { case SyntaxKind.ImportDeclaration: // Import declaration without import clause is visible, otherwise it is not visible return emitModuleElement(node, /*isModuleElementVisible*/!(node).importClause); + case SyntaxKind.ExportDeclaration: + return emitExportDeclaration(node); case SyntaxKind.Constructor: case SyntaxKind.MethodDeclaration: case SyntaxKind.MethodSignature: diff --git a/tests/baselines/reference/es6ExportAll.errors.txt b/tests/baselines/reference/es6ExportAll.errors.txt new file mode 100644 index 00000000000..98d40006854 --- /dev/null +++ b/tests/baselines/reference/es6ExportAll.errors.txt @@ -0,0 +1,20 @@ +tests/cases/compiler/server.ts(2,14): error TS1148: Cannot compile external modules unless the '--module' flag is provided. + + +==== tests/cases/compiler/server.ts (1 errors) ==== + + export class c { + ~ +!!! error TS1148: Cannot compile external modules unless the '--module' flag is provided. + } + export interface i { + } + export module m { + export var x = 10; + } + export var x = 10; + export module uninstantiated { + } + +==== tests/cases/compiler/client.ts (0 errors) ==== + export * from "server"; \ No newline at end of file diff --git a/tests/baselines/reference/es6ExportAll.js b/tests/baselines/reference/es6ExportAll.js new file mode 100644 index 00000000000..7b99a3819e2 --- /dev/null +++ b/tests/baselines/reference/es6ExportAll.js @@ -0,0 +1,48 @@ +//// [tests/cases/compiler/es6ExportAll.ts] //// + +//// [server.ts] + +export class c { +} +export interface i { +} +export module m { + export var x = 10; +} +export var x = 10; +export module uninstantiated { +} + +//// [client.ts] +export * from "server"; + +//// [server.js] +var c = (function () { + function c() { + } + return c; +})(); +exports.c = c; +var m; +(function (m) { + m.x = 10; +})(m = exports.m || (exports.m = {})); +exports.x = 10; +//// [client.js] +var _server = require("server"); +for (var _a in _server) if (!exports.hasOwnProperty(_a)) exports[_a] = _server[_a]; + + +//// [server.d.ts] +export declare class c { +} +export interface i { +} +export declare module m { + var x: number; +} +export declare var x: number; +export declare module uninstantiated { +} +//// [client.d.ts] +export * from "server"; diff --git a/tests/baselines/reference/es6ExportAllInEs5.js b/tests/baselines/reference/es6ExportAllInEs5.js new file mode 100644 index 00000000000..dd83c31b72a --- /dev/null +++ b/tests/baselines/reference/es6ExportAllInEs5.js @@ -0,0 +1,48 @@ +//// [tests/cases/compiler/es6ExportAllInEs5.ts] //// + +//// [server.ts] + +export class c { +} +export interface i { +} +export module m { + export var x = 10; +} +export var x = 10; +export module uninstantiated { +} + +//// [client.ts] +export * from "server"; + +//// [server.js] +var c = (function () { + function c() { + } + return c; +})(); +exports.c = c; +var m; +(function (m) { + m.x = 10; +})(m = exports.m || (exports.m = {})); +exports.x = 10; +//// [client.js] +var _server = require("server"); +for (var _a in _server) if (!exports.hasOwnProperty(_a)) exports[_a] = _server[_a]; + + +//// [server.d.ts] +export declare class c { +} +export interface i { +} +export declare module m { + var x: number; +} +export declare var x: number; +export declare module uninstantiated { +} +//// [client.d.ts] +export * from "server"; diff --git a/tests/baselines/reference/es6ExportAllInEs5.types b/tests/baselines/reference/es6ExportAllInEs5.types new file mode 100644 index 00000000000..99e8fde9d40 --- /dev/null +++ b/tests/baselines/reference/es6ExportAllInEs5.types @@ -0,0 +1,24 @@ +=== tests/cases/compiler/server.ts === + +export class c { +>c : c +} +export interface i { +>i : i +} +export module m { +>m : typeof m + + export var x = 10; +>x : number +} +export var x = 10; +>x : number + +export module uninstantiated { +>uninstantiated : unknown +} + +=== tests/cases/compiler/client.ts === +export * from "server"; +No type information for this code. \ No newline at end of file diff --git a/tests/baselines/reference/es6ExportClause.errors.txt b/tests/baselines/reference/es6ExportClause.errors.txt new file mode 100644 index 00000000000..464839e03a4 --- /dev/null +++ b/tests/baselines/reference/es6ExportClause.errors.txt @@ -0,0 +1,22 @@ +tests/cases/compiler/es6ExportClause.ts(12,1): error TS1148: Cannot compile external modules unless the '--module' flag is provided. + + +==== tests/cases/compiler/es6ExportClause.ts (1 errors) ==== + + class c { + } + interface i { + } + module m { + export var x = 10; + } + var x = 10; + module uninstantiated { + } + export { c }; + ~~~~~~~~~~~~~ +!!! error TS1148: Cannot compile external modules unless the '--module' flag is provided. + export { c as c2 }; + export { i, m as instantiatedModule }; + export { uninstantiated }; + export { x }; \ No newline at end of file diff --git a/tests/baselines/reference/es6ExportClause.js b/tests/baselines/reference/es6ExportClause.js new file mode 100644 index 00000000000..27fc611692d --- /dev/null +++ b/tests/baselines/reference/es6ExportClause.js @@ -0,0 +1,47 @@ +//// [es6ExportClause.ts] + +class c { +} +interface i { +} +module m { + export var x = 10; +} +var x = 10; +module uninstantiated { +} +export { c }; +export { c as c2 }; +export { i, m as instantiatedModule }; +export { uninstantiated }; +export { x }; + +//// [es6ExportClause.js] +var c = (function () { + function c() { + } + return c; +})(); +var m; +(function (m) { + m.x = 10; +})(m || (m = {})); +var x = 10; + + +//// [es6ExportClause.d.ts] +declare class c { +} +interface i { +} +declare module m { + var x: number; +} +declare var x: number; +declare module uninstantiated { +} +export { c }; +export { c as c2 }; +export { i, m as instantiatedModule }; +export { uninstantiated }; +export { x }; diff --git a/tests/baselines/reference/es6ExportClauseInEs5.js b/tests/baselines/reference/es6ExportClauseInEs5.js new file mode 100644 index 00000000000..f987b48aeca --- /dev/null +++ b/tests/baselines/reference/es6ExportClauseInEs5.js @@ -0,0 +1,51 @@ +//// [es6ExportClauseInEs5.ts] + +class c { +} +interface i { +} +module m { + export var x = 10; +} +var x = 10; +module uninstantiated { +} +export { c }; +export { c as c2 }; +export { i, m as instantiatedModule }; +export { uninstantiated }; +export { x }; + +//// [es6ExportClauseInEs5.js] +var c = (function () { + function c() { + } + return c; +})(); +exports.c = c; +exports.c2 = c; +var m; +(function (m) { + m.x = 10; +})(m || (m = {})); +exports.instantiatedModule = m; +var x = 10; +exports.x = x; + + +//// [es6ExportClauseInEs5.d.ts] +declare class c { +} +interface i { +} +declare module m { + var x: number; +} +declare var x: number; +declare module uninstantiated { +} +export { c }; +export { c as c2 }; +export { i, m as instantiatedModule }; +export { uninstantiated }; +export { x }; diff --git a/tests/baselines/reference/es6ExportClauseInEs5.types b/tests/baselines/reference/es6ExportClauseInEs5.types new file mode 100644 index 00000000000..667a021466b --- /dev/null +++ b/tests/baselines/reference/es6ExportClauseInEs5.types @@ -0,0 +1,38 @@ +=== tests/cases/compiler/es6ExportClauseInEs5.ts === + +class c { +>c : c +} +interface i { +>i : i +} +module m { +>m : typeof m + + export var x = 10; +>x : number +} +var x = 10; +>x : number + +module uninstantiated { +>uninstantiated : unknown +} +export { c }; +>c : typeof c + +export { c as c2 }; +>c : unknown +>c2 : typeof c + +export { i, m as instantiatedModule }; +>i : unknown +>m : unknown +>instantiatedModule : typeof m + +export { uninstantiated }; +>uninstantiated : unknown + +export { x }; +>x : number + diff --git a/tests/baselines/reference/es6ExportClauseWithoutModuleSpecifier.errors.txt b/tests/baselines/reference/es6ExportClauseWithoutModuleSpecifier.errors.txt new file mode 100644 index 00000000000..7ac6946aedf --- /dev/null +++ b/tests/baselines/reference/es6ExportClauseWithoutModuleSpecifier.errors.txt @@ -0,0 +1,24 @@ +tests/cases/compiler/server.ts(2,14): error TS1148: Cannot compile external modules unless the '--module' flag is provided. + + +==== tests/cases/compiler/server.ts (1 errors) ==== + + export class c { + ~ +!!! error TS1148: Cannot compile external modules unless the '--module' flag is provided. + } + export interface i { + } + export module m { + export var x = 10; + } + export var x = 10; + export module uninstantiated { + } + +==== tests/cases/compiler/client.ts (0 errors) ==== + export { c } from "server"; + export { c as c2 } from "server"; + export { i, m as instantiatedModule } from "server"; + export { uninstantiated } from "server"; + export { x } from "server"; \ No newline at end of file diff --git a/tests/baselines/reference/es6ExportClauseWithoutModuleSpecifier.js b/tests/baselines/reference/es6ExportClauseWithoutModuleSpecifier.js new file mode 100644 index 00000000000..69bb325ec75 --- /dev/null +++ b/tests/baselines/reference/es6ExportClauseWithoutModuleSpecifier.js @@ -0,0 +1,65 @@ +//// [tests/cases/compiler/es6ExportClauseWithoutModuleSpecifier.ts] //// + +//// [server.ts] + +export class c { +} +export interface i { +} +export module m { + export var x = 10; +} +export var x = 10; +export module uninstantiated { +} + +//// [client.ts] +export { c } from "server"; +export { c as c2 } from "server"; +export { i, m as instantiatedModule } from "server"; +export { uninstantiated } from "server"; +export { x } from "server"; + +//// [server.js] +var c = (function () { + function c() { + } + return c; +})(); +exports.c = c; +var m; +(function (m) { + m.x = 10; +})(m = exports.m || (exports.m = {})); +exports.x = 10; +//// [client.js] +var _server = require("server"); +exports.c = _server.c; +var _server_1 = require("server"); +exports.c2 = _server_1.c; +var _server_2 = require("server"); +exports.i = _server_2.i; +exports.instantiatedModule = _server_2.m; +var _server_3 = require("server"); +exports.uninstantiated = _server_3.uninstantiated; +var _server_4 = require("server"); +exports.x = _server_4.x; + + +//// [server.d.ts] +export declare class c { +} +export interface i { +} +export declare module m { + var x: number; +} +export declare var x: number; +export declare module uninstantiated { +} +//// [client.d.ts] +export { c } from "server"; +export { c as c2 } from "server"; +export { i, m as instantiatedModule } from "server"; +export { uninstantiated } from "server"; +export { x } from "server"; diff --git a/tests/baselines/reference/es6ExportClauseWithoutModuleSpecifierInEs5.js b/tests/baselines/reference/es6ExportClauseWithoutModuleSpecifierInEs5.js new file mode 100644 index 00000000000..3e412c13a32 --- /dev/null +++ b/tests/baselines/reference/es6ExportClauseWithoutModuleSpecifierInEs5.js @@ -0,0 +1,65 @@ +//// [tests/cases/compiler/es6ExportClauseWithoutModuleSpecifierInEs5.ts] //// + +//// [server.ts] + +export class c { +} +export interface i { +} +export module m { + export var x = 10; +} +export var x = 10; +export module uninstantiated { +} + +//// [client.ts] +export { c } from "server"; +export { c as c2 } from "server"; +export { i, m as instantiatedModule } from "server"; +export { uninstantiated } from "server"; +export { x } from "server"; + +//// [server.js] +var c = (function () { + function c() { + } + return c; +})(); +exports.c = c; +var m; +(function (m) { + m.x = 10; +})(m = exports.m || (exports.m = {})); +exports.x = 10; +//// [client.js] +var _server = require("server"); +exports.c = _server.c; +var _server_1 = require("server"); +exports.c2 = _server_1.c; +var _server_2 = require("server"); +exports.i = _server_2.i; +exports.instantiatedModule = _server_2.m; +var _server_3 = require("server"); +exports.uninstantiated = _server_3.uninstantiated; +var _server_4 = require("server"); +exports.x = _server_4.x; + + +//// [server.d.ts] +export declare class c { +} +export interface i { +} +export declare module m { + var x: number; +} +export declare var x: number; +export declare module uninstantiated { +} +//// [client.d.ts] +export { c } from "server"; +export { c as c2 } from "server"; +export { i, m as instantiatedModule } from "server"; +export { uninstantiated } from "server"; +export { x } from "server"; diff --git a/tests/baselines/reference/es6ExportClauseWithoutModuleSpecifierInEs5.types b/tests/baselines/reference/es6ExportClauseWithoutModuleSpecifierInEs5.types new file mode 100644 index 00000000000..de81cf3f74f --- /dev/null +++ b/tests/baselines/reference/es6ExportClauseWithoutModuleSpecifierInEs5.types @@ -0,0 +1,40 @@ +=== tests/cases/compiler/server.ts === + +export class c { +>c : c +} +export interface i { +>i : i +} +export module m { +>m : typeof m + + export var x = 10; +>x : number +} +export var x = 10; +>x : number + +export module uninstantiated { +>uninstantiated : unknown +} + +=== tests/cases/compiler/client.ts === +export { c } from "server"; +>c : typeof c + +export { c as c2 } from "server"; +>c : unknown +>c2 : typeof c + +export { i, m as instantiatedModule } from "server"; +>i : unknown +>m : unknown +>instantiatedModule : typeof instantiatedModule + +export { uninstantiated } from "server"; +>uninstantiated : unknown + +export { x } from "server"; +>x : number + diff --git a/tests/cases/compiler/es6ExportAll.ts b/tests/cases/compiler/es6ExportAll.ts new file mode 100644 index 00000000000..ed2b47c23c4 --- /dev/null +++ b/tests/cases/compiler/es6ExportAll.ts @@ -0,0 +1,17 @@ +// @target: es6 +// @declaration: true + +// @filename: server.ts +export class c { +} +export interface i { +} +export module m { + export var x = 10; +} +export var x = 10; +export module uninstantiated { +} + +// @filename: client.ts +export * from "server"; \ No newline at end of file diff --git a/tests/cases/compiler/es6ExportAllInEs5.ts b/tests/cases/compiler/es6ExportAllInEs5.ts new file mode 100644 index 00000000000..ed754e5cacb --- /dev/null +++ b/tests/cases/compiler/es6ExportAllInEs5.ts @@ -0,0 +1,18 @@ +// @target: es5 +// @module: commonjs +// @declaration: true + +// @filename: server.ts +export class c { +} +export interface i { +} +export module m { + export var x = 10; +} +export var x = 10; +export module uninstantiated { +} + +// @filename: client.ts +export * from "server"; \ No newline at end of file diff --git a/tests/cases/compiler/es6ExportClause.ts b/tests/cases/compiler/es6ExportClause.ts new file mode 100644 index 00000000000..a16e2c9e7aa --- /dev/null +++ b/tests/cases/compiler/es6ExportClause.ts @@ -0,0 +1,19 @@ +// @target: es6 +// @declaration: true + +// @filename: server.ts +class c { +} +interface i { +} +module m { + export var x = 10; +} +var x = 10; +module uninstantiated { +} +export { c }; +export { c as c2 }; +export { i, m as instantiatedModule }; +export { uninstantiated }; +export { x }; \ No newline at end of file diff --git a/tests/cases/compiler/es6ExportClauseInEs5.ts b/tests/cases/compiler/es6ExportClauseInEs5.ts new file mode 100644 index 00000000000..675f302bcb6 --- /dev/null +++ b/tests/cases/compiler/es6ExportClauseInEs5.ts @@ -0,0 +1,20 @@ +// @target: es5 +// @module: commonjs +// @declaration: true + +// @filename: server.ts +class c { +} +interface i { +} +module m { + export var x = 10; +} +var x = 10; +module uninstantiated { +} +export { c }; +export { c as c2 }; +export { i, m as instantiatedModule }; +export { uninstantiated }; +export { x }; \ No newline at end of file diff --git a/tests/cases/compiler/es6ExportClauseWithoutModuleSpecifier.ts b/tests/cases/compiler/es6ExportClauseWithoutModuleSpecifier.ts new file mode 100644 index 00000000000..22a6cef4203 --- /dev/null +++ b/tests/cases/compiler/es6ExportClauseWithoutModuleSpecifier.ts @@ -0,0 +1,21 @@ +// @target: es6 +// @declaration: true + +// @filename: server.ts +export class c { +} +export interface i { +} +export module m { + export var x = 10; +} +export var x = 10; +export module uninstantiated { +} + +// @filename: client.ts +export { c } from "server"; +export { c as c2 } from "server"; +export { i, m as instantiatedModule } from "server"; +export { uninstantiated } from "server"; +export { x } from "server"; \ No newline at end of file diff --git a/tests/cases/compiler/es6ExportClauseWithoutModuleSpecifierInEs5.ts b/tests/cases/compiler/es6ExportClauseWithoutModuleSpecifierInEs5.ts new file mode 100644 index 00000000000..6361c013367 --- /dev/null +++ b/tests/cases/compiler/es6ExportClauseWithoutModuleSpecifierInEs5.ts @@ -0,0 +1,22 @@ +// @target: es5 +// @module: commonjs +// @declaration: true + +// @filename: server.ts +export class c { +} +export interface i { +} +export module m { + export var x = 10; +} +export var x = 10; +export module uninstantiated { +} + +// @filename: client.ts +export { c } from "server"; +export { c as c2 } from "server"; +export { i, m as instantiatedModule } from "server"; +export { uninstantiated } from "server"; +export { x } from "server"; \ No newline at end of file