@@ -135,25 +135,16 @@ function isInLifeCycleMethod(node, checkAsyncSafeLifeCycles) {
135
135
}
136
136
137
137
/**
138
- * Check if the current node is in a setState updater method
139
- * @return {boolean } true if we are in a setState updater, false if not
138
+ * Check if a function node is a setState updater
139
+ * @param {ASTNode } node a function node
140
+ * @return {boolean }
140
141
*/
141
- function inSetStateUpdater ( context ) {
142
- let scope = context . getScope ( ) ;
143
- while ( scope ) {
144
- if (
145
- scope . block && scope . block . parent &&
146
- scope . block . parent . type === 'CallExpression' &&
147
- scope . block . parent . callee . property &&
148
- scope . block . parent . callee . property . name === 'setState' &&
149
- // Make sure we are in the updater not the callback
150
- scope . block . parent . arguments [ 0 ] . start === scope . block . start
151
- ) {
152
- return true ;
153
- }
154
- scope = scope . upper ;
155
- }
156
- return false ;
142
+ function isSetStateUpdater ( node ) {
143
+ return node . parent . type === 'CallExpression' &&
144
+ node . parent . callee . property &&
145
+ node . parent . callee . property . name === 'setState' &&
146
+ // Make sure we are in the updater not the callback
147
+ node . parent . arguments [ 0 ] === node ;
157
148
}
158
149
159
150
function isPropArgumentInSetStateUpdater ( context , name ) {
@@ -324,7 +315,7 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
324
315
break ;
325
316
}
326
317
type = 'destructuring' ;
327
- const propParam = inSetStateUpdater ( context ) ? node . params [ 1 ] : node . params [ 0 ] ;
318
+ const propParam = isSetStateUpdater ( node ) ? node . params [ 1 ] : node . params [ 0 ] ;
328
319
properties = propParam . type === 'AssignmentPattern' ?
329
320
propParam . left . properties :
330
321
propParam . properties ;
@@ -399,7 +390,7 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
399
390
* FunctionDeclaration, or FunctionExpression
400
391
*/
401
392
function markDestructuredFunctionArgumentsAsUsed ( node ) {
402
- const param = node . params && inSetStateUpdater ( context ) ? node . params [ 1 ] : node . params [ 0 ] ;
393
+ const param = node . params && isSetStateUpdater ( node ) ? node . params [ 1 ] : node . params [ 0 ] ;
403
394
404
395
const destructuring = param && (
405
396
param . type === 'ObjectPattern' ||
@@ -412,7 +403,7 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
412
403
}
413
404
414
405
function handleSetStateUpdater ( node ) {
415
- if ( ! node . params || node . params . length < 2 || ! inSetStateUpdater ( context ) ) {
406
+ if ( ! node . params || node . params . length < 2 || ! isSetStateUpdater ( node ) ) {
416
407
return ;
417
408
}
418
409
markPropTypesAsUsed ( node ) ;
0 commit comments