diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e9fc569dd..258495898a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rules/display-name.js b/lib/rules/display-name.js index 59591606d5..b771eadb75 100644 --- a/lib/rules/display-name.js +++ b/lib/rules/display-name.js @@ -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)) ); @@ -161,6 +161,7 @@ module.exports = { if (ignoreTranspilerName || !hasTranspilerName(node)) { return; } + if (components.get(node)) { markDisplayNameAsDeclared(node); } diff --git a/tests/lib/rules/display-name.js b/tests/lib/rules/display-name.js index 72b9c73d03..6a93d50b9c 100644 --- a/tests/lib/rules/display-name.js +++ b/tests/lib/rules/display-name.js @@ -504,6 +504,26 @@ ruleTester.run('display-name', rule, { class Link extends Component {} `, parser: parsers.BABEL_ESLINT + }, { + code: ` + const x = { + title: "URL", + dataIndex: "url", + key: "url", + render: url => ( + +

lol

+
+ ) + } + `, + parser: parsers.BABEL_ESLINT + }, { + code: ` + const renderer = a => function Component(listItem) { + return
{a} {listItem}
; + }; + ` }], invalid: [{ @@ -921,5 +941,12 @@ ruleTester.run('display-name', rule, { endLine: 11, endColumn: 11 }] + }, { + code: ` + const renderer = a => listItem => ( +
{a} {listItem}
+ ); + `, + errors: [{message: 'Component definition is missing display name'}] }] });