@@ -456,15 +456,16 @@ function componentRule(rule, context) {
456
456
let scope = context . getScope ( ) ;
457
457
while ( scope ) {
458
458
const node = scope . block ;
459
- const isClass = node . type === 'ClassExpression' ;
460
459
const isFunction = / F u n c t i o n / . test ( node . type ) ; // Functions
461
460
const isArrowFunction = node . type === 'ArrowFunctionExpression' ;
462
- let functionScope = scope ;
461
+ let enclosingScope = scope ;
463
462
if ( isArrowFunction ) {
464
- functionScope = utils . getParentFunctionScope ( scope ) ;
463
+ enclosingScope = utils . getArrowFunctionScope ( scope ) ;
465
464
}
466
- const methodNode = functionScope && functionScope . block . parent ;
467
- const isMethod = methodNode && methodNode . type === 'MethodDefinition' ; // Classes methods
465
+ const enclosingScopeType = enclosingScope && enclosingScope . block . type ;
466
+ const enclosingScopeParent = enclosingScope && enclosingScope . block . parent ;
467
+ const isClass = enclosingScopeType === 'ClassDeclaration' || enclosingScopeType === 'ClassExpression' ;
468
+ const isMethod = enclosingScopeParent && enclosingScopeParent . type === 'MethodDefinition' ; // Classes methods
468
469
const isArgument = node . parent && node . parent . type === 'CallExpression' ; // Arguments (callback, etc.)
469
470
// Attribute Expressions inside JSX Elements (<button onClick={() => props.handleClick()}></button>)
470
471
const isJSXExpressionContainer = node . parent && node . parent . type === 'JSXExpressionContainer' ;
@@ -482,15 +483,16 @@ function componentRule(rule, context) {
482
483
} ,
483
484
484
485
/**
485
- * Get a parent scope created by a FunctionExpression or FunctionDeclaration
486
- * @param {Scope } scope The child scope
487
- * @returns {Scope } A parent function scope
486
+ * Get an enclosing scope used to find `this` value by an arrow function
487
+ * @param {Scope } scope Current scope
488
+ * @returns {Scope } An enclosing scope used by an arrow function
488
489
*/
489
- getParentFunctionScope ( scope ) {
490
+ getArrowFunctionScope ( scope ) {
490
491
scope = scope . upper ;
491
492
while ( scope ) {
492
493
const type = scope . block . type ;
493
- if ( type === 'FunctionExpression' || type === 'FunctionDeclaration' ) {
494
+ if ( type === 'FunctionExpression' || type === 'FunctionDeclaration'
495
+ || type === 'ClassDeclaration' || type === 'ClassExpression' ) {
494
496
return scope ;
495
497
}
496
498
scope = scope . upper ;
0 commit comments