Skip to content

Commit 7302a2a

Browse files
Noah Negin-Ulsterljharb
Noah Negin-Ulster
authored andcommitted
[Fix] require-default-props: avoid a crash when function has no props param
1 parent 9139830 commit 7302a2a

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1616
* [`jsx-key`]: avoid a crash on a non-array node.body from [#3320][] ([#3328][] @ljharb)
1717
* [`display-name`]: fix false positive for assignment of function returning null ([#3331][] @apbarrero)
1818
* [`display-name`]: fix identifying `_` as a capital letter ([#3335][] @apbarrero)
19+
* [`require-default-props`]: avoid a crash when function has no props param ([#3350][] @noahnu)
1920

2021
### Changed
2122
* [Refactor] [`jsx-indent-props`]: improved readability of the checkNodesIndent function ([#3315][] @caroline223)
2223
* [Tests] [`jsx-indent`], [`jsx-one-expression-per-line`]: add passing test cases ([#3314][] @ROSSROSALES)
2324
* [Refactor] `boolean-prop-naming`, `jsx-indent`: avoid assigning to arguments ([#3316][] @caroline223)
2425
* [Docs] `sort-comp`: add class component examples ([#3339][] @maurer2)
2526

27+
[#3350]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3350
2628
[#3339]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3339
2729
[#3335]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3335
2830
[#3331]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3331

lib/rules/require-default-props.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ module.exports = {
125125
const props = componentNode.params[0];
126126
const propTypes = declaredPropTypes;
127127

128+
if (!props) {
129+
return;
130+
}
131+
128132
if (props.type === 'Identifier') {
129133
const hasOptionalProp = values(propTypes).some((propType) => !propType.isRequired);
130134
if (hasOptionalProp) {

tests/lib/rules/require-default-props.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,15 @@ ruleTester.run('require-default-props', rule, {
361361
`,
362362
options: [{ functions: 'defaultArguments' }],
363363
},
364+
{
365+
code: `
366+
const NoPropsComponent = function () {
367+
return <div>Hello, world!</div>;
368+
}
369+
NoPropsComponent.propTypes = {};
370+
`,
371+
options: [{ functions: 'defaultArguments' }],
372+
},
364373

365374
// stateless components as arrow function expressions
366375
{

0 commit comments

Comments
 (0)