Skip to content

Commit

Permalink
[Fix] prop-types/propTypes: follow a returned identifier to see i…
Browse files Browse the repository at this point in the history
…f it is JSX

Fixes #1046
  • Loading branch information
ljharb committed Feb 3, 2022
1 parent 7a975a9 commit 1cca82f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -18,6 +18,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
* `propTypes`: Handle TSTypeReference in no-unused-prop-type ([#3195][] @niik)
* [`sort-prop-types`]: avoid repeated warnings of the same node/reason ([#519][] @ljharb)
* [`jsx-indent`]: Fix indent handling for closing parentheses ([#620][] @stefanbuck])
* [`prop-types`/`propTypes`]: follow a returned identifier to see if it is JSX ([#1046][] @ljharb)

### Changed
* [readme] change [`jsx-runtime`] link from branch to sha ([#3160][] @tatsushitoji)
Expand All @@ -41,6 +42,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
[#3133]: https://github.com/yannickcr/eslint-plugin-react/pull/3133
[#2921]: https://github.com/yannickcr/eslint-plugin-react/pull/2921
[#2753]: https://github.com/yannickcr/eslint-plugin-react/pull/2753
[#1046]: https://github.com/yannickcr/eslint-plugin-react/issues/1046
[#620]: https://github.com/yannickcr/eslint-plugin-react/pull/620
[#519]: https://github.com/yannickcr/eslint-plugin-react/issues/519

Expand Down
11 changes: 10 additions & 1 deletion lib/util/jsx.js
Expand Up @@ -8,6 +8,7 @@ const estraverse = require('estraverse');
const elementType = require('jsx-ast-utils/elementType');

const astUtil = require('./ast');
const variableUtil = require('./variable');

// See https://github.com/babel/babel/blob/ce420ba51c68591e057696ef43e028f41c6e04cd/packages/babel-types/src/validators/react/isCompatTag.js
// for why we only test for the first character
Expand Down Expand Up @@ -125,7 +126,8 @@ function isReturningJSX(isCreateElement, ASTnode, context, strict, ignoreNull) {
break;
case 'JSXElement':
case 'JSXFragment':
setFound(); break;
setFound();
break;
case 'CallExpression':
if (isCreateElement(childNode)) {
setFound();
Expand All @@ -137,6 +139,13 @@ function isReturningJSX(isCreateElement, ASTnode, context, strict, ignoreNull) {
setFound();
}
break;
case 'Identifier': {
const variable = variableUtil.findVariableByName(context, childNode.name);
if (isJSX(variable)) {
setFound();
}
break;
}
default:
}
},
Expand Down
27 changes: 27 additions & 0 deletions tests/lib/rules/prop-types.js
Expand Up @@ -7624,6 +7624,33 @@ ruleTester.run('prop-types', rule, {
},
],
features: ['ts', 'no-babel'],
},
{
code: `
const Foo = ({ foo }) => {
return <SomeJSX />;
}
`,
errors: [
{
messageId: 'missingPropType',
data: { name: 'foo' },
},
],
},
{
code: `
const Foo = ({ foo }) => {
const returnValue = <SomeJSX />;
return returnValue;
}
`,
errors: [
{
messageId: 'missingPropType',
data: { name: 'foo' },
},
],
}
)),
});
11 changes: 10 additions & 1 deletion tests/util/jsx.js
Expand Up @@ -20,7 +20,16 @@ const parseCode = (code) => {
return ASTnode.body[0];
};

const mockContext = {};
const mockContext = {
getScope() {
return {
type: 'global',
upper: null,
childScopes: [],
variables: [],
};
},
};

describe('jsxUtil', () => {
describe('isReturningJSX', () => {
Expand Down

0 comments on commit 1cca82f

Please sign in to comment.