Skip to content

fix: infer displayName given forwardRef() node #436

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

Merged
merged 1 commit into from
Feb 13, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/handlers/__tests__/displayNameHandler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,17 @@ describe('defaultPropsHandler', () => {
expect(documentation.displayName).toBe('Foo');
});

it('considers the variable name when handling forwardRef', () => {
const definition = statement(
[
'var Foo = React.forwardRef(() => {});',
'import React from "react";',
].join('\n'),
).get('declarations', 0, 'init');
expect(() => displayNameHandler(documentation, definition)).not.toThrow();
expect(documentation.displayName).toBe('Foo');
});

it('considers the variable name on assign', () => {
const definition = statement('Foo = () => {};').get(
'expression',
Expand All @@ -201,6 +212,17 @@ describe('defaultPropsHandler', () => {
expect(documentation.displayName).toBe('Foo');
});

it('considers the variable name on assign when handling forwardRef call', () => {
const definition = statement(
[
'Foo = React.forwardRef(() => {});',
'import React from "react";',
].join('\n'),
).get('expression', 'right');
expect(() => displayNameHandler(documentation, definition)).not.toThrow();
expect(documentation.displayName).toBe('Foo');
});

it('considers a static displayName object property over variable name', () => {
const definition = statement(`
var Foo = () => {};
Expand Down
4 changes: 3 additions & 1 deletion src/handlers/displayNameHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import { namedTypes as t } from 'ast-types';
import getMemberValuePath from '../utils/getMemberValuePath';
import getNameOrValue from '../utils/getNameOrValue';
import isReactForwardRefCall from '../utils/isReactForwardRefCall';
import resolveToValue from '../utils/resolveToValue';
import resolveFunctionDefinitionToReturnValue from '../utils/resolveFunctionDefinitionToReturnValue';
import type Documentation from '../Documentation';
Expand All @@ -29,7 +30,8 @@ export default function displayNameHandler(
documentation.set('displayName', getNameOrValue(path.get('id')));
} else if (
t.ArrowFunctionExpression.check(path.node) ||
t.FunctionExpression.check(path.node)
t.FunctionExpression.check(path.node) ||
isReactForwardRefCall(path)
) {
let currentPath = path;
while (currentPath.parent) {
Expand Down