8
8
const docsUrl = require ( '../util/docsUrl' ) ;
9
9
const componentUtil = require ( '../util/componentUtil' ) ;
10
10
const report = require ( '../util/report' ) ;
11
+ const getScope = require ( '../util/eslint' ) . getScope ;
11
12
12
13
// ------------------------------------------------------------------------------
13
14
// Rule Definition
@@ -47,8 +48,15 @@ module.exports = {
47
48
return current . arguments [ 0 ] === node ;
48
49
}
49
50
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
+ ) ;
52
60
}
53
61
54
62
// The methods array contains all methods or functions that are using this.state
@@ -58,7 +66,7 @@ module.exports = {
58
66
const vars = [ ] ;
59
67
return {
60
68
CallExpression ( node ) {
61
- if ( ! isClassComponent ( ) ) {
69
+ if ( ! isClassComponent ( node ) ) {
62
70
return ;
63
71
}
64
72
// Appends all the methods that are calling another
@@ -103,7 +111,7 @@ module.exports = {
103
111
if (
104
112
node . property . name === 'state'
105
113
&& node . object . type === 'ThisExpression'
106
- && isClassComponent ( )
114
+ && isClassComponent ( node )
107
115
) {
108
116
let current = node ;
109
117
while ( current . type !== 'Program' ) {
@@ -134,7 +142,7 @@ module.exports = {
134
142
if ( current . type === 'VariableDeclarator' ) {
135
143
vars . push ( {
136
144
node,
137
- scope : context . getScope ( ) ,
145
+ scope : getScope ( context , node ) ,
138
146
variableName : current . id . name ,
139
147
} ) ;
140
148
break ;
@@ -158,7 +166,7 @@ module.exports = {
158
166
while ( current . type !== 'Program' ) {
159
167
if ( isFirstArgumentInSetStateCall ( current , node ) ) {
160
168
vars
161
- . filter ( ( v ) => v . scope === context . getScope ( ) && v . variableName === node . name )
169
+ . filter ( ( v ) => v . scope === getScope ( context , node ) && v . variableName === node . name )
162
170
. forEach ( ( v ) => {
163
171
report ( context , messages . useCallback , 'useCallback' , {
164
172
node : v . node ,
@@ -176,7 +184,7 @@ module.exports = {
176
184
if ( property && property . key && property . key . name === 'state' && isDerivedFromThis ) {
177
185
vars . push ( {
178
186
node : property . key ,
179
- scope : context . getScope ( ) ,
187
+ scope : getScope ( context , node ) ,
180
188
variableName : property . key . name ,
181
189
} ) ;
182
190
}
0 commit comments