Skip to content

Added post-processing step for documentation objects #227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"scripts": {
"build": "rimraf dist/ && babel src/ --out-dir dist/ --ignore __tests__,__mocks__",
"lint": "eslint src/ bin/",
"prepublish": "yarn run build",
"preversion": "yarn run lint",
"prepublish": "yarn build",
"preversion": "yarn lint",
"test": "jest",
"test:ci": "yarn lint && yarn flow && yarn test --runInBand",
"watch": "yarn build --watch"
Expand Down
29 changes: 29 additions & 0 deletions src/__tests__/__snapshots__/main-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Object {
"computed": false,
"value": "\\"primary\\"",
},
"required": false,
},
},
}
Expand Down Expand Up @@ -260,3 +261,31 @@ Object {
},
}
`;

exports[`main fixtures processes component "component_11.js" without errors 1`] = `
Object {
"description": "",
"displayName": "Foo",
"methods": Array [],
"props": Object {
"prop1": Object {
"description": "",
"flowType": Object {
"name": "string",
},
"required": true,
},
"prop2": Object {
"defaultValue": Object {
"computed": false,
"value": "'bar'",
},
"description": "",
"flowType": Object {
"name": "string",
},
"required": false,
},
},
}
`;
38 changes: 38 additions & 0 deletions src/__tests__/fixtures/component_11.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

/**
* Test for documentation of React components with Flow annotations for props.
*/

import React from 'react';

type Props = {
prop1: string,
prop2: string,
};

class Foo extends React.Component<Props> {
render() {
return (
<div>
{this.props.prop1}
I am Foo!
{this.props.prop2}
</div>
);
}
}

Foo.defaultProps = {
prop2: 'bar',
};

export default Foo;
3 changes: 2 additions & 1 deletion src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/

import Documentation from './Documentation';
import postProcessDocumentation from './utils/postProcessDocumentation';

import babylon from './babylon';
import recast from 'recast';
Expand All @@ -21,7 +22,7 @@ function executeHandlers(handlers, componentDefinitions) {
return componentDefinitions.map(componentDefinition => {
var documentation = new Documentation();
handlers.forEach(handler => handler(documentation, componentDefinition));
return documentation.toObject();
return postProcessDocumentation(documentation.toObject());
});
}

Expand Down
30 changes: 30 additions & 0 deletions src/utils/postProcessDocumentation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

function postProcessProps(props) {
// props with default values should not be required
Object.keys(props).forEach(prop => {
const propInfo = props[prop];

if (propInfo.defaultValue) {
propInfo.required = false;
}
});
}

export default function (documentation) {
const props = documentation.props;

if (props) {
postProcessProps(props);
}

return documentation;
}