Skip to content

Commit f9d5490

Browse files
motiz88danez
authored andcommitted
fix: infer displayName given forwardRef() node
1 parent 04f573f commit f9d5490

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/handlers/__tests__/displayNameHandler-test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,17 @@ describe('defaultPropsHandler', () => {
181181
expect(documentation.displayName).toBe('Foo');
182182
});
183183

184+
it('considers the variable name when handling forwardRef', () => {
185+
const definition = statement(
186+
[
187+
'var Foo = React.forwardRef(() => {});',
188+
'import React from "react";',
189+
].join('\n'),
190+
).get('declarations', 0, 'init');
191+
expect(() => displayNameHandler(documentation, definition)).not.toThrow();
192+
expect(documentation.displayName).toBe('Foo');
193+
});
194+
184195
it('considers the variable name on assign', () => {
185196
const definition = statement('Foo = () => {};').get(
186197
'expression',
@@ -201,6 +212,17 @@ describe('defaultPropsHandler', () => {
201212
expect(documentation.displayName).toBe('Foo');
202213
});
203214

215+
it('considers the variable name on assign when handling forwardRef call', () => {
216+
const definition = statement(
217+
[
218+
'Foo = React.forwardRef(() => {});',
219+
'import React from "react";',
220+
].join('\n'),
221+
).get('expression', 'right');
222+
expect(() => displayNameHandler(documentation, definition)).not.toThrow();
223+
expect(documentation.displayName).toBe('Foo');
224+
});
225+
204226
it('considers a static displayName object property over variable name', () => {
205227
const definition = statement(`
206228
var Foo = () => {};

src/handlers/displayNameHandler.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import { namedTypes as t } from 'ast-types';
1111
import getMemberValuePath from '../utils/getMemberValuePath';
1212
import getNameOrValue from '../utils/getNameOrValue';
13+
import isReactForwardRefCall from '../utils/isReactForwardRefCall';
1314
import resolveToValue from '../utils/resolveToValue';
1415
import resolveFunctionDefinitionToReturnValue from '../utils/resolveFunctionDefinitionToReturnValue';
1516
import type Documentation from '../Documentation';
@@ -29,7 +30,8 @@ export default function displayNameHandler(
2930
documentation.set('displayName', getNameOrValue(path.get('id')));
3031
} else if (
3132
t.ArrowFunctionExpression.check(path.node) ||
32-
t.FunctionExpression.check(path.node)
33+
t.FunctionExpression.check(path.node) ||
34+
isReactForwardRefCall(path)
3335
) {
3436
let currentPath = path;
3537
while (currentPath.parent) {

0 commit comments

Comments
 (0)