Skip to content

Commit

Permalink
[Fix] prop-types, propTypes: prevent crash introduced in #3051
Browse files Browse the repository at this point in the history
Fixes #3053
  • Loading branch information
ljharb committed Aug 29, 2021
1 parent 6f78b75 commit 4b509af
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -24,6 +24,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
* [`jsx-no-bind`]: handle local function declarations ([#3048][] @p7g)
* [`prop-types`], `propTypes`: handle React.* TypeScript types ([#3049][] @vedadeepta)
* [`prop-types`], `propTypes`: add handling for `FC<Props>`, improve tests ([#3051][] @vedadeepta)
* [`prop-types`], `propTypes`: prevent crash introduced in [#3051][] ([#3053][] @ljharb)

### Changed
* [Docs] [`jsx-no-bind`]: updates discussion of refs ([#2998][] @dimitropoulos)
Expand All @@ -34,6 +35,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
* [Docs] improve instructions for `jsx-runtime` config ([#3052][] @ljharb)

[bb64df65]: https://github.com/yannickcr/eslint-plugin-react/commit/bb64df6505b3e9a01da5b61626ab9f544caea438
[#3053]: https://github.com/yannickcr/eslint-plugin-react/issues/3053
[#3052]: https://github.com/yannickcr/eslint-plugin-react/issues/3052
[#3051]: https://github.com/yannickcr/eslint-plugin-react/pull/3051
[#3049]: https://github.com/yannickcr/eslint-plugin-react/pull/3049
Expand Down
22 changes: 12 additions & 10 deletions lib/util/propTypes.js
Expand Up @@ -942,17 +942,19 @@ module.exports = function propTypesInstructions(context, components, utils) {
return;
}

if (annotation.typeName.name) { // if FC<Props>
const typeName = annotation.typeName.name;
if (!genericReactTypesImport.has(typeName)) {
return;
}
} else if (annotation.typeName.right.name) { // if React.FC<Props>
const right = annotation.typeName.right.name;
const left = annotation.typeName.left.name;
if (annotation.typeName) {
if (annotation.typeName.name) { // if FC<Props>
const typeName = annotation.typeName.name;
if (!genericReactTypesImport.has(typeName)) {
return;
}
} else if (annotation.typeName.right.name) { // if React.FC<Props>
const right = annotation.typeName.right.name;
const left = annotation.typeName.left.name;

if (!genericReactTypesImport.has(left) || !allowedGenericTypes.has(right)) {
return;
if (!genericReactTypesImport.has(left) || !allowedGenericTypes.has(right)) {
return;
}
}
}
markPropTypesAsDeclared(node, resolveTypeAnnotation(siblingIdentifier));
Expand Down
12 changes: 12 additions & 0 deletions tests/lib/rules/prop-types.js
Expand Up @@ -3280,6 +3280,18 @@ ruleTester.run('prop-types', rule, {
};
`,
parser: parsers['@TYPESCRIPT_ESLINT']
},
{
code: `
export const EuiSuperSelectControl: <T extends string>(
props: EuiSuperSelectControlProps<T>
) => ReturnType<FunctionComponent<EuiSuperSelectControlProps<T>>> = ({
...rest
}) => {
return null;
}
`,
parser: parsers['@TYPESCRIPT_ESLINT']
}
]),
{
Expand Down

0 comments on commit 4b509af

Please sign in to comment.