6
6
'use strict' ;
7
7
8
8
const has = require ( 'has' ) ;
9
+ const includes = require ( 'array-includes' ) ;
9
10
const variableUtil = require ( '../util/variable' ) ;
10
11
const jsxUtil = require ( '../util/jsx' ) ;
11
12
const docsUrl = require ( '../util/docsUrl' ) ;
@@ -83,8 +84,8 @@ module.exports = {
83
84
} ) ;
84
85
}
85
86
86
- function findJSXElementOrFragment ( variables , name ) {
87
- function find ( refs ) {
87
+ function findJSXElementOrFragment ( variables , name , previousReferences ) {
88
+ function find ( refs , prevRefs ) {
88
89
let i = refs . length ;
89
90
90
91
while ( -- i >= 0 ) {
@@ -94,15 +95,26 @@ module.exports = {
94
95
return ( jsxUtil . isJSX ( writeExpr )
95
96
&& writeExpr )
96
97
|| ( ( writeExpr && writeExpr . type === 'Identifier' )
97
- && findJSXElementOrFragment ( variables , writeExpr . name ) ) ;
98
+ && findJSXElementOrFragment ( variables , writeExpr . name , prevRefs ) ) ;
98
99
}
99
100
}
100
101
101
102
return null ;
102
103
}
103
104
104
105
const variable = variableUtil . getVariable ( variables , name ) ;
105
- return variable && variable . references && find ( variable . references ) ;
106
+ if ( variable && variable . references ) {
107
+ const containDuplicates = previousReferences . some ( ( ref ) => includes ( variable . references , ref ) ) ;
108
+
109
+ // Prevent getting stuck in circular references
110
+ if ( containDuplicates ) {
111
+ return false ;
112
+ }
113
+
114
+ return find ( variable . references , previousReferences . concat ( variable . references ) ) ;
115
+ }
116
+
117
+ return false ;
106
118
}
107
119
108
120
function checkDescendant ( baseDepth , children ) {
@@ -141,7 +153,7 @@ module.exports = {
141
153
}
142
154
143
155
const variables = variableUtil . variablesInScope ( context ) ;
144
- const element = findJSXElementOrFragment ( variables , node . expression . name ) ;
156
+ const element = findJSXElementOrFragment ( variables , node . expression . name , [ ] ) ;
145
157
146
158
if ( element ) {
147
159
const baseDepth = getDepth ( node ) ;
0 commit comments