10
10
var Components = require ( '../util/Components' ) ;
11
11
var variable = require ( '../util/variable' ) ;
12
12
13
- // ------------------------------------------------------------------------------
14
- // Constants
15
- // ------------------------------------------------------------------------------
16
-
17
- var DIRECT_PROPS_REGEX = / ^ p r o p s \s * ( \. | \[ ) / ;
18
-
19
13
// ------------------------------------------------------------------------------
20
14
// Rule Definition
21
15
// ------------------------------------------------------------------------------
@@ -74,17 +68,15 @@ module.exports = {
74
68
}
75
69
76
70
/**
77
- * Checks if we are using a prop
71
+ * Checks if this node is `this.props`.
78
72
* @param {ASTNode } node The AST node being checked.
79
- * @returns {Boolean } True if we are using a prop , false if not.
73
+ * @returns {Boolean } True if this node is `this.props` , false if not.
80
74
*/
81
- function isPropTypesUsage ( node ) {
82
- var isClassUsage = (
75
+ function isPropsMemberExpression ( node ) {
76
+ return (
83
77
( utils . getParentES6Component ( ) || utils . getParentES5Component ( ) ) &&
84
78
node . object . type === 'ThisExpression' && node . property . name === 'props'
85
79
) ;
86
- var isStatelessFunctionUsage = node . object . name === 'props' ;
87
- return isClassUsage || isStatelessFunctionUsage ;
88
80
}
89
81
90
82
/**
@@ -209,15 +201,26 @@ module.exports = {
209
201
}
210
202
211
203
/**
212
- * Checks if a prop init name matches common naming patterns
204
+ * Checks if a node is an identifier referring to props
213
205
* @param {ASTNode } node The AST node being checked.
214
206
* @returns {Boolean } True if the prop name matches
215
207
*/
216
- function isPropAttributeName ( node ) {
208
+ function isPropsIdentifier ( node ) {
217
209
return (
218
- node . init . name === 'props' ||
219
- node . init . name === 'nextProps' ||
220
- node . init . name === 'prevProps'
210
+ node . type === 'Identifier' && (
211
+ node . parent . type === 'MemberExpression' ||
212
+ node . parent . type === 'VariableDeclarator'
213
+ ) && (
214
+ (
215
+ ( isInLifeCycleMethod ( node ) || utils . getParentStatelessComponent ( ) ) &&
216
+ node . name === 'props'
217
+ ) || (
218
+ isInLifeCycleMethod ( node ) && (
219
+ node . name === 'nextProps' ||
220
+ node . name === 'prevProps'
221
+ )
222
+ )
223
+ )
221
224
) ;
222
225
}
223
226
@@ -483,41 +486,17 @@ module.exports = {
483
486
}
484
487
}
485
488
486
- /**
487
- * Check if we are in a class constructor
488
- * @return {boolean } true if we are in a class constructor, false if not
489
- */
490
- function inConstructor ( ) {
491
- var scope = context . getScope ( ) ;
492
- while ( scope ) {
493
- if ( scope . block && scope . block . parent && scope . block . parent . kind === 'constructor' ) {
494
- return true ;
495
- }
496
- scope = scope . upper ;
497
- }
498
- return false ;
499
- }
500
-
501
489
/**
502
490
* Retrieve the name of a property node
503
491
* @param {ASTNode } node The AST node with the property.
504
492
* @return {string } the name of the property or undefined if not found
505
493
*/
506
494
function getPropertyName ( node ) {
507
- var isDirectProp = DIRECT_PROPS_REGEX . test ( sourceCode . getText ( node ) ) ;
508
- var isInClassComponent = utils . getParentES6Component ( ) || utils . getParentES5Component ( ) ;
509
- var isNotInConstructor = ! inConstructor ( node ) ;
510
- if ( isDirectProp && isInClassComponent && isNotInConstructor ) {
511
- return void 0 ;
512
- }
513
- if ( ! isDirectProp ) {
514
- node = node . parent ;
515
- }
516
- var property = node . property ;
495
+ var property = node . parent . property ;
517
496
if ( property ) {
518
497
switch ( property . type ) {
519
498
case 'Identifier' :
520
- if ( node . computed ) {
499
+ if ( node . parent . computed ) {
521
500
return '__COMPUTED_PROP__' ;
522
501
}
523
502
return property . name ;
@@ -530,7 +509,7 @@ module.exports = {
530
509
}
531
510
// falls through
532
511
default :
533
- if ( node . computed ) {
512
+ if ( node . parent . computed ) {
534
513
return '__COMPUTED_PROP__' ;
535
514
}
536
515
break ;
@@ -550,6 +529,7 @@ module.exports = {
550
529
var allNames ;
551
530
var properties ;
552
531
switch ( node . type ) {
532
+ case 'Identifier' :
553
533
case 'MemberExpression' :
554
534
name = getPropertyName ( node ) ;
555
535
if ( name ) {
@@ -582,16 +562,9 @@ module.exports = {
582
562
( node . id . properties [ i ] . key . name === 'props' || node . id . properties [ i ] . key . value === 'props' ) &&
583
563
node . id . properties [ i ] . value . type === 'ObjectPattern'
584
564
) ;
585
- // let {firstname} = props
586
- var genericDestructuring = isPropAttributeName ( node ) && (
587
- utils . getParentStatelessComponent ( ) ||
588
- isInLifeCycleMethod ( node )
589
- ) ;
590
565
591
566
if ( thisDestructuring ) {
592
567
properties = node . id . properties [ i ] . value . properties ;
593
- } else if ( genericDestructuring ) {
594
- properties = node . id . properties ;
595
568
} else {
596
569
continue ;
597
570
}
@@ -828,13 +801,8 @@ module.exports = {
828
801
var destructuring = node . init && node . id && node . id . type === 'ObjectPattern' ;
829
802
// let {props: {firstname}} = this
830
803
var thisDestructuring = destructuring && node . init . type === 'ThisExpression' ;
831
- // let {firstname} = props
832
- var statelessDestructuring = destructuring && isPropAttributeName ( node ) && (
833
- utils . getParentStatelessComponent ( ) ||
834
- isInLifeCycleMethod ( node )
835
- ) ;
836
804
837
- if ( ! thisDestructuring && ! statelessDestructuring ) {
805
+ if ( ! thisDestructuring ) {
838
806
return ;
839
807
}
840
808
markPropTypesAsUsed ( node ) ;
@@ -848,7 +816,7 @@ module.exports = {
848
816
849
817
MemberExpression : function ( node ) {
850
818
var type ;
851
- if ( isPropTypesUsage ( node ) ) {
819
+ if ( isPropsMemberExpression ( node ) ) {
852
820
type = 'usage' ;
853
821
} else if ( isPropTypesDeclaration ( node . property ) ) {
854
822
type = 'declaration' ;
@@ -870,6 +838,12 @@ module.exports = {
870
838
}
871
839
} ,
872
840
841
+ Identifier : function ( node ) {
842
+ if ( isPropsIdentifier ( node ) ) {
843
+ markPropTypesAsUsed ( node ) ;
844
+ }
845
+ } ,
846
+
873
847
MethodDefinition : function ( node ) {
874
848
if ( ! isPropTypesDeclaration ( node . key ) ) {
875
849
return ;
0 commit comments