@@ -9,6 +9,8 @@ const has = require('has');
9
9
const Components = require ( '../util/Components' ) ;
10
10
const variableUtil = require ( '../util/variable' ) ;
11
11
const annotations = require ( '../util/annotations' ) ;
12
+ const astUtil = require ( '../util/ast' ) ;
13
+ const propsUtil = require ( '../util/props' ) ;
12
14
13
15
// ------------------------------------------------------------------------------
14
16
// Rule Definition
@@ -38,53 +40,6 @@ module.exports = {
38
40
const allowRequiredDefaults = configuration . allowRequiredDefaults || false ;
39
41
const propWrapperFunctions = new Set ( context . settings . propWrapperFunctions || [ ] ) ;
40
42
41
- /**
42
- * Get properties name
43
- * @param {Object } node - Property.
44
- * @returns {String } Property name.
45
- */
46
- function getPropertyName ( node ) {
47
- if ( node . key || [ 'MethodDefinition' , 'Property' ] . indexOf ( node . type ) !== - 1 ) {
48
- return node . key . name ;
49
- } else if ( node . type === 'MemberExpression' ) {
50
- return node . property . name ;
51
- // Special case for class properties
52
- // (babel-eslint@5 does not expose property name so we have to rely on tokens)
53
- } else if ( node . type === 'ClassProperty' ) {
54
- const tokens = context . getFirstTokens ( node , 2 ) ;
55
- return tokens [ 1 ] && tokens [ 1 ] . type === 'Identifier' ? tokens [ 1 ] . value : tokens [ 0 ] . value ;
56
- }
57
- return '' ;
58
- }
59
-
60
- /**
61
- * Checks if the Identifier node passed in looks like a propTypes declaration.
62
- * @param {ASTNode } node The node to check. Must be an Identifier node.
63
- * @returns {Boolean } `true` if the node is a propTypes declaration, `false` if not
64
- */
65
- function isPropTypesDeclaration ( node ) {
66
- return getPropertyName ( node ) === 'propTypes' ;
67
- }
68
-
69
- /**
70
- * Checks if the Identifier node passed in looks like a defaultProps declaration.
71
- * @param {ASTNode } node The node to check. Must be an Identifier node.
72
- * @returns {Boolean } `true` if the node is a defaultProps declaration, `false` if not
73
- */
74
- function isDefaultPropsDeclaration ( node ) {
75
- const propName = getPropertyName ( node ) ;
76
- return ( propName === 'defaultProps' || propName === 'getDefaultProps' ) ;
77
- }
78
-
79
- /**
80
- * Checks if the PropTypes MemberExpression node passed in declares a required propType.
81
- * @param {ASTNode } propTypeExpression node to check. Must be a `PropTypes` MemberExpression.
82
- * @returns {Boolean } `true` if this PropType is required, `false` if not.
83
- */
84
- function isRequiredPropType ( propTypeExpression ) {
85
- return propTypeExpression . type === 'MemberExpression' && propTypeExpression . property . name === 'isRequired' ;
86
- }
87
-
88
43
/**
89
44
* Find a variable by name in the current scope.
90
45
* @param {string } name Name of the variable to look for.
@@ -158,7 +113,7 @@ module.exports = {
158
113
159
114
return props . map ( property => ( {
160
115
name : property . key . name ,
161
- isRequired : isRequiredPropType ( property . value ) ,
116
+ isRequired : propsUtil . isRequiredPropType ( property . value ) ,
162
117
node : property
163
118
} ) ) ;
164
119
}
@@ -345,7 +300,7 @@ module.exports = {
345
300
}
346
301
347
302
function isPropTypeAnnotation ( node ) {
348
- return ( getPropertyName ( node ) === 'props' && ! ! node . typeAnnotation ) ;
303
+ return ( astUtil . getPropertyName ( node ) === 'props' && ! ! node . typeAnnotation ) ;
349
304
}
350
305
351
306
function propFromName ( propTypes , name ) {
@@ -395,8 +350,8 @@ module.exports = {
395
350
396
351
return {
397
352
MemberExpression : function ( node ) {
398
- const isPropType = isPropTypesDeclaration ( node ) ;
399
- const isDefaultProp = isDefaultPropsDeclaration ( node ) ;
353
+ const isPropType = propsUtil . isPropTypesDeclaration ( node ) ;
354
+ const isDefaultProp = propsUtil . isDefaultPropsDeclaration ( node ) ;
400
355
401
356
if ( ! isPropType && ! isDefaultProp ) {
402
357
return ;
@@ -446,7 +401,7 @@ module.exports = {
446
401
if ( isPropType ) {
447
402
addPropTypesToComponent ( component , [ {
448
403
name : node . parent . property . name ,
449
- isRequired : isRequiredPropType ( node . parent . parent . right ) ,
404
+ isRequired : propsUtil . isRequiredPropType ( node . parent . parent . right ) ,
450
405
node : node . parent . parent
451
406
} ] ) ;
452
407
} else {
@@ -481,8 +436,8 @@ module.exports = {
481
436
return ;
482
437
}
483
438
484
- const isPropType = isPropTypesDeclaration ( node ) ;
485
- const isDefaultProp = isDefaultPropsDeclaration ( node ) ;
439
+ const isPropType = propsUtil . isPropTypesDeclaration ( node ) ;
440
+ const isDefaultProp = propsUtil . isDefaultPropsDeclaration ( node ) ;
486
441
487
442
if ( ! isPropType && ! isDefaultProp ) {
488
443
return ;
@@ -537,7 +492,7 @@ module.exports = {
537
492
return ;
538
493
}
539
494
540
- const propName = getPropertyName ( node ) ;
495
+ const propName = astUtil . getPropertyName ( node ) ;
541
496
const isPropType = propName === 'propTypes' ;
542
497
const isDefaultProp = propName === 'defaultProps' || propName === 'getDefaultProps' ;
543
498
@@ -590,8 +545,8 @@ module.exports = {
590
545
return ;
591
546
}
592
547
593
- const isPropType = isPropTypesDeclaration ( property ) ;
594
- const isDefaultProp = isDefaultPropsDeclaration ( property ) ;
548
+ const isPropType = propsUtil . isPropTypesDeclaration ( property ) ;
549
+ const isDefaultProp = propsUtil . isDefaultPropsDeclaration ( property ) ;
595
550
596
551
if ( ! isPropType && ! isDefaultProp ) {
597
552
return ;
0 commit comments