Skip to content

extract display name correctly even if components are wrapped #277

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
Jul 13, 2018
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
18 changes: 18 additions & 0 deletions src/__tests__/__snapshots__/main-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -613,3 +613,21 @@ Object {
},
}
`;

exports[`main fixtures processes component "component_14.js" without errors 1`] = `
Object {
"description": "",
"displayName": "UncoloredView",
"methods": Array [],
"props": Object {
"color": Object {
"description": "",
"flowType": Object {
"name": "string",
"nullable": true,
},
"required": false,
},
},
}
`;
11 changes: 11 additions & 0 deletions src/__tests__/fixtures/component_14.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type Props = $ReadOnly<{|
color?: ?string,
|}>;

const ColoredView = React.forwardRef((props: Props, ref) => (
<View style={{backgroundColor: props.color}} />
));

ColoredView.displayName = 'UncoloredView';

module.exports = ColoredView;
32 changes: 32 additions & 0 deletions src/handlers/__tests__/displayNameHandler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ describe('defaultPropsHandler', () => {
expect(documentation.displayName).toBe('Foo');
});

it('considers the variable name even if wrapped', () => {
const definition = statement('var Foo = React.forwardRef(() => {});').get(
'declarations',
0,
'init',
'arguments',
0,
);
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 @@ -183,6 +195,17 @@ describe('defaultPropsHandler', () => {
expect(documentation.displayName).toBe('Foo');
});

it('considers the variable name on assign even if wrapped', () => {
const definition = statement('Foo = React.forwardRef(() => {});').get(
'expression',
'right',
'arguments',
0,
);
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 All @@ -191,5 +214,14 @@ describe('defaultPropsHandler', () => {
expect(() => displayNameHandler(documentation, definition)).not.toThrow();
expect(documentation.displayName).toBe('Bar');
});

it('considers a static displayName object property over variable name even if wrapped', () => {
const definition = statement(`
var Foo = React.forwardRef(() => {});
Foo.displayName = 'Bar';
`);
expect(() => displayNameHandler(documentation, definition)).not.toThrow();
expect(documentation.displayName).toBe('Bar');
});
});
});
26 changes: 16 additions & 10 deletions src/handlers/displayNameHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,22 @@ export default function displayNameHandler(
types.ArrowFunctionExpression.check(path.node) ||
types.FunctionExpression.check(path.node)
) {
if (types.VariableDeclarator.check(path.parentPath.node)) {
documentation.set(
'displayName',
getNameOrValue(path.parentPath.get('id')),
);
} else if (types.AssignmentExpression.check(path.parentPath.node)) {
documentation.set(
'displayName',
getNameOrValue(path.parentPath.get('left')),
);
let currentPath = path;
while (currentPath.parent) {
if (types.VariableDeclarator.check(currentPath.parent.node)) {
documentation.set(
'displayName',
getNameOrValue(currentPath.parent.get('id')),
);
return;
} else if (types.AssignmentExpression.check(currentPath.parent.node)) {
documentation.set(
'displayName',
getNameOrValue(currentPath.parent.get('left')),
);
return;
}
currentPath = currentPath.parent;
}
}
return;
Expand Down
11 changes: 8 additions & 3 deletions src/utils/getMemberExpressionValuePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,16 @@ function resolveName(path) {
types.ArrowFunctionExpression.check(path.node) ||
types.TaggedTemplateExpression.check(path.node)
) {
if (!types.VariableDeclarator.check(path.parent.node)) {
return;
let currentPath = path;
while (currentPath.parent) {
if (types.VariableDeclarator.check(currentPath.parent.node)) {
return currentPath.parent.get('id', 'name').value;
}

currentPath = currentPath.parent;
}

return path.parent.get('id', 'name').value;
return;
}

throw new TypeError(
Expand Down