diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 32ea13b6338f7..f3082256936e7 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -3090,23 +3090,28 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi } } - function emitExportMemberAssignments(name: Identifier) { - if (compilerOptions.module === ModuleKind.System) { - return; - } - - if (!exportEquals && exportSpecifiers && hasProperty(exportSpecifiers, name.text)) { - for (let specifier of exportSpecifiers[name.text]) { - writeLine(); + function emitExportMemberAssignments(specifier: ExportSpecifier) { + if (resolver.isValueAliasDeclaration(specifier)) { + writeLine(); + emitStart(specifier); + if (compilerOptions.module === ModuleKind.System) { + write(`${exportFunctionForFile}("`); + emit(specifier.name); + write(`", `); + emitExpressionIdentifier(specifier.propertyName || specifier.name); + write(")"); + } + else { emitStart(specifier.name); emitContainingModuleName(specifier); write("."); emitNodeWithoutSourceMap(specifier.name); emitEnd(specifier.name); write(" = "); - emitExpressionIdentifier(name); - write(";"); + emitExpressionIdentifier(specifier.propertyName || specifier.name); } + emitEnd(specifier); + write(";"); } } @@ -3406,19 +3411,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi } } - function emitExportVariableAssignments(node: VariableDeclaration | BindingElement) { - if (node.kind === SyntaxKind.OmittedExpression) { - return; - } - let name = node.name; - if (name.kind === SyntaxKind.Identifier) { - emitExportMemberAssignments(name); - } - else if (isBindingPattern(name)) { - forEach((name).elements, emitExportVariableAssignments); - } - } - function getCombinedFlagsForIdentifier(node: Identifier): NodeFlags { if (!node.parent || (node.parent.kind !== SyntaxKind.VariableDeclaration && node.parent.kind !== SyntaxKind.BindingElement)) { return 0; @@ -3457,9 +3449,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi write(";"); } } - if (languageVersion < ScriptTarget.ES6 && node.parent === currentSourceFile) { - forEach(node.declarationList.declarations, emitExportVariableAssignments); - } } function shouldEmitLeadingAndTrailingCommentsForVariableStatement(node: VariableStatement) { @@ -3666,9 +3655,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi } emitSignatureAndBody(node); - if (languageVersion < ScriptTarget.ES6 && node.kind === SyntaxKind.FunctionDeclaration && node.parent === currentSourceFile && node.name) { - emitExportMemberAssignments((node).name); - } if (node.kind !== SyntaxKind.MethodDeclaration && node.kind !== SyntaxKind.MethodSignature) { emitTrailingComments(node); } @@ -4588,10 +4574,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi if (node.kind === SyntaxKind.ClassDeclaration) { emitExportMemberAssignment(node); } - - if (languageVersion < ScriptTarget.ES6 && node.parent === currentSourceFile && node.name) { - emitExportMemberAssignments(node.name); - } } function emitClassMemberPrefix(node: ClassLikeDeclaration, member: Node) { @@ -5188,7 +5170,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi emitDeclarationName(node); write(");"); } - emitExportMemberAssignments(node.name); } } @@ -5309,7 +5290,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi emitDeclarationName(node); write(");"); } - emitExportMemberAssignments(node.name); } } @@ -5340,13 +5320,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi return node.kind === SyntaxKind.ImportDeclaration && (node).importClause && !!(node).importClause.name; } - function emitExportImportAssignments(node: Node) { - if (isAliasSymbolDeclaration(node) && resolver.isValueAliasDeclaration(node)) { - emitExportMemberAssignments((node).name); - } - forEachChild(node, emitExportImportAssignments); - } - function emitImportDeclaration(node: ImportDeclaration) { if (languageVersion < ScriptTarget.ES6) { return emitExternalImportDeclaration(node); @@ -5432,7 +5405,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi } write(";"); emitEnd(node); - emitExportImportAssignments(node); emitTrailingComments(node); } else { @@ -5450,7 +5422,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi write(getGeneratedNameForNode(node)); write(";"); } - emitExportImportAssignments(node); } } } @@ -5505,13 +5476,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi write(";"); emitEnd(node); - emitExportImportAssignments(node); emitTrailingComments(node); } } function emitExportDeclaration(node: ExportDeclaration) { - Debug.assert(compilerOptions.module !== ModuleKind.System); + Debug.assert(compilerOptions.module !== ModuleKind.System || !!node.exportClause); if (languageVersion < ScriptTarget.ES6) { if (node.moduleSpecifier && (!node.exportClause || resolver.isValueAliasDeclaration(node))) { @@ -5556,6 +5526,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi } emitEnd(node); } + else if (node.parent === currentSourceFile && !node.moduleSpecifier && node.exportClause) { + // export { x, y, ... } + forEach(node.exportClause.elements, emitExportMemberAssignments); + } } else { if (!node.exportClause || resolver.isValueAliasDeclaration(node)) { @@ -5674,7 +5648,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi externalImports.push(node); } } - else { + else if (compilerOptions.module === ModuleKind.System) { // export { x, y } for (let specifier of (node).exportClause.elements) { let name = (specifier.propertyName || specifier.name).text; @@ -5824,7 +5798,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi for (let element of exportDecl.exportClause.elements) { // write name of indirectly exported entry, i.e. 'export {x} from ...' - writeExportedName(element.name || element.propertyName); + writeExportedName(element.name); } } @@ -6152,12 +6126,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi // fall-through case SyntaxKind.ImportEqualsDeclaration: Debug.assert(importVariableName !== ""); - writeLine(); // save import into the local write(`${importVariableName} = ${parameterName};`); writeLine(); break; + case SyntaxKind.ExportDeclaration: Debug.assert(importVariableName !== ""); @@ -6218,20 +6192,16 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi for (let i = startIndex; i < node.statements.length; ++i) { let statement = node.statements[i]; switch (statement.kind) { - // - function declarations are not emitted because they were already hoisted - // - import declarations are not emitted since they are already handled in setters // - export declarations with module specifiers are not emitted since they were already written in setters // - export declarations without module specifiers are emitted preserving the order - case SyntaxKind.FunctionDeclaration: - case SyntaxKind.ImportDeclaration: - continue; case SyntaxKind.ExportDeclaration: if (!(statement).moduleSpecifier) { - for (let element of (statement).exportClause.elements) { - // write call to exporter function for every export specifier in exports list - emitExportSpecifierInSystemModule(element); - } + break; } + // - function declarations are not emitted because they were already hoisted + // - import declarations are not emitted since they are already handled in setters + case SyntaxKind.FunctionDeclaration: + case SyntaxKind.ImportDeclaration: continue; case SyntaxKind.ImportEqualsDeclaration: if (!isInternalModuleImportEqualsDeclaration(statement)) { @@ -6239,10 +6209,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi continue; } // fall-though for import declarations that import internal modules - default: - writeLine(); - emit(statement); - } + } + writeLine(); + emit(statement); } decreaseIndent(); writeLine(); diff --git a/tests/baselines/reference/amdReexportAmbientVar.js b/tests/baselines/reference/amdReexportAmbientVar.js new file mode 100644 index 0000000000000..a44f75bda7b31 --- /dev/null +++ b/tests/baselines/reference/amdReexportAmbientVar.js @@ -0,0 +1,10 @@ +//// [amdReexportAmbientVar.ts] +/// +declare var view: string; +export {view}; +export function test() { } + +//// [amdReexportAmbientVar.js] +exports.view = view; +function test() { } +exports.test = test; diff --git a/tests/baselines/reference/amdReexportAmbientVar.symbols b/tests/baselines/reference/amdReexportAmbientVar.symbols new file mode 100644 index 0000000000000..b98d99283b61b --- /dev/null +++ b/tests/baselines/reference/amdReexportAmbientVar.symbols @@ -0,0 +1,11 @@ +=== tests/cases/compiler/amdReexportAmbientVar.ts === +/// +declare var view: string; +>view : Symbol(view, Decl(amdReexportAmbientVar.ts, 1, 11)) + +export {view}; +>view : Symbol(view, Decl(amdReexportAmbientVar.ts, 2, 8)) + +export function test() { } +>test : Symbol(test, Decl(amdReexportAmbientVar.ts, 2, 14)) + diff --git a/tests/baselines/reference/amdReexportAmbientVar.types b/tests/baselines/reference/amdReexportAmbientVar.types new file mode 100644 index 0000000000000..9adfc95c4e0e7 --- /dev/null +++ b/tests/baselines/reference/amdReexportAmbientVar.types @@ -0,0 +1,11 @@ +=== tests/cases/compiler/amdReexportAmbientVar.ts === +/// +declare var view: string; +>view : string + +export {view}; +>view : string + +export function test() { } +>test : () => void + diff --git a/tests/baselines/reference/es6ExportClauseInEs5.js b/tests/baselines/reference/es6ExportClauseInEs5.js index f987b48aecadd..939b26797d888 100644 --- a/tests/baselines/reference/es6ExportClauseInEs5.js +++ b/tests/baselines/reference/es6ExportClauseInEs5.js @@ -22,14 +22,14 @@ var c = (function () { } return c; })(); -exports.c = c; -exports.c2 = c; var m; (function (m) { m.x = 10; })(m || (m = {})); -exports.instantiatedModule = m; var x = 10; +exports.c = c; +exports.c2 = c; +exports.instantiatedModule = m; exports.x = x; diff --git a/tests/baselines/reference/exportClauseEmit.js b/tests/baselines/reference/exportClauseEmit.js new file mode 100644 index 0000000000000..8c1c812924d0d --- /dev/null +++ b/tests/baselines/reference/exportClauseEmit.js @@ -0,0 +1,16 @@ +//// [exportClauseEmit.ts] +var str = "Hello"; +// Change str +str = "Hello World!!!"; +export {str}; + +//// [exportClauseEmit.js] +var str = "Hello"; +// Change str +str = "Hello World!!!"; +exports.str = str; + + +//// [exportClauseEmit.d.ts] +declare var str: string; +export { str }; diff --git a/tests/baselines/reference/exportClauseEmit.symbols b/tests/baselines/reference/exportClauseEmit.symbols new file mode 100644 index 0000000000000..83176a581c49c --- /dev/null +++ b/tests/baselines/reference/exportClauseEmit.symbols @@ -0,0 +1,11 @@ +=== tests/cases/compiler/exportClauseEmit.ts === +var str = "Hello"; +>str : Symbol(str, Decl(exportClauseEmit.ts, 0, 3)) + +// Change str +str = "Hello World!!!"; +>str : Symbol(str, Decl(exportClauseEmit.ts, 0, 3)) + +export {str}; +>str : Symbol(str, Decl(exportClauseEmit.ts, 3, 8)) + diff --git a/tests/baselines/reference/exportClauseEmit.types b/tests/baselines/reference/exportClauseEmit.types new file mode 100644 index 0000000000000..96646adbecca6 --- /dev/null +++ b/tests/baselines/reference/exportClauseEmit.types @@ -0,0 +1,14 @@ +=== tests/cases/compiler/exportClauseEmit.ts === +var str = "Hello"; +>str : string +>"Hello" : string + +// Change str +str = "Hello World!!!"; +>str = "Hello World!!!" : string +>str : string +>"Hello World!!!" : string + +export {str}; +>str : string + diff --git a/tests/baselines/reference/exportSpecifierForAGlobal.js b/tests/baselines/reference/exportSpecifierForAGlobal.js index 6b4343a299bb3..faade147268e9 100644 --- a/tests/baselines/reference/exportSpecifierForAGlobal.js +++ b/tests/baselines/reference/exportSpecifierForAGlobal.js @@ -13,6 +13,7 @@ export function f() { //// [b.js] +exports.X = X; function f() { var x; return x; diff --git a/tests/baselines/reference/exportStar-amd.js b/tests/baselines/reference/exportStar-amd.js index 534a23b4d5bd7..63f2766ee3121 100644 --- a/tests/baselines/reference/exportStar-amd.js +++ b/tests/baselines/reference/exportStar-amd.js @@ -44,10 +44,10 @@ define(["require", "exports"], function (require, exports) { //// [t3.js] define(["require", "exports"], function (require, exports) { var x = "x"; - exports.x = x; var y = "y"; - exports.y = y; var z = "z"; + exports.x = x; + exports.y = y; exports.z = z; }); //// [t4.js] diff --git a/tests/baselines/reference/exportStar.js b/tests/baselines/reference/exportStar.js index 74cf24a0f9718..ce2ebd0c1fccb 100644 --- a/tests/baselines/reference/exportStar.js +++ b/tests/baselines/reference/exportStar.js @@ -39,10 +39,10 @@ function foo() { } exports.foo = foo; //// [t3.js] var x = "x"; -exports.x = x; var y = "y"; -exports.y = y; var z = "z"; +exports.x = x; +exports.y = y; exports.z = z; //// [t4.js] function __export(m) { diff --git a/tests/baselines/reference/exportsAndImports1-amd.js b/tests/baselines/reference/exportsAndImports1-amd.js index 39744c7f142eb..9634a18fb2511 100644 --- a/tests/baselines/reference/exportsAndImports1-amd.js +++ b/tests/baselines/reference/exportsAndImports1-amd.js @@ -37,27 +37,27 @@ export { v, f, C, I, E, D, M, N, T, a }; //// [t1.js] define(["require", "exports"], function (require, exports) { var v = 1; - exports.v = v; function f() { } - exports.f = f; var C = (function () { function C() { } return C; })(); - exports.C = C; var E; (function (E) { E[E["A"] = 0] = "A"; E[E["B"] = 1] = "B"; E[E["C"] = 2] = "C"; })(E || (E = {})); - exports.E = E; var M; (function (M) { })(M || (M = {})); - exports.M = M; var a = M.x; + exports.v = v; + exports.f = f; + exports.C = C; + exports.E = E; + exports.M = M; exports.a = a; }); //// [t2.js] diff --git a/tests/baselines/reference/exportsAndImports1.js b/tests/baselines/reference/exportsAndImports1.js index cd7db07b5694a..43a4d19435825 100644 --- a/tests/baselines/reference/exportsAndImports1.js +++ b/tests/baselines/reference/exportsAndImports1.js @@ -36,27 +36,27 @@ export { v, f, C, I, E, D, M, N, T, a }; //// [t1.js] var v = 1; -exports.v = v; function f() { } -exports.f = f; var C = (function () { function C() { } return C; })(); -exports.C = C; var E; (function (E) { E[E["A"] = 0] = "A"; E[E["B"] = 1] = "B"; E[E["C"] = 2] = "C"; })(E || (E = {})); -exports.E = E; var M; (function (M) { })(M || (M = {})); -exports.M = M; var a = M.x; +exports.v = v; +exports.f = f; +exports.C = C; +exports.E = E; +exports.M = M; exports.a = a; //// [t2.js] var t1_1 = require("./t1"); diff --git a/tests/baselines/reference/exportsAndImports3-amd.js b/tests/baselines/reference/exportsAndImports3-amd.js index 3a33b12c7ffbe..7c9dbb7752104 100644 --- a/tests/baselines/reference/exportsAndImports3-amd.js +++ b/tests/baselines/reference/exportsAndImports3-amd.js @@ -37,29 +37,29 @@ export { v, f, C, I, E, D, M, N, T, a }; //// [t1.js] define(["require", "exports"], function (require, exports) { exports.v = 1; - exports.v1 = exports.v; function f() { } exports.f = f; - exports.f1 = f; var C = (function () { function C() { } return C; })(); exports.C = C; - exports.C1 = C; (function (E) { E[E["A"] = 0] = "A"; E[E["B"] = 1] = "B"; E[E["C"] = 2] = "C"; })(exports.E || (exports.E = {})); var E = exports.E; - exports.E1 = E; var M; (function (M) { })(M = exports.M || (exports.M = {})); - exports.M1 = M; exports.a = M.x; + exports.v1 = exports.v; + exports.f1 = f; + exports.C1 = C; + exports.E1 = E; + exports.M1 = M; exports.a1 = exports.a; }); //// [t2.js] diff --git a/tests/baselines/reference/exportsAndImports3.js b/tests/baselines/reference/exportsAndImports3.js index 47491ccbc04af..8e59b98a25ae6 100644 --- a/tests/baselines/reference/exportsAndImports3.js +++ b/tests/baselines/reference/exportsAndImports3.js @@ -36,29 +36,29 @@ export { v, f, C, I, E, D, M, N, T, a }; //// [t1.js] exports.v = 1; -exports.v1 = exports.v; function f() { } exports.f = f; -exports.f1 = f; var C = (function () { function C() { } return C; })(); exports.C = C; -exports.C1 = C; (function (E) { E[E["A"] = 0] = "A"; E[E["B"] = 1] = "B"; E[E["C"] = 2] = "C"; })(exports.E || (exports.E = {})); var E = exports.E; -exports.E1 = E; var M; (function (M) { })(M = exports.M || (exports.M = {})); -exports.M1 = M; exports.a = M.x; +exports.v1 = exports.v; +exports.f1 = f; +exports.C1 = C; +exports.E1 = E; +exports.M1 = M; exports.a1 = exports.a; //// [t2.js] var t1_1 = require("./t1"); diff --git a/tests/baselines/reference/exportsAndImports4-amd.js b/tests/baselines/reference/exportsAndImports4-amd.js index e6a431854b27b..d0add8f390ba9 100644 --- a/tests/baselines/reference/exportsAndImports4-amd.js +++ b/tests/baselines/reference/exportsAndImports4-amd.js @@ -46,21 +46,21 @@ define(["require", "exports"], function (require, exports) { }); //// [t3.js] define(["require", "exports", "./t1", "./t1", "./t1", "./t1", "./t1", "./t1"], function (require, exports, a, t1_1, c, t1_2, t1_3, t1_4) { - exports.a = a; a.default; - exports.b = t1_1.default; t1_1.default; - exports.c = c; c.default; - exports.d = t1_2.default; t1_2.default; var e2 = t1_3; - exports.e1 = t1_3.default; - exports.e2 = e2; t1_3.default; e2.default; - exports.f1 = t1_4.default; - exports.f2 = t1_4.default; t1_4.default; t1_4.default; + exports.a = a; + exports.b = t1_1.default; + exports.c = c; + exports.d = t1_2.default; + exports.e1 = t1_3.default; + exports.e2 = e2; + exports.f1 = t1_4.default; + exports.f2 = t1_4.default; }); diff --git a/tests/baselines/reference/exportsAndImports4.js b/tests/baselines/reference/exportsAndImports4.js index 7358b31eacec3..51f1ca2a54bb8 100644 --- a/tests/baselines/reference/exportsAndImports4.js +++ b/tests/baselines/reference/exportsAndImports4.js @@ -44,24 +44,24 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.default = "hello"; //// [t3.js] var a = require("./t1"); -exports.a = a; a.default; var t1_1 = require("./t1"); -exports.b = t1_1.default; t1_1.default; var c = require("./t1"); -exports.c = c; c.default; var t1_2 = require("./t1"); -exports.d = t1_2.default; t1_2.default; var t1_3 = require("./t1"), e2 = t1_3; -exports.e1 = t1_3.default; -exports.e2 = e2; t1_3.default; e2.default; var t1_4 = require("./t1"); -exports.f1 = t1_4.default; -exports.f2 = t1_4.default; t1_4.default; t1_4.default; +exports.a = a; +exports.b = t1_1.default; +exports.c = c; +exports.d = t1_2.default; +exports.e1 = t1_3.default; +exports.e2 = e2; +exports.f1 = t1_4.default; +exports.f2 = t1_4.default; diff --git a/tests/baselines/reference/exportsAndImportsWithContextualKeywordNames01.js b/tests/baselines/reference/exportsAndImportsWithContextualKeywordNames01.js index 4b1564b5ab289..dd9774965a9e9 100644 --- a/tests/baselines/reference/exportsAndImportsWithContextualKeywordNames01.js +++ b/tests/baselines/reference/exportsAndImportsWithContextualKeywordNames01.js @@ -24,8 +24,8 @@ var set = { set foo(x) { } }; -exports.set = set; var get = 10; +exports.set = set; exports.get = get; //// [t2.js] //// [t3.js] diff --git a/tests/cases/compiler/amdReexportAmbientVar.ts b/tests/cases/compiler/amdReexportAmbientVar.ts new file mode 100644 index 0000000000000..45141037cb3de --- /dev/null +++ b/tests/cases/compiler/amdReexportAmbientVar.ts @@ -0,0 +1,5 @@ +//@module: commonjs +/// +declare var view: string; +export {view}; +export function test() { } \ No newline at end of file diff --git a/tests/cases/compiler/exportClauseEmit.ts b/tests/cases/compiler/exportClauseEmit.ts new file mode 100644 index 0000000000000..237209a6e6cfc --- /dev/null +++ b/tests/cases/compiler/exportClauseEmit.ts @@ -0,0 +1,6 @@ +//@module: commonjs +//@declaration: true +var str = "Hello"; +// Change str +str = "Hello World!!!"; +export {str}; \ No newline at end of file