Skip to content

Commit 5f9863e

Browse files
committed
Fix display-name false positive for React.memo
1 parent c93cbc6 commit 5f9863e

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/util/Components.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ function componentRule(rule, context) {
479479
const isArgument = node.parent && node.parent.type === 'CallExpression'; // Arguments (callback, etc.)
480480
// Attribute Expressions inside JSX Elements (<button onClick={() => props.handleClick()}></button>)
481481
const isJSXExpressionContainer = node.parent && node.parent.type === 'JSXExpressionContainer';
482-
if (node.parent && this.isPragmaComponentWrapper(node.parent)) {
482+
if (isFunction && node.parent && this.isPragmaComponentWrapper(node.parent)) {
483483
return node.parent;
484484
}
485485
// Stop moving up if we reach a class or an argument (like a callback)
@@ -620,7 +620,9 @@ function componentRule(rule, context) {
620620
if (!utils.isPragmaComponentWrapper(node)) {
621621
return;
622622
}
623-
components.add(node, 2);
623+
if (node.arguments.length > 0 && astUtil.isFunctionLikeExpression(node.arguments[0])) {
624+
components.add(node, 2);
625+
}
624626
},
625627

626628
ClassExpression: function(node) {

tests/lib/rules/display-name.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,21 @@ ruleTester.run('display-name', rule, {
437437
createElement("a");
438438
`,
439439
parser: 'babel-eslint'
440+
}, {
441+
code: `
442+
import React from 'react'
443+
import { string } from 'prop-types'
444+
445+
function Component({ world }) {
446+
return <div>Hello {world}</div>
447+
}
448+
449+
Component.propTypes = {
450+
world: string,
451+
}
452+
453+
export default React.memo(Component)
454+
`
440455
}],
441456

442457
invalid: [{

0 commit comments

Comments
 (0)