Skip to content

Commit 3963193

Browse files
apbarreroljharb
authored andcommitted
[Fix] display-name: fix false positive for assignment of function returning null
Fixes #3329
1 parent f7fe38f commit 3963193

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1414
* [`display-name`]: Accept forwardRef and Memo nesting in newer React versions ([#3321][] @TildaDares)
1515
* [`jsx-key`]: avoid a crash from optional chaining from [#3320][] ([#3327][] @ljharb)
1616
* [`jsx-key`]: avoid a crash on a non-array node.body from [#3320][] ([#3328][] @ljharb)
17+
* [`display-name`]: fix false positive for assignment of function returning null ([#3331][] @apbarrero)
1718

1819
### Changed
1920
* [Refactor] [`jsx-indent-props`]: improved readability of the checkNodesIndent function ([#3315][] @caroline223)
2021
* [Tests] [`jsx-indent`], [`jsx-one-expression-per-line`]: add passing test cases ([#3314][] @ROSSROSALES)
2122
* [Refactor] `boolean-prop-naming`, `jsx-indent`: avoid assigning to arguments ([#3316][] @caroline223)
2223

24+
[#3331]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3331
2325
[#3328]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3328
2426
[#3327]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3327
2527
[#3321]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3321

lib/util/Components.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,15 @@ function componentRule(rule, context) {
508508
return undefined;
509509
}
510510

511+
// case: any = () => { return => null }
512+
// case: any = () => null
513+
if (node.parent.type === 'AssignmentExpression' && !isPropertyAssignment && utils.isReturningJSXOrNull(node)) {
514+
if (isFirstLetterCapitalized(node.parent.left.name)) {
515+
return node;
516+
}
517+
return undefined;
518+
}
519+
511520
// for case abc = { [someobject.somekey]: props => { ... return not-jsx } }
512521
if (node.parent && node.parent.key && node.parent.key.type === 'MemberExpression' && !utils.isReturningJSX(node) && !utils.isReturningOnlyNull(node)) {
513522
return undefined;

tests/lib/rules/display-name.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,15 @@ ruleTester.run('display-name', rule, {
588588
}
589589
`,
590590
},
591+
{
592+
// issue #3329
593+
code: `
594+
let demo = null;
595+
demo = (a) => {
596+
if (a == null) return null;
597+
return f(a);
598+
};`,
599+
},
591600
{
592601
// issue #3303
593602
code: `

0 commit comments

Comments
 (0)