Skip to content

Commit

Permalink
[Fix] display-name: unwrap TS as expressions
Browse files Browse the repository at this point in the history
Fixes #3032
  • Loading branch information
ljharb committed Oct 22, 2021
1 parent 86b3712 commit 8a0b352
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -14,7 +14,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
* [`jsx-fragments`], [`jsx-no-useless-fragment`]: avoid a crash on fragment syntax in `typescript-eslint` parser (@ljharb)
* [`jsx-props-no-multi-spaces`]: avoid a crash on long member chains in tag names in `typescript-eslint` parser (@ljharb)
* [`no-unused-prop-types`], `usedPropTypes`: avoid crash with typescript-eslint parser (@ljharb)
* [`display-name`]: unwrap TS `as` expressions ([#3110][] @ljharb)

[#3110]: https://github.com/yannickcr/eslint-plugin-react/pull/3110
[#3092]: https://github.com/yannickcr/eslint-plugin-react/pull/3092
[#2166]: https://github.com/yannickcr/eslint-plugin-react/pull/2166
[#1980]: https://github.com/yannickcr/eslint-plugin-react/pull/1980
Expand Down
10 changes: 5 additions & 5 deletions lib/rules/display-name.js
Expand Up @@ -5,6 +5,8 @@

'use strict';

const values = require('object.values');

const Components = require('../util/Components');
const astUtil = require('../util/ast');
const docsUrl = require('../util/docsUrl');
Expand Down Expand Up @@ -122,7 +124,6 @@ module.exports = {
// --------------------------------------------------------------------------

return {

ClassProperty(node) {
if (!propsUtil.isDisplayNameDeclaration(node)) {
return;
Expand All @@ -138,7 +139,7 @@ module.exports = {
if (!component) {
return;
}
markDisplayNameAsDeclared(component.node);
markDisplayNameAsDeclared(component.node.type === 'TSAsExpression' ? component.node.expression : component.node);
},

FunctionExpression(node) {
Expand Down Expand Up @@ -215,7 +216,6 @@ module.exports = {
// This means that we raise a single error for the call to React.memo
// instead of one for React.memo and one for React.forwardRef
const isWrappedInAnotherPragma = utils.getPragmaComponentWrapper(node);

if (
!isWrappedInAnotherPragma
&& (ignoreTranspilerName || !hasTranspilerName(node.arguments[0]))
Expand All @@ -232,8 +232,8 @@ module.exports = {
'Program:exit'() {
const list = components.list();
// Report missing display name for all components
Object.keys(list).filter((component) => !list[component].hasDisplayName).forEach((component) => {
reportMissingDisplayName(list[component]);
values(list).filter((component) => !component.hasDisplayName).forEach((component) => {
reportMissingDisplayName(component);
});
},
};
Expand Down
15 changes: 15 additions & 0 deletions tests/lib/rules/display-name.js
Expand Up @@ -520,6 +520,21 @@ ruleTester.run('display-name', rule, {
};
`,
},
{
// issue 3032
code: `
const Comp = React.forwardRef((props, ref) => <main />);
Comp.displayName = 'MyCompName';
`,
},
{
// issue 3032
code: `
const Comp = React.forwardRef((props, ref) => <main data-as="yes" />) as SomeComponent;
Comp.displayName = 'MyCompNameAs';
`,
features: ['ts', 'no-babel'],
},
]),

invalid: parsers.all([
Expand Down

0 comments on commit 8a0b352

Please sign in to comment.