Skip to content

Commit 1cca82f

Browse files
committed
[Fix] prop-types/propTypes: follow a returned identifier to see if it is JSX
Fixes #1046
1 parent 7a975a9 commit 1cca82f

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1818
* `propTypes`: Handle TSTypeReference in no-unused-prop-type ([#3195][] @niik)
1919
* [`sort-prop-types`]: avoid repeated warnings of the same node/reason ([#519][] @ljharb)
2020
* [`jsx-indent`]: Fix indent handling for closing parentheses ([#620][] @stefanbuck])
21+
* [`prop-types`/`propTypes`]: follow a returned identifier to see if it is JSX ([#1046][] @ljharb)
2122

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

lib/util/jsx.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const estraverse = require('estraverse');
88
const elementType = require('jsx-ast-utils/elementType');
99

1010
const astUtil = require('./ast');
11+
const variableUtil = require('./variable');
1112

1213
// See https://github.com/babel/babel/blob/ce420ba51c68591e057696ef43e028f41c6e04cd/packages/babel-types/src/validators/react/isCompatTag.js
1314
// for why we only test for the first character
@@ -125,7 +126,8 @@ function isReturningJSX(isCreateElement, ASTnode, context, strict, ignoreNull) {
125126
break;
126127
case 'JSXElement':
127128
case 'JSXFragment':
128-
setFound(); break;
129+
setFound();
130+
break;
129131
case 'CallExpression':
130132
if (isCreateElement(childNode)) {
131133
setFound();
@@ -137,6 +139,13 @@ function isReturningJSX(isCreateElement, ASTnode, context, strict, ignoreNull) {
137139
setFound();
138140
}
139141
break;
142+
case 'Identifier': {
143+
const variable = variableUtil.findVariableByName(context, childNode.name);
144+
if (isJSX(variable)) {
145+
setFound();
146+
}
147+
break;
148+
}
140149
default:
141150
}
142151
},

tests/lib/rules/prop-types.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7624,6 +7624,33 @@ ruleTester.run('prop-types', rule, {
76247624
},
76257625
],
76267626
features: ['ts', 'no-babel'],
7627+
},
7628+
{
7629+
code: `
7630+
const Foo = ({ foo }) => {
7631+
return <SomeJSX />;
7632+
}
7633+
`,
7634+
errors: [
7635+
{
7636+
messageId: 'missingPropType',
7637+
data: { name: 'foo' },
7638+
},
7639+
],
7640+
},
7641+
{
7642+
code: `
7643+
const Foo = ({ foo }) => {
7644+
const returnValue = <SomeJSX />;
7645+
return returnValue;
7646+
}
7647+
`,
7648+
errors: [
7649+
{
7650+
messageId: 'missingPropType',
7651+
data: { name: 'foo' },
7652+
},
7653+
],
76277654
}
76287655
)),
76297656
});

tests/util/jsx.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,16 @@ const parseCode = (code) => {
2020
return ASTnode.body[0];
2121
};
2222

23-
const mockContext = {};
23+
const mockContext = {
24+
getScope() {
25+
return {
26+
type: 'global',
27+
upper: null,
28+
childScopes: [],
29+
variables: [],
30+
};
31+
},
32+
};
2433

2534
describe('jsxUtil', () => {
2635
describe('isReturningJSX', () => {

0 commit comments

Comments
 (0)