@@ -16,7 +16,7 @@ import fs from 'fs';
16
16
17
17
const { namedTypes : t , NodePath } = types ;
18
18
19
- export default function resolveImportedValue ( path : NodePath , name : string ) {
19
+ export default function resolveImportedValue ( path : NodePath , name : string , seen : Set < string > = new Set ( ) ) {
20
20
// Bail if no filename was provided for the current source file.
21
21
// Also never traverse into react itself.
22
22
const source = path . node . source . value ;
@@ -38,6 +38,13 @@ export default function resolveImportedValue(path: NodePath, name: string) {
38
38
return null ;
39
39
}
40
40
41
+ // Prevent recursive imports
42
+ if ( seen . has ( resolvedSource ) ) {
43
+ return null ;
44
+ }
45
+
46
+ seen . add ( resolvedSource ) ;
47
+
41
48
// Read and parse the code
42
49
// TODO: cache and reuse
43
50
const code = fs . readFileSync ( resolvedSource , 'utf8' ) ;
@@ -49,7 +56,7 @@ export default function resolveImportedValue(path: NodePath, name: string) {
49
56
50
57
const parser = buildParser ( parseOptions ) ;
51
58
const ast = parser . parse ( code ) ;
52
- return findExportedValue ( ast . program , name ) ;
59
+ return findExportedValue ( ast . program , name , seen ) ;
53
60
}
54
61
55
62
// Find the root Program node, which we attached our options too in babelParser.js
@@ -62,7 +69,7 @@ function getOptions(path: NodePath): Options {
62
69
}
63
70
64
71
// Traverses the program looking for an export that matches the requested name
65
- function findExportedValue ( ast , name ) {
72
+ function findExportedValue ( ast , name , seen ) {
66
73
let resultPath : ?NodePath = null ;
67
74
68
75
traverseShallow ( ast , {
@@ -100,7 +107,7 @@ function findExportedValue(ast, name) {
100
107
return false ;
101
108
} ,
102
109
visitExportAllDeclaration ( path : NodePath ) {
103
- const resolvedPath = resolveImportedValue ( path , name ) ;
110
+ const resolvedPath = resolveImportedValue ( path , name , seen ) ;
104
111
if ( resolvedPath ) {
105
112
resultPath = resolvedPath ;
106
113
}
0 commit comments