Skip to content

Commit

Permalink
[Fix] display-name: fix misinterpreted function components
Browse files Browse the repository at this point in the history
- when determining if an `ArrowFunctionExpression` returns JSX, do not check arguments to a returned `CallExpression`
  • Loading branch information
danielfinke committed Sep 1, 2021
1 parent 810806e commit 0ed3bdc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
## Unreleased

### Fixed
* [`display-name`]: fix arrow function returning result of function call with JSX arguments being interpreted as component ([#3065][], @danielfinke)
* [`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)

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(`
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));

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

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

0 comments on commit 0ed3bdc

Please sign in to comment.