Skip to content

Commit 1a5a970

Browse files
committed
[Fix] display-name: fix misinterpreted function components
- when determining if an `ArrowFunctionExpression` returns JSX, do not check arguments to a returned `CallExpression`
1 parent 810806e commit 1a5a970

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
88
### Fixed
99
* [`jsx-no-useless-fragments`]: Handle insignificant whitespace correctly when `allowExpressions` is `true` ([#3061][] @benj-dobs)
1010
* [`prop-types`], `propTypes`: handle implicit `children` prop in react's generic types ([#3064][] @vedadeepta)
11+
* [`display-name`]: fix arrow function returning result of function call with JSX arguments being interpreted as component ([#3065][], @danielfinke)
1112

13+
[#3065]: https://github.com/yannickcr/eslint-plugin-react/pull/3065
1214
[#3064]: https://github.com/yannickcr/eslint-plugin-react/pull/3064
1315
[#3061]: https://github.com/yannickcr/eslint-plugin-react/pull/3061
1416

lib/util/jsx.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ function isReturningJSX(isCreateElement, ASTnode, context, strict, ignoreNull) {
130130
if (isCreateElement(childNode)) {
131131
setFound();
132132
}
133+
this.skip();
133134
break;
134135
case 'Literal':
135136
if (!ignoreNull && childNode.value === null) {

tests/util/jsx.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ describe('jsxUtil', () => {
2727
const assertValid = (codeStr) => assert(
2828
isReturningJSX(() => false, parseCode(codeStr), mockContext)
2929
);
30-
const assertInValid = (codeStr) => assert(
31-
!!isReturningJSX(() => false, parseCode(codeStr), mockContext)
32-
);
30+
3331
it('Works when returning JSX', () => {
3432
assertValid(`
3533
function Test() {
@@ -71,11 +69,27 @@ describe('jsxUtil', () => {
7169
});
7270

7371
it('Can ignore null', () => {
74-
assertInValid(`
72+
assertValid(`
7573
function Test() {
7674
return null;
7775
}
7876
`);
7977
});
78+
79+
it('Ignores JSX arguments to function calls used as return value of arrow functions', () => {
80+
let astNode = parseCode(`const obj = {
81+
prop: () => test(<a>something</a>)
82+
}`);
83+
let arrowFunctionExpression = astNode.declarations[0].init.properties[0].value;
84+
85+
assert(!isReturningJSX(() => false, arrowFunctionExpression, mockContext));
86+
87+
astNode = parseCode(`const obj = {
88+
prop: () => { return test(<a>something</a>); }
89+
}`);
90+
arrowFunctionExpression = astNode.declarations[0].init.properties[0].value;
91+
92+
assert(!isReturningJSX(() => false, arrowFunctionExpression, mockContext));
93+
});
8094
});
8195
});

0 commit comments

Comments
 (0)