Skip to content

Commit c81a4bc

Browse files
committed
fix(displayName): Get displayName from variable if nothing else found
1 parent b7b0de4 commit c81a4bc

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

src/__tests__/__snapshots__/main-test.js.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Object {
4747
exports[`main fixtures processes component "component_3.js" without errors 1`] = `
4848
Object {
4949
"description": "",
50+
"displayName": "Test",
5051
"methods": Array [],
5152
"props": Object {
5253
"style": Object {
@@ -102,6 +103,7 @@ Object {
102103
exports[`main fixtures processes component "component_5.js" without errors 1`] = `
103104
Object {
104105
"description": "",
106+
"displayName": "Button",
105107
"methods": Array [],
106108
"props": Object {
107109
"children": Object {
@@ -136,6 +138,7 @@ Object {
136138
exports[`main fixtures processes component "component_6.js" without errors 1`] = `
137139
Object {
138140
"description": "",
141+
"displayName": "Button",
139142
"methods": Array [],
140143
"props": Object {
141144
"children": Object {

src/handlers/__tests__/displayNameHandler-test.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,68 @@ describe('defaultPropsHandler', () => {
115115
expect(() => displayNameHandler(documentation, definition)).not.toThrow();
116116
expect(documentation.displayName).toBe('bar');
117117
});
118+
});
119+
120+
describe('FunctionDeclaration', () => {
121+
it('considers the function name', () => {
122+
const definition = statement('function Foo () {}');
123+
expect(() => displayNameHandler(documentation, definition)).not.toThrow();
124+
expect(documentation.displayName).toBe('Foo');
125+
});
126+
127+
it('considers a static displayName object property', () => {
128+
const definition = statement(`
129+
function Foo () {}
130+
Foo.displayName = 'Bar';
131+
`);
132+
expect(() => displayNameHandler(documentation, definition)).not.toThrow();
133+
expect(documentation.displayName).toBe('Bar');
134+
});
135+
});
136+
137+
describe('FunctionExpression', () => {
138+
it('considers the variable name', () => {
139+
const definition = statement('var Foo = function () {};').get('declarations', 0, 'init');
140+
expect(() => displayNameHandler(documentation, definition)).not.toThrow();
141+
expect(documentation.displayName).toBe('Foo');
142+
});
143+
144+
it('considers the variable name on assign', () => {
145+
const definition = statement('Foo = function () {};').get('expression', 'right');
146+
expect(() => displayNameHandler(documentation, definition)).not.toThrow();
147+
expect(documentation.displayName).toBe('Foo');
148+
});
118149

150+
it('considers a static displayName object property over variable name', () => {
151+
const definition = statement(`
152+
var Foo = function () {};
153+
Foo.displayName = 'Bar';
154+
`);
155+
expect(() => displayNameHandler(documentation, definition)).not.toThrow();
156+
expect(documentation.displayName).toBe('Bar');
157+
});
119158
});
120159

160+
describe('ArrowFunctionExpression', () => {
161+
it('considers the variable name', () => {
162+
const definition = statement('var Foo = () => {};').get('declarations', 0, 'init');
163+
expect(() => displayNameHandler(documentation, definition)).not.toThrow();
164+
expect(documentation.displayName).toBe('Foo');
165+
});
166+
167+
it('considers the variable name on assign', () => {
168+
const definition = statement('Foo = () => {};').get('expression', 'right');
169+
expect(() => displayNameHandler(documentation, definition)).not.toThrow();
170+
expect(documentation.displayName).toBe('Foo');
171+
});
172+
173+
it('considers a static displayName object property over variable name', () => {
174+
const definition = statement(`
175+
var Foo = () => {};
176+
Foo.displayName = 'Bar';
177+
`);
178+
expect(() => displayNameHandler(documentation, definition)).not.toThrow();
179+
expect(documentation.displayName).toBe('Bar');
180+
});
181+
});
121182
});

src/handlers/displayNameHandler.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ export default function displayNameHandler(
3333
types.FunctionDeclaration.check(path.node)
3434
) {
3535
documentation.set('displayName', getNameOrValue(path.get('id')));
36+
} else if (
37+
types.ArrowFunctionExpression.check(path.node) ||
38+
types.FunctionExpression.check(path.node)
39+
) {
40+
if (types.VariableDeclarator.check(path.parentPath.node)) {
41+
documentation.set('displayName', getNameOrValue(path.parentPath.get('id')));
42+
} else if (types.AssignmentExpression.check(path.parentPath.node)) {
43+
documentation.set('displayName', getNameOrValue(path.parentPath.get('left')));
44+
}
3645
}
3746
return;
3847
}

src/utils/__tests__/getFlowType-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ describe('getFlowType', () => {
7676

7777
it('detects array type shorthand optional type', () => {
7878
var typePath = expression('x: (?number)[]').get('typeAnnotation').get('typeAnnotation');
79-
expect(getFlowType(typePath)).toEqual({ name: 'Array', elements: [{ name: 'number', nullable: true }], raw: 'number[]' });
79+
expect(getFlowType(typePath)).toEqual({ name: 'Array', elements: [{ name: 'number', nullable: true }], raw: '(?number)[]' });
8080
});
8181

8282
it('detects array type shorthand', () => {

0 commit comments

Comments
 (0)