Skip to content

Commit 9799131

Browse files
iiisonljharb
authored andcommitted
[Fix] display-name: Get rid of false position on component detection
Fixes #2751. Closes #2758.
1 parent 53a0d84 commit 9799131

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
88
### Added
99
* add [`no-namespace`] rule ([#2640] @yacinehmito @ljharb)
1010

11+
### Fixed
12+
* [`display-name`]: Get rid of false position on component detection ([#2759] @iiison)
13+
1114
[#2640]: https://github.com/yannickcr/eslint-plugin-react/pull/2640
15+
[#2759]: https://github.com/yannickcr/eslint-plugin-react/pull/2759
1216

1317
## [7.25.3] - 2021.09.19
1418

lib/rules/display-name.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ module.exports = {
101101
const namedFunctionExpression = (
102102
astUtil.isFunctionLikeExpression(node)
103103
&& node.parent
104-
&& (node.parent.type === 'VariableDeclarator' || node.parent.method === true)
104+
&& (node.parent.type === 'VariableDeclarator' || node.parent.type === 'Property' || node.parent.method === true)
105105
&& (!node.parent.parent || !utils.isES5Component(node.parent.parent))
106106
);
107107

@@ -161,6 +161,7 @@ module.exports = {
161161
if (ignoreTranspilerName || !hasTranspilerName(node)) {
162162
return;
163163
}
164+
164165
if (components.get(node)) {
165166
markDisplayNameAsDeclared(node);
166167
}

tests/lib/rules/display-name.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,26 @@ ruleTester.run('display-name', rule, {
504504
class Link extends Component<LinkProps> {}
505505
`,
506506
parser: parsers.BABEL_ESLINT
507+
}, {
508+
code: `
509+
const x = {
510+
title: "URL",
511+
dataIndex: "url",
512+
key: "url",
513+
render: url => (
514+
<a href={url} target="_blank" rel="noopener noreferrer">
515+
<p>lol</p>
516+
</a>
517+
)
518+
}
519+
`,
520+
parser: parsers.BABEL_ESLINT
521+
}, {
522+
code: `
523+
const renderer = a => function Component(listItem) {
524+
return <div>{a} {listItem}</div>;
525+
};
526+
`
507527
}],
508528

509529
invalid: [{
@@ -921,5 +941,12 @@ ruleTester.run('display-name', rule, {
921941
endLine: 11,
922942
endColumn: 11
923943
}]
944+
}, {
945+
code: `
946+
const renderer = a => listItem => (
947+
<div>{a} {listItem}</div>
948+
);
949+
`,
950+
errors: [{message: 'Component definition is missing display name'}]
924951
}]
925952
});

0 commit comments

Comments
 (0)