Skip to content

Commit 8a0b352

Browse files
committed
[Fix] display-name: unwrap TS as expressions
Fixes #3032
1 parent 86b3712 commit 8a0b352

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
1414
* [`jsx-fragments`], [`jsx-no-useless-fragment`]: avoid a crash on fragment syntax in `typescript-eslint` parser (@ljharb)
1515
* [`jsx-props-no-multi-spaces`]: avoid a crash on long member chains in tag names in `typescript-eslint` parser (@ljharb)
1616
* [`no-unused-prop-types`], `usedPropTypes`: avoid crash with typescript-eslint parser (@ljharb)
17+
* [`display-name`]: unwrap TS `as` expressions ([#3110][] @ljharb)
1718

19+
[#3110]: https://github.com/yannickcr/eslint-plugin-react/pull/3110
1820
[#3092]: https://github.com/yannickcr/eslint-plugin-react/pull/3092
1921
[#2166]: https://github.com/yannickcr/eslint-plugin-react/pull/2166
2022
[#1980]: https://github.com/yannickcr/eslint-plugin-react/pull/1980

lib/rules/display-name.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
'use strict';
77

8+
const values = require('object.values');
9+
810
const Components = require('../util/Components');
911
const astUtil = require('../util/ast');
1012
const docsUrl = require('../util/docsUrl');
@@ -122,7 +124,6 @@ module.exports = {
122124
// --------------------------------------------------------------------------
123125

124126
return {
125-
126127
ClassProperty(node) {
127128
if (!propsUtil.isDisplayNameDeclaration(node)) {
128129
return;
@@ -138,7 +139,7 @@ module.exports = {
138139
if (!component) {
139140
return;
140141
}
141-
markDisplayNameAsDeclared(component.node);
142+
markDisplayNameAsDeclared(component.node.type === 'TSAsExpression' ? component.node.expression : component.node);
142143
},
143144

144145
FunctionExpression(node) {
@@ -215,7 +216,6 @@ module.exports = {
215216
// This means that we raise a single error for the call to React.memo
216217
// instead of one for React.memo and one for React.forwardRef
217218
const isWrappedInAnotherPragma = utils.getPragmaComponentWrapper(node);
218-
219219
if (
220220
!isWrappedInAnotherPragma
221221
&& (ignoreTranspilerName || !hasTranspilerName(node.arguments[0]))
@@ -232,8 +232,8 @@ module.exports = {
232232
'Program:exit'() {
233233
const list = components.list();
234234
// Report missing display name for all components
235-
Object.keys(list).filter((component) => !list[component].hasDisplayName).forEach((component) => {
236-
reportMissingDisplayName(list[component]);
235+
values(list).filter((component) => !component.hasDisplayName).forEach((component) => {
236+
reportMissingDisplayName(component);
237237
});
238238
},
239239
};

tests/lib/rules/display-name.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,21 @@ ruleTester.run('display-name', rule, {
520520
};
521521
`,
522522
},
523+
{
524+
// issue 3032
525+
code: `
526+
const Comp = React.forwardRef((props, ref) => <main />);
527+
Comp.displayName = 'MyCompName';
528+
`,
529+
},
530+
{
531+
// issue 3032
532+
code: `
533+
const Comp = React.forwardRef((props, ref) => <main data-as="yes" />) as SomeComponent;
534+
Comp.displayName = 'MyCompNameAs';
535+
`,
536+
features: ['ts', 'no-babel'],
537+
},
523538
]),
524539

525540
invalid: parsers.all([

0 commit comments

Comments
 (0)