-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
113 lines (99 loc) · 3.49 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
// NOTE: all the code must be inside this function,
// since a webpack loader is using stringified version of it to patch prop-types
module.exports = function(ReactPropTypes) {
function unwrapValueItem(value) {
if (value) {
if (typeof value.getTypeDefinition === 'function') {
return value.getTypeDefinition();
}
if (typeof value === 'function') {
return { name: 'custom' };
}
}
return { name: 'unknown' };
}
function wrapFunction(fn, name, value, getTypeDefinition) {
fn.getTypeDefinition = getTypeDefinition.bind(null, name, false, value);
if (fn.isRequired) {
fn.isRequired.getTypeDefinition = getTypeDefinition.bind(null, name, true, value);
}
return fn;
}
function wrapMethod(dict, name, getTypeDefinition) {
var orig = dict[name];
dict[name] = function(value) {
return wrapFunction(orig.apply(this, arguments), name, value, getTypeDefinition);
};
}
for (var method in ReactPropTypes) {
switch (method) {
case 'any':
case 'array':
case 'bool':
case 'func':
case 'number':
case 'object':
case 'string':
case 'symbol':
case 'node':
case 'element':
wrapFunction(ReactPropTypes[method], method, null, function(name, required) {
return {
type: { name: name },
required: required
};
});
break;
case 'instanceOf':
case 'oneOf':
wrapMethod(ReactPropTypes, method, function(name, required, value) {
return {
type: {
name: name,
value: value
},
required: required
};
});
break;
case 'oneOfType':
wrapMethod(ReactPropTypes, method, function(name, required, value) {
return {
type: {
name: name,
value: Array.isArray(value) ? value.map(unwrapValueItem) : []
},
required: required
};
});
break;
case 'arrayOf':
case 'objectOf':
wrapMethod(ReactPropTypes, method, function(name, required, value) {
return {
type: {
name: name,
value: unwrapValueItem(value)
},
required: required
};
});
break;
case 'shape':
wrapMethod(ReactPropTypes, method, function(name, required, value) {
var unwrapped = {};
for (var key in value) {
unwrapped[key] = unwrapValueItem(value[key]);
}
return {
type: {
name: name,
value: unwrapped
},
required: required
};
});
break;
}
}
};