Skip to content

Commit

Permalink
Make sure circular export * from X does not stack overflow
Browse files Browse the repository at this point in the history
closes #2815
  • Loading branch information
Swatinem committed May 3, 2019
1 parent 61a7947 commit 8b31835
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Module.ts
Expand Up @@ -308,20 +308,28 @@ export default class Module {
);
}

getReexports() {
getReexports(walkedModuleIds = new Set<string>()) {
const reexports = Object.create(null);

for (const name in this.reexports) {
reexports[name] = true;
}

// avoid infinite recursion when using circular `export * from X`
if (walkedModuleIds.has(this.id)) {
return Object.keys(reexports);
}
walkedModuleIds.add(this.id);

this.exportAllModules.forEach(module => {
if (module.isExternal) {
reexports[`*${module.id}`] = true;
return;
}

for (const name of (<Module>module).getExports().concat((<Module>module).getReexports())) {
for (const name of (<Module>module)
.getExports()
.concat((<Module>module).getReexports(walkedModuleIds))) {
if (name !== 'default') reexports[name] = true;
}
});
Expand Down
18 changes: 18 additions & 0 deletions test/function/samples/cycles-export-star/_config.js
@@ -0,0 +1,18 @@
const assert = require('assert');

module.exports = {
description: 'does not stack overflow on `export * from X` cycles',
code(code) {
assert.equal(
code,
`'use strict';\n\nfunction b() {\n\treturn 'b';\n}\n\nassert.equal(b(), 'b');\n`
);
},
warnings: [
{
code: 'CIRCULAR_DEPENDENCY',
importer: 'a.js',
message: 'Circular dependency: a.js -> b.js -> a.js'
}
]
};
5 changes: 5 additions & 0 deletions test/function/samples/cycles-export-star/a.js
@@ -0,0 +1,5 @@
export * from './b.js';

export function a() {
return 'a';
}
5 changes: 5 additions & 0 deletions test/function/samples/cycles-export-star/b.js
@@ -0,0 +1,5 @@
export * from './a.js';

export function b() {
return 'b';
}
3 changes: 3 additions & 0 deletions test/function/samples/cycles-export-star/main.js
@@ -0,0 +1,3 @@
import { b } from './a.js';

assert.equal(b(), 'b');

0 comments on commit 8b31835

Please sign in to comment.