@@ -20,6 +20,24 @@ function isObjectWithProperties(node) {
20
20
return true ;
21
21
}
22
22
23
+ function getComponentFromNode ( node ) {
24
+ if ( isDefaultExport ( node ) && isObjectWithProperties ( node . declaration ) ) {
25
+ return node . declaration ;
26
+ }
27
+
28
+ if ( node . expression ) {
29
+ const {
30
+ left,
31
+ right,
32
+ } = node . expression ;
33
+ if ( isModuleExports ( left ) && isObjectWithProperties ( right ) ) {
34
+ return right ;
35
+ }
36
+ }
37
+
38
+ return null ;
39
+ }
40
+
23
41
// Objects can contain key names surrounded by quotes, or not
24
42
// propertyArray is the array of Property nodes in the component object
25
43
function astIncludesProperty ( name , propertyArray ) {
@@ -44,20 +62,7 @@ function findPropertyWithName(name, propertyArray) {
44
62
45
63
// Does a component contain the right property? e.g. key, version
46
64
function componentContainsPropertyCheck ( context , node , propertyName , message ) {
47
- let component ;
48
- if ( isDefaultExport ( node ) ) {
49
- component = node . declaration ;
50
- }
51
-
52
- if ( node . expression ) {
53
- const {
54
- left,
55
- right,
56
- } = node . expression ;
57
- if ( isModuleExports ( left ) && isObjectWithProperties ( right ) ) {
58
- component = right ;
59
- }
60
- }
65
+ const component = getComponentFromNode ( node ) ;
61
66
62
67
if ( ! component ) return ;
63
68
if ( ! astIncludesProperty ( propertyName , component . properties ) ) {
@@ -80,19 +85,7 @@ function getProps(moduleProperties) {
80
85
81
86
// Do component props contain the right properties? e.g. label, description
82
87
function componentPropsContainsPropertyCheck ( context , node , propertyName ) {
83
- let component ;
84
- if ( isDefaultExport ( node ) ) {
85
- component = node . declaration ;
86
- }
87
- if ( node . expression ) {
88
- const {
89
- left,
90
- right,
91
- } = node . expression ;
92
- if ( isModuleExports ( left ) && isObjectWithProperties ( right ) ) {
93
- component = right ;
94
- }
95
- }
88
+ const component = getComponentFromNode ( node ) ;
96
89
97
90
if ( ! component ) return ;
98
91
@@ -119,20 +112,7 @@ function componentPropsContainsPropertyCheck(context, node, propertyName) {
119
112
}
120
113
121
114
function optionalComponentPropsHaveDefaultProperty ( context , node ) {
122
- let component ;
123
- if ( isDefaultExport ( node ) ) {
124
- component = node . declaration ;
125
- }
126
-
127
- if ( node . expression ) {
128
- const {
129
- left,
130
- right,
131
- } = node . expression ;
132
- if ( isModuleExports ( left ) && isObjectWithProperties ( right ) ) {
133
- component = right ;
134
- }
135
- }
115
+ const component = getComponentFromNode ( node ) ;
136
116
137
117
if ( ! component ) return ;
138
118
const { properties } = component ;
@@ -166,20 +146,7 @@ function optionalComponentPropsHaveDefaultProperty(context, node) {
166
146
// Checks to confirm the component is a source, and returns
167
147
// the node with the name specified by the user
168
148
function checkComponentIsSourceAndReturnTargetProp ( node , propertyName ) {
169
- let component ;
170
- if ( isDefaultExport ( node ) ) {
171
- component = node . declaration ;
172
- }
173
-
174
- if ( node . expression ) {
175
- const {
176
- left,
177
- right,
178
- } = node . expression ;
179
- if ( isModuleExports ( left ) && isObjectWithProperties ( right ) ) {
180
- component = right ;
181
- }
182
- }
149
+ const component = getComponentFromNode ( node ) ;
183
150
184
151
if ( ! component ) return ;
185
152
const { properties } = component ;
@@ -215,20 +182,7 @@ function componentSourceDescriptionCheck(context, node) {
215
182
}
216
183
217
184
function componentVersionTsMacroCheck ( context , node ) {
218
- let component ;
219
- if ( isDefaultExport ( node ) ) {
220
- component = node . declaration ;
221
- }
222
-
223
- if ( node . expression ) {
224
- const {
225
- left,
226
- right,
227
- } = node . expression ;
228
- if ( isModuleExports ( left ) && isObjectWithProperties ( right ) ) {
229
- component = right ;
230
- }
231
- }
185
+ const component = getComponentFromNode ( node ) ;
232
186
233
187
if ( ! component ) return ;
234
188
const { properties } = component ;
0 commit comments