Skip to content

Commit bf7e891

Browse files
committed
Add displayName handler (closes #16)
This handler finds the `displayName` property/member and adds it to the documentation as `displayName`.
1 parent 49fb67c commit bf7e891

File tree

5 files changed

+125
-0
lines changed

5 files changed

+125
-0
lines changed

src/__tests__/main-test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe('main', () => {
2323
it('parses with default resolver/handlers', () => {
2424
var docs = docgen.parse(source);
2525
expect(docs).toEqual({
26+
displayName: 'ABC',
2627
description: 'Example component description',
2728
props: {
2829
foo: {
@@ -58,6 +59,7 @@ describe('main', () => {
5859
* Example component description
5960
*/
6061
var Component = React.createClass({
62+
displayName: 'ABC',
6163
propTypes: {
6264
/**
6365
* Example prop description
@@ -93,6 +95,7 @@ describe('main', () => {
9395
Component.defaultProps = {
9496
foo: true,
9597
};
98+
Component.displayName = 'ABC';
9699
`);
97100
});
98101

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
*/
10+
11+
/*global jest, describe, beforeEach, it, expect*/
12+
13+
jest.autoMockOff();
14+
jest.mock('../../Documentation');
15+
16+
describe('defaultPropsHandler', () => {
17+
var documentation;
18+
var displayNameHandler;
19+
var expression, statement;
20+
21+
beforeEach(() => {
22+
({expression, statement} = require('../../../tests/utils'));
23+
documentation = new (require('../../Documentation'));
24+
displayNameHandler = require('../displayNameHandler');
25+
});
26+
27+
it('extracts the displayName', () => {
28+
var definition = expression('({displayName: "FooBar"})');
29+
displayNameHandler(documentation, definition);
30+
expect(documentation.displayName).toBe('FooBar');
31+
32+
definition = statement(`
33+
class Foo {
34+
static displayName = "BarFoo";
35+
}
36+
`);
37+
displayNameHandler(documentation, definition);
38+
expect(documentation.displayName).toBe('BarFoo');
39+
});
40+
41+
it('resolves identifiers', () => {
42+
var definition = statement(`
43+
({displayName: name})
44+
var name = 'abc';
45+
`).get('expression');
46+
displayNameHandler(documentation, definition);
47+
expect(documentation.displayName).toBe('abc');
48+
49+
definition = statement(`
50+
class Foo {
51+
static displayName = name;
52+
}
53+
var name = 'xyz';
54+
`);
55+
displayNameHandler(documentation, definition);
56+
expect(documentation.displayName).toBe('xyz');
57+
});
58+
59+
it('ignores non-literal names', () => {
60+
var definition = expression('({displayName: foo.bar})');
61+
expect(() => displayNameHandler(documentation, definition)).not.toThrow();
62+
expect(documentation.displayName).not.toBeDefined();
63+
64+
definition = statement(`
65+
class Foo {
66+
static displayName = foo.bar;
67+
}
68+
`);
69+
expect(() => displayNameHandler(documentation, definition)).not.toThrow();
70+
expect(documentation.displayName).not.toBeDefined();
71+
});
72+
73+
it('ignores non-literal names', () => {
74+
var definition = expression('({displayName: foo.bar})');
75+
expect(() => displayNameHandler(documentation, definition)).not.toThrow();
76+
expect(documentation.displayName).not.toBeDefined();
77+
78+
definition = statement(`
79+
class Foo {
80+
static displayName = foo.bar;
81+
}
82+
`);
83+
expect(() => displayNameHandler(documentation, definition)).not.toThrow();
84+
expect(documentation.displayName).not.toBeDefined();
85+
});
86+
});

src/handlers/displayNameHandler.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @flow
10+
*
11+
*/
12+
13+
import type Documentation from '../Documentation';
14+
15+
import getMemberValuePath from '../utils/getMemberValuePath';
16+
import recast from 'recast';
17+
import resolveToValue from '../utils/resolveToValue';
18+
19+
var {types: {namedTypes: types}} = recast;
20+
21+
export default function displayNameHandler(
22+
documentation: Documentation,
23+
path: NodePath
24+
) {
25+
var displayNamePath = getMemberValuePath(path, 'displayName');
26+
if (!displayNamePath) {
27+
return;
28+
}
29+
displayNamePath = resolveToValue(displayNamePath);
30+
if (!displayNamePath || !types.Literal.check(displayNamePath.node)) {
31+
return;
32+
}
33+
documentation.set('displayName', displayNamePath.node.value);
34+
}

src/handlers/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ export {default as defaultPropsHandler} from './defaultPropsHandler';
1616
export {default as propTypeHandler} from './propTypeHandler';
1717
export {default as propTypeCompositionHandler} from './propTypeCompositionHandler';
1818
export {default as propDocBlockHandler} from './propDocBlockHandler';
19+
export {default as displayNameHandler} from './displayNameHandler';

src/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var defaultHandlers = [
2121
handlers.propDocBlockHandler,
2222
handlers.defaultPropsHandler,
2323
handlers.componentDocblockHandler,
24+
handlers.displayNameHandler,
2425
];
2526

2627
/**

0 commit comments

Comments
 (0)