Skip to content

Commit

Permalink
[Fix] display-name: fix HOF returning null as Components
Browse files Browse the repository at this point in the history
Fixes #3346
  • Loading branch information
jxm-math committed Jul 27, 2022
1 parent 9139830 commit cefec6f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
44 changes: 44 additions & 0 deletions lib/util/Components.js
Expand Up @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions tests/lib/rules/display-name.js
Expand Up @@ -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: `
Expand Down

0 comments on commit cefec6f

Please sign in to comment.