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: fix misinterpreted function components #3065

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 @@ -8,7 +8,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
### Fixed
* [`jsx-no-useless-fragments`]: Handle insignificant whitespace correctly when `allowExpressions` is `true` ([#3061][] @benj-dobs)
* [`prop-types`], `propTypes`: handle implicit `children` prop in react's generic types ([#3064][] @vedadeepta)
* [`display-name`]: fix arrow function returning result of function call with JSX arguments being interpreted as component ([#3065][], @danielfinke)

[#3065]: https://github.com/yannickcr/eslint-plugin-react/pull/3065
[#3064]: https://github.com/yannickcr/eslint-plugin-react/pull/3064
[#3061]: https://github.com/yannickcr/eslint-plugin-react/pull/3061

Expand Down
1 change: 1 addition & 0 deletions lib/util/jsx.js
Expand Up @@ -130,6 +130,7 @@ function isReturningJSX(isCreateElement, ASTnode, context, strict, ignoreNull) {
if (isCreateElement(childNode)) {
setFound();
}
this.skip();
break;
case 'Literal':
if (!ignoreNull && childNode.value === null) {
Expand Down
22 changes: 18 additions & 4 deletions tests/util/jsx.js
Expand Up @@ -27,9 +27,7 @@ describe('jsxUtil', () => {
const assertValid = (codeStr) => assert(
isReturningJSX(() => false, parseCode(codeStr), mockContext)
);
const assertInValid = (codeStr) => assert(
!!isReturningJSX(() => false, parseCode(codeStr), mockContext)
);

it('Works when returning JSX', () => {
assertValid(`
function Test() {
Expand Down Expand Up @@ -71,11 +69,27 @@ describe('jsxUtil', () => {
});

it('Can ignore null', () => {
assertInValid(`
assertValid(`
ljharb marked this conversation as resolved.
Show resolved Hide resolved
function Test() {
return null;
}
`);
});

it('Ignores JSX arguments to function calls used as return value of arrow functions', () => {
let astNode = parseCode(`const obj = {
prop: () => test(<a>something</a>)
}`);
let arrowFunctionExpression = astNode.declarations[0].init.properties[0].value;

assert(!isReturningJSX(() => false, arrowFunctionExpression, mockContext));
ljharb marked this conversation as resolved.
Show resolved Hide resolved

astNode = parseCode(`const obj = {
prop: () => { return test(<a>something</a>); }
}`);
arrowFunctionExpression = astNode.declarations[0].init.properties[0].value;

assert(!isReturningJSX(() => false, arrowFunctionExpression, mockContext));
});
});
});