@@ -73,6 +73,75 @@ module.exports = (file, api, options) => {
73
73
}
74
74
}
75
75
76
+ // ---------------------------------------------------------------------------
77
+ // Helpers
78
+ const createFindPropFn = prop => property => (
79
+ property . key &&
80
+ property . key . type === 'Identifier' &&
81
+ property . key . name === prop
82
+ ) ;
83
+
84
+ const filterDefaultPropsField = node =>
85
+ createFindPropFn ( DEFAULT_PROPS_FIELD ) ( node ) ;
86
+
87
+ const filterGetInitialStateField = node =>
88
+ createFindPropFn ( GET_INITIAL_STATE_FIELD ) ( node ) ;
89
+
90
+ const findGetInitialState = specPath =>
91
+ specPath . properties . find ( createFindPropFn ( GET_INITIAL_STATE_FIELD ) ) ;
92
+
93
+ const withComments = ( to , from ) => {
94
+ to . comments = from . comments ;
95
+ return to ;
96
+ } ;
97
+
98
+ const isPrimExpression = node => (
99
+ node . type === 'Literal' || ( // NOTE this might change in babylon v6
100
+ node . type === 'Identifier' &&
101
+ node . name === 'undefined'
102
+ ) ) ;
103
+
104
+ const isFunctionExpression = node => (
105
+ node . key &&
106
+ node . key . type === 'Identifier' &&
107
+ node . value &&
108
+ node . value . type === 'FunctionExpression'
109
+ ) ;
110
+
111
+ const isPrimProperty = prop => (
112
+ prop . key &&
113
+ prop . key . type === 'Identifier' &&
114
+ prop . value &&
115
+ isPrimExpression ( prop . value )
116
+ ) ;
117
+
118
+ const isPrimPropertyWithTypeAnnotation = prop => (
119
+ prop . key &&
120
+ prop . key . type === 'Identifier' &&
121
+ prop . value &&
122
+ prop . value . type === 'TypeCastExpression' &&
123
+ isPrimExpression ( prop . value . expression )
124
+ ) ;
125
+
126
+ const hasSingleReturnStatementWithObject = value => (
127
+ value . type === 'FunctionExpression' &&
128
+ value . body &&
129
+ value . body . type === 'BlockStatement' &&
130
+ value . body . body &&
131
+ value . body . body . length === 1 &&
132
+ value . body . body [ 0 ] . type === 'ReturnStatement' &&
133
+ value . body . body [ 0 ] . argument &&
134
+ value . body . body [ 0 ] . argument . type === 'ObjectExpression'
135
+ ) ;
136
+
137
+ const isInitialStateLiftable = getInitialState => {
138
+ if ( ! getInitialState || ! ( getInitialState . value ) ) {
139
+ return true ;
140
+ }
141
+
142
+ return hasSingleReturnStatementWithObject ( getInitialState . value ) ;
143
+ } ;
144
+
76
145
// ---------------------------------------------------------------------------
77
146
// Checks if the module uses mixins or accesses deprecated APIs.
78
147
const checkDeprecatedAPICalls = classPath =>
@@ -232,57 +301,28 @@ module.exports = (file, api, options) => {
232
301
return true ;
233
302
} ;
234
303
235
- // ---------------------------------------------------------------------------
236
- // Helpers
237
- const createFindPropFn = prop => property => (
238
- property . key &&
239
- property . key . type === 'Identifier' &&
240
- property . key . name === prop
241
- ) ;
242
-
243
- const filterDefaultPropsField = node =>
244
- createFindPropFn ( DEFAULT_PROPS_FIELD ) ( node ) ;
245
-
246
- const filterGetInitialStateField = node =>
247
- createFindPropFn ( GET_INITIAL_STATE_FIELD ) ( node ) ;
248
-
249
- const findGetInitialState = specPath =>
250
- specPath . properties . find ( createFindPropFn ( GET_INITIAL_STATE_FIELD ) ) ;
251
-
252
- const withComments = ( to , from ) => {
253
- to . comments = from . comments ;
254
- return to ;
255
- } ;
256
-
257
304
// ---------------------------------------------------------------------------
258
305
// Collectors
259
- const isFunctionExpression = node => (
260
- node . key &&
261
- node . key . type === 'Identifier' &&
262
- node . value &&
263
- node . value . type === 'FunctionExpression'
264
- ) ;
265
-
266
- const isPrimProperty = prop => (
267
- prop . key &&
268
- prop . key . type === 'Identifier' &&
269
- prop . value &&
270
- isPrimExpression ( prop . value )
271
- ) ;
272
-
273
- const isPrimPropertyWithTypeAnnotation = prop => (
274
- prop . key &&
275
- prop . key . type === 'Identifier' &&
276
- prop . value &&
277
- prop . value . type === 'TypeCastExpression' &&
278
- isPrimExpression ( prop . value . expression )
279
- ) ;
306
+ const pickReturnValueOrCreateIIFE = value => {
307
+ if ( hasSingleReturnStatementWithObject ( value ) ) {
308
+ return value . body . body [ 0 ] . argument ;
309
+ } else {
310
+ return j . callExpression (
311
+ value ,
312
+ [ ]
313
+ ) ;
314
+ }
315
+ } ;
280
316
281
- const isPrimExpression = node => (
282
- node . type === 'Literal' || ( // NOTE this might change in babylon v6
283
- node . type === 'Identifier' &&
284
- node . name === 'undefined'
285
- ) ) ;
317
+ const createDefaultProps = prop =>
318
+ withComments (
319
+ j . property (
320
+ 'init' ,
321
+ j . identifier ( DEFAULT_PROPS_KEY ) ,
322
+ pickReturnValueOrCreateIIFE ( prop . value )
323
+ ) ,
324
+ prop
325
+ ) ;
286
326
287
327
// Collects `childContextTypes`, `contextTypes`, `displayName`, and `propTypes`;
288
328
// simplifies `getDefaultProps` or converts it to an IIFE;
@@ -361,14 +401,6 @@ module.exports = (file, api, options) => {
361
401
fn . value
362
402
) , fn ) ;
363
403
364
- const isInitialStateLiftable = getInitialState => {
365
- if ( ! getInitialState || ! ( getInitialState . value ) ) {
366
- return true ;
367
- }
368
-
369
- return hasSingleReturnStatementWithObject ( getInitialState . value ) ;
370
- } ;
371
-
372
404
const updatePropsAccess = getInitialState =>
373
405
j ( getInitialState )
374
406
. find ( j . MemberExpression , {
@@ -440,17 +472,6 @@ module.exports = (file, api, options) => {
440
472
} ) . getAST ( ) [ 0 ] . value . body . body ;
441
473
} ;
442
474
443
- const pickReturnValueOrCreateIIFE = value => {
444
- if ( hasSingleReturnStatementWithObject ( value ) ) {
445
- return value . body . body [ 0 ] . argument ;
446
- } else {
447
- return j . callExpression (
448
- value ,
449
- [ ]
450
- ) ;
451
- }
452
- } ;
453
-
454
475
const convertInitialStateToClassProperty = getInitialState =>
455
476
withComments ( j . classProperty (
456
477
j . identifier ( 'state' ) ,
@@ -853,27 +874,6 @@ module.exports = (file, api, options) => {
853
874
const createStaticClassProperties = statics =>
854
875
statics . map ( createStaticClassProperty ) ;
855
876
856
- const hasSingleReturnStatementWithObject = value => (
857
- value . type === 'FunctionExpression' &&
858
- value . body &&
859
- value . body . type === 'BlockStatement' &&
860
- value . body . body &&
861
- value . body . body . length === 1 &&
862
- value . body . body [ 0 ] . type === 'ReturnStatement' &&
863
- value . body . body [ 0 ] . argument &&
864
- value . body . body [ 0 ] . argument . type === 'ObjectExpression'
865
- ) ;
866
-
867
- const createDefaultProps = prop =>
868
- withComments (
869
- j . property (
870
- 'init' ,
871
- j . identifier ( DEFAULT_PROPS_KEY ) ,
872
- pickReturnValueOrCreateIIFE ( prop . value )
873
- ) ,
874
- prop
875
- ) ;
876
-
877
877
const getComments = classPath => {
878
878
if ( classPath . value . comments ) {
879
879
return classPath . value . comments ;
0 commit comments