Skip to content

Commit

Permalink
[Fix] display-name/component detection: avoid a crash on anonymous …
Browse files Browse the repository at this point in the history
…components

Fixes #2840.
  • Loading branch information
ljharb committed Oct 21, 2020
1 parent ac98c0f commit 57d0df7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
* [`jsx-newline`]: add new rule ([#2693][] @jzabala)
* [`jsx-no-constructed-context-values`]: add new rule which checks when the value passed to a Context Provider will cause needless rerenders ([#2763][] @dylanOshima)

### Fixed
* [`display-name`]/component detection: avoid a crash on anonymous components ([#2840][] @ljharb)

[#2840]: https://github.com/yannickcr/eslint-plugin-react/issues/2840
[#2835]: https://github.com/yannickcr/eslint-plugin-react/pull/2835
[#2763]: https://github.com/yannickcr/eslint-plugin-react/pull/2763
[#2693]: https://github.com/yannickcr/eslint-plugin-react/pull/2693
Expand Down
2 changes: 1 addition & 1 deletion lib/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ function componentRule(rule, context) {
return false;
}).map((val) => {
if (val.node.type === 'ArrowFunctionExpression') return val.node.parent.id.name;
return val.node.id.name;
return val.node.id && val.node.id.name;
});
},

Expand Down
27 changes: 27 additions & 0 deletions tests/lib/rules/display-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -894,5 +894,32 @@ ruleTester.run('display-name', rule, {
errors: [{
message: 'Component definition is missing display name'
}]
}, {
code: `
export default class extends React.PureComponent {
render() {
return <Card />;
}
}
const Card = (() => {
return React.memo(({ }) => (
<div />
));
})();
`,
errors: [{
message: 'Component definition is missing display name',
line: 2,
column: 22,
endLine: 6,
endColumn: 8
}, {
message: 'Component definition is missing display name',
line: 9,
column: 16,
endLine: 11,
endColumn: 11
}]
}]
});

0 comments on commit 57d0df7

Please sign in to comment.