@@ -45,6 +45,32 @@ function getReturnStatement(node) {
45
45
) ;
46
46
}
47
47
48
+ function isTreeDone ( node , excludeComponentNames ) {
49
+ return (
50
+ node . type === 'JSXElement' &&
51
+ excludeComponentNames . every (
52
+ ( regex ) =>
53
+ ! regex . test (
54
+ node . openingElement . name . property
55
+ ? node . openingElement . name . property . name
56
+ : node . openingElement . name . name ,
57
+ ) ,
58
+ ) &&
59
+ ! node . openingElement . attributes . find (
60
+ ( attributeNode ) => attributeNode . name ?. name === 'data-component' ,
61
+ )
62
+ ) ;
63
+ }
64
+
65
+ function isSubtreeDone ( node ) {
66
+ return (
67
+ node . type === 'JSXElement' &&
68
+ node . openingElement . attributes . find (
69
+ ( attributeNode ) => attributeNode . name ?. name === 'data-component' ,
70
+ )
71
+ ) ;
72
+ }
73
+
48
74
const rules = {
49
75
'data-component' : {
50
76
meta : {
@@ -98,29 +124,9 @@ const rules = {
98
124
getReturnStatement ( child ) ,
99
125
visitorKeys ,
100
126
( current ) => {
101
- if (
102
- current . type === 'JSXElement' &&
103
- current . openingElement . attributes . find (
104
- ( attributeNode ) =>
105
- attributeNode . name ?. name === 'data-component' ,
106
- )
107
- ) {
127
+ if ( isSubtreeDone ( current ) ) {
108
128
throw DONE_WITH_SUBTREE ;
109
- } else if (
110
- current . type === 'JSXElement' &&
111
- excludeComponentNames . every (
112
- ( regex ) =>
113
- ! regex . test (
114
- current . openingElement . name . property
115
- ? current . openingElement . name . property . name
116
- : current . openingElement . name . name ,
117
- ) ,
118
- ) &&
119
- ! current . openingElement . attributes . find (
120
- ( attributeNode ) =>
121
- attributeNode . name ?. name === 'data-component' ,
122
- )
123
- ) {
129
+ } else if ( isTreeDone ( current , excludeComponentNames ) ) {
124
130
flag = true ;
125
131
126
132
throw DONE_WITH_TREE ;
@@ -160,29 +166,9 @@ const rules = {
160
166
getReturnStatement ( componentNode ) ,
161
167
visitorKeys ,
162
168
( current ) => {
163
- if (
164
- current . type === 'JSXElement' &&
165
- current . openingElement . attributes . find (
166
- ( attributeNode ) =>
167
- attributeNode . name ?. name === 'data-component' ,
168
- )
169
- ) {
169
+ if ( isSubtreeDone ( current ) ) {
170
170
throw DONE_WITH_SUBTREE ;
171
- } else if (
172
- current . type === 'JSXElement' &&
173
- excludeComponentNames . every (
174
- ( regex ) =>
175
- ! regex . test (
176
- current . openingElement . name . property
177
- ? current . openingElement . name . property . name
178
- : current . openingElement . name . name ,
179
- ) ,
180
- ) &&
181
- ! current . openingElement . attributes . find (
182
- ( attributeNode ) =>
183
- attributeNode . name ?. name === 'data-component' ,
184
- )
185
- ) {
171
+ } else if ( isTreeDone ( current , excludeComponentNames ) ) {
186
172
fixNode = current . openingElement ;
187
173
188
174
throw DONE_WITH_TREE ;
0 commit comments