88const docsUrl = require ( '../util/docsUrl' ) ;
99const componentUtil = require ( '../util/componentUtil' ) ;
1010const report = require ( '../util/report' ) ;
11+ const getScope = require ( '../util/eslint' ) . getScope ;
1112
1213// ------------------------------------------------------------------------------
1314// Rule Definition
@@ -47,8 +48,15 @@ module.exports = {
4748 return current . arguments [ 0 ] === node ;
4849 }
4950
50- function isClassComponent ( ) {
51- return ! ! ( componentUtil . getParentES6Component ( context ) || componentUtil . getParentES5Component ( context ) ) ;
51+ /**
52+ * @param {ASTNode } node
53+ * @returns {boolean }
54+ */
55+ function isClassComponent ( node ) {
56+ return ! ! (
57+ componentUtil . getParentES6Component ( context , node )
58+ || componentUtil . getParentES5Component ( context , node )
59+ ) ;
5260 }
5361
5462 // The methods array contains all methods or functions that are using this.state
@@ -58,7 +66,7 @@ module.exports = {
5866 const vars = [ ] ;
5967 return {
6068 CallExpression ( node ) {
61- if ( ! isClassComponent ( ) ) {
69+ if ( ! isClassComponent ( node ) ) {
6270 return ;
6371 }
6472 // Appends all the methods that are calling another
@@ -103,7 +111,7 @@ module.exports = {
103111 if (
104112 node . property . name === 'state'
105113 && node . object . type === 'ThisExpression'
106- && isClassComponent ( )
114+ && isClassComponent ( node )
107115 ) {
108116 let current = node ;
109117 while ( current . type !== 'Program' ) {
@@ -134,7 +142,7 @@ module.exports = {
134142 if ( current . type === 'VariableDeclarator' ) {
135143 vars . push ( {
136144 node,
137- scope : context . getScope ( ) ,
145+ scope : getScope ( context , node ) ,
138146 variableName : current . id . name ,
139147 } ) ;
140148 break ;
@@ -158,7 +166,7 @@ module.exports = {
158166 while ( current . type !== 'Program' ) {
159167 if ( isFirstArgumentInSetStateCall ( current , node ) ) {
160168 vars
161- . filter ( ( v ) => v . scope === context . getScope ( ) && v . variableName === node . name )
169+ . filter ( ( v ) => v . scope === getScope ( context , node ) && v . variableName === node . name )
162170 . forEach ( ( v ) => {
163171 report ( context , messages . useCallback , 'useCallback' , {
164172 node : v . node ,
@@ -176,7 +184,7 @@ module.exports = {
176184 if ( property && property . key && property . key . name === 'state' && isDerivedFromThis ) {
177185 vars . push ( {
178186 node : property . key ,
179- scope : context . getScope ( ) ,
187+ scope : getScope ( context , node ) ,
180188 variableName : property . key . name ,
181189 } ) ;
182190 }
0 commit comments