Skip to content

Commit 3f22b3c

Browse files
committed
Use class/function declaration name as displayName (closes #201)
1 parent 67fcd09 commit 3f22b3c

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Object {
6262
exports[`main fixtures processes component "component_4.js" without errors 1`] = `
6363
Object {
6464
"description": "",
65+
"displayName": "Parent",
6566
"methods": Array [],
6667
"props": Object {
6768
"child": Object {
@@ -176,6 +177,7 @@ Object {
176177
exports[`main fixtures processes component "component_8.js" without errors 1`] = `
177178
Object {
178179
"description": "",
180+
"displayName": "Parent",
179181
"methods": Array [
180182
Object {
181183
"docblock": null,
@@ -197,13 +199,15 @@ Object {
197199
exports[`main fixtures processes component "component_9.js" without errors 1`] = `
198200
Object {
199201
"description": "Should be recognized as component.",
202+
"displayName": "ExampleComponent",
200203
"methods": Array [],
201204
}
202205
`;
203206

204207
exports[`main fixtures processes component "component_10.js" without errors 1`] = `
205208
Object {
206209
"description": "React component that display current time at current location.",
210+
"displayName": "Clock",
207211
"methods": Array [
208212
Object {
209213
"description": "Update clock state with new time",

src/handlers/__tests__/displayNameHandler-test.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,28 @@ describe('defaultPropsHandler', () => {
7070
expect(documentation.displayName).not.toBeDefined();
7171
});
7272

73-
describe('ClassDeclaration displayName getter', () => {
73+
describe('ClassDeclaration', () => {
7474

75-
it('considers class methods', () => {
75+
it('considers the class name', () => {
76+
const definition = statement(`
77+
class Foo {
78+
}
79+
`);
80+
expect(() => displayNameHandler(documentation, definition)).not.toThrow();
81+
expect(documentation.displayName).toBe('Foo');
82+
});
83+
84+
it('considers a static displayName class property', () => {
85+
const definition = statement(`
86+
class Foo {
87+
static displayName = 'foo';
88+
}
89+
`);
90+
expect(() => displayNameHandler(documentation, definition)).not.toThrow();
91+
expect(documentation.displayName).toBe('foo');
92+
});
93+
94+
it('considers static displayName getter', () => {
7695
const definition = statement(`
7796
class Foo {
7897
static get displayName() {
@@ -84,7 +103,7 @@ describe('defaultPropsHandler', () => {
84103
expect(documentation.displayName).toBe('foo');
85104
});
86105

87-
it('resolves variables in class methods', () => {
106+
it('resolves variables in displayName getter', () => {
88107
const definition = statement(`
89108
class Foo {
90109
static get displayName() {

src/handlers/displayNameHandler.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import type Documentation from '../Documentation';
1414

1515
import getMemberValuePath from '../utils/getMemberValuePath';
16+
import getNameOrValue from '../utils/getNameOrValue';
1617
import recast from 'recast';
1718
import resolveToValue from '../utils/resolveToValue';
1819
import {traverseShallow} from '../utils/traverse';
@@ -25,6 +26,14 @@ export default function displayNameHandler(
2526
) {
2627
let displayNamePath = getMemberValuePath(path, 'displayName');
2728
if (!displayNamePath) {
29+
// Function and class declarations need special treatment. The name of the
30+
// function / class is the displayName
31+
if (
32+
types.ClassDeclaration.check(path.node) ||
33+
types.FunctionDeclaration.check(path.node)
34+
) {
35+
documentation.set('displayName', getNameOrValue(path.get('id')));
36+
}
2837
return;
2938
}
3039
displayNamePath = resolveToValue(displayNamePath);

0 commit comments

Comments
 (0)