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
`react-docgen` is a CLI and toolbox to help extracting information from React components, and generate documentation from it.
3
+
`react-docgen` is a CLI and toolbox to help extracting information from [React][] components, and generate documentation from it.
4
4
5
-
It uses [recast][] 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.
5
+
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 `React.createClass`. These component definitions must follow certain guidelines in order to be analyzable (see below for more info).
7
+
It provides a default implementation for React components defined via `React.createClass` or [ES2015 class definitions][classes]. These component definitions must follow certain guidelines in order to be analyzable (see below for more info).
8
8
9
9
## Install
10
10
@@ -36,7 +36,7 @@ Extract meta information from React components.
36
36
If a directory is passed, it is recursively traversed.
37
37
```
38
38
39
-
By default, `react-docgen` will look for the exported component created through `React.createClass` in each file. Have a look below for how to customize this behavior.
39
+
By default, `react-docgen` will look for the exported component created through `React.createClass`or a class definition in each file. Have a look below for how to customize this behavior.
40
40
41
41
Have a look at `example/` for an example of how to use the result to generate
42
42
a markdown version of the documentation.
@@ -50,7 +50,7 @@ var reactDocs = require('react-docgen');
50
50
var componentInfo =reactDocs.parse(src);
51
51
```
52
52
53
-
As with the CLI, this will look for the exported component created through `React.createClass` in the provided source. The whole process of analyzing the source code is separated into two parts:
53
+
As with the CLI, this will look for the exported component created through `React.createClass`or a class definition in the provided source. The whole process of analyzing the source code is separated into two parts:
54
54
55
55
- Locating/finding the nodes in the AST which define the component
56
56
- Extracting information from those nodes
@@ -68,32 +68,39 @@ As with the CLI, this will look for the exported component created through `Reac
68
68
69
69
#### resolver
70
70
71
-
The resolver's task is to extract those parts from the source code which the handlers can analyze. For example, the `findExportedReactCreateClassCall` resolver inspects the AST to find
71
+
The resolver's task is to extract those parts from the source code which the handlers can analyze. For example, the `findExportedComponentDefinition` resolver inspects the AST to find
72
72
73
73
```js
74
74
var Component =React.createClass(<def>);
75
75
module.exports= Component;
76
+
77
+
// or
78
+
79
+
classComponentextendsReact.Component {
80
+
// ...
81
+
}
82
+
module.exports= Component;
76
83
```
77
84
78
-
and returns the ObjectExpression to which `<def>` resolves.
85
+
and returns the ObjectExpression to which `<def>` resolves to, or the class declaration itself.
79
86
80
-
`findAllReactCreateClassCalls` works similarly, but simply finds all `React.createClass` calls, not only the one that creates the exported component.
87
+
`findAllComponentDefinitions` works similarly, but finds *all*`React.createClass` calls and class definitions, not only the one that is exported.
88
+
89
+
This makes it easy, together with the utility methods created to analyze the AST, to introduce new or custom resolver methods. For example, a resolver could look for plain ObjectExpressions with a `render` method.
81
90
82
-
This makes it easy, together with the utility methods created to analyze the AST, to introduce new or custom resolver methods. For example, a resolver could look for plain ObjectExpressions with a `render` method or `class Component extends React.Component` instead (**note:** a default resolver for `class` based react components is planned).
83
-
84
91
#### handlers
85
92
86
93
Handlers do the actual work and extract the desired information from the result the resolver returned. Like the resolver, they try to delegate as much work as possible to the reusable utility functions.
87
94
88
-
For example, while the `propTypesHandler` expects the prop types definition to be an ObjectExpression and be located inside an ObjectExpression under the property name `propTypes`, most of the work is actually performed by the `getPropType` utility function.
95
+
For example, while the `propTypesHandler` expects the prop types definition to be an ObjectExpression and be available as `propTypes` in the component definition, most of the work is actually performed by the `getPropType` utility function.
89
96
90
97
## Guidelines for default resolvers and handlers
91
98
92
99
- Modules have to export a single component, and only that component is
93
100
analyzed.
94
-
-The component definition must be an object literal.
95
-
-`propTypes` must be an object literal or resolve to an object literal in the
96
-
same file.
101
+
-When using `React.createClass`, the component definition (the value passed to it) must resolve to an object literal.
102
+
-When using classes, the class must either `extend React.Component`*or* define a `render()` method.
103
+
-`propTypes` must be an object literal or resolve to an object literal in the same file.
97
104
- The `return` statement in `getDefaultProps` must contain an object literal.
98
105
99
106
## Example
@@ -192,8 +199,8 @@ The structure of the JSON blob / JavaScript object is as follows:
192
199
193
200
```
194
201
{
195
-
"description": string
196
-
"props": {
202
+
["description": string,]
203
+
["props": {
197
204
"<propName>": {
198
205
"type": {
199
206
"name": "<typeName>",
@@ -208,7 +215,7 @@ The structure of the JSON blob / JavaScript object is as follows:
208
215
}]
209
216
},
210
217
...
211
-
},
218
+
},]
212
219
["composes": <componentNames>]
213
220
}
214
221
```
@@ -219,4 +226,7 @@ The structure of the JSON blob / JavaScript object is as follows:
219
226
-`<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.
0 commit comments