@@ -9,7 +9,6 @@ const values = require('object.values');
9
9
const astUtil = require ( './ast' ) ;
10
10
const componentUtil = require ( './componentUtil' ) ;
11
11
const testReactVersion = require ( './version' ) . testReactVersion ;
12
- const ast = require ( './ast' ) ;
13
12
const eslintUtil = require ( './eslint' ) ;
14
13
15
14
const getScope = eslintUtil . getScope ;
@@ -164,7 +163,7 @@ function isInLifeCycleMethod(node, checkAsyncSafeLifeCycles) {
164
163
*/
165
164
function isSetStateUpdater ( node ) {
166
165
const unwrappedParentCalleeNode = astUtil . isCallExpression ( node . parent )
167
- && ast . unwrapTSAsExpression ( node . parent . callee ) ;
166
+ && astUtil . unwrapTSAsExpression ( node . parent . callee ) ;
168
167
169
168
return unwrappedParentCalleeNode
170
169
&& unwrappedParentCalleeNode . property
@@ -181,7 +180,7 @@ function isPropArgumentInSetStateUpdater(context, node, name) {
181
180
while ( scope ) {
182
181
const unwrappedParentCalleeNode = scope . block
183
182
&& astUtil . isCallExpression ( scope . block . parent )
184
- && ast . unwrapTSAsExpression ( scope . block . parent . callee ) ;
183
+ && astUtil . unwrapTSAsExpression ( scope . block . parent . callee ) ;
185
184
if (
186
185
unwrappedParentCalleeNode
187
186
&& unwrappedParentCalleeNode . property
@@ -215,7 +214,7 @@ function isInClassComponent(context, node) {
215
214
function isThisDotProps ( node ) {
216
215
return ! ! node
217
216
&& node . type === 'MemberExpression'
218
- && ast . unwrapTSAsExpression ( node . object ) . type === 'ThisExpression'
217
+ && astUtil . unwrapTSAsExpression ( node . object ) . type === 'ThisExpression'
219
218
&& node . property . name === 'props' ;
220
219
}
221
220
@@ -239,7 +238,7 @@ function hasSpreadOperator(context, node) {
239
238
* @returns {boolean }
240
239
*/
241
240
function isPropTypesUsageByMemberExpression ( context , node , utils , checkAsyncSafeLifeCycles ) {
242
- const unwrappedObjectNode = ast . unwrapTSAsExpression ( node . object ) ;
241
+ const unwrappedObjectNode = astUtil . unwrapTSAsExpression ( node . object ) ;
243
242
244
243
if ( isInClassComponent ( context , node ) ) {
245
244
// this.props.*
@@ -260,7 +259,7 @@ function isPropTypesUsageByMemberExpression(context, node, utils, checkAsyncSafe
260
259
return false ;
261
260
}
262
261
// props.* in function component
263
- return unwrappedObjectNode . name === 'props' && ! ast . isAssignmentLHS ( node ) ;
262
+ return unwrappedObjectNode . name === 'props' && ! astUtil . isAssignmentLHS ( node ) ;
264
263
}
265
264
266
265
/**
@@ -321,109 +320,91 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
321
320
let name ;
322
321
let allNames ;
323
322
let properties ;
324
- switch ( node . type ) {
325
- case 'OptionalMemberExpression' :
326
- case 'MemberExpression' :
327
- name = getPropertyName ( context , node , utils , checkAsyncSafeLifeCycles ) ;
328
- if ( name ) {
329
- allNames = parentNames . concat ( name ) ;
330
- if (
331
- // Match props.foo.bar, don't match bar[props.foo]
332
- node . parent . type === 'MemberExpression'
333
- && node . parent . object === node
334
- ) {
335
- markPropTypesAsUsed ( node . parent , allNames ) ;
336
- }
337
- // Handle the destructuring part of `const {foo} = props.a.b`
338
- if (
339
- node . parent . type === 'VariableDeclarator'
340
- && node . parent . id . type === 'ObjectPattern'
341
- ) {
342
- node . parent . id . parent = node . parent ; // patch for bug in eslint@4 in which ObjectPattern has no parent
343
- markPropTypesAsUsed ( node . parent . id , allNames ) ;
344
- }
345
-
346
- // const a = props.a
347
- if (
348
- node . parent . type === 'VariableDeclarator'
349
- && node . parent . id . type === 'Identifier'
350
- ) {
351
- propVariables . set ( node . parent . id . name , allNames ) ;
352
- }
353
- // Do not mark computed props as used.
354
- type = name !== '__COMPUTED_PROP__' ? 'direct' : null ;
323
+ if ( astUtil . isMemberExpression ( node ) ) {
324
+ name = getPropertyName ( context , node , utils , checkAsyncSafeLifeCycles ) ;
325
+ if ( name ) {
326
+ allNames = parentNames . concat ( name ) ;
327
+ const parent = node . parent ;
328
+ if (
329
+ // Match props.foo.bar, don't match bar[props.foo]
330
+ parent . type === 'MemberExpression'
331
+ && 'object' in parent
332
+ && parent . object === node
333
+ ) {
334
+ markPropTypesAsUsed ( parent , allNames ) ;
355
335
}
356
- break ;
357
- case 'ArrowFunctionExpression' :
358
- case 'FunctionDeclaration' :
359
- case 'FunctionExpression' : {
360
- if ( node . params . length === 0 ) {
361
- break ;
336
+ // Handle the destructuring part of `const {foo} = props.a.b`
337
+ if (
338
+ astUtil . isVariableDeclarator ( parent )
339
+ && parent . id . type === 'ObjectPattern'
340
+ ) {
341
+ parent . id . parent = parent ; // patch for bug in eslint@4 in which ObjectPattern has no parent
342
+ markPropTypesAsUsed ( parent . id , allNames ) ;
343
+ }
344
+
345
+ // const a = props.a
346
+ if (
347
+ astUtil . isVariableDeclarator ( parent )
348
+ && parent . id . type === 'Identifier'
349
+ ) {
350
+ propVariables . set ( parent . id . name , allNames ) ;
362
351
}
352
+ // Do not mark computed props as used.
353
+ type = name !== '__COMPUTED_PROP__' ? 'direct' : null ;
354
+ }
355
+ } else if ( astUtil . isFunctionLike ( node ) ) {
356
+ if ( node . params . length > 0 ) {
363
357
type = 'destructuring' ;
364
358
const propParam = isSetStateUpdater ( node ) ? node . params [ 1 ] : node . params [ 0 ] ;
365
359
properties = propParam . type === 'AssignmentPattern'
366
360
? propParam . left . properties
367
361
: propParam . properties ;
368
- break ;
369
362
}
370
- case 'ObjectPattern' :
371
- type = 'destructuring' ;
372
- properties = node . properties ;
373
- break ;
374
- case 'TSEmptyBodyFunctionExpression' :
375
- break ;
376
- default :
377
- throw new Error ( `${ node . type } ASTNodes are not handled by markPropTypesAsUsed` ) ;
363
+ } else if ( astUtil . isObjectPattern ( node ) ) {
364
+ type = 'destructuring' ;
365
+ properties = node . properties ;
366
+ } else if ( node . type !== 'TSEmptyBodyFunctionExpression' ) {
367
+ throw new Error ( `${ node . type } ASTNodes are not handled by markPropTypesAsUsed` ) ;
378
368
}
379
369
380
370
const component = components . get ( utils . getParentComponent ( node ) ) ;
381
371
const usedPropTypes = ( component && component . usedPropTypes ) || [ ] ;
382
372
let ignoreUnusedPropTypesValidation = ( component && component . ignoreUnusedPropTypesValidation ) || false ;
383
373
384
- switch ( type ) {
385
- case 'direct' : {
386
- // Ignore Object methods
387
- if ( name in Object . prototype ) {
388
- break ;
389
- }
390
-
374
+ if ( type === 'direct' ) {
375
+ // Ignore Object methods
376
+ if ( ! ( name in Object . prototype ) ) {
391
377
const reportedNode = node . property ;
392
378
usedPropTypes . push ( {
393
379
name,
394
380
allNames,
395
381
node : reportedNode ,
396
382
} ) ;
397
- break ;
398
383
}
399
- case 'destructuring' : {
400
- for ( let k = 0 , l = ( properties || [ ] ) . length ; k < l ; k ++ ) {
401
- if ( hasSpreadOperator ( context , properties [ k ] ) || properties [ k ] . computed ) {
402
- ignoreUnusedPropTypesValidation = true ;
403
- break ;
404
- }
405
- const propName = ast . getKeyValue ( context , properties [ k ] ) ;
384
+ } else if ( type === 'destructuring' ) {
385
+ for ( let k = 0 , l = ( properties || [ ] ) . length ; k < l ; k ++ ) {
386
+ if ( hasSpreadOperator ( context , properties [ k ] ) || properties [ k ] . computed ) {
387
+ ignoreUnusedPropTypesValidation = true ;
388
+ break ;
389
+ }
390
+ const propName = astUtil . getKeyValue ( context , properties [ k ] ) ;
406
391
407
- if ( ! propName || properties [ k ] . type !== 'Property' ) {
408
- break ;
409
- }
392
+ if ( ! propName || properties [ k ] . type !== 'Property' ) {
393
+ break ;
394
+ }
410
395
411
- usedPropTypes . push ( {
412
- allNames : parentNames . concat ( [ propName ] ) ,
413
- name : propName ,
414
- node : properties [ k ] ,
415
- } ) ;
396
+ usedPropTypes . push ( {
397
+ allNames : parentNames . concat ( [ propName ] ) ,
398
+ name : propName ,
399
+ node : properties [ k ] ,
400
+ } ) ;
416
401
417
- if ( properties [ k ] . value . type === 'ObjectPattern' ) {
418
- markPropTypesAsUsed ( properties [ k ] . value , parentNames . concat ( [ propName ] ) ) ;
419
- } else if ( properties [ k ] . value . type === 'Identifier' ) {
420
- propVariables . set ( properties [ k ] . value . name , parentNames . concat ( propName ) ) ;
421
- }
402
+ if ( properties [ k ] . value . type === 'ObjectPattern' ) {
403
+ markPropTypesAsUsed ( properties [ k ] . value , parentNames . concat ( [ propName ] ) ) ;
404
+ } else if ( properties [ k ] . value . type === 'Identifier' ) {
405
+ propVariables . set ( properties [ k ] . value . name , parentNames . concat ( propName ) ) ;
422
406
}
423
- break ;
424
407
}
425
- default :
426
- break ;
427
408
}
428
409
429
410
components . set ( component ? component . node : node , {
@@ -484,7 +465,7 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
484
465
485
466
return {
486
467
VariableDeclarator ( node ) {
487
- const unwrappedInitNode = ast . unwrapTSAsExpression ( node . init ) ;
468
+ const unwrappedInitNode = astUtil . unwrapTSAsExpression ( node . init ) ;
488
469
489
470
// let props = this.props
490
471
if ( isThisDotProps ( unwrappedInitNode ) && isInClassComponent ( context , node ) && node . id . type === 'Identifier' ) {
@@ -559,7 +540,7 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
559
540
return ;
560
541
}
561
542
562
- const propVariable = propVariables . get ( ast . unwrapTSAsExpression ( node . object ) . name ) ;
543
+ const propVariable = propVariables . get ( astUtil . unwrapTSAsExpression ( node . object ) . name ) ;
563
544
if ( propVariable ) {
564
545
markPropTypesAsUsed ( node , propVariable ) ;
565
546
}
0 commit comments