From 5f9863e0856703c28e1e7cb9b1c78724dc12416b Mon Sep 17 00:00:00 2001 From: Joshua Stiefer Date: Wed, 2 Jan 2019 18:33:04 -0700 Subject: [PATCH] Fix display-name false positive for React.memo --- lib/util/Components.js | 6 ++++-- tests/lib/rules/display-name.js | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/util/Components.js b/lib/util/Components.js index 4cbb2ea760..852254e8fe 100644 --- a/lib/util/Components.js +++ b/lib/util/Components.js @@ -479,7 +479,7 @@ function componentRule(rule, context) { const isArgument = node.parent && node.parent.type === 'CallExpression'; // Arguments (callback, etc.) // Attribute Expressions inside JSX Elements () const isJSXExpressionContainer = node.parent && node.parent.type === 'JSXExpressionContainer'; - if (node.parent && this.isPragmaComponentWrapper(node.parent)) { + if (isFunction && node.parent && this.isPragmaComponentWrapper(node.parent)) { return node.parent; } // Stop moving up if we reach a class or an argument (like a callback) @@ -620,7 +620,9 @@ function componentRule(rule, context) { if (!utils.isPragmaComponentWrapper(node)) { return; } - components.add(node, 2); + if (node.arguments.length > 0 && astUtil.isFunctionLikeExpression(node.arguments[0])) { + components.add(node, 2); + } }, ClassExpression: function(node) { diff --git a/tests/lib/rules/display-name.js b/tests/lib/rules/display-name.js index 7776c4b472..7cd70f68d4 100644 --- a/tests/lib/rules/display-name.js +++ b/tests/lib/rules/display-name.js @@ -437,6 +437,21 @@ ruleTester.run('display-name', rule, { createElement("a"); `, parser: 'babel-eslint' + }, { + code: ` + import React from 'react' + import { string } from 'prop-types' + + function Component({ world }) { + return
Hello {world}
+ } + + Component.propTypes = { + world: string, + } + + export default React.memo(Component) + ` }], invalid: [{