Skip to content

Commit 49fb67c

Browse files
committed
Make it easier to add data to Documentation
This changes the API of the Documentation object. `getDescription` and `setDescription` are gone. It was replaced by `get(key)` and `set(key, value)`. This allows custom handlers to add arbitrary data. This also removes `props` from the result object if there aren't any. This makes it easier to test for their existence (e.g. `if (doc.props) {...}`).
1 parent e125c65 commit 49fb67c

File tree

5 files changed

+30
-26
lines changed

5 files changed

+30
-26
lines changed

flow/react-docgen.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ type PropDescriptor = {
2323

2424
declare class Documentation {
2525
addComposes(moduleName: string): void;
26-
getDescription(): string;
27-
setDescription(description: string): void;
26+
get(key: string): any;
27+
set(key: string, value: any): void;
2828
getPropDescriptor(propName: string): PropDescriptor;
2929
toObject(): Object;
3030
}

src/Documentation.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,51 @@
1212

1313
class Documentation {
1414
_props: Object;
15-
_description: string;
1615
_composes: Array<string>;
16+
_data: Object;
1717

1818
constructor() {
19-
this._props = {};
20-
this._description = '';
21-
this._composes = [];
19+
this._props = new Map();
20+
this._composes = new Set();
21+
this._data = new Map();
2222
}
2323

2424
addComposes(moduleName: string) {
25-
if (this._composes.indexOf(moduleName) === -1) {
26-
this._composes.push(moduleName);
27-
}
25+
this._composes.add(moduleName);
2826
}
2927

30-
getDescription(): string {
31-
return this._description;
28+
set(key: string, value: any) {
29+
this._data.set(key, value);
3230
}
3331

34-
setDescription(description: string): void {
35-
this._description = description;
32+
get(key: string) {
33+
return this._data.get(key);
3634
}
3735

3836
getPropDescriptor(propName: string): PropDescriptor {
39-
var propDescriptor = this._props[propName];
37+
var propDescriptor = this._props.get(propName);
4038
if (!propDescriptor) {
41-
propDescriptor = this._props[propName] = {};
39+
this._props.set(propName, propDescriptor = {});
4240
}
4341
return propDescriptor;
4442
}
4543

4644
toObject(): Object {
47-
var obj = {
48-
description: this._description,
49-
props: this._props,
50-
};
45+
var obj = {};
46+
47+
for (var [key, value] of this._data) {
48+
obj[key] = value;
49+
}
50+
51+
if (this._props.size > 0) {
52+
obj.props = {};
53+
for (var [name, descriptor] of this._props) {
54+
obj.props[name] = descriptor;
55+
}
56+
}
5157

52-
if (this._composes.length) {
53-
obj.composes = this._composes;
58+
if (this._composes.size > 0) {
59+
obj.composes = Array.from(this._composes);
5460
}
5561
return obj;
5662
}

src/__mocks__/Documentation.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
function Documentation() {
1212
return {
13-
description: '',
1413
composes: [],
1514
descriptors: {},
1615
getPropDescriptor(name) {
@@ -19,8 +18,8 @@ function Documentation() {
1918
addComposes(name) {
2019
this.composes.push(name);
2120
},
22-
setDescription(descr) {
23-
this.description = descr;
21+
set(key, value) {
22+
this[key] = value;
2423
},
2524
};
2625
}

src/__tests__/main-test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ describe('main', () => {
4646
]);
4747
expect(docs).toEqual({
4848
description: 'Example component description',
49-
props: {},
5049
});
5150
});
5251
}

src/handlers/componentDocblockHandler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,5 @@ export default function componentDocblockHandler(
5353
description = getDocblock(programPath);
5454
}
5555
}
56-
documentation.setDescription(description || '');
56+
documentation.set('description', description || '');
5757
}

0 commit comments

Comments
 (0)