@@ -107,7 +107,8 @@ module.exports = {
107
107
if ( current . type === 'VariableDeclarator' ) {
108
108
vars . push ( {
109
109
node : node ,
110
- scope : context . getScope ( )
110
+ scope : context . getScope ( ) ,
111
+ variableName : current . id . name
111
112
} ) ;
112
113
break ;
113
114
}
@@ -123,11 +124,14 @@ module.exports = {
123
124
while ( current . parent . type === 'BinaryExpression' ) {
124
125
current = current . parent ;
125
126
}
126
- if ( current . parent . value === current ) {
127
+ if (
128
+ current . parent . value === current ||
129
+ current . parent . object === current
130
+ ) {
127
131
while ( current . type !== 'Program' ) {
128
132
if ( isSetStateCall ( current ) ) {
129
133
vars
130
- . filter ( v => v . scope === context . getScope ( ) )
134
+ . filter ( v => v . scope === context . getScope ( ) && v . variableName === node . name )
131
135
. map ( v => context . report (
132
136
v . node ,
133
137
'Use callback in setState when referencing the previous state.'
@@ -136,6 +140,19 @@ module.exports = {
136
140
current = current . parent ;
137
141
}
138
142
}
143
+ } ,
144
+
145
+ ObjectPattern ( node ) {
146
+ const isDerivedFromThis = node . parent . init . type === 'ThisExpression' ;
147
+ node . properties . forEach ( property => {
148
+ if ( property . key . name === 'state' && isDerivedFromThis ) {
149
+ vars . push ( {
150
+ node : property . key ,
151
+ scope : context . getScope ( ) ,
152
+ variableName : property . key . name
153
+ } ) ;
154
+ }
155
+ } ) ;
139
156
}
140
157
} ;
141
158
}
0 commit comments