Skip to content

Commit cefec6f

Browse files
committed
[Fix] display-name: fix HOF returning null as Components
Fixes #3346
1 parent 9139830 commit cefec6f

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

lib/util/Components.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,50 @@ function componentRule(rule, context) {
517517
return undefined;
518518
}
519519

520+
// case: any = () => () => null
521+
if (node.parent.type === 'ArrowFunctionExpression' && node.parent.parent.type === 'AssignmentExpression' && !isPropertyAssignment && utils.isReturningJSXOrNull(node)) {
522+
if (isFirstLetterCapitalized(node.parent.parent.left.name)) {
523+
return node;
524+
}
525+
return undefined;
526+
}
527+
528+
// case: { any: () => () => null }
529+
if (node.parent.type === 'ArrowFunctionExpression' && node.parent.parent.type === 'ExpressionStatement' && !isPropertyAssignment && utils.isReturningJSXOrNull(node) && node.parent.parent.parent.label) {
530+
if (isFirstLetterCapitalized(node.parent.parent.parent.label.name)) {
531+
return node;
532+
}
533+
return undefined;
534+
}
535+
536+
// case: any = function() {return function() {return null;};}
537+
if (node.parent.type === 'ReturnStatement') {
538+
if (isFirstLetterCapitalized(node.id && node.id.name)) {
539+
return node;
540+
}
541+
const functionExpr = node.parent.parent.parent;
542+
if (functionExpr.parent.type === 'AssignmentExpression' && !isPropertyAssignment && utils.isReturningJSXOrNull(node)) {
543+
if (isFirstLetterCapitalized(functionExpr.parent.left.name)) {
544+
return node;
545+
}
546+
return undefined;
547+
}
548+
}
549+
550+
// case: { any: function() {return function() {return null;};} }
551+
if (node.parent.type === 'ReturnStatement') {
552+
if (isFirstLetterCapitalized(node.id && node.id.name)) {
553+
return node;
554+
}
555+
const functionExpr = node.parent.parent.parent;
556+
if (functionExpr.parent.type === 'Property' && !isPropertyAssignment && utils.isReturningJSXOrNull(node)) {
557+
if (isFirstLetterCapitalized(functionExpr.parent.key.name)) {
558+
return node;
559+
}
560+
return undefined;
561+
}
562+
}
563+
520564
// for case abc = { [someobject.somekey]: props => { ... return not-jsx } }
521565
if (node.parent && node.parent.key && node.parent.key.type === 'MemberExpression' && !utils.isReturningJSX(node) && !utils.isReturningOnlyNull(node)) {
522566
return undefined;

tests/lib/rules/display-name.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,34 @@ ruleTester.run('display-name', rule, {
615615
};
616616
`,
617617
},
618+
{
619+
// issue #3346
620+
code: `
621+
demo = () => () => null;
622+
`,
623+
},
624+
{
625+
// issue #3346
626+
code: `
627+
{
628+
property: () => () => null;
629+
}
630+
`,
631+
},
632+
{
633+
// issue #3346
634+
code: `
635+
demo = function() {return function() {return null;};};
636+
`,
637+
},
638+
{
639+
// issue #3346
640+
code: `
641+
{
642+
property = function() {return function() {return null;};};
643+
}
644+
`,
645+
},
618646
{
619647
// issue #3303
620648
code: `

0 commit comments

Comments
 (0)