-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Support detecting React.forwardRef/React.memo #2089
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
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 |
|---|---|---|
|
|
@@ -6,6 +6,8 @@ | |
|
|
||
| const util = require('util'); | ||
| const doctrine = require('doctrine'); | ||
| const arrayIncludes = require('array-includes'); | ||
|
|
||
| const variableUtil = require('./variable'); | ||
| const pragmaUtil = require('./pragma'); | ||
| const astUtil = require('./ast'); | ||
|
|
@@ -253,18 +255,17 @@ function componentRule(rule, context) { | |
| }, | ||
|
|
||
| /** | ||
| * Check if createElement is destructured from React import | ||
| * Check if variable is destructured from React import | ||
| * | ||
| * @param {variable} String The variable name to check | ||
| * @returns {Boolean} True if createElement is destructured from React | ||
| */ | ||
| hasDestructuredReactCreateElement: function() { | ||
| isDestructuredFromReactImport: function(variable) { | ||
| const variables = variableUtil.variablesInScope(context); | ||
| const variable = variableUtil.getVariable(variables, 'createElement'); | ||
| if (variable) { | ||
| const map = variable.scope.set; | ||
| if (map.has('React')) { | ||
| return true; | ||
| } | ||
| const variableInScope = variableUtil.getVariable(variables, variable); | ||
| if (variableInScope) { | ||
| const map = variableInScope.scope.set; | ||
| return map.has('React'); | ||
| } | ||
| return false; | ||
| }, | ||
|
|
@@ -291,7 +292,7 @@ function componentRule(rule, context) { | |
| node.callee.name === 'createElement' | ||
| ); | ||
|
|
||
| if (this.hasDestructuredReactCreateElement()) { | ||
| if (this.isDestructuredFromReactImport('createElement')) { | ||
| return calledDirectly || calledOnReact; | ||
| } | ||
| return calledOnReact; | ||
|
|
@@ -394,6 +395,18 @@ function componentRule(rule, context) { | |
| return utils.isReturningJSX(ASTNode, strict) || utils.isReturningNull(ASTNode); | ||
| }, | ||
|
|
||
| isReactComponentWrapper(node) { | ||
| if (node.type !== 'CallExpression') { | ||
| return false; | ||
| } | ||
| const propertyNames = ['forwardRef', 'memo']; | ||
|
Member
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. Should these kick in only when the React version setting is 16.3 and 16.6 respectively?
Contributor
Author
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. Should they? Would this break anything for people on earlier versions?
Member
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. Probably not, no - but it might be weird to see a warning about a React feature that you can’t use yet.
Contributor
Author
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. This wouldn't show any warnings related to the features. It only improves the detection of components using them. So, if they aren't use the features, nothing happens, right? But I could see the case for not running the check on earlier versions.
Member
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 that’s a fair point too. I’m not really sure whether it’s better to keep it simple and run the checks always, or to only run the checks when the version dictates. |
||
| const calleeObject = node.callee.object; | ||
| if (calleeObject) { | ||
| return arrayIncludes(propertyNames, node.callee.property.name) && node.callee.object.name === 'React'; | ||
jomasti marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| return arrayIncludes(propertyNames, node.callee.name) && this.isDestructuredFromReactImport(node.callee.name); | ||
| }, | ||
|
|
||
| /** | ||
| * Find a return statment in the current node | ||
| * | ||
|
|
@@ -463,7 +476,7 @@ function componentRule(rule, context) { | |
| const enclosingScopeParent = enclosingScope && enclosingScope.block.parent; | ||
| const isClass = enclosingScope && astUtil.isClass(enclosingScope.block); | ||
| const isMethod = enclosingScopeParent && enclosingScopeParent.type === 'MethodDefinition'; // Classes methods | ||
| const isArgument = node.parent && node.parent.type === 'CallExpression'; // Arguments (callback, etc.) | ||
| const isArgument = node.parent && node.parent.type === 'CallExpression' && !this.isReactComponentWrapper(node.parent); // Arguments (callback, etc.) | ||
| // Attribute Expressions inside JSX Elements (<button onClick={() => props.handleClick()}></button>) | ||
| const isJSXExpressionContainer = node.parent && node.parent.type === 'JSXExpressionContainer'; | ||
| // Stop moving up if we reach a class or an argument (like a callback) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.