@@ -35,10 +35,7 @@ function isPropAttributeName (node) {
3535 * @returns {Boolean } True if the component must be validated, false if not.
3636 */
3737function mustBeValidated ( component ) {
38- return Boolean (
39- component &&
40- ! component . ignorePropsValidation
41- ) ;
38+ return ! ! ( component && ! component . ignorePropsValidation ) ;
4239}
4340
4441module . exports = function usedPropTypesInstructions ( context , components , utils ) {
@@ -53,8 +50,10 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
5350 let scope = context . getScope ( ) ;
5451 while ( scope ) {
5552 if (
56- scope . block && scope . block . parent &&
57- scope . block . parent . key && scope . block . parent . key . name === 'componentWillReceiveProps'
53+ scope . block
54+ && scope . block . parent
55+ && scope . block . parent . key
56+ && scope . block . parent . key . name === 'componentWillReceiveProps'
5857 ) {
5958 return true ;
6059 }
@@ -75,7 +74,8 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
7574
7675 if ( LIFE_CYCLE_METHODS . indexOf ( name ) >= 0 ) {
7776 return true ;
78- } else if ( checkAsyncSafeLifeCycles && ASYNC_SAFE_LIFE_CYCLE_METHODS . indexOf ( name ) >= 0 ) {
77+ }
78+ if ( checkAsyncSafeLifeCycles && ASYNC_SAFE_LIFE_CYCLE_METHODS . indexOf ( name ) >= 0 ) {
7979 return true ;
8080 }
8181 }
@@ -94,9 +94,11 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
9494
9595 if ( node . kind === 'constructor' ) {
9696 return true ;
97- } else if ( LIFE_CYCLE_METHODS . indexOf ( nodeKeyName ) >= 0 ) {
97+ }
98+ if ( LIFE_CYCLE_METHODS . indexOf ( nodeKeyName ) >= 0 ) {
9899 return true ;
99- } else if ( checkAsyncSafeLifeCycles && ASYNC_SAFE_LIFE_CYCLE_METHODS . indexOf ( nodeKeyName ) >= 0 ) {
100+ }
101+ if ( checkAsyncSafeLifeCycles && ASYNC_SAFE_LIFE_CYCLE_METHODS . indexOf ( nodeKeyName ) >= 0 ) {
100102 return true ;
101103 }
102104
@@ -371,7 +373,7 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
371373 switch ( type ) {
372374 case 'direct' :
373375 // Ignore Object methods
374- if ( Object . prototype [ name ] ) {
376+ if ( name in Object . prototype ) {
375377 break ;
376378 }
377379
@@ -503,22 +505,15 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
503505 ObjectPattern : function ( node ) {
504506 // If the object pattern is a destructured props object in a lifecycle
505507 // method -- mark it for used props.
506- if ( isNodeALifeCycleMethod ( node . parent . parent ) ) {
507- node . properties . forEach ( ( property , i ) => {
508- if ( i === 0 ) {
509- markPropTypesAsUsed ( node . parent ) ;
510- }
511- } ) ;
508+ if ( isNodeALifeCycleMethod ( node . parent . parent ) && node . properties . length > 0 ) {
509+ markPropTypesAsUsed ( node . parent ) ;
512510 }
513511 } ,
514512
515513 'Program:exit' : function ( ) {
516514 const list = components . list ( ) ;
517515
518- Object . keys ( list ) . forEach ( component => {
519- if ( ! mustBeValidated ( list [ component ] ) ) {
520- return ;
521- }
516+ Object . keys ( list ) . filter ( component => mustBeValidated ( list [ component ] ) ) . forEach ( component => {
522517 handleCustomValidators ( list [ component ] ) ;
523518 } ) ;
524519 }
0 commit comments