@@ -9,6 +9,8 @@ const has = require('has');
99const Components = require ( '../util/Components' ) ;
1010const variableUtil = require ( '../util/variable' ) ;
1111const annotations = require ( '../util/annotations' ) ;
12+ const astUtil = require ( '../util/ast' ) ;
13+ const propsUtil = require ( '../util/props' ) ;
1214
1315// ------------------------------------------------------------------------------
1416// Rule Definition
@@ -38,53 +40,6 @@ module.exports = {
3840 const allowRequiredDefaults = configuration . allowRequiredDefaults || false ;
3941 const propWrapperFunctions = new Set ( context . settings . propWrapperFunctions || [ ] ) ;
4042
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-
8843 /**
8944 * Find a variable by name in the current scope.
9045 * @param {string } name Name of the variable to look for.
@@ -158,7 +113,7 @@ module.exports = {
158113
159114 return props . map ( property => ( {
160115 name : property . key . name ,
161- isRequired : isRequiredPropType ( property . value ) ,
116+ isRequired : propsUtil . isRequiredPropType ( property . value ) ,
162117 node : property
163118 } ) ) ;
164119 }
@@ -345,7 +300,7 @@ module.exports = {
345300 }
346301
347302 function isPropTypeAnnotation ( node ) {
348- return ( getPropertyName ( node ) === 'props' && ! ! node . typeAnnotation ) ;
303+ return ( astUtil . getPropertyName ( node ) === 'props' && ! ! node . typeAnnotation ) ;
349304 }
350305
351306 function propFromName ( propTypes , name ) {
@@ -395,8 +350,8 @@ module.exports = {
395350
396351 return {
397352 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 ) ;
400355
401356 if ( ! isPropType && ! isDefaultProp ) {
402357 return ;
@@ -446,7 +401,7 @@ module.exports = {
446401 if ( isPropType ) {
447402 addPropTypesToComponent ( component , [ {
448403 name : node . parent . property . name ,
449- isRequired : isRequiredPropType ( node . parent . parent . right ) ,
404+ isRequired : propsUtil . isRequiredPropType ( node . parent . parent . right ) ,
450405 node : node . parent . parent
451406 } ] ) ;
452407 } else {
@@ -481,8 +436,8 @@ module.exports = {
481436 return ;
482437 }
483438
484- const isPropType = isPropTypesDeclaration ( node ) ;
485- const isDefaultProp = isDefaultPropsDeclaration ( node ) ;
439+ const isPropType = propsUtil . isPropTypesDeclaration ( node ) ;
440+ const isDefaultProp = propsUtil . isDefaultPropsDeclaration ( node ) ;
486441
487442 if ( ! isPropType && ! isDefaultProp ) {
488443 return ;
@@ -537,7 +492,7 @@ module.exports = {
537492 return ;
538493 }
539494
540- const propName = getPropertyName ( node ) ;
495+ const propName = astUtil . getPropertyName ( node ) ;
541496 const isPropType = propName === 'propTypes' ;
542497 const isDefaultProp = propName === 'defaultProps' || propName === 'getDefaultProps' ;
543498
@@ -590,8 +545,8 @@ module.exports = {
590545 return ;
591546 }
592547
593- const isPropType = isPropTypesDeclaration ( property ) ;
594- const isDefaultProp = isDefaultPropsDeclaration ( property ) ;
548+ const isPropType = propsUtil . isPropTypesDeclaration ( property ) ;
549+ const isDefaultProp = propsUtil . isDefaultPropsDeclaration ( property ) ;
595550
596551 if ( ! isPropType && ! isDefaultProp ) {
597552 return ;
0 commit comments