@@ -25,13 +25,33 @@ module.exports = {
25
25
} ,
26
26
27
27
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
+
28
48
/**
29
49
* Checks if the Identifier node passed in looks like a propTypes declaration.
30
50
* @param {ASTNode } node The node to check. Must be an Identifier node.
31
51
* @returns {Boolean } `true` if the node is a propTypes declaration, `false` if not
32
52
*/
33
53
function isPropTypesDeclaration ( node ) {
34
- return node . type === 'Identifier' && node . name === 'propTypes' ;
54
+ return getPropertyName ( node ) === 'propTypes' ;
35
55
}
36
56
37
57
/**
@@ -40,8 +60,7 @@ module.exports = {
40
60
* @returns {Boolean } `true` if the node is a defaultProps declaration, `false` if not
41
61
*/
42
62
function isDefaultPropsDeclaration ( node ) {
43
- return node . type === 'Identifier' &&
44
- ( node . name === 'defaultProps' || node . name === 'getDefaultProps' ) ;
63
+ return ( getPropertyName ( node ) === 'defaultProps' || getPropertyName ( node ) === 'getDefaultProps' ) ;
45
64
}
46
65
47
66
/**
@@ -289,7 +308,7 @@ module.exports = {
289
308
}
290
309
291
310
function isPropTypeAnnotation ( node ) {
292
- return ( node . key . name === 'props' && ! ! node . typeAnnotation ) ;
311
+ return ( getPropertyName ( node ) === 'props' && ! ! node . typeAnnotation ) ;
293
312
}
294
313
295
314
/**
@@ -328,8 +347,8 @@ module.exports = {
328
347
329
348
return {
330
349
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 ) ;
333
352
334
353
if ( ! isPropType && ! isDefaultProp ) {
335
354
return ;
@@ -412,8 +431,8 @@ module.exports = {
412
431
return ;
413
432
}
414
433
415
- var isPropType = isPropTypesDeclaration ( node . key ) ;
416
- var isDefaultProp = isDefaultPropsDeclaration ( node . key ) ;
434
+ var isPropType = isPropTypesDeclaration ( node ) ;
435
+ var isDefaultProp = isDefaultPropsDeclaration ( node ) ;
417
436
418
437
if ( ! isPropType && ! isDefaultProp ) {
419
438
return ;
@@ -468,8 +487,8 @@ module.exports = {
468
487
return ;
469
488
}
470
489
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' ;
473
492
474
493
if ( ! isPropType && ! isDefaultProp ) {
475
494
return ;
@@ -520,8 +539,8 @@ module.exports = {
520
539
return ;
521
540
}
522
541
523
- var isPropType = isPropTypesDeclaration ( property . key ) ;
524
- var isDefaultProp = isDefaultPropsDeclaration ( property . key ) ;
542
+ var isPropType = isPropTypesDeclaration ( property ) ;
543
+ var isDefaultProp = isDefaultPropsDeclaration ( property ) ;
525
544
526
545
if ( ! isPropType && ! isDefaultProp ) {
527
546
return ;
0 commit comments