@@ -11,33 +11,37 @@ function getJSXElementName(jsx) {
1111 }
1212}
1313
14- function handleJSX ( context , name , jsx ) {
14+ function handleJSX ( context , name , exported , jsx ) {
1515 if (
1616 ! providerRegex . test ( getJSXElementName ( jsx ) ) &&
1717 ! jsx . openingElement . attributes . find (
1818 ( a ) => a . name ?. name === 'data-component' ,
1919 )
2020 ) {
21- context . report ( {
22- node : jsx ,
23- message : `${ name } is missing the data-component attribute for the top-level element.` ,
24- fix ( fixer ) {
25- return fixer . insertTextAfterRange (
26- jsx . openingElement . typeParameters
27- ? jsx . openingElement . typeParameters . range
28- : jsx . openingElement . name . range ,
29- ` data-component="${ name } "` ,
30- ) ;
21+ context . possibleReports . push ( {
22+ name,
23+ exported,
24+ report : {
25+ node : jsx . openingElement ,
26+ message : `${ name } is missing the data-component attribute for the top-level element.` ,
27+ fix ( fixer ) {
28+ return fixer . insertTextAfterRange (
29+ jsx . openingElement . typeParameters
30+ ? jsx . openingElement . typeParameters . range
31+ : jsx . openingElement . name . range ,
32+ ` data-component="${ name } "` ,
33+ ) ;
34+ } ,
3135 } ,
3236 } ) ;
3337 }
3438}
3539
36- function handleBlockStatement ( context , name , block ) {
40+ function handleBlockStatement ( context , name , exported , block ) {
3741 // Find the root return statement. Are there any other types of returns we need to handle?
3842 const ret = block . body . find ( ( c ) => c . type === 'ReturnStatement' ) ;
3943 if ( ret && ret . argument . type === 'JSXElement' ) {
40- handleJSX ( context , name , ret . argument ) ;
44+ handleJSX ( context , name , exported , ret . argument ) ;
4145 }
4246}
4347
@@ -49,31 +53,31 @@ function isForwardRef(expression) {
4953 return calleeName === 'forwardRef' && expression . arguments . length == 1 ;
5054}
5155
52- function handleExpression ( context , name , expression ) {
56+ function handleExpression ( context , name , exported , expression ) {
5357 switch ( expression . type ) {
5458 case 'FunctionExpression' :
55- handleBlockStatement ( context , name , expression . body ) ;
59+ handleBlockStatement ( context , name , exported , expression . body ) ;
5660 break ;
5761 case 'ArrowFunctionExpression' :
5862 switch ( expression . body . type ) {
5963 case 'JSXElement' :
60- handleJSX ( context , name , expression . body ) ;
64+ handleJSX ( context , name , exported , expression . body ) ;
6165 break ;
6266 case 'BlockStatement' :
63- handleBlockStatement ( context , name , expression . body ) ;
67+ handleBlockStatement ( context , name , exported , expression . body ) ;
6468 break ;
6569 }
6670 break ;
6771 case 'ClassExpression' :
6872 expression . body . body . forEach ( ( x ) => {
6973 if ( x . type === 'MethodDefinition' && x . key . name === 'render' ) {
70- handleBlockStatement ( context , name , x . value . body ) ;
74+ handleBlockStatement ( context , name , exported , x . value . body ) ;
7175 }
7276 } ) ;
7377 break ;
7478 case 'CallExpression' :
7579 if ( isForwardRef ( expression ) ) {
76- handleExpression ( context , name , expression . arguments [ 0 ] ) ;
80+ handleExpression ( context , name , exported , expression . arguments [ 0 ] ) ;
7781 }
7882 break ;
7983 }
@@ -94,6 +98,7 @@ const rules = {
9498 create ( context ) {
9599 return {
96100 Program ( root ) {
101+ context = { ...context , possibleReports : [ ] } ;
97102 root . body . forEach ( ( node ) => {
98103 // We will need to save any non-exported declarations and handle them only if they get exported at the end
99104 let exported = false ;
@@ -109,19 +114,34 @@ const rules = {
109114 switch ( node . type ) {
110115 case 'VariableDeclaration' :
111116 node . declarations . forEach ( ( variable ) => {
112- handleExpression ( context , variable . id . name , variable . init ) ;
117+ handleExpression (
118+ context ,
119+ variable . id . name ,
120+ exported ,
121+ variable . init ,
122+ ) ;
113123 } ) ;
114124 break ;
115125 case 'FunctionDeclaration' :
116- handleBlockStatement ( context , node . id . name , node . body ) ;
126+ handleBlockStatement (
127+ context ,
128+ node . id . name ,
129+ exported ,
130+ node . body ,
131+ ) ;
117132 break ;
118133 case 'ClassDeclaration' :
119134 node . body . body . forEach ( ( x ) => {
120135 if (
121136 x . type === 'MethodDefinition' &&
122137 x . key . name === 'render'
123138 ) {
124- handleBlockStatement ( context , node . id . name , x . value . body ) ;
139+ handleBlockStatement (
140+ context ,
141+ node . id . name ,
142+ exported ,
143+ x . value . body ,
144+ ) ;
125145 return ;
126146 }
127147 } ) ;
@@ -135,10 +155,31 @@ const rules = {
135155 handleExpression (
136156 context ,
137157 node . arguments [ 0 ] . id . name ,
158+ exported ,
138159 node . arguments [ 0 ] ,
139160 ) ;
140161 }
141162 break ;
163+ case 'AssignmentExpression' :
164+ handleExpression ( context , node . left . name , exported , node . right ) ;
165+ break ;
166+ case 'ExportNamedDeclaration' :
167+ node . specifiers . forEach ( ( s ) => {
168+ const report = context . possibleReports . find (
169+ ( r ) => r . name === s . local . name ,
170+ ) ;
171+ if ( report ) {
172+ report . exported = true ;
173+ }
174+ } ) ;
175+ break ;
176+ }
177+ } ) ;
178+
179+ // Report all issues for exported components
180+ context . possibleReports . forEach ( ( r ) => {
181+ if ( r . exported ) {
182+ context . report ( r . report ) ;
142183 }
143184 } ) ;
144185 } ,
0 commit comments