From 67d73aa0b7eed389ce5e9b6ce2ce38660c2676ae Mon Sep 17 00:00:00 2001 From: "Tyler C Laprade, CFA" Date: Mon, 8 Jul 2024 16:25:11 -0400 Subject: [PATCH] [Fix] `sort-prop-types`: Check for undefined before accessing `node.typeAnnotation.typeAnnotation` --- CHANGELOG.md | 2 ++ lib/rules/sort-prop-types.js | 5 ++++- tests/lib/rules/sort-prop-types.js | 8 ++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5db11b6487..4750122ca1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange ### Fixed * [`prop-types`]: fix `className` missing in prop validation false negative ([#3749] @akulsr0) +* [`sort-prop-types`]: Check for undefined before accessing `node.typeAnnotation.typeAnnotation` ([#3779] @tylerlaprade) +[#3779]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3779 [#3749]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3749 ## [7.34.3] - 2024.06.18 diff --git a/lib/rules/sort-prop-types.js b/lib/rules/sort-prop-types.js index 808dd1df1f..20effacc44 100644 --- a/lib/rules/sort-prop-types.js +++ b/lib/rules/sort-prop-types.js @@ -230,7 +230,10 @@ module.exports = { } function handleFunctionComponent(node) { - const firstArg = node.params[0].typeAnnotation && node.params[0].typeAnnotation.typeAnnotation; + const firstArg = node.params + && node.params.length > 0 + && node.params[0].typeAnnotation + && node.params[0].typeAnnotation.typeAnnotation; if (firstArg && firstArg.type === 'TSTypeReference') { const propType = typeAnnotations.get(firstArg.typeName.name) && typeAnnotations.get(firstArg.typeName.name)[0]; diff --git a/tests/lib/rules/sort-prop-types.js b/tests/lib/rules/sort-prop-types.js index a89ebeaf97..7fbe1b4f97 100644 --- a/tests/lib/rules/sort-prop-types.js +++ b/tests/lib/rules/sort-prop-types.js @@ -493,6 +493,14 @@ ruleTester.run('sort-prop-types', rule, { `, features: ['types'], options: [{ checkTypes: false }], + }, + { + code: ` + function Foo() { + return
; + } + `, + options: [{ checkTypes: true }], } )), invalid: parsers.all([].concat(