Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] display-name, component detection: fix HOF returning null as Components #3347

Merged
merged 1 commit into from Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -17,6 +17,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
* [`display-name`]: fix false positive for assignment of function returning null ([#3331][] @apbarrero)
* [`display-name`]: fix identifying `_` as a capital letter ([#3335][] @apbarrero)
* [`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)

### Changed
* [Refactor] [`jsx-indent-props`]: improved readability of the checkNodesIndent function ([#3315][] @caroline223)
Expand All @@ -28,6 +29,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

[#3350]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3350
[#3349]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3349
[#3347]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3347
[#3344]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3344
[#3339]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3339
[#3335]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3335
Expand Down
41 changes: 41 additions & 0 deletions lib/util/Components.js
Expand Up @@ -517,6 +517,47 @@ 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 === 'Property' && !isPropertyAssignment && utils.isReturningJSXOrNull(node)) {
if (isFirstLetterCapitalized(node.parent.parent.key.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') {
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
56 changes: 56 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: `
demo = {
property: () => () => null
}
`,
},
{
// issue #3346
code: `
demo = function() {return function() {return null;};};
`,
},
{
// issue #3346
code: `
demo = {
property: function() {return function() {return null;};}
}
`,
},
{
// issue #3303
code: `
Expand Down Expand Up @@ -1141,5 +1169,33 @@ 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' }],
},
]),
});