Skip to content

Commit

Permalink
[Fix] require-default-props: avoid a crash when function has no pro…
Browse files Browse the repository at this point in the history
…ps param
  • Loading branch information
Noah Negin-Ulster authored and ljharb committed Aug 4, 2022
1 parent 9139830 commit 7302a2a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -16,13 +16,15 @@ 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)
* [Tests] [`jsx-indent`], [`jsx-one-expression-per-line`]: add passing test cases ([#3314][] @ROSSROSALES)
* [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
Expand Down
4 changes: 4 additions & 0 deletions lib/rules/require-default-props.js
Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions tests/lib/rules/require-default-props.js
Expand Up @@ -361,6 +361,15 @@ ruleTester.run('require-default-props', rule, {
`,
options: [{ functions: 'defaultArguments' }],
},
{
code: `
const NoPropsComponent = function () {
return <div>Hello, world!</div>;
}
NoPropsComponent.propTypes = {};
`,
options: [{ functions: 'defaultArguments' }],
},

// stateless components as arrow function expressions
{
Expand Down

0 comments on commit 7302a2a

Please sign in to comment.