Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix]: avoid a crash when function has no props param #3350

Merged
merged 1 commit into from Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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