Skip to content

Commit 575df6a

Browse files
author
Ivan Grekov
committed
initial release
1 parent 9e742b8 commit 575df6a

File tree

5 files changed

+187
-1
lines changed

5 files changed

+187
-1
lines changed

README.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,40 @@
11
# typescript-react-function-component-props-handler
2-
Custom react function component props handler for react-docgen. It allows you to parse props, which are typed as React.functionComponent<props> without rewriting your code
2+
3+
Custom react function component props handler for react-docgen. It allows you to parse props, which are typed as React.functionComponent<props> without rewriting your code.
4+
5+
## intro
6+
7+
React-docgen 5.3.0 allow you to parse React.components and generate documentation. But if you don't explicitly set type of pros parameter, information about props is not visible in output. This custom handler allows you to fix [this problem](https://github.com/reactjs/react-docgen/issues/387). This is one-purpose, zero-dependency project.
8+
9+
## Getting started
10+
11+
### Add nececcary dependencies to your project
12+
13+
```bash
14+
yarn add typescript-react-function-component-props-handler react-docgen
15+
```
16+
17+
### Write script for running react-docgen
18+
19+
Add the following content to the file:
20+
21+
```js
22+
23+
const reactDocs = require('react-docgen');
24+
25+
const setParamsTypeDefinitionFromFunctionType = require('typescript-react-function-component-props-handler');
26+
27+
const doc = reactDocs.parse(
28+
source,
29+
null,
30+
[setParamsTypeDefinitionFromFunctionType, ...reactDocs.defaultHandlers],
31+
null
32+
);
33+
34+
```
35+
36+
**Note:** `source` is a path to your component to parse.
37+
38+
## What code-examples are covered
39+
40+
Most of general and corner-cases for `React.functionComponents` were covered.

index.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
function setParamTypeName(path) {
2+
if (
3+
path.parentPath.node.id.typeAnnotation &&
4+
path.parentPath.node.id.typeAnnotation.typeAnnotation.typeParameters &&
5+
path.parentPath.node.id.typeAnnotation.typeAnnotation.typeParameters.params[0].typeName.name
6+
) {
7+
return path.parentPath.node.id.typeAnnotation.typeAnnotation.typeParameters.params[0]
8+
.typeName.name;
9+
}
10+
11+
return path.parentPath.node.id.name;
12+
}
13+
14+
function getTypePath(path) {
15+
if (
16+
path.parentPath.parentPath.parentPath.parentPath.value &&
17+
path.parentPath.parentPath.parentPath.parentPath.value.find
18+
) {
19+
return path.parentPath.parentPath.parentPath.parentPath.value;
20+
}
21+
22+
if (
23+
path.parentPath.parentPath.parentPath.parentPath.parentPath &&
24+
path.parentPath.parentPath.parentPath.parentPath.parentPath.value
25+
) {
26+
return path.parentPath.parentPath.parentPath.parentPath.parentPath.value;
27+
}
28+
29+
return [];
30+
}
31+
32+
function checkForProptypes(path, paramTypeName) {
33+
const propsDefinition = getTypePath(path).find(propertyPath => {
34+
return (
35+
propertyPath.type &&
36+
propertyPath.type === 'ExpressionStatement' &&
37+
propertyPath.expression &&
38+
propertyPath.expression.left.object.name === paramTypeName &&
39+
propertyPath.expression.right.type === 'ObjectExpression' &&
40+
Boolean(propertyPath.expression.right.properties)
41+
);
42+
});
43+
44+
return Boolean(propsDefinition);
45+
}
46+
47+
function setParamsTypeDefinitionFromFunctionType(documentation, path) {
48+
if (path.parentPath.node.init && path.parentPath.node.init.params.length === 0) {
49+
return;
50+
}
51+
52+
if (
53+
path.node.type === 'ArrowFunctionExpression' &&
54+
!path.parentPath.node.init.params[0].typeAnnotation
55+
) {
56+
const paramTypeName = setParamTypeName(path);
57+
58+
const hasPropTypes = checkForProptypes(path, paramTypeName);
59+
60+
if (hasPropTypes) {
61+
return;
62+
}
63+
64+
let typePath = getTypePath(path).find(propertyPath => {
65+
if (
66+
propertyPath.type === 'ExportNamedDeclaration' &&
67+
propertyPath.declaration &&
68+
propertyPath.declaration.id &&
69+
propertyPath.declaration.id.name === paramTypeName
70+
) {
71+
return true;
72+
}
73+
74+
if (
75+
propertyPath.type === 'TSInterfaceDeclaration' &&
76+
propertyPath.id &&
77+
propertyPath.id.name === paramTypeName
78+
) {
79+
return true;
80+
}
81+
82+
return (
83+
propertyPath.type === 'TSTypeAliasDeclaration' &&
84+
propertyPath.id.name === paramTypeName
85+
);
86+
});
87+
88+
typePath =
89+
typePath && typePath.type && typePath.type === 'ExportNamedDeclaration'
90+
? typePath.declaration
91+
: typePath;
92+
93+
if (typePath) {
94+
const typedParam = Object.assign({}, path.parentPath.node.init.params[0], {
95+
typeAnnotation: typePath,
96+
});
97+
98+
path.parentPath.node.init.params = [typedParam];
99+
}
100+
}
101+
}
102+
103+
module.exports = setParamsTypeDefinitionFromFunctionType;

node_modules/.yarn-integrity

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "typescript-react-function-component-props-handler",
3+
"version": "1.0.0",
4+
"description": "a custom react function components props handler for react-docgen. It allows you to parse props, which are typed as React.functionComponent<props> without rewriting your code",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "yarn test"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/Winner95/typescript-react-function-component-props-handler.git"
12+
},
13+
"keywords": [
14+
"react-docgen",
15+
"react",
16+
"function",
17+
"components",
18+
"props",
19+
"handler",
20+
"documentation-generation"
21+
],
22+
"author": "Ivan Grekov",
23+
"license": "MIT",
24+
"bugs": {
25+
"url": "https://github.com/Winner95/typescript-react-function-component-props-handler/issues"
26+
},
27+
"homepage": "https://github.com/Winner95/typescript-react-function-component-props-handler#readme"
28+
}

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+

0 commit comments

Comments
 (0)