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 4, 2019
1 parent 61a7947 commit 07a1dea
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,7 +308,13 @@ export default class Module {
);
}

getReexports() {
getReexports(walkedModuleIds = new Set<string>()) {
// avoid infinite recursion when using circular `export * from X`
if (walkedModuleIds.has(this.id)) {
return [];
}
walkedModuleIds.add(this.id);

const reexports = Object.create(null);

for (const name in this.reexports) {
Expand All @@ -321,7 +327,9 @@ export default class Module {
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 07a1dea

Please sign in to comment.