Skip to content

Commit 6a59f3f

Browse files
Aditya Vohradanez
authored andcommitted
Added post-processing step for documentation (#227)
1 parent d2c096f commit 6a59f3f

File tree

5 files changed

+101
-3
lines changed

5 files changed

+101
-3
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
"scripts": {
2020
"build": "rimraf dist/ && babel src/ --out-dir dist/ --ignore __tests__,__mocks__",
2121
"lint": "eslint src/ bin/",
22-
"prepublish": "yarn run build",
23-
"preversion": "yarn run lint",
22+
"prepublish": "yarn build",
23+
"preversion": "yarn lint",
2424
"test": "jest",
2525
"test:ci": "yarn lint && yarn flow && yarn test --runInBand",
2626
"watch": "yarn build --watch"

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Object {
3838
"computed": false,
3939
"value": "\\"primary\\"",
4040
},
41+
"required": false,
4142
},
4243
},
4344
}
@@ -260,3 +261,31 @@ Object {
260261
},
261262
}
262263
`;
264+
265+
exports[`main fixtures processes component "component_11.js" without errors 1`] = `
266+
Object {
267+
"description": "",
268+
"displayName": "Foo",
269+
"methods": Array [],
270+
"props": Object {
271+
"prop1": Object {
272+
"description": "",
273+
"flowType": Object {
274+
"name": "string",
275+
},
276+
"required": true,
277+
},
278+
"prop2": Object {
279+
"defaultValue": Object {
280+
"computed": false,
281+
"value": "'bar'",
282+
},
283+
"description": "",
284+
"flowType": Object {
285+
"name": "string",
286+
},
287+
"required": false,
288+
},
289+
},
290+
}
291+
`;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
/**
12+
* Test for documentation of React components with Flow annotations for props.
13+
*/
14+
15+
import React from 'react';
16+
17+
type Props = {
18+
prop1: string,
19+
prop2: string,
20+
};
21+
22+
class Foo extends React.Component<Props> {
23+
render() {
24+
return (
25+
<div>
26+
{this.props.prop1}
27+
I am Foo!
28+
{this.props.prop2}
29+
</div>
30+
);
31+
}
32+
}
33+
34+
Foo.defaultProps = {
35+
prop2: 'bar',
36+
};
37+
38+
export default Foo;

src/parse.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212

1313
import Documentation from './Documentation';
14+
import postProcessDocumentation from './utils/postProcessDocumentation';
1415

1516
import babylon from './babylon';
1617
import recast from 'recast';
@@ -21,7 +22,7 @@ function executeHandlers(handlers, componentDefinitions) {
2122
return componentDefinitions.map(componentDefinition => {
2223
var documentation = new Documentation();
2324
handlers.forEach(handler => handler(documentation, componentDefinition));
24-
return documentation.toObject();
25+
return postProcessDocumentation(documentation.toObject());
2526
});
2627
}
2728

src/utils/postProcessDocumentation.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
function postProcessProps(props) {
12+
// props with default values should not be required
13+
Object.keys(props).forEach(prop => {
14+
const propInfo = props[prop];
15+
16+
if (propInfo.defaultValue) {
17+
propInfo.required = false;
18+
}
19+
});
20+
}
21+
22+
export default function (documentation) {
23+
const props = documentation.props;
24+
25+
if (props) {
26+
postProcessProps(props);
27+
}
28+
29+
return documentation;
30+
}

0 commit comments

Comments
 (0)