6
6
7
7
const has = require ( 'has' ) ;
8
8
const variableUtil = require ( '../util/variable' ) ;
9
+ const jsxUtil = require ( '../util/jsx' ) ;
9
10
const docsUrl = require ( '../util/docsUrl' ) ;
10
11
11
12
// ------------------------------------------------------------------------------
@@ -39,16 +40,12 @@ module.exports = {
39
40
const option = context . options [ 0 ] || { } ;
40
41
const maxDepth = has ( option , 'max' ) ? option . max : DEFAULT_DEPTH ;
41
42
42
- function isJSXElement ( node ) {
43
- return node . type === 'JSXElement' ;
44
- }
45
-
46
43
function isExpression ( node ) {
47
44
return node . type === 'JSXExpressionContainer' ;
48
45
}
49
46
50
47
function hasJSX ( node ) {
51
- return isJSXElement ( node ) || isExpression ( node ) && isJSXElement ( node . expression ) ;
48
+ return jsxUtil . isJSX ( node ) || isExpression ( node ) && jsxUtil . isJSX ( node . expression ) ;
52
49
}
53
50
54
51
function isLeaf ( node ) {
@@ -60,9 +57,9 @@ module.exports = {
60
57
function getDepth ( node ) {
61
58
let count = 0 ;
62
59
63
- while ( isJSXElement ( node . parent ) || isExpression ( node . parent ) ) {
60
+ while ( jsxUtil . isJSX ( node . parent ) || isExpression ( node . parent ) ) {
64
61
node = node . parent ;
65
- if ( isJSXElement ( node ) ) {
62
+ if ( jsxUtil . isJSX ( node ) ) {
66
63
count ++ ;
67
64
}
68
65
}
@@ -82,18 +79,18 @@ module.exports = {
82
79
} ) ;
83
80
}
84
81
85
- function findJSXElement ( variables , name ) {
82
+ function findJSXElementOrFragment ( variables , name ) {
86
83
function find ( refs ) {
87
84
let i = refs . length ;
88
85
89
86
while ( -- i >= 0 ) {
90
87
if ( has ( refs [ i ] , 'writeExpr' ) ) {
91
88
const writeExpr = refs [ i ] . writeExpr ;
92
89
93
- return isJSXElement ( writeExpr )
90
+ return jsxUtil . isJSX ( writeExpr )
94
91
&& writeExpr
95
92
|| writeExpr . type === 'Identifier'
96
- && findJSXElement ( variables , writeExpr . name ) ;
93
+ && findJSXElementOrFragment ( variables , writeExpr . name ) ;
97
94
}
98
95
}
99
96
@@ -119,24 +116,28 @@ module.exports = {
119
116
} ) ;
120
117
}
121
118
119
+ function handleJSX ( node ) {
120
+ if ( ! isLeaf ( node ) ) {
121
+ return ;
122
+ }
123
+
124
+ const depth = getDepth ( node ) ;
125
+ if ( depth > maxDepth ) {
126
+ report ( node , depth ) ;
127
+ }
128
+ }
129
+
122
130
return {
123
- JSXElement : function ( node ) {
124
- if ( ! isLeaf ( node ) ) {
125
- return ;
126
- }
131
+ JSXElement : handleJSX ,
132
+ JSXFragment : handleJSX ,
127
133
128
- const depth = getDepth ( node ) ;
129
- if ( depth > maxDepth ) {
130
- report ( node , depth ) ;
131
- }
132
- } ,
133
134
JSXExpressionContainer : function ( node ) {
134
135
if ( node . expression . type !== 'Identifier' ) {
135
136
return ;
136
137
}
137
138
138
139
const variables = variableUtil . variablesInScope ( context ) ;
139
- const element = findJSXElement ( variables , node . expression . name ) ;
140
+ const element = findJSXElementOrFragment ( variables , node . expression . name ) ;
140
141
141
142
if ( element ) {
142
143
const baseDepth = getDepth ( node ) ;
0 commit comments