-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
160 lines (136 loc) · 4.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'use strict';
function padStr(str) {
const padding = ' '.repeat(2);
return str.split('\n').map(s=>(padding+s).trimRight()).join('\n');
}
//Can't support custom validators
function isCustomValidator(type) {
return typeof type === "function" && type.name !== '_fpr';
}
function fakeProp(type, propToString, isRequired){
if (isCustomValidator(type)) {
type = 'any';
}
const _fpr = ()=>{};
_fpr.toString = (path = '', options = {}) => {
const result = propToString ? propToString(type, path, options) : type
return result;
};
_fpr._type = type;
_fpr._req = isRequired;
if (!isRequired) {
_fpr.isRequired = fakeProp(type, propToString, true);
}
return _fpr;
}
function oneOfToString(props) {
return props
.map(prop=>JSON.stringify(prop))
.filter(str=>typeof str === 'string')
.map(str=>str.replace(/"/g, `'`))
.join(' | ');
}
function oneOfTypeToString(props, path, options) {
return props.map(prop=>prop.toString(path, options)).join(' | ');
}
function shapeToString(shape, path, options) {
return `{\n`+padStr(
Object.keys(shape)
.map(key=>`${key}${shape[key]._req ? '' : '?'}: ${shape[key].toString(`${path}.${key}`, options)}`)
.join(';\n')
)+`;\n}`;
}
function arrayOfToString(prop, path, options) {
return `Array<${prop.toString(path, options)}>`;
}
function objectOfToString(prop, path, options) {
return `{[key: string]: ${prop.toString(path, options)}}`;
}
function genericCallback(name) {
return (prop, path, {[name]: cb})=> cb ? cb(path, prop) : prop;
}
const fakePropTypes = {
//simple values
bool: fakeProp('boolean'),
number: fakeProp('number'),
string: fakeProp('string'),
symbol: fakeProp('Symbol'),
//values that could be mapped
element: fakeProp('ReactChild', genericCallback('mapReactChild')),
node: fakeProp('ReactNode', genericCallback('mapReactNode')),
array: fakeProp('Array<any>', genericCallback('mapArray')),
func: fakeProp('Function', genericCallback('mapFunction')),
object: fakeProp('{[key: string]: any}', genericCallback('mapObject')),
any: fakeProp('any', genericCallback('mapAny')),
//complex values
oneOf: (props)=>fakeProp(props, oneOfToString),
oneOfType: (props)=>fakeProp(props, oneOfTypeToString),
arrayOf: (prop)=>fakeProp(prop, arrayOfToString),
objectOf: (prop)=>fakeProp(prop, objectOfToString),
instanceOf: (func)=>fakeProp(func.name), //prop should be class|function
shape: (shape)=>fakeProp(shape, shapeToString),
exact: (shape)=>fakeProp(shape, shapeToString),
};
function getComponentProps(comp, path, options) {
const parentPropTypes = comp.__proto__.propTypes || {};
const parentDefaultProps = comp.__proto__.defaultProps || {};
const parentKeys = [...Object.keys(parentPropTypes), ...Object.keys(parentDefaultProps)];
const result = Object.keys(comp.propTypes)
.filter(key=>!parentKeys.some(parentKey=>key===parentKey))
.map((key)=>{
const prop = isCustomValidator(comp.propTypes[key]) ? 'any' : comp.propTypes[key];
const defaultProp = comp.defaultProps && comp.defaultProps[key];
const required = prop._req && (defaultProp === undefined);
const propValue = prop.toString(`${path}.${key}`, options);
const propKey = key + (required ? '' : '?');
return `${propKey}: ${propValue};`
})
.join('\n');
return result ? `\n${result}\n` : '';
}
function getComponent(name, comp, options) {
const parentName = comp.__proto__.name;
const props = padStr(getComponentProps(comp, name, options));
let result;
if (parentName) {
result =
`export interface ${name}Props {${props}}\n`+
`export class ${name} extends ${parentName}<${name}Props> {}\n`;
} else {
result =
`export interface ${name}Props {${props}}\n`+
`export const ${name}: SFC<${name}Props>;\n`;
}
if (options.getComponent) {
result = options.getComponent(name, props, parentName, result);
}
return result;
};
function generateTypes(moduleName, moduleExport, options = {}) {
const reactImport = options.skipReactImport ? '' :
`import { Component, PureComponent, ReactChild, ReactNode, SFC } from 'react';\n\n`;
let customDeclarations = options.customDeclarations || '';
if (Array.isArray(customDeclarations)) {
customDeclarations = customDeclarations.join('\n');
}
const moduleContent = moduleExport.propTypes
//single component
? getComponent(moduleExport.name, moduleExport, options)
//set of components
: Object.keys(moduleExport)
.filter(key=>moduleExport[key].propTypes)
.map(key=>getComponent(key, moduleExport[key], options))
.join('\n');
return `declare module '${moduleName}' {\n\n`+
padStr(
reactImport+
customDeclarations+'\n'+
moduleContent+'\n'
)+
`}\n`;
}
const injectPropTypes = (propTypes) => {
Object.keys(propTypes).forEach(key=>propTypes[key] = fakePropTypes[key]);
return propTypes;
}
module.exports = {injectPropTypes, generateTypes};