Skip to content

fix parsing displayName for components defined using function expression #516

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 3 additions & 4 deletions src/__tests__/data/MultipleAllWithDisplayName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import * as React from 'react';
interface ButtonProps
extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'type'> {}

export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
(props, ref) => <button {...props} ref={ref} type="button" />
);

export function Button(props: ButtonProps) {
return <button {...props} type="button" />;
}
Button.displayName = 'First';

export const SubmitButton = React.forwardRef<HTMLButtonElement, ButtonProps>(
Expand Down
14 changes: 14 additions & 0 deletions src/__tests__/data/StatelessDisplayNameFunctionExpression.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export interface StatelessFunctionExpressionProps {
/** myProp description */
myProp: string;
}

/** StatelessFunctionExpression description */
export function StatelessFunctionExpression(
props: StatelessFunctionExpressionProps
) {
return <div>My Property = {props.myProp}</div>;
}

StatelessFunctionExpression.displayName =
'StatelessDisplayNameFunctionExpression';
10 changes: 10 additions & 0 deletions src/__tests__/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,16 @@ describe('parser', () => {
assert.equal(parsed.displayName, 'StatelessDisplayName');
});

it('should be taken from stateless component `displayName` property (using named export and function expression)', () => {
Copy link
Author

@radnan radnan Jun 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you give me an example of a file that would fail? I am surprised we don't have it in our test suite.

Did these multiple function expressions in the single file ever work?

@pvasek

added a test to showcase how existing behavior is broken - it's not really related to exporting single or multiple components (you really shouldn't be exporting multiple components in same file in the first place 😛 )

rather just that if you use traditional function expression instead of arrow expression the computeComponentName is no longer working because of the additional flowNodeNameEscapedText check

const [parsed] = parse(
fixturePath('StatelessDisplayNameFunctionExpression')
);
assert.equal(
parsed.displayName,
'StatelessDisplayNameFunctionExpression'
);
});

it('should be taken from stateful component `displayName` property (using named export)', () => {
const [parsed] = parse(fixturePath('StatefulDisplayName'));
assert.equal(parsed.displayName, 'StatefulDisplayName');
Expand Down
3 changes: 2 additions & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,8 @@ function getTextValueOfFunctionProperty(
(expr.left as ts.PropertyAccessExpression).name &&
(expr.left as ts.PropertyAccessExpression).name.escapedText ===
propertyName &&
flowNodeNameEscapedText === exp.escapedName
(!flowNodeNameEscapedText ||
flowNodeNameEscapedText === exp.escapedName)
);
})
.filter(statement => {
Expand Down