Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure circular export * from X does not stack overflow #2836

Merged
merged 2 commits into from May 4, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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>()) {
Swatinem marked this conversation as resolved.
Show resolved Hide resolved
// 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');