extract emitExportStar in separate function

This commit is contained in:
Vladimir Matveev 2015-04-28 20:17:55 -07:00
parent 3af5592243
commit 5f18d9b912
5 changed files with 84 additions and 58 deletions

View File

@ -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
// {
// <exported-name-from-current-module> : void 0,
// <exported-name-obtained-from star export in module 'm'>: '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(<Declaration>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();

View File

@ -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'.

View File

@ -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() {
}
}
});

View File

@ -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();

View File

@ -31,4 +31,9 @@ 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'
// @filename: file5.ts
function foo() {}
export * from 'a';