@@ -45,6 +45,32 @@ function getReturnStatement(node) {
4545 ) ;
4646}
4747
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+
4874const rules = {
4975 'data-component' : {
5076 meta : {
@@ -98,29 +124,9 @@ const rules = {
98124 getReturnStatement ( child ) ,
99125 visitorKeys ,
100126 ( 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 ) ) {
108128 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 ) ) {
124130 flag = true ;
125131
126132 throw DONE_WITH_TREE ;
@@ -160,29 +166,9 @@ const rules = {
160166 getReturnStatement ( componentNode ) ,
161167 visitorKeys ,
162168 ( 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 ) ) {
170170 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 ) ) {
186172 fixNode = current . openingElement ;
187173
188174 throw DONE_WITH_TREE ;
0 commit comments