From 8e4d17a264d7f493ad936ea685dfb69dd3638e6e Mon Sep 17 00:00:00 2001 From: Chiawen Chen Date: Tue, 9 Aug 2022 19:31:57 +0800 Subject: [PATCH] [Fix] `display-name`, component detection: fix false positive for HOF returning only nulls and literals --- CHANGELOG.md | 2 ++ lib/util/Components.js | 1 - tests/lib/rules/display-name.js | 39 +++++++---------------- tests/lib/rules/no-this-in-sfc.js | 53 ------------------------------- 4 files changed, 13 insertions(+), 82 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dfa0a6f6b4..4491394d8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange * [`require-default-props`]: avoid a crash when function has no props param ([#3350][] @noahnu) * [`display-name`], component detection: fix HOF returning null as Components ([#3347][] @jxm-math) * [`forbid-prop-types`]: Ignore objects that are not of type React.PropTypes ([#3326][] @TildaDares) +* [`display-name`], component detection: fix false positive for HOF returning only nulls and literals ([#3305][] @golopot) ### Changed * [Refactor] [`jsx-indent-props`]: improved readability of the checkNodesIndent function ([#3315][] @caroline223) @@ -49,6 +50,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange [#3315]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3315 [#3314]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3314 [#3311]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3311 +[#3305]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3305 [#3262]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3262 ## [7.30.1] - 2022.06.23 diff --git a/lib/util/Components.js b/lib/util/Components.js index 7abfbee72f..273e424283 100644 --- a/lib/util/Components.js +++ b/lib/util/Components.js @@ -503,7 +503,6 @@ function componentRule(rule, context) { 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 841af839fc..420bc47c7b 100644 --- a/tests/lib/rules/display-name.js +++ b/tests/lib/rules/display-name.js @@ -556,6 +556,17 @@ ruleTester.run('display-name', rule, { } `, }, + { + // issue #3300 + code: ` + const f = (a) => () => { + if (a) { + return null; + } + return 1; + }; + `, + }, { code: ` class Test { @@ -1169,33 +1180,5 @@ ruleTester.run('display-name', rule, { }, ], }, - { - code: ` - Demo = () => () => null; - `, - errors: [{ messageId: 'noDisplayName' }], - }, - { - code: ` - demo = { - Property: () => () => null - } - `, - errors: [{ messageId: 'noDisplayName' }], - }, - { - code: ` - Demo = function() {return function() {return null;};}; - `, - errors: [{ messageId: 'noDisplayName' }], - }, - { - code: ` - demo = { - Property: function() {return function() {return null;};} - } - `, - errors: [{ messageId: 'noDisplayName' }], - }, ]), }); diff --git a/tests/lib/rules/no-this-in-sfc.js b/tests/lib/rules/no-this-in-sfc.js index a8e80b8b45..0bbf84af28 100644 --- a/tests/lib/rules/no-this-in-sfc.js +++ b/tests/lib/rules/no-this-in-sfc.js @@ -275,58 +275,5 @@ ruleTester.run('no-this-in-sfc', rule, { { messageId: 'noThisInSFC' }, ], }, - { - code: ` - class Foo { - bar() { - return () => { - this.something(); - return null; - } - } - } - `, - errors: [{ messageId: 'noThisInSFC' }], - }, - { - code: ` - class Foo { - bar = () => () => { - this.something(); - return null; - }; - } - `, - features: ['class fields', 'no-ts-old'], // TODO: FIXME: remove `no-ts-old` and fix - errors: [{ messageId: 'noThisInSFC' }], - }, - { - code: ` - class Foo { - bar() { - function Bar(){ - return () => { - this.something(); - return null; - } - } - } - } - `, - errors: [{ messageId: 'noThisInSFC' }], - }, - { - code: ` - class Foo { - bar() { - () => () => { - this.something(); - return null; - }; - } - } - `, - errors: [{ messageId: 'noThisInSFC' }], - }, ]), });