@@ -47,8 +47,9 @@ module.exports = {
47
47
return current . arguments [ 0 ] === node ;
48
48
}
49
49
50
- function isClassComponent ( ) {
51
- return ! ! ( componentUtil . getParentES6Component ( context ) || componentUtil . getParentES5Component ( context ) ) ;
50
+ function isClassComponent ( node ) {
51
+ return ! ! ( componentUtil . getParentES6Component ( context , node )
52
+ || componentUtil . getParentES5Component ( context , node ) ) ;
52
53
}
53
54
54
55
// The methods array contains all methods or functions that are using this.state
@@ -58,7 +59,7 @@ module.exports = {
58
59
const vars = [ ] ;
59
60
return {
60
61
CallExpression ( node ) {
61
- if ( ! isClassComponent ( ) ) {
62
+ if ( ! isClassComponent ( node ) ) {
62
63
return ;
63
64
}
64
65
// Appends all the methods that are calling another
@@ -103,7 +104,7 @@ module.exports = {
103
104
if (
104
105
node . property . name === 'state'
105
106
&& node . object . type === 'ThisExpression'
106
- && isClassComponent ( )
107
+ && isClassComponent ( node )
107
108
) {
108
109
let current = node ;
109
110
while ( current . type !== 'Program' ) {
@@ -134,7 +135,7 @@ module.exports = {
134
135
if ( current . type === 'VariableDeclarator' ) {
135
136
vars . push ( {
136
137
node,
137
- scope : context . getScope ( ) ,
138
+ scope : ( context . sourceCode || context ) . getScope ( node ) ,
138
139
variableName : current . id . name ,
139
140
} ) ;
140
141
break ;
@@ -155,10 +156,11 @@ module.exports = {
155
156
current . parent . value === current
156
157
|| current . parent . object === current
157
158
) {
159
+ const scope = ( context . sourceCode || context ) . getScope ( node ) ;
158
160
while ( current . type !== 'Program' ) {
159
161
if ( isFirstArgumentInSetStateCall ( current , node ) ) {
160
162
vars
161
- . filter ( ( v ) => v . scope === context . getScope ( ) && v . variableName === node . name )
163
+ . filter ( ( v ) => v . scope === scope && v . variableName === node . name )
162
164
. forEach ( ( v ) => {
163
165
report ( context , messages . useCallback , 'useCallback' , {
164
166
node : v . node ,
@@ -176,7 +178,7 @@ module.exports = {
176
178
if ( property && property . key && property . key . name === 'state' && isDerivedFromThis ) {
177
179
vars . push ( {
178
180
node : property . key ,
179
- scope : context . getScope ( ) ,
181
+ scope : ( context . sourceCode || context ) . getScope ( node ) ,
180
182
variableName : property . key . name ,
181
183
} ) ;
182
184
}
0 commit comments