Skip to content

Commit

Permalink
[Fix] sort-prop-types: Check for undefined before accessing `node.t…
Browse files Browse the repository at this point in the history
…ypeAnnotation.typeAnnotation`
  • Loading branch information
tylerlaprade authored and ljharb committed Jul 8, 2024
1 parent 51d342b commit 67d73aa
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion lib/rules/sort-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
8 changes: 8 additions & 0 deletions tests/lib/rules/sort-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,14 @@ ruleTester.run('sort-prop-types', rule, {
`,
features: ['types'],
options: [{ checkTypes: false }],
},
{
code: `
function Foo() {
return <div />;
}
`,
options: [{ checkTypes: true }],
}
)),
invalid: parsers.all([].concat(
Expand Down

0 comments on commit 67d73aa

Please sign in to comment.