Skip to content

Commit

Permalink
[Fix] prefer-stateless-function: avoid a crash inside doctrine
Browse files Browse the repository at this point in the history
Fixes #2596
  • Loading branch information
ljharb committed Feb 19, 2022
1 parent 99b38a3 commit 227cf88
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -24,6 +24,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
* [`no-unused-state`]: TS: support `getDerivedStateFromProps` as an arrow function ([#2061][] @ljharb)
* [`no-array-index-key`]: catch `.toString` and `String()` usage ([#2813][] @RedTn)
* [`function-component-definition`]: do not break on dollar signs ([#3207][] @ljharb)
* [`prefer-stateless-function`]: avoid a crash inside `doctrine` ([#2596][] @ljharb)

### Changed
* [readme] change [`jsx-runtime`] link from branch to sha ([#3160][] @tatsushitoji)
Expand Down Expand Up @@ -56,6 +57,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
[#2921]: https://github.com/yannickcr/eslint-plugin-react/pull/2921
[#2813]: https://github.com/yannickcr/eslint-plugin-react/pull/2813
[#2753]: https://github.com/yannickcr/eslint-plugin-react/pull/2753
[#2596]: https://github.com/yannickcr/eslint-plugin-react/issues/2596
[#2061]: https://github.com/yannickcr/eslint-plugin-react/issues/2061
[#1817]: https://github.com/yannickcr/eslint-plugin-react/issues/1817
[#1815]: https://github.com/yannickcr/eslint-plugin-react/issues/1815
Expand Down
14 changes: 10 additions & 4 deletions lib/util/Components.js
Expand Up @@ -310,10 +310,16 @@ function componentRule(rule, context) {
return false;
}

const commentAst = doctrine.parse(comment.value, {
unwrap: true,
tags: ['extends', 'augments'],
});
let commentAst;
try {
commentAst = doctrine.parse(comment.value, {
unwrap: true,
tags: ['extends', 'augments'],
});
} catch (e) {
// handle a bug in the archived `doctrine`, see #2596
return false;
}

const relevantTags = commentAst.tags.filter((tag) => tag.name === 'React.Component' || tag.name === 'React.PureComponent');

Expand Down
10 changes: 10 additions & 0 deletions tests/lib/rules/prefer-stateless-function.js
Expand Up @@ -399,6 +399,16 @@ ruleTester.run('prefer-stateless-function', rule, {
features: ['class fields'],
options: [{ ignorePureComponents: true }],
},
{
code: `
/**
* @param a.
*/
function Comp() {
return <a></a>
}
`,
},
]),

invalid: parsers.all([
Expand Down

0 comments on commit 227cf88

Please sign in to comment.