You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It uses [recast][] and [babylon][] to parse the source into an AST and provides methods to process this AST to extract the desired information. The output / return value is a JSON blob / JavaScript object.
6
6
7
-
It provides a default implementation for React components defined via
8
-
`React.createClass`, [ES2015 class definitions][classes] or functions
9
-
(stateless components). These component definitions must follow certain
7
+
It provides a default implementation for React components defined via
8
+
`React.createClass`, [ES2015 class definitions][classes] or functions
9
+
(stateless components). These component definitions must follow certain
10
10
guidelines in order to be analyzable (see below for more info).
11
11
12
12
## Install
@@ -41,11 +41,11 @@ Extract meta information from React components.
41
41
If a directory is passed, it is recursively traversed.
42
42
```
43
43
44
-
By default, `react-docgen` will look for the exported component created through
45
-
`React.createClass`, a class definition or a function (stateless component) in
46
-
each file. You can change that behavior with the `--resolver` option, which
47
-
either expects the name of a built-in resolver or a path to JavaScript module
48
-
exporting a resolver function. Have a look below for [more information about
44
+
By default, `react-docgen` will look for the exported component created through
45
+
`React.createClass`, a class definition or a function (stateless component) in
46
+
each file. You can change that behavior with the `--resolver` option, which
47
+
either expects the name of a built-in resolver or a path to JavaScript module
48
+
exporting a resolver function. Have a look below for [more information about
49
49
resolvers](#resolver).
50
50
51
51
Have a look at `example/` for an example of how to use the result to generate a
@@ -111,7 +111,9 @@ For example, while the `propTypesHandler` expects the prop types definition to b
111
111
-`propTypes` must be an object literal or resolve to an object literal in the same file.
112
112
- The `return` statement in `getDefaultProps` must contain an object literal.
113
113
114
-
## Example
114
+
## PropTypes
115
+
116
+
### Example
115
117
116
118
For the following component
117
119
@@ -156,7 +158,7 @@ module.exports = Component;
156
158
157
159
we are getting this output:
158
160
159
-
```
161
+
```json
160
162
{
161
163
"props": {
162
164
"foo": {
@@ -201,6 +203,131 @@ we are getting this output:
201
203
}
202
204
```
203
205
206
+
## Flow Type support
207
+
208
+
If you are using [flow][flow] then react-docgen can also extract the flow type annotations. As flow has a way more advanced and fine granular type system, the returned types from react-docgen are different in comparison when using `React.PropTypes`.
209
+
210
+
> **Note**: react-docgen will not be able to grab the type definition if the type is imported or declared in a different file.
The structure of the JSON blob / JavaScript object is as follows:
@@ -215,10 +342,11 @@ The structure of the JSON blob / JavaScript object is as follows:
215
342
["value": <typeValue>]
216
343
["raw": string]
217
344
},
345
+
"flowType": <flowType>,
218
346
"required": boolean,
219
347
"description": string,
220
348
["defaultValue": {
221
-
"value": number | string,
349
+
"value": string,
222
350
"computed": boolean
223
351
}]
224
352
},
@@ -232,9 +360,11 @@ The structure of the JSON blob / JavaScript object is as follows:
232
360
- `<propName>`: For each prop that was found, there will be an entry in `props` under the same name.
233
361
- `<typeName>`: The name of the type, which is usually corresponds to the function name in `React.PropTypes`. However, for types define with `oneOf`, we use `"enum"` and for `oneOfType` we use `"union"`. If a custom function is provided or the type cannot be resolved to anything of `React.PropTypes`, we use `"custom"`.
234
362
- `<typeValue>`: Some types accept parameters which define the type in more detail (such as `arrayOf`, `instanceOf`, `oneOf`, etc). Those are stored in `<typeValue>`. The data type of `<typeValue>` depends on the type definition.
363
+
- `<flowType>`: If using flow type this property contains the parsed flow type as can be seen in the table above.
0 commit comments