From fc9664fca84291764a4a66820c7e3e91ec9fe8c1 Mon Sep 17 00:00:00 2001 From: Chiawen Chen Date: Fri, 20 May 2022 08:31:41 +0800 Subject: [PATCH] [Fix] `display-name`: fix false positive for HOF returning only nulls Fixes #3289. --- CHANGELOG.md | 5 +++++ lib/util/Components.js | 9 +++++++-- tests/lib/rules/display-name.js | 9 +++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b2562c83c8..5fd1bfbf7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange ## Unreleased +### Fixed +* [`display-name`]: fix false positive for HOF returning only nulls ([#3291][] @golopot) + +[#3291]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3291 + ## [7.30.0] - 2022.05.18 ### Added diff --git a/lib/util/Components.js b/lib/util/Components.js index 30fa7841a4..eb60d845a5 100644 --- a/lib/util/Components.js +++ b/lib/util/Components.js @@ -498,8 +498,13 @@ function componentRule(rule, context) { return undefined; } - // case: function any() { return (props) { return not-jsx-and-not-null } } - if (node.parent.type === 'ReturnStatement' && !utils.isReturningJSX(node) && !utils.isReturningOnlyNull(node)) { + // case: const any = () => { return (props) => null } + // case: const any = () => (props) => null + if ( + (node.parent.type === 'ReturnStatement' || (node.parent.type === 'ArrowFunctionExpression' && node.parent.expression)) + && !utils.isReturningJSX(node) + && !utils.isReturningOnlyNull(node) + ) { return undefined; } diff --git a/tests/lib/rules/display-name.js b/tests/lib/rules/display-name.js index 662f8e273c..be0c8442e5 100644 --- a/tests/lib/rules/display-name.js +++ b/tests/lib/rules/display-name.js @@ -579,6 +579,15 @@ ruleTester.run('display-name', rule, { } `, }, + { + // issue #3289 + code: ` + export const demo = (a) => (b) => { + if (a == null) return null; + return b; + } + `, + }, ]), invalid: parsers.all([