From cefec6f832cf6f375c64b1d7fadcd78ab78e4d06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Madrigal?= Date: Wed, 27 Jul 2022 09:16:01 +0200 Subject: [PATCH] [Fix] display-name: fix HOF returning null as Components Fixes #3346 --- lib/util/Components.js | 44 +++++++++++++++++++++++++++++++++ tests/lib/rules/display-name.js | 28 +++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/lib/util/Components.js b/lib/util/Components.js index dec1143162..109ad34cea 100644 --- a/lib/util/Components.js +++ b/lib/util/Components.js @@ -517,6 +517,50 @@ function componentRule(rule, context) { return undefined; } + // case: any = () => () => null + if (node.parent.type === 'ArrowFunctionExpression' && node.parent.parent.type === 'AssignmentExpression' && !isPropertyAssignment && utils.isReturningJSXOrNull(node)) { + if (isFirstLetterCapitalized(node.parent.parent.left.name)) { + return node; + } + return undefined; + } + + // case: { any: () => () => null } + if (node.parent.type === 'ArrowFunctionExpression' && node.parent.parent.type === 'ExpressionStatement' && !isPropertyAssignment && utils.isReturningJSXOrNull(node) && node.parent.parent.parent.label) { + if (isFirstLetterCapitalized(node.parent.parent.parent.label.name)) { + return node; + } + return undefined; + } + + // case: any = function() {return function() {return null;};} + if (node.parent.type === 'ReturnStatement') { + if (isFirstLetterCapitalized(node.id && node.id.name)) { + return node; + } + const functionExpr = node.parent.parent.parent; + if (functionExpr.parent.type === 'AssignmentExpression' && !isPropertyAssignment && utils.isReturningJSXOrNull(node)) { + if (isFirstLetterCapitalized(functionExpr.parent.left.name)) { + return node; + } + return undefined; + } + } + + // case: { any: function() {return function() {return null;};} } + if (node.parent.type === 'ReturnStatement') { + if (isFirstLetterCapitalized(node.id && node.id.name)) { + return node; + } + const functionExpr = node.parent.parent.parent; + if (functionExpr.parent.type === 'Property' && !isPropertyAssignment && utils.isReturningJSXOrNull(node)) { + if (isFirstLetterCapitalized(functionExpr.parent.key.name)) { + return node; + } + return undefined; + } + } + // for case abc = { [someobject.somekey]: props => { ... return not-jsx } } if (node.parent && node.parent.key && node.parent.key.type === 'MemberExpression' && !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 ba4b9181fe..b9bf8e93fb 100644 --- a/tests/lib/rules/display-name.js +++ b/tests/lib/rules/display-name.js @@ -615,6 +615,34 @@ ruleTester.run('display-name', rule, { }; `, }, + { + // issue #3346 + code: ` + demo = () => () => null; + `, + }, + { + // issue #3346 + code: ` + { + property: () => () => null; + } + `, + }, + { + // issue #3346 + code: ` + demo = function() {return function() {return null;};}; + `, + }, + { + // issue #3346 + code: ` + { + property = function() {return function() {return null;};}; + } + `, + }, { // issue #3303 code: `