@@ -360,22 +360,46 @@ module.exports = function(context) {
360
360
function markPropTypesAsUsed ( node , parentNames ) {
361
361
parentNames = parentNames || [ ] ;
362
362
var type ;
363
- var name = getPropertyName ( node . parent ) ;
363
+ var name ;
364
364
var allNames ;
365
- if ( name ) {
366
- allNames = parentNames . concat ( name ) ;
367
- if ( node . parent . type === 'MemberExpression' ) {
368
- markPropTypesAsUsed ( node . parent , allNames ) ;
369
- }
370
- // Do not mark computed props as used.
371
- type = name !== '__COMPUTED_PROP__' ? 'direct' : null ;
372
- } else if (
373
- node . parent . parent . declarations &&
374
- node . parent . parent . declarations [ 0 ] . id . properties &&
375
- getKeyValue ( node . parent . parent . declarations [ 0 ] . id . properties [ 0 ] )
376
- ) {
377
- type = 'destructuring' ;
365
+ var properties ;
366
+ switch ( node . type ) {
367
+ case 'MemberExpression' :
368
+ name = getPropertyName ( node . parent ) ;
369
+ if ( name ) {
370
+ allNames = parentNames . concat ( name ) ;
371
+ if ( node . parent . type === 'MemberExpression' ) {
372
+ markPropTypesAsUsed ( node . parent , allNames ) ;
373
+ }
374
+ // Do not mark computed props as used.
375
+ type = name !== '__COMPUTED_PROP__' ? 'direct' : null ;
376
+ } else if (
377
+ node . parent . parent . declarations &&
378
+ node . parent . parent . declarations [ 0 ] . id . properties &&
379
+ getKeyValue ( node . parent . parent . declarations [ 0 ] . id . properties [ 0 ] )
380
+ ) {
381
+ type = 'destructuring' ;
382
+ properties = node . parent . parent . declarations [ 0 ] . id . properties ;
383
+ }
384
+ break ;
385
+ case 'VariableDeclarator' :
386
+ type = 'destructuring' ;
387
+
388
+ for ( var i = 0 , j = node . id . properties . length ; i < j ; i ++ ) {
389
+ if (
390
+ ( node . id . properties [ i ] . key . name !== 'props' && node . id . properties [ i ] . key . value !== 'props' ) ||
391
+ node . id . properties [ i ] . value . type !== 'ObjectPattern'
392
+ ) {
393
+ continue ;
394
+ }
395
+ properties = node . id . properties [ i ] . value . properties ;
396
+ break ;
397
+ }
398
+ break ;
399
+ default :
400
+ throw new Error ( node . type + ' ASTNodes are not handled by markPropTypesAsUsed' ) ;
378
401
}
402
+
379
403
var component = componentList . getByNode ( context , node ) ;
380
404
var usedPropTypes = component && component . usedPropTypes || [ ] ;
381
405
@@ -392,17 +416,16 @@ module.exports = function(context) {
392
416
} ) ;
393
417
break ;
394
418
case 'destructuring' :
395
- var properties = node . parent . parent . declarations [ 0 ] . id . properties ;
396
- for ( var i = 0 , j = properties . length ; i < j ; i ++ ) {
397
- if ( hasSpreadOperator ( properties [ i ] ) ) {
419
+ for ( var k = 0 , l = properties . length ; k < l ; k ++ ) {
420
+ if ( hasSpreadOperator ( properties [ k ] ) ) {
398
421
continue ;
399
422
}
400
- var propName = getKeyValue ( properties [ i ] ) ;
423
+ var propName = getKeyValue ( properties [ k ] ) ;
401
424
if ( propName ) {
402
425
usedPropTypes . push ( {
403
426
name : propName ,
404
427
allNames : [ propName ] ,
405
- node : properties [ i ]
428
+ node : properties [ k ]
406
429
} ) ;
407
430
}
408
431
}
@@ -509,6 +532,13 @@ module.exports = function(context) {
509
532
markPropTypesAsDeclared ( node , node . value ) ;
510
533
} ,
511
534
535
+ VariableDeclarator : function ( node ) {
536
+ if ( node . init . type !== 'ThisExpression' || node . id . type !== 'ObjectPattern' ) {
537
+ return ;
538
+ }
539
+ markPropTypesAsUsed ( node ) ;
540
+ } ,
541
+
512
542
MemberExpression : function ( node ) {
513
543
var type ;
514
544
if ( isPropTypesUsage ( node ) ) {
0 commit comments