Skip to content

Commit 5f18d9b

Browse files
committed
extract emitExportStar in separate function
1 parent 3af5592 commit 5f18d9b

File tree

5 files changed

+84
-58
lines changed

5 files changed

+84
-58
lines changed

src/compiler/emitter.ts

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4968,7 +4968,6 @@ if (typeof __param !== "function") __param = function (paramIndex, decorator) {
49684968
// should always win over entries with similar names that were added via star exports
49694969
// to support this we store names of local/indirect exported entries in a set.
49704970
// this set is used to filter names brought by star expors.
4971-
49724971
if (!hasExportStars) {
49734972
// local names set is needed only in presence of star exports
49744973
return undefined;
@@ -4987,19 +4986,11 @@ if (typeof __param !== "function") __param = function (paramIndex, decorator) {
49874986
}
49884987

49894988
if (!hasExportDeclarationWithExportClause) {
4990-
// nothing is exported
4991-
return undefined;
4989+
// we still need to emit exportStar helper
4990+
return emitExportStarFunction(/*localNames*/ undefined);
49924991
}
49934992
}
49944993

4995-
// storage has the following structure
4996-
// {
4997-
// <exported-name-from-current-module> : void 0,
4998-
// <exported-name-obtained-from star export in module 'm'>: 'm'
4999-
// }
5000-
// this allows to:
5001-
// - prevent star exports to overwrite locally exported names
5002-
// - bind star exported names to particular module so they won't be overwritten by other star exports
50034994
const exportedNamesStorageRef = makeUniqueName("exportedNames");
50044995

50054996
writeLine();
@@ -5044,27 +5035,31 @@ if (typeof __param !== "function") __param = function (paramIndex, decorator) {
50445035
writeLine();
50455036
write("};");
50465037

5047-
writeLine();
5048-
5049-
const exportStarFunction = makeUniqueName("exportStar");
5038+
return emitExportStarFunction(exportedNamesStorageRef);
50505039

5051-
// define an export star helper function
5052-
write(`function ${exportStarFunction}(m, name) {`);
5053-
writeLine();
5054-
write(` for(var n in m) {`);
5055-
writeLine();
5056-
// if name is not yet taken by either local names or names in other modules - reserve it
5057-
write(` if (!${exportedNamesStorageRef}.hasOwnProperty(n)) ${exportedNamesStorageRef}[n] = name;`);
5058-
writeLine();
5059-
// only export value if it was exported from the module with name 'name';
5060-
write(` if (${exportedNamesStorageRef}[n] === name) ${exportFunctionForFile}(n, m[n]);`);
5061-
writeLine();
5062-
write(" }");
5063-
writeLine();
5064-
write("}")
5040+
function emitExportStarFunction(localNames: string): string {
5041+
const exportStarFunction = makeUniqueName("exportStar");
50655042

5066-
return exportStarFunction;
5043+
writeLine();
5044+
5045+
// define an export star helper function
5046+
write(`function ${exportStarFunction}(m) {`);
5047+
writeLine();
5048+
write(` for(var n in m) {`);
5049+
writeLine();
5050+
write(` `);
5051+
if (localNames) {
5052+
write(`if (!${localNames}.hasOwnProperty(n)) `);
5053+
}
5054+
write(`${exportFunctionForFile}(n, m[n]);`);
5055+
writeLine();
5056+
write(" }");
5057+
writeLine();
5058+
write("}")
50675059

5060+
return exportStarFunction;
5061+
}
5062+
50685063
function writeExportedName(node: Identifier | Declaration): void {
50695064
if (started) {
50705065
write(",");
@@ -5085,7 +5080,7 @@ if (typeof __param !== "function") __param = function (paramIndex, decorator) {
50855080
emitDeclarationName(<Declaration>node);
50865081
}
50875082

5088-
write("': void 0");
5083+
write("': true");
50895084
}
50905085
}
50915086

@@ -5368,8 +5363,8 @@ if (typeof __param !== "function") __param = function (paramIndex, decorator) {
53685363
writeLine();
53695364
// export * from 'foo'
53705365
// emit as:
5371-
// exportStar(_foo, 'foo');
5372-
write(`${exportStarFunction}(${parameterName}, ${getExternalModuleNameText(importNode)});`);
5366+
// exportStar(_foo);
5367+
write(`${exportStarFunction}(${parameterName});`);
53735368
}
53745369

53755370
writeLine();

tests/baselines/reference/systemModule11.errors.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ tests/cases/compiler/file2.ts(7,15): error TS2307: Cannot find module 'bar'.
33
tests/cases/compiler/file3.ts(2,25): error TS2307: Cannot find module 'a'.
44
tests/cases/compiler/file3.ts(3,15): error TS2307: Cannot find module 'bar'.
55
tests/cases/compiler/file4.ts(8,27): error TS2307: Cannot find module 'a'.
6+
tests/cases/compiler/file5.ts(3,15): error TS2307: Cannot find module 'a'.
67

78

89
==== tests/cases/compiler/file1.ts (1 errors) ====
@@ -46,4 +47,11 @@ tests/cases/compiler/file4.ts(8,27): error TS2307: Cannot find module 'a'.
4647

4748
export {s, s1 as s2} from 'a'
4849
~~~
50+
!!! error TS2307: Cannot find module 'a'.
51+
52+
==== tests/cases/compiler/file5.ts (1 errors) ====
53+
54+
function foo() {}
55+
export * from 'a';
56+
~~~
4957
!!! error TS2307: Cannot find module 'a'.

tests/baselines/reference/systemModule11.js

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ export function foo() {}
3131
var z, z1;
3232
export {z, z1 as z2};
3333

34-
export {s, s1 as s2} from 'a'
34+
export {s, s1 as s2} from 'a'
35+
36+
//// [file5.ts]
37+
38+
function foo() {}
39+
export * from 'a';
3540

3641
//// [file1.js]
3742
// set of tests cases that checks generation of local storage for exported names
@@ -40,19 +45,18 @@ System.register(['bar'], function(exports_1) {
4045
function foo() { }
4146
exports_1("foo", foo);
4247
var exportedNames_1 = {
43-
'x': void 0,
44-
'foo': void 0
48+
'x': true,
49+
'foo': true
4550
};
46-
function exportStar_1(m, name) {
51+
function exportStar_1(m) {
4752
for(var n in m) {
48-
if (!exportedNames_1.hasOwnProperty(n)) exportedNames_1[n] = name;
49-
if (exportedNames_1[n] === name) exports_1(n, m[n]);
53+
if (!exportedNames_1.hasOwnProperty(n)) exports_1(n, m[n]);
5054
}
5155
}
5256
return {
5357
setters:[
5458
function (_bar_1) {
55-
exportStar_1(_bar_1, 'bar');
59+
exportStar_1(_bar_1);
5660
}],
5761
execute: function() {
5862
exports_1("x", x);
@@ -63,19 +67,18 @@ System.register(['bar'], function(exports_1) {
6367
System.register(['bar'], function(exports_1) {
6468
var x, y;
6569
var exportedNames_1 = {
66-
'x': void 0,
67-
'y1': void 0
70+
'x': true,
71+
'y1': true
6872
};
69-
function exportStar_1(m, name) {
73+
function exportStar_1(m) {
7074
for(var n in m) {
71-
if (!exportedNames_1.hasOwnProperty(n)) exportedNames_1[n] = name;
72-
if (exportedNames_1[n] === name) exports_1(n, m[n]);
75+
if (!exportedNames_1.hasOwnProperty(n)) exports_1(n, m[n]);
7376
}
7477
}
7578
return {
7679
setters:[
7780
function (_bar_1) {
78-
exportStar_1(_bar_1, 'bar');
81+
exportStar_1(_bar_1);
7982
}],
8083
execute: function() {
8184
exports_1("x", x);
@@ -86,13 +89,12 @@ System.register(['bar'], function(exports_1) {
8689
//// [file3.js]
8790
System.register(['a', 'bar'], function(exports_1) {
8891
var exportedNames_1 = {
89-
'x': void 0,
90-
'z': void 0
92+
'x': true,
93+
'z': true
9194
};
92-
function exportStar_1(m, name) {
95+
function exportStar_1(m) {
9396
for(var n in m) {
94-
if (!exportedNames_1.hasOwnProperty(n)) exportedNames_1[n] = name;
95-
if (exportedNames_1[n] === name) exports_1(n, m[n]);
97+
if (!exportedNames_1.hasOwnProperty(n)) exports_1(n, m[n]);
9698
}
9799
}
98100
return {
@@ -102,7 +104,7 @@ System.register(['a', 'bar'], function(exports_1) {
102104
exports_1("z", _a_1["y"]);
103105
},
104106
function (_bar_1) {
105-
exportStar_1(_bar_1, 'bar');
107+
exportStar_1(_bar_1);
106108
}],
107109
execute: function() {
108110
}
@@ -126,3 +128,20 @@ System.register(['a'], function(exports_1) {
126128
}
127129
}
128130
});
131+
//// [file5.js]
132+
System.register(['a'], function(exports_1) {
133+
function foo() { }
134+
function exportStar_1(m) {
135+
for(var n in m) {
136+
exports_1(n, m[n]);
137+
}
138+
}
139+
return {
140+
setters:[
141+
function (_a_1) {
142+
exportStar_1(_a_1);
143+
}],
144+
execute: function() {
145+
}
146+
}
147+
});

tests/baselines/reference/systemModule9.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@ System.register(['file1', 'file2', 'file3', 'file4', 'file5', 'file6', 'file7'],
2626
var ns, file2_1, file3_1, file5_1, ns3;
2727
var x, y;
2828
var exportedNames_1 = {
29-
'x': void 0,
30-
'z': void 0
29+
'x': true,
30+
'z': true
3131
};
32-
function exportStar_1(m, name) {
32+
function exportStar_1(m) {
3333
for(var n in m) {
34-
if (!exportedNames_1.hasOwnProperty(n)) exportedNames_1[n] = name;
35-
if (exportedNames_1[n] === name) exports_1(n, m[n]);
34+
if (!exportedNames_1.hasOwnProperty(n)) exports_1(n, m[n]);
3635
}
3736
}
3837
return {
@@ -54,7 +53,7 @@ System.register(['file1', 'file2', 'file3', 'file4', 'file5', 'file6', 'file7'],
5453
ns3 = _ns3;
5554
},
5655
function (_file7_1) {
57-
exportStar_1(_file7_1, 'file7');
56+
exportStar_1(_file7_1);
5857
}],
5958
execute: function() {
6059
ns.f();

tests/cases/compiler/systemModule11.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,9 @@ export function foo() {}
3131
var z, z1;
3232
export {z, z1 as z2};
3333

34-
export {s, s1 as s2} from 'a'
34+
export {s, s1 as s2} from 'a'
35+
36+
// @filename: file5.ts
37+
38+
function foo() {}
39+
export * from 'a';

0 commit comments

Comments
 (0)