@@ -25,13 +25,33 @@ module.exports = {
2525 } ,
2626
2727 create : Components . detect ( function ( context , components , utils ) {
28+
29+ /**
30+ * Get properties name
31+ * @param {Object } node - Property.
32+ * @returns {String } Property name.
33+ */
34+ function getPropertyName ( node ) {
35+ if ( node . key || [ 'MethodDefinition' , 'Property' ] . indexOf ( node . type ) !== - 1 ) {
36+ return node . key . name ;
37+ } else if ( node . type === 'MemberExpression' ) {
38+ return node . property . name ;
39+ // Special case for class properties
40+ // (babel-eslint@5 does not expose property name so we have to rely on tokens)
41+ } else if ( node . type === 'ClassProperty' ) {
42+ var tokens = context . getFirstTokens ( node , 2 ) ;
43+ return tokens [ 1 ] && tokens [ 1 ] . type === 'Identifier' ? tokens [ 1 ] . value : tokens [ 0 ] . value ;
44+ }
45+ return '' ;
46+ }
47+
2848 /**
2949 * Checks if the Identifier node passed in looks like a propTypes declaration.
3050 * @param {ASTNode } node The node to check. Must be an Identifier node.
3151 * @returns {Boolean } `true` if the node is a propTypes declaration, `false` if not
3252 */
3353 function isPropTypesDeclaration ( node ) {
34- return node . type === 'Identifier' && node . name === 'propTypes' ;
54+ return getPropertyName ( node ) === 'propTypes' ;
3555 }
3656
3757 /**
@@ -40,8 +60,7 @@ module.exports = {
4060 * @returns {Boolean } `true` if the node is a defaultProps declaration, `false` if not
4161 */
4262 function isDefaultPropsDeclaration ( node ) {
43- return node . type === 'Identifier' &&
44- ( node . name === 'defaultProps' || node . name === 'getDefaultProps' ) ;
63+ return ( getPropertyName ( node ) === 'defaultProps' || getPropertyName ( node ) === 'getDefaultProps' ) ;
4564 }
4665
4766 /**
@@ -289,7 +308,7 @@ module.exports = {
289308 }
290309
291310 function isPropTypeAnnotation ( node ) {
292- return ( node . key . name === 'props' && ! ! node . typeAnnotation ) ;
311+ return ( getPropertyName ( node ) === 'props' && ! ! node . typeAnnotation ) ;
293312 }
294313
295314 /**
@@ -328,8 +347,8 @@ module.exports = {
328347
329348 return {
330349 MemberExpression : function ( node ) {
331- var isPropType = isPropTypesDeclaration ( node . property ) ;
332- var isDefaultProp = isDefaultPropsDeclaration ( node . property ) ;
350+ var isPropType = isPropTypesDeclaration ( node ) ;
351+ var isDefaultProp = isDefaultPropsDeclaration ( node ) ;
333352
334353 if ( ! isPropType && ! isDefaultProp ) {
335354 return ;
@@ -412,8 +431,8 @@ module.exports = {
412431 return ;
413432 }
414433
415- var isPropType = isPropTypesDeclaration ( node . key ) ;
416- var isDefaultProp = isDefaultPropsDeclaration ( node . key ) ;
434+ var isPropType = isPropTypesDeclaration ( node ) ;
435+ var isDefaultProp = isDefaultPropsDeclaration ( node ) ;
417436
418437 if ( ! isPropType && ! isDefaultProp ) {
419438 return ;
@@ -468,8 +487,8 @@ module.exports = {
468487 return ;
469488 }
470489
471- var isPropType = isPropTypesDeclaration ( node . key ) ;
472- var isDefaultProp = isDefaultPropsDeclaration ( node . key ) ;
490+ var isPropType = getPropertyName ( node ) === 'propTypes' ;
491+ var isDefaultProp = getPropertyName ( node ) === 'defaultProps' || getPropertyName ( node ) === 'getDefaultProps' ;
473492
474493 if ( ! isPropType && ! isDefaultProp ) {
475494 return ;
@@ -520,8 +539,8 @@ module.exports = {
520539 return ;
521540 }
522541
523- var isPropType = isPropTypesDeclaration ( property . key ) ;
524- var isDefaultProp = isDefaultPropsDeclaration ( property . key ) ;
542+ var isPropType = isPropTypesDeclaration ( property ) ;
543+ var isDefaultProp = isDefaultPropsDeclaration ( property ) ;
525544
526545 if ( ! isPropType && ! isDefaultProp ) {
527546 return ;
0 commit comments