@@ -10,6 +10,7 @@ const Components = require('../util/Components');
10
10
const variableUtil = require ( '../util/variable' ) ;
11
11
const annotations = require ( '../util/annotations' ) ;
12
12
const astUtil = require ( '../util/ast' ) ;
13
+ const propsUtil = require ( '../util/props' ) ;
13
14
14
15
// ------------------------------------------------------------------------------
15
16
// Rule Definition
@@ -39,34 +40,6 @@ module.exports = {
39
40
const allowRequiredDefaults = configuration . allowRequiredDefaults || false ;
40
41
const propWrapperFunctions = new Set ( context . settings . propWrapperFunctions || [ ] ) ;
41
42
42
- /**
43
- * Checks if the Identifier node passed in looks like a propTypes declaration.
44
- * @param {ASTNode } node The node to check. Must be an Identifier node.
45
- * @returns {Boolean } `true` if the node is a propTypes declaration, `false` if not
46
- */
47
- function isPropTypesDeclaration ( node ) {
48
- return astUtil . getPropertyName ( node ) === 'propTypes' ;
49
- }
50
-
51
- /**
52
- * Checks if the Identifier node passed in looks like a defaultProps declaration.
53
- * @param {ASTNode } node The node to check. Must be an Identifier node.
54
- * @returns {Boolean } `true` if the node is a defaultProps declaration, `false` if not
55
- */
56
- function isDefaultPropsDeclaration ( node ) {
57
- const propName = astUtil . getPropertyName ( node ) ;
58
- return ( propName === 'defaultProps' || propName === 'getDefaultProps' ) ;
59
- }
60
-
61
- /**
62
- * Checks if the PropTypes MemberExpression node passed in declares a required propType.
63
- * @param {ASTNode } propTypeExpression node to check. Must be a `PropTypes` MemberExpression.
64
- * @returns {Boolean } `true` if this PropType is required, `false` if not.
65
- */
66
- function isRequiredPropType ( propTypeExpression ) {
67
- return propTypeExpression . type === 'MemberExpression' && propTypeExpression . property . name === 'isRequired' ;
68
- }
69
-
70
43
/**
71
44
* Find a variable by name in the current scope.
72
45
* @param {string } name Name of the variable to look for.
@@ -140,7 +113,7 @@ module.exports = {
140
113
141
114
return props . map ( property => ( {
142
115
name : property . key . name ,
143
- isRequired : isRequiredPropType ( property . value ) ,
116
+ isRequired : propsUtil . isRequiredPropType ( property . value ) ,
144
117
node : property
145
118
} ) ) ;
146
119
}
@@ -373,8 +346,8 @@ module.exports = {
373
346
374
347
return {
375
348
MemberExpression : function ( node ) {
376
- const isPropType = isPropTypesDeclaration ( node ) ;
377
- const isDefaultProp = isDefaultPropsDeclaration ( node ) ;
349
+ const isPropType = propsUtil . isPropTypesDeclaration ( node ) ;
350
+ const isDefaultProp = propsUtil . isDefaultPropsDeclaration ( node ) ;
378
351
379
352
if ( ! isPropType && ! isDefaultProp ) {
380
353
return ;
@@ -424,7 +397,7 @@ module.exports = {
424
397
if ( isPropType ) {
425
398
addPropTypesToComponent ( component , [ {
426
399
name : node . parent . property . name ,
427
- isRequired : isRequiredPropType ( node . parent . parent . right ) ,
400
+ isRequired : propsUtil . isRequiredPropType ( node . parent . parent . right ) ,
428
401
node : node . parent . parent
429
402
} ] ) ;
430
403
} else {
@@ -459,8 +432,8 @@ module.exports = {
459
432
return ;
460
433
}
461
434
462
- const isPropType = isPropTypesDeclaration ( node ) ;
463
- const isDefaultProp = isDefaultPropsDeclaration ( node ) ;
435
+ const isPropType = propsUtil . isPropTypesDeclaration ( node ) ;
436
+ const isDefaultProp = propsUtil . isDefaultPropsDeclaration ( node ) ;
464
437
465
438
if ( ! isPropType && ! isDefaultProp ) {
466
439
return ;
@@ -568,8 +541,8 @@ module.exports = {
568
541
return ;
569
542
}
570
543
571
- const isPropType = isPropTypesDeclaration ( property ) ;
572
- const isDefaultProp = isDefaultPropsDeclaration ( property ) ;
544
+ const isPropType = propsUtil . isPropTypesDeclaration ( property ) ;
545
+ const isDefaultProp = propsUtil . isDefaultPropsDeclaration ( property ) ;
573
546
574
547
if ( ! isPropType && ! isDefaultProp ) {
575
548
return ;
0 commit comments