@@ -47,8 +47,9 @@ module.exports = {
4747 return current . arguments [ 0 ] === node ;
4848 }
4949
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 ) ) ;
5253 }
5354
5455 // The methods array contains all methods or functions that are using this.state
@@ -58,7 +59,7 @@ module.exports = {
5859 const vars = [ ] ;
5960 return {
6061 CallExpression ( node ) {
61- if ( ! isClassComponent ( ) ) {
62+ if ( ! isClassComponent ( node ) ) {
6263 return ;
6364 }
6465 // Appends all the methods that are calling another
@@ -103,7 +104,7 @@ module.exports = {
103104 if (
104105 node . property . name === 'state'
105106 && node . object . type === 'ThisExpression'
106- && isClassComponent ( )
107+ && isClassComponent ( node )
107108 ) {
108109 let current = node ;
109110 while ( current . type !== 'Program' ) {
@@ -134,7 +135,7 @@ module.exports = {
134135 if ( current . type === 'VariableDeclarator' ) {
135136 vars . push ( {
136137 node,
137- scope : context . getScope ( ) ,
138+ scope : ( context . sourceCode || context ) . getScope ( node ) ,
138139 variableName : current . id . name ,
139140 } ) ;
140141 break ;
@@ -155,10 +156,11 @@ module.exports = {
155156 current . parent . value === current
156157 || current . parent . object === current
157158 ) {
159+ const scope = ( context . sourceCode || context ) . getScope ( node ) ;
158160 while ( current . type !== 'Program' ) {
159161 if ( isFirstArgumentInSetStateCall ( current , node ) ) {
160162 vars
161- . filter ( ( v ) => v . scope === context . getScope ( ) && v . variableName === node . name )
163+ . filter ( ( v ) => v . scope === scope && v . variableName === node . name )
162164 . forEach ( ( v ) => {
163165 report ( context , messages . useCallback , 'useCallback' , {
164166 node : v . node ,
@@ -176,7 +178,7 @@ module.exports = {
176178 if ( property && property . key && property . key . name === 'state' && isDerivedFromThis ) {
177179 vars . push ( {
178180 node : property . key ,
179- scope : context . getScope ( ) ,
181+ scope : ( context . sourceCode || context ) . getScope ( node ) ,
180182 variableName : property . key . name ,
181183 } ) ;
182184 }
0 commit comments