From 4b509afda0188edfa0909d779b79a9ac01aab2cd Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sat, 28 Aug 2021 22:37:45 -0700 Subject: [PATCH] [Fix] `prop-types`, `propTypes`: prevent crash introduced in #3051 Fixes #3053 --- CHANGELOG.md | 2 ++ lib/util/propTypes.js | 22 ++++++++++++---------- tests/lib/rules/prop-types.js | 12 ++++++++++++ 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fae1848cc7..275af82699 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`, 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) @@ -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 diff --git a/lib/util/propTypes.js b/lib/util/propTypes.js index df24de496e..889d84596e 100644 --- a/lib/util/propTypes.js +++ b/lib/util/propTypes.js @@ -942,17 +942,19 @@ module.exports = function propTypesInstructions(context, components, utils) { return; } - if (annotation.typeName.name) { // if FC - const typeName = annotation.typeName.name; - if (!genericReactTypesImport.has(typeName)) { - return; - } - } else if (annotation.typeName.right.name) { // if React.FC - const right = annotation.typeName.right.name; - const left = annotation.typeName.left.name; + if (annotation.typeName) { + if (annotation.typeName.name) { // if FC + const typeName = annotation.typeName.name; + if (!genericReactTypesImport.has(typeName)) { + return; + } + } else if (annotation.typeName.right.name) { // if React.FC + 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)); diff --git a/tests/lib/rules/prop-types.js b/tests/lib/rules/prop-types.js index 3eaa9277d2..1e778596ec 100644 --- a/tests/lib/rules/prop-types.js +++ b/tests/lib/rules/prop-types.js @@ -3280,6 +3280,18 @@ ruleTester.run('prop-types', rule, { }; `, parser: parsers['@TYPESCRIPT_ESLINT'] + }, + { + code: ` + export const EuiSuperSelectControl: ( + props: EuiSuperSelectControlProps + ) => ReturnType>> = ({ + ...rest + }) => { + return null; + } + `, + parser: parsers['@TYPESCRIPT_ESLINT'] } ]), {