Skip to content

Commit 893dbff

Browse files
committed
[Fix] display-name: avoid false positives on non-creatClass object expressions
Fixes #3144
1 parent eb81897 commit 893dbff

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
1010
* [`no-unused-class-component-methods`]: add `getChildContext` lifecycle method ([#3136][] @yoyo837)
1111
* [`prop-types`]: fix false positives on renames in object destructuring ([#3142][] @golopot)
1212
* [`no-arrow-function-lifecycle`]: fix invalid autofix from a concise arrow method to a regular one ([#3145][] @ljharb)
13+
* [`display-name`]: avoid false positives on non-creatClass object expressions ([#3144] @ljharb)
1314

1415
### Changed
1516
* [readme] fix syntax typo ([#3141][] @moselhy)
1617

1718
[#3145]: https://github.com/yannickcr/eslint-plugin-react/issue/3145
19+
[#3144]: https://github.com/yannickcr/eslint-plugin-react/issue/3144
1820
[#3142]: https://github.com/yannickcr/eslint-plugin-react/pull/3142
1921
[#3141]: https://github.com/yannickcr/eslint-plugin-react/pull/3141
2022
[#3136]: https://github.com/yannickcr/eslint-plugin-react/pull/3136

lib/rules/display-name.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ module.exports = {
192192
},
193193

194194
ObjectExpression(node) {
195+
if (!utils.isES5Component(node)) {
196+
return;
197+
}
195198
if (ignoreTranspilerName || !hasTranspilerName(node)) {
196199
// Search for the displayName declaration
197200
node.properties.forEach((property) => {

tests/lib/rules/display-name.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,5 +914,32 @@ ruleTester.run('display-name', rule, {
914914
{ message: 'Component definition is missing display name' },
915915
],
916916
},
917+
{
918+
code: `
919+
const processData = (options?: { value: string }) => options?.value || 'no data';
920+
921+
export const Component = observer(() => {
922+
const data = processData({ value: 'data' });
923+
return <div>{data}</div>;
924+
});
925+
926+
export const Component2 = observer(() => {
927+
const data = processData();
928+
return <div>{data}</div>;
929+
});
930+
`,
931+
features: ['optional chaining', 'types'],
932+
settings: { componentWrapperFunctions: ['observer'] },
933+
errors: [
934+
{
935+
message: 'Component definition is missing display name',
936+
line: 4,
937+
},
938+
{
939+
message: 'Component definition is missing display name',
940+
line: 9,
941+
},
942+
],
943+
},
917944
]),
918945
});

0 commit comments

Comments
 (0)