Skip to content

Commit 57d0df7

Browse files
committed
[Fix] display-name/component detection: avoid a crash on anonymous components
Fixes #2840.
1 parent ac98c0f commit 57d0df7

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
1010
* [`jsx-newline`]: add new rule ([#2693][] @jzabala)
1111
* [`jsx-no-constructed-context-values`]: add new rule which checks when the value passed to a Context Provider will cause needless rerenders ([#2763][] @dylanOshima)
1212

13+
### Fixed
14+
* [`display-name`]/component detection: avoid a crash on anonymous components ([#2840][] @ljharb)
15+
16+
[#2840]: https://github.com/yannickcr/eslint-plugin-react/issues/2840
1317
[#2835]: https://github.com/yannickcr/eslint-plugin-react/pull/2835
1418
[#2763]: https://github.com/yannickcr/eslint-plugin-react/pull/2763
1519
[#2693]: https://github.com/yannickcr/eslint-plugin-react/pull/2693

lib/util/Components.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ function componentRule(rule, context) {
577577
return false;
578578
}).map((val) => {
579579
if (val.node.type === 'ArrowFunctionExpression') return val.node.parent.id.name;
580-
return val.node.id.name;
580+
return val.node.id && val.node.id.name;
581581
});
582582
},
583583

tests/lib/rules/display-name.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,5 +894,32 @@ ruleTester.run('display-name', rule, {
894894
errors: [{
895895
message: 'Component definition is missing display name'
896896
}]
897+
}, {
898+
code: `
899+
export default class extends React.PureComponent {
900+
render() {
901+
return <Card />;
902+
}
903+
}
904+
905+
const Card = (() => {
906+
return React.memo(({ }) => (
907+
<div />
908+
));
909+
})();
910+
`,
911+
errors: [{
912+
message: 'Component definition is missing display name',
913+
line: 2,
914+
column: 22,
915+
endLine: 6,
916+
endColumn: 8
917+
}, {
918+
message: 'Component definition is missing display name',
919+
line: 9,
920+
column: 16,
921+
endLine: 11,
922+
endColumn: 11
923+
}]
897924
}]
898925
});

0 commit comments

Comments
 (0)