New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix no-this-in-sfc
for class properties
#1995
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -456,15 +456,16 @@ function componentRule(rule, context) { | |
let scope = context.getScope(); | ||
while (scope) { | ||
const node = scope.block; | ||
const isClass = node.type === 'ClassExpression'; | ||
const isFunction = /Function/.test(node.type); // Functions | ||
const isArrowFunction = node.type === 'ArrowFunctionExpression'; | ||
let functionScope = scope; | ||
let enclosingScope = scope; | ||
if (isArrowFunction) { | ||
functionScope = utils.getParentFunctionScope(scope); | ||
enclosingScope = utils.getArrowFunctionScope(scope); | ||
} | ||
const methodNode = functionScope && functionScope.block.parent; | ||
const isMethod = methodNode && methodNode.type === 'MethodDefinition'; // Classes methods | ||
const enclosingScopeType = enclosingScope && enclosingScope.block.type; | ||
const enclosingScopeParent = enclosingScope && enclosingScope.block.parent; | ||
const isClass = enclosingScopeType === 'ClassDeclaration' || enclosingScopeType === 'ClassExpression'; | ||
const isMethod = enclosingScopeParent && enclosingScopeParent.type === 'MethodDefinition'; // Classes methods | ||
const isArgument = node.parent && node.parent.type === 'CallExpression'; // Arguments (callback, etc.) | ||
// Attribute Expressions inside JSX Elements (<button onClick={() => props.handleClick()}></button>) | ||
const isJSXExpressionContainer = node.parent && node.parent.type === 'JSXExpressionContainer'; | ||
|
@@ -482,15 +483,16 @@ function componentRule(rule, context) { | |
}, | ||
|
||
/** | ||
* Get a parent scope created by a FunctionExpression or FunctionDeclaration | ||
* @param {Scope} scope The child scope | ||
* @returns {Scope} A parent function scope | ||
* Get an enclosing scope used to find `this` value by an arrow function | ||
* @param {Scope} scope Current scope | ||
* @returns {Scope} An enclosing scope used by an arrow function | ||
*/ | ||
getParentFunctionScope(scope) { | ||
getArrowFunctionScope(scope) { | ||
scope = scope.upper; | ||
while (scope) { | ||
const type = scope.block.type; | ||
if (type === 'FunctionExpression' || type === 'FunctionDeclaration') { | ||
if (type === 'FunctionExpression' || type === 'FunctionDeclaration' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think we have a utility function for "is a function" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't find |
||
|| type === 'ClassDeclaration' || type === 'ClassExpression') { | ||
return scope; | ||
} | ||
scope = scope.upper; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ const ERROR_MESSAGE = 'Stateless functional components should not use this'; | |
const rule = require('../../../lib/rules/no-this-in-sfc'); | ||
const RuleTester = require('eslint').RuleTester; | ||
|
||
require('babel-eslint'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ljharb do you know if this is really necessary? Not all of the test files that use this parser actually have the require. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you run those test files in isolation, do they pass? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, they do - just checked with ESLint 3, 4, and 5. could be an artifact from an even earlier time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see any differences after removing |
||
|
||
const parserOptions = { | ||
ecmaVersion: 2018, | ||
sourceType: 'module', | ||
|
@@ -119,6 +121,24 @@ ruleTester.run('no-this-in-sfc', rule, { | |
}; | ||
} | ||
}` | ||
}, { | ||
code: ` | ||
class Foo { | ||
bar = () => { | ||
this.something(); | ||
return null; | ||
}; | ||
}`, | ||
parser: 'babel-eslint' | ||
}, { | ||
code: ` | ||
class Foo { | ||
bar = () => () => { | ||
this.something(); | ||
return null; | ||
}; | ||
}`, | ||
parser: 'babel-eslint' | ||
}], | ||
invalid: [{ | ||
code: ` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems like it could be
const enclosingScope = isArrowFunction ? utils.getArrowFunctionScope(scope) : scope;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure thing, fixed