From 5f18d9b912320bbddaffef8ab74207c094f5ec3d Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Tue, 28 Apr 2015 20:17:55 -0700 Subject: [PATCH] extract emitExportStar in separate function --- src/compiler/emitter.ts | 59 +++++++++---------- .../reference/systemModule11.errors.txt | 8 +++ tests/baselines/reference/systemModule11.js | 57 ++++++++++++------ tests/baselines/reference/systemModule9.js | 11 ++-- tests/cases/compiler/systemModule11.ts | 7 ++- 5 files changed, 84 insertions(+), 58 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index aa65406b426..672a6b32126 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -4968,7 +4968,6 @@ if (typeof __param !== "function") __param = function (paramIndex, decorator) { // should always win over entries with similar names that were added via star exports // to support this we store names of local/indirect exported entries in a set. // this set is used to filter names brought by star expors. - if (!hasExportStars) { // local names set is needed only in presence of star exports return undefined; @@ -4987,19 +4986,11 @@ if (typeof __param !== "function") __param = function (paramIndex, decorator) { } if (!hasExportDeclarationWithExportClause) { - // nothing is exported - return undefined; + // we still need to emit exportStar helper + return emitExportStarFunction(/*localNames*/ undefined); } } - // storage has the following structure - // { - // : void 0, - // : 'm' - // } - // this allows to: - // - prevent star exports to overwrite locally exported names - // - bind star exported names to particular module so they won't be overwritten by other star exports const exportedNamesStorageRef = makeUniqueName("exportedNames"); writeLine(); @@ -5044,27 +5035,31 @@ if (typeof __param !== "function") __param = function (paramIndex, decorator) { writeLine(); write("};"); - writeLine(); - - const exportStarFunction = makeUniqueName("exportStar"); + return emitExportStarFunction(exportedNamesStorageRef); - // define an export star helper function - write(`function ${exportStarFunction}(m, name) {`); - writeLine(); - write(` for(var n in m) {`); - writeLine(); - // if name is not yet taken by either local names or names in other modules - reserve it - write(` if (!${exportedNamesStorageRef}.hasOwnProperty(n)) ${exportedNamesStorageRef}[n] = name;`); - writeLine(); - // only export value if it was exported from the module with name 'name'; - write(` if (${exportedNamesStorageRef}[n] === name) ${exportFunctionForFile}(n, m[n]);`); - writeLine(); - write(" }"); - writeLine(); - write("}") + function emitExportStarFunction(localNames: string): string { + const exportStarFunction = makeUniqueName("exportStar"); - return exportStarFunction; + writeLine(); + // define an export star helper function + write(`function ${exportStarFunction}(m) {`); + writeLine(); + write(` for(var n in m) {`); + writeLine(); + write(` `); + if (localNames) { + write(`if (!${localNames}.hasOwnProperty(n)) `); + } + write(`${exportFunctionForFile}(n, m[n]);`); + writeLine(); + write(" }"); + writeLine(); + write("}") + + return exportStarFunction; + } + function writeExportedName(node: Identifier | Declaration): void { if (started) { write(","); @@ -5085,7 +5080,7 @@ if (typeof __param !== "function") __param = function (paramIndex, decorator) { emitDeclarationName(node); } - write("': void 0"); + write("': true"); } } @@ -5368,8 +5363,8 @@ if (typeof __param !== "function") __param = function (paramIndex, decorator) { writeLine(); // export * from 'foo' // emit as: - // exportStar(_foo, 'foo'); - write(`${exportStarFunction}(${parameterName}, ${getExternalModuleNameText(importNode)});`); + // exportStar(_foo); + write(`${exportStarFunction}(${parameterName});`); } writeLine(); diff --git a/tests/baselines/reference/systemModule11.errors.txt b/tests/baselines/reference/systemModule11.errors.txt index 8f3ff1c87df..f45de9a7783 100644 --- a/tests/baselines/reference/systemModule11.errors.txt +++ b/tests/baselines/reference/systemModule11.errors.txt @@ -3,6 +3,7 @@ tests/cases/compiler/file2.ts(7,15): error TS2307: Cannot find module 'bar'. tests/cases/compiler/file3.ts(2,25): error TS2307: Cannot find module 'a'. tests/cases/compiler/file3.ts(3,15): error TS2307: Cannot find module 'bar'. tests/cases/compiler/file4.ts(8,27): error TS2307: Cannot find module 'a'. +tests/cases/compiler/file5.ts(3,15): error TS2307: Cannot find module 'a'. ==== tests/cases/compiler/file1.ts (1 errors) ==== @@ -46,4 +47,11 @@ tests/cases/compiler/file4.ts(8,27): error TS2307: Cannot find module 'a'. export {s, s1 as s2} from 'a' ~~~ +!!! error TS2307: Cannot find module 'a'. + +==== tests/cases/compiler/file5.ts (1 errors) ==== + + function foo() {} + export * from 'a'; + ~~~ !!! error TS2307: Cannot find module 'a'. \ No newline at end of file diff --git a/tests/baselines/reference/systemModule11.js b/tests/baselines/reference/systemModule11.js index c42719ac031..56c3125f553 100644 --- a/tests/baselines/reference/systemModule11.js +++ b/tests/baselines/reference/systemModule11.js @@ -31,7 +31,12 @@ export function foo() {} var z, z1; export {z, z1 as z2}; -export {s, s1 as s2} from 'a' +export {s, s1 as s2} from 'a' + +//// [file5.ts] + +function foo() {} +export * from 'a'; //// [file1.js] // set of tests cases that checks generation of local storage for exported names @@ -40,19 +45,18 @@ System.register(['bar'], function(exports_1) { function foo() { } exports_1("foo", foo); var exportedNames_1 = { - 'x': void 0, - 'foo': void 0 + 'x': true, + 'foo': true }; - function exportStar_1(m, name) { + function exportStar_1(m) { for(var n in m) { - if (!exportedNames_1.hasOwnProperty(n)) exportedNames_1[n] = name; - if (exportedNames_1[n] === name) exports_1(n, m[n]); + if (!exportedNames_1.hasOwnProperty(n)) exports_1(n, m[n]); } } return { setters:[ function (_bar_1) { - exportStar_1(_bar_1, 'bar'); + exportStar_1(_bar_1); }], execute: function() { exports_1("x", x); @@ -63,19 +67,18 @@ System.register(['bar'], function(exports_1) { System.register(['bar'], function(exports_1) { var x, y; var exportedNames_1 = { - 'x': void 0, - 'y1': void 0 + 'x': true, + 'y1': true }; - function exportStar_1(m, name) { + function exportStar_1(m) { for(var n in m) { - if (!exportedNames_1.hasOwnProperty(n)) exportedNames_1[n] = name; - if (exportedNames_1[n] === name) exports_1(n, m[n]); + if (!exportedNames_1.hasOwnProperty(n)) exports_1(n, m[n]); } } return { setters:[ function (_bar_1) { - exportStar_1(_bar_1, 'bar'); + exportStar_1(_bar_1); }], execute: function() { exports_1("x", x); @@ -86,13 +89,12 @@ System.register(['bar'], function(exports_1) { //// [file3.js] System.register(['a', 'bar'], function(exports_1) { var exportedNames_1 = { - 'x': void 0, - 'z': void 0 + 'x': true, + 'z': true }; - function exportStar_1(m, name) { + function exportStar_1(m) { for(var n in m) { - if (!exportedNames_1.hasOwnProperty(n)) exportedNames_1[n] = name; - if (exportedNames_1[n] === name) exports_1(n, m[n]); + if (!exportedNames_1.hasOwnProperty(n)) exports_1(n, m[n]); } } return { @@ -102,7 +104,7 @@ System.register(['a', 'bar'], function(exports_1) { exports_1("z", _a_1["y"]); }, function (_bar_1) { - exportStar_1(_bar_1, 'bar'); + exportStar_1(_bar_1); }], execute: function() { } @@ -126,3 +128,20 @@ System.register(['a'], function(exports_1) { } } }); +//// [file5.js] +System.register(['a'], function(exports_1) { + function foo() { } + function exportStar_1(m) { + for(var n in m) { + exports_1(n, m[n]); + } + } + return { + setters:[ + function (_a_1) { + exportStar_1(_a_1); + }], + execute: function() { + } + } +}); diff --git a/tests/baselines/reference/systemModule9.js b/tests/baselines/reference/systemModule9.js index 27b274e8ab6..2cc7bc68d73 100644 --- a/tests/baselines/reference/systemModule9.js +++ b/tests/baselines/reference/systemModule9.js @@ -26,13 +26,12 @@ System.register(['file1', 'file2', 'file3', 'file4', 'file5', 'file6', 'file7'], var ns, file2_1, file3_1, file5_1, ns3; var x, y; var exportedNames_1 = { - 'x': void 0, - 'z': void 0 + 'x': true, + 'z': true }; - function exportStar_1(m, name) { + function exportStar_1(m) { for(var n in m) { - if (!exportedNames_1.hasOwnProperty(n)) exportedNames_1[n] = name; - if (exportedNames_1[n] === name) exports_1(n, m[n]); + if (!exportedNames_1.hasOwnProperty(n)) exports_1(n, m[n]); } } return { @@ -54,7 +53,7 @@ System.register(['file1', 'file2', 'file3', 'file4', 'file5', 'file6', 'file7'], ns3 = _ns3; }, function (_file7_1) { - exportStar_1(_file7_1, 'file7'); + exportStar_1(_file7_1); }], execute: function() { ns.f(); diff --git a/tests/cases/compiler/systemModule11.ts b/tests/cases/compiler/systemModule11.ts index 9e27cc919ab..fb32af1e068 100644 --- a/tests/cases/compiler/systemModule11.ts +++ b/tests/cases/compiler/systemModule11.ts @@ -31,4 +31,9 @@ export function foo() {} var z, z1; export {z, z1 as z2}; -export {s, s1 as s2} from 'a' \ No newline at end of file +export {s, s1 as s2} from 'a' + +// @filename: file5.ts + +function foo() {} +export * from 'a'; \ No newline at end of file