|
| 1 | +# react-docgen |
| 2 | + |
| 3 | +`react-docgen` extracts information from React components with which |
| 4 | +you can generate documentation for those components. |
| 5 | + |
| 6 | +It uses [recast][] to parse the provided files into an AST, looks for React |
| 7 | +component definitions, and inspects the `propTypes` and `getDefaultProps` |
| 8 | +declarations. The output is a JSON blob with the extracted information. |
| 9 | + |
| 10 | +Note that component definitions must follow certain guidelines in order to be |
| 11 | +analyzable by this tool. We will work towards less strict guidelines, but there |
| 12 | +is a limit to what is statically analyzable. |
| 13 | + |
| 14 | +## Install |
| 15 | + |
| 16 | +Install the module directly from npm: |
| 17 | + |
| 18 | +``` |
| 19 | +npm install -g react-docgen |
| 20 | +``` |
| 21 | + |
| 22 | +## CLI |
| 23 | + |
| 24 | +Installing the module adds a `react-docgen` executable which allows you do convert |
| 25 | +a single file, multiple files or an input stream. We are trying to make the |
| 26 | +executable as versatile as possible so that it can be integrated into many |
| 27 | +workflows. |
| 28 | + |
| 29 | +``` |
| 30 | +Usage: react-docgen [path]... [options] |
| 31 | +
|
| 32 | +path A component file or directory. If no path is provided it reads from stdin. |
| 33 | +
|
| 34 | +Options: |
| 35 | + -o FILE, --out FILE store extracted information in FILE |
| 36 | + --pretty pretty print JSON |
| 37 | + -x, --extension File extensions to consider. Repeat to define multiple extensions. Default: [js,jsx] |
| 38 | + -i, --ignore Folders to ignore. Default: [node_modules,__tests__] |
| 39 | +
|
| 40 | +Extract meta information from React components. |
| 41 | +If a directory is passed, it is recursively traversed. |
| 42 | +``` |
| 43 | + |
| 44 | +## API |
| 45 | + |
| 46 | +The tool can also be used programmatically to extract component information: |
| 47 | + |
| 48 | +```js |
| 49 | +var reactDocs = require('react-docgen'); |
| 50 | +var componentInfo reactDocs.parseSource(src); |
| 51 | +``` |
| 52 | + |
| 53 | +## Guidelines |
| 54 | + |
| 55 | +- Modules have to export a single component, and only that component is |
| 56 | + analyzed. |
| 57 | +- `propTypes` must be an object literal or resolve to an object literal in the |
| 58 | + same file. |
| 59 | +- The `return` statement in `getDefaultProps` must consist of an object literal. |
| 60 | + |
| 61 | +## Example |
| 62 | + |
| 63 | +For the following component |
| 64 | + |
| 65 | +```js |
| 66 | +var React = require('react'); |
| 67 | + |
| 68 | +/** |
| 69 | + * General component description. |
| 70 | + */ |
| 71 | +var Component = React.createClass({ |
| 72 | + propTypes: { |
| 73 | + /** |
| 74 | + * Description of prop "foo". |
| 75 | + */ |
| 76 | + foo: React.PropTypes.number, |
| 77 | + /** |
| 78 | + * Description of prop "bar" (a custom validation function). |
| 79 | + */ |
| 80 | + bar: function(props, propName, componentName) { |
| 81 | + // ... |
| 82 | + }, |
| 83 | + baz: React.PropTypes.oneOfType([ |
| 84 | + React.PropTypes.number, |
| 85 | + React.PropTypes.string |
| 86 | + ]), |
| 87 | + }, |
| 88 | + |
| 89 | + getDefaultProps: function() { |
| 90 | + return { |
| 91 | + foo: 42, |
| 92 | + bar: 21 |
| 93 | + }; |
| 94 | + }, |
| 95 | + |
| 96 | + render: function() { |
| 97 | + // ... |
| 98 | + } |
| 99 | +}); |
| 100 | + |
| 101 | +module.exports = Component; |
| 102 | +``` |
| 103 | + |
| 104 | +we are getting this output: |
| 105 | + |
| 106 | +``` |
| 107 | +{ |
| 108 | + "props": { |
| 109 | + "foo": { |
| 110 | + "type": { |
| 111 | + "name": "number" |
| 112 | + }, |
| 113 | + "required": false, |
| 114 | + "description": "Description of prop \"foo\".", |
| 115 | + "defaultValue": { |
| 116 | + "value": "42", |
| 117 | + "computed": false |
| 118 | + } |
| 119 | + }, |
| 120 | + "bar": { |
| 121 | + "type": { |
| 122 | + "name": "custom" |
| 123 | + }, |
| 124 | + "required": false, |
| 125 | + "description": "Description of prop \"bar\" (a custom validation function).", |
| 126 | + "defaultValue": { |
| 127 | + "value": "21", |
| 128 | + "computed": false |
| 129 | + } |
| 130 | + }, |
| 131 | + "baz": { |
| 132 | + "type": { |
| 133 | + "name": "union", |
| 134 | + "value": [ |
| 135 | + { |
| 136 | + "name": "number" |
| 137 | + }, |
| 138 | + { |
| 139 | + "name": "string" |
| 140 | + } |
| 141 | + ] |
| 142 | + }, |
| 143 | + "required": false, |
| 144 | + "description": "" |
| 145 | + } |
| 146 | + }, |
| 147 | + "description": "General component description." |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +## Result data structure |
| 152 | + |
| 153 | +The structure of the JSON blob / JavaScript object is as follows: |
| 154 | + |
| 155 | +``` |
| 156 | +{ |
| 157 | + "description": string |
| 158 | + "props": { |
| 159 | + "<propName>": { |
| 160 | + "type": { |
| 161 | + "name": "<typeName>", |
| 162 | + ["value": <typeValue>] |
| 163 | + ["raw": string] |
| 164 | + }, |
| 165 | + "required": boolean, |
| 166 | + "description": string, |
| 167 | + ["defaultValue": { |
| 168 | + "value": number | string, |
| 169 | + "computed": boolean |
| 170 | + }] |
| 171 | + }, |
| 172 | + ... |
| 173 | + }, |
| 174 | + ["composes": <componentNames>] |
| 175 | +} |
| 176 | +``` |
| 177 | + |
| 178 | +[recast]: https://github.com/benjamn/recast |
0 commit comments