@@ -135,9 +135,6 @@ module.exports = (file, api, options) => {
135
135
const filterGetInitialStateField = node =>
136
136
createFindPropFn ( GET_INITIAL_STATE_FIELD ) ( node ) ;
137
137
138
- const findGetDefaultProps = specPath =>
139
- specPath . properties . find ( createFindPropFn ( DEFAULT_PROPS_FIELD ) ) ;
140
-
141
138
const findGetInitialState = specPath =>
142
139
specPath . properties . find ( createFindPropFn ( GET_INITIAL_STATE_FIELD ) ) ;
143
140
@@ -155,24 +152,23 @@ module.exports = (file, api, options) => {
155
152
node . value . type === 'FunctionExpression'
156
153
) ;
157
154
158
- // Collects `childContextTypes`, `contextTypes`, `displayName`, and `propTypes` first ;
159
- // then simplifies `getDefaultProps` or converts it to an IIFE;
160
- // finally it collects everything in the `statics` property object.
155
+ // Collects `childContextTypes`, `contextTypes`, `displayName`, and `propTypes`;
156
+ // simplifies `getDefaultProps` or converts it to an IIFE;
157
+ // and collects everything else in the `statics` property object.
161
158
const collectStatics = specPath => {
162
- let result = specPath . properties . filter ( property =>
163
- property . key && STATIC_KEYS [ property . key . name ]
164
- ) ;
165
-
166
- const getDefaultProps = findGetDefaultProps ( specPath ) ;
167
- if ( getDefaultProps ) {
168
- result . push ( createDefaultProps ( getDefaultProps ) ) ;
159
+ const result = [ ] ;
160
+
161
+ for ( let i = 0 ; i < specPath . properties . length ; i ++ ) {
162
+ const property = specPath . properties [ i ] ;
163
+ if ( createFindPropFn ( 'statics' ) ( property ) && property . value && property . value . properties ) {
164
+ result . push ( ...property . value . properties ) ;
165
+ } else if ( createFindPropFn ( DEFAULT_PROPS_FIELD ) ( property ) ) {
166
+ result . push ( createDefaultProps ( property ) ) ;
167
+ } else if ( property . key && STATIC_KEYS [ property . key . name ] ) {
168
+ result . push ( property ) ;
169
+ }
169
170
}
170
171
171
- const statics = specPath . properties . find ( createFindPropFn ( 'statics' ) ) ;
172
- result = result . concat (
173
- ( statics && statics . value && statics . value . properties ) || [ ]
174
- ) ;
175
-
176
172
return result ;
177
173
} ;
178
174
@@ -182,69 +178,6 @@ module.exports = (file, api, options) => {
182
178
)
183
179
. filter ( isFunctionExpression ) ;
184
180
185
- const findAutobindNamesFor = ( subtree , fnNames , literalOrIdentifier ) => {
186
- const node = literalOrIdentifier ;
187
- const autobindNames = { } ;
188
-
189
- j ( subtree )
190
- . find ( j . MemberExpression , {
191
- object : node . name ? {
192
- type : node . type ,
193
- name : node . name ,
194
- } : { type : node . type } ,
195
- property : {
196
- type : 'Identifier' ,
197
- } ,
198
- } )
199
- . filter ( path => path . value . property && fnNames [ path . value . property . name ] )
200
- . filter ( path => {
201
- const call = path . parent . value ;
202
- return ! (
203
- call &&
204
- call . type === 'CallExpression' &&
205
- call . callee . type === 'MemberExpression' &&
206
- call . callee . object . type === node . type &&
207
- call . callee . object . name === node . name &&
208
- call . callee . property . type === 'Identifier' &&
209
- call . callee . property . name === path . value . property . name
210
- ) ;
211
- } )
212
- . forEach ( path => autobindNames [ path . value . property . name ] = true ) ;
213
-
214
- return Object . keys ( autobindNames ) ;
215
- } ;
216
-
217
- const collectAutoBindFunctions = ( functions , classPath ) => {
218
- const fnNames = { } ;
219
- functions
220
- . filter ( fn => ! AUTOBIND_IGNORE_KEYS [ fn . key . name ] )
221
- . forEach ( fn => fnNames [ fn . key . name ] = true ) ;
222
-
223
- const autobindNames = { } ;
224
- const add = name => autobindNames [ name ] = true ;
225
-
226
- // Find `this.<foo>`
227
- findAutobindNamesFor ( classPath , fnNames , j . thisExpression ( ) ) . forEach ( add ) ;
228
-
229
- // Find `self.<foo>` if `self = this`
230
- j ( classPath )
231
- . findVariableDeclarators ( )
232
- . filter ( path => (
233
- path . value . id . type === 'Identifier' &&
234
- path . value . init &&
235
- path . value . init . type === 'ThisExpression'
236
- ) )
237
- . forEach ( path =>
238
- findAutobindNamesFor (
239
- j ( path ) . closest ( j . FunctionExpression ) . get ( ) ,
240
- fnNames ,
241
- path . value . id
242
- ) . forEach ( add )
243
- ) ;
244
-
245
- return Object . keys ( autobindNames ) ;
246
- } ;
247
-
248
181
const findRequirePathAndBinding = ( moduleName ) => {
249
182
let result = null ;
250
183
@@ -435,42 +368,34 @@ module.exports = (file, api, options) => {
435
368
baseClassName ,
436
369
staticProperties ,
437
370
getInitialState ,
438
- autobindFunctionNames ,
439
371
methods ,
440
372
comments
441
373
) => {
442
- let newConstructor = [ ] ;
443
- const newProperties = [ ] ;
374
+ let maybeConstructor = [ ] ;
375
+ const initialStateProperty = [ ] ;
444
376
445
377
if ( isInitialStateLiftable ( getInitialState ) ) {
446
378
if ( getInitialState ) {
447
- newProperties . push ( convertInitialStateToClassProperty ( getInitialState ) ) ;
379
+ initialStateProperty . push ( convertInitialStateToClassProperty ( getInitialState ) ) ;
448
380
}
449
381
} else {
450
- newConstructor = createConstructor ( getInitialState ) ;
382
+ maybeConstructor = createConstructor ( getInitialState ) ;
451
383
}
452
384
453
- const arrowBindFunctions = [ ] ;
454
- const newMethods = [ ] ;
455
-
456
- for ( let i = 0 ; i < methods . length ; i ++ ) {
457
- const method = methods [ i ] ;
458
- if ( autobindFunctionNames . indexOf ( method . key . name ) !== - 1 ) {
459
- arrowBindFunctions . push ( method ) ;
460
- } else {
461
- newMethods . push ( method ) ;
462
- }
463
- }
385
+ const arrowBindFunctionsAndMethods = methods . map ( method =>
386
+ AUTOBIND_IGNORE_KEYS [ method . key . name ] ?
387
+ method :
388
+ createArrowPropertyFromMethod ( method )
389
+ ) ;
464
390
465
391
return withComments ( j . classDeclaration (
466
392
name ? j . identifier ( name ) : null ,
467
393
j . classBody (
468
394
[ ] . concat (
469
395
staticProperties ,
470
- newConstructor ,
471
- newProperties ,
472
- arrowBindFunctions . map ( createArrowPropertyFromMethod ) ,
473
- newMethods
396
+ maybeConstructor ,
397
+ initialStateProperty ,
398
+ arrowBindFunctionsAndMethods
474
399
)
475
400
) ,
476
401
j . memberExpression (
@@ -531,7 +456,6 @@ module.exports = (file, api, options) => {
531
456
const functions = collectFunctions ( specPath ) ;
532
457
const comments = getComments ( classPath ) ;
533
458
534
- const autobindFunctionNames = collectAutoBindFunctions ( functions , classPath ) ;
535
459
const getInitialState = findGetInitialState ( specPath ) ;
536
460
537
461
var path ;
@@ -554,7 +478,6 @@ module.exports = (file, api, options) => {
554
478
baseClassName ,
555
479
staticProperties ,
556
480
getInitialState ,
557
- autobindFunctionNames ,
558
481
functions . map ( createMethodDefinition ) ,
559
482
comments
560
483
)
0 commit comments