Skip to content

Commit 227cf88

Browse files
committed
[Fix] prefer-stateless-function: avoid a crash inside doctrine
Fixes #2596
1 parent 99b38a3 commit 227cf88

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
2424
* [`no-unused-state`]: TS: support `getDerivedStateFromProps` as an arrow function ([#2061][] @ljharb)
2525
* [`no-array-index-key`]: catch `.toString` and `String()` usage ([#2813][] @RedTn)
2626
* [`function-component-definition`]: do not break on dollar signs ([#3207][] @ljharb)
27+
* [`prefer-stateless-function`]: avoid a crash inside `doctrine` ([#2596][] @ljharb)
2728

2829
### Changed
2930
* [readme] change [`jsx-runtime`] link from branch to sha ([#3160][] @tatsushitoji)
@@ -56,6 +57,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
5657
[#2921]: https://github.com/yannickcr/eslint-plugin-react/pull/2921
5758
[#2813]: https://github.com/yannickcr/eslint-plugin-react/pull/2813
5859
[#2753]: https://github.com/yannickcr/eslint-plugin-react/pull/2753
60+
[#2596]: https://github.com/yannickcr/eslint-plugin-react/issues/2596
5961
[#2061]: https://github.com/yannickcr/eslint-plugin-react/issues/2061
6062
[#1817]: https://github.com/yannickcr/eslint-plugin-react/issues/1817
6163
[#1815]: https://github.com/yannickcr/eslint-plugin-react/issues/1815

lib/util/Components.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,16 @@ function componentRule(rule, context) {
310310
return false;
311311
}
312312

313-
const commentAst = doctrine.parse(comment.value, {
314-
unwrap: true,
315-
tags: ['extends', 'augments'],
316-
});
313+
let commentAst;
314+
try {
315+
commentAst = doctrine.parse(comment.value, {
316+
unwrap: true,
317+
tags: ['extends', 'augments'],
318+
});
319+
} catch (e) {
320+
// handle a bug in the archived `doctrine`, see #2596
321+
return false;
322+
}
317323

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

tests/lib/rules/prefer-stateless-function.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,16 @@ ruleTester.run('prefer-stateless-function', rule, {
399399
features: ['class fields'],
400400
options: [{ ignorePureComponents: true }],
401401
},
402+
{
403+
code: `
404+
/**
405+
* @param a.
406+
*/
407+
function Comp() {
408+
return <a></a>
409+
}
410+
`,
411+
},
402412
]),
403413

404414
invalid: parsers.all([

0 commit comments

Comments
 (0)