Skip to content

Commit 80200c8

Browse files
committed
Handle recursive imports
1 parent 79a814f commit 80200c8

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/__tests__/fixtures/component_31.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export function SuperCustomButton({color, ...otherProps}) {
77

88
SuperCustomButton.propTypes = sharedProps;
99
export {sharedProps};
10+
export * from './component_32';

src/utils/resolveImportedValue.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import fs from 'fs';
1616

1717
const { namedTypes: t, NodePath } = types;
1818

19-
export default function resolveImportedValue(path: NodePath, name: string) {
19+
export default function resolveImportedValue(path: NodePath, name: string, seen: Set<string> = new Set()) {
2020
// Bail if no filename was provided for the current source file.
2121
// Also never traverse into react itself.
2222
const source = path.node.source.value;
@@ -38,6 +38,13 @@ export default function resolveImportedValue(path: NodePath, name: string) {
3838
return null;
3939
}
4040

41+
// Prevent recursive imports
42+
if (seen.has(resolvedSource)) {
43+
return null;
44+
}
45+
46+
seen.add(resolvedSource);
47+
4148
// Read and parse the code
4249
// TODO: cache and reuse
4350
const code = fs.readFileSync(resolvedSource, 'utf8');
@@ -49,7 +56,7 @@ export default function resolveImportedValue(path: NodePath, name: string) {
4956

5057
const parser = buildParser(parseOptions);
5158
const ast = parser.parse(code);
52-
return findExportedValue(ast.program, name);
59+
return findExportedValue(ast.program, name, seen);
5360
}
5461

5562
// Find the root Program node, which we attached our options too in babelParser.js
@@ -62,7 +69,7 @@ function getOptions(path: NodePath): Options {
6269
}
6370

6471
// Traverses the program looking for an export that matches the requested name
65-
function findExportedValue(ast, name) {
72+
function findExportedValue(ast, name, seen) {
6673
let resultPath: ?NodePath = null;
6774

6875
traverseShallow(ast, {
@@ -100,7 +107,7 @@ function findExportedValue(ast, name) {
100107
return false;
101108
},
102109
visitExportAllDeclaration(path: NodePath) {
103-
const resolvedPath = resolveImportedValue(path, name);
110+
const resolvedPath = resolveImportedValue(path, name, seen);
104111
if (resolvedPath) {
105112
resultPath = resolvedPath;
106113
}

0 commit comments

Comments
 (0)