Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] display-name: Get rid of false position on component detection #2759

Merged
merged 1 commit into from Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,7 +8,11 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
### Added
* add [`no-namespace`] rule ([#2640] @yacinehmito @ljharb)

### Fixed
* [`display-name`]: Get rid of false position on component detection ([#2759] @iiison)

[#2640]: https://github.com/yannickcr/eslint-plugin-react/pull/2640
[#2759]: https://github.com/yannickcr/eslint-plugin-react/pull/2759

## [7.25.3] - 2021.09.19

Expand Down
3 changes: 2 additions & 1 deletion lib/rules/display-name.js
Expand Up @@ -101,7 +101,7 @@ module.exports = {
const namedFunctionExpression = (
astUtil.isFunctionLikeExpression(node)
&& node.parent
&& (node.parent.type === 'VariableDeclarator' || node.parent.method === true)
&& (node.parent.type === 'VariableDeclarator' || node.parent.type === 'Property' || node.parent.method === true)
&& (!node.parent.parent || !utils.isES5Component(node.parent.parent))
);

Expand Down Expand Up @@ -161,6 +161,7 @@ module.exports = {
if (ignoreTranspilerName || !hasTranspilerName(node)) {
return;
}

if (components.get(node)) {
markDisplayNameAsDeclared(node);
}
Expand Down
27 changes: 27 additions & 0 deletions tests/lib/rules/display-name.js
Expand Up @@ -504,6 +504,26 @@ ruleTester.run('display-name', rule, {
class Link extends Component<LinkProps> {}
`,
parser: parsers.BABEL_ESLINT
}, {
code: `
const x = {
title: "URL",
dataIndex: "url",
key: "url",
render: url => (
<a href={url} target="_blank" rel="noopener noreferrer">
<p>lol</p>
</a>
)
}
`,
parser: parsers.BABEL_ESLINT
}, {
code: `
const renderer = a => function Component(listItem) {
return <div>{a} {listItem}</div>;
};
`
}],

invalid: [{
Expand Down Expand Up @@ -921,5 +941,12 @@ ruleTester.run('display-name', rule, {
endLine: 11,
endColumn: 11
}]
}, {
code: `
const renderer = a => listItem => (
<div>{a} {listItem}</div>
);
`,
errors: [{message: 'Component definition is missing display name'}]
}]
});