@@ -18,6 +18,7 @@ var annotations = require('../util/annotations');
18
18
// ------------------------------------------------------------------------------
19
19
20
20
var DIRECT_PROPS_REGEX = / ^ p r o p s \s * ( \. | \[ ) / ;
21
+ var DIRECT_NEXT_PROPS_REGEX = / ^ n e x t P r o p s \s * ( \. | \[ ) / ;
21
22
22
23
// ------------------------------------------------------------------------------
23
24
// Rule Definition
@@ -77,6 +78,24 @@ module.exports = {
77
78
return value ;
78
79
}
79
80
81
+ /**
82
+ * Check if we are in a class constructor
83
+ * @return {boolean } true if we are in a class constructor, false if not
84
+ **/
85
+ function inComponentWillReceiveProps ( ) {
86
+ var scope = context . getScope ( ) ;
87
+ while ( scope ) {
88
+ if (
89
+ scope . block && scope . block . parent &&
90
+ scope . block . parent . key && scope . block . parent . key . name === 'componentWillReceiveProps'
91
+ ) {
92
+ return true ;
93
+ }
94
+ scope = scope . upper ;
95
+ }
96
+ return false ;
97
+ }
98
+
80
99
/**
81
100
* Checks if we are using a prop
82
101
* @param {ASTNode } node The AST node being checked.
@@ -88,7 +107,8 @@ module.exports = {
88
107
node . object . type === 'ThisExpression' && node . property . name === 'props'
89
108
) ;
90
109
var isStatelessFunctionUsage = node . object . name === 'props' ;
91
- return isClassUsage || isStatelessFunctionUsage ;
110
+ var isNextPropsUsage = node . object . name === 'nextProps' && inComponentWillReceiveProps ( ) ;
111
+ return isClassUsage || isStatelessFunctionUsage || isNextPropsUsage ;
92
112
}
93
113
94
114
/**
@@ -491,12 +511,17 @@ module.exports = {
491
511
*/
492
512
function getPropertyName ( node ) {
493
513
var isDirectProp = DIRECT_PROPS_REGEX . test ( sourceCode . getText ( node ) ) ;
514
+ var isDirectNextProp = DIRECT_NEXT_PROPS_REGEX . test ( sourceCode . getText ( node ) ) ;
494
515
var isInClassComponent = utils . getParentES6Component ( ) || utils . getParentES5Component ( ) ;
495
516
var isNotInConstructor = ! inConstructor ( node ) ;
496
- if ( isDirectProp && isInClassComponent && isNotInConstructor ) {
517
+ var isNotInComponentWillReceiveProps = ! inComponentWillReceiveProps ( ) ;
518
+ if ( ( isDirectProp || isDirectNextProp )
519
+ && isInClassComponent
520
+ && isNotInConstructor
521
+ && isNotInComponentWillReceiveProps ) {
497
522
return void 0 ;
498
523
}
499
- if ( ! isDirectProp ) {
524
+ if ( ! isDirectProp && ! isDirectNextProp ) {
500
525
node = node . parent ;
501
526
}
502
527
var property = node . property ;
0 commit comments