From 7302a2a644ff92eba4bba99a332e04883f674a15 Mon Sep 17 00:00:00 2001 From: Noah Negin-Ulster Date: Thu, 4 Aug 2022 09:08:51 -0400 Subject: [PATCH] [Fix] `require-default-props`: avoid a crash when function has no props param --- CHANGELOG.md | 2 ++ lib/rules/require-default-props.js | 4 ++++ tests/lib/rules/require-default-props.js | 9 +++++++++ 3 files changed, 15 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 90bc4e80e5..56573ef758 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange * [`jsx-key`]: avoid a crash on a non-array node.body from [#3320][] ([#3328][] @ljharb) * [`display-name`]: fix false positive for assignment of function returning null ([#3331][] @apbarrero) * [`display-name`]: fix identifying `_` as a capital letter ([#3335][] @apbarrero) +* [`require-default-props`]: avoid a crash when function has no props param ([#3350][] @noahnu) ### Changed * [Refactor] [`jsx-indent-props`]: improved readability of the checkNodesIndent function ([#3315][] @caroline223) @@ -23,6 +24,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange * [Refactor] `boolean-prop-naming`, `jsx-indent`: avoid assigning to arguments ([#3316][] @caroline223) * [Docs] `sort-comp`: add class component examples ([#3339][] @maurer2) +[#3350]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3350 [#3339]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3339 [#3335]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3335 [#3331]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3331 diff --git a/lib/rules/require-default-props.js b/lib/rules/require-default-props.js index c779b9293c..b5de236c5f 100644 --- a/lib/rules/require-default-props.js +++ b/lib/rules/require-default-props.js @@ -125,6 +125,10 @@ module.exports = { const props = componentNode.params[0]; const propTypes = declaredPropTypes; + if (!props) { + return; + } + if (props.type === 'Identifier') { const hasOptionalProp = values(propTypes).some((propType) => !propType.isRequired); if (hasOptionalProp) { diff --git a/tests/lib/rules/require-default-props.js b/tests/lib/rules/require-default-props.js index 972df97930..8ba5d53d7c 100644 --- a/tests/lib/rules/require-default-props.js +++ b/tests/lib/rules/require-default-props.js @@ -361,6 +361,15 @@ ruleTester.run('require-default-props', rule, { `, options: [{ functions: 'defaultArguments' }], }, + { + code: ` + const NoPropsComponent = function () { + return
Hello, world!
; + } + NoPropsComponent.propTypes = {}; + `, + options: [{ functions: 'defaultArguments' }], + }, // stateless components as arrow function expressions {