This repository has been archived by the owner on Dec 31, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 348
/
Copy pathpropTypes.ts
210 lines (196 loc) · 7.21 KB
/
propTypes.ts
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { isObservableArray, isObservableObject, isObservableMap, untracked } from "mobx"
// Copied from React.PropTypes
function createChainableTypeChecker(validator: React.Validator<any>): React.Requireable<any> {
function checkType(
isRequired: boolean,
props: any,
propName: string,
componentName: string,
location: string,
propFullName: string,
...rest: any[]
) {
return untracked(() => {
componentName = componentName || "<<anonymous>>"
propFullName = propFullName || propName
if (props[propName] == null) {
if (isRequired) {
const actual = props[propName] === null ? "null" : "undefined"
return new Error(
"The " +
location +
" `" +
propFullName +
"` is marked as required " +
"in `" +
componentName +
"`, but its value is `" +
actual +
"`."
)
}
return null
} else {
// @ts-ignore rest arg is necessary for some React internals - fails tests otherwise
return validator(props, propName, componentName, location, propFullName, ...rest)
}
})
}
const chainedCheckType: any = checkType.bind(null, false)
// Add isRequired to satisfy Requirable
chainedCheckType.isRequired = checkType.bind(null, true)
return chainedCheckType
}
// Copied from React.PropTypes
function isSymbol(propType: any, propValue: any): boolean {
// Native Symbol.
if (propType === "symbol") {
return true
}
// 19.4.3.5 Symbol.prototype[@@toStringTag] === 'Symbol'
if (propValue["@@toStringTag"] === "Symbol") {
return true
}
// Fallback for non-spec compliant Symbols which are polyfilled.
if (typeof Symbol === "function" && propValue instanceof Symbol) {
return true
}
return false
}
// Copied from React.PropTypes
function getPropType(propValue: any): string {
const propType = typeof propValue
if (Array.isArray(propValue)) {
return "array"
}
if (propValue instanceof RegExp) {
// Old webkits (at least until Android 4.0) return 'function' rather than
// 'object' for typeof a RegExp. We'll normalize this here so that /bla/
// passes PropTypes.object.
return "object"
}
if (isSymbol(propType, propValue)) {
return "symbol"
}
return propType
}
// This handles more types than `getPropType`. Only used for error messages.
// Copied from React.PropTypes
function getPreciseType(propValue: any): string {
const propType = getPropType(propValue)
if (propType === "object") {
if (propValue instanceof Date) {
return "date"
} else if (propValue instanceof RegExp) {
return "regexp"
}
}
return propType
}
function createObservableTypeCheckerCreator(
allowNativeType: any,
mobxType: any
): React.Requireable<any> {
return createChainableTypeChecker((props, propName, componentName, location, propFullName) => {
return untracked(() => {
if (allowNativeType) {
if (getPropType(props[propName]) === mobxType.toLowerCase()) return null
}
let mobxChecker
switch (mobxType) {
case "Array":
mobxChecker = isObservableArray
break
case "Object":
mobxChecker = isObservableObject
break
case "Map":
mobxChecker = isObservableMap
break
default:
throw new Error(`Unexpected mobxType: ${mobxType}`)
}
const propValue = props[propName]
if (!mobxChecker(propValue)) {
const preciseType = getPreciseType(propValue)
const nativeTypeExpectationMessage = allowNativeType
? " or javascript `" + mobxType.toLowerCase() + "`"
: ""
return new Error(
"Invalid prop `" +
propFullName +
"` of type `" +
preciseType +
"` supplied to" +
" `" +
componentName +
"`, expected `mobx.Observable" +
mobxType +
"`" +
nativeTypeExpectationMessage +
"."
)
}
return null
})
})
}
function createObservableArrayOfTypeChecker(
allowNativeType: boolean,
typeChecker: React.Validator<any>
) {
return createChainableTypeChecker(
(props, propName, componentName, location, propFullName, ...rest) => {
return untracked(() => {
if (typeof typeChecker !== "function") {
return new Error(
"Property `" +
propFullName +
"` of component `" +
componentName +
"` has " +
"invalid PropType notation."
)
} else {
let error = createObservableTypeCheckerCreator(allowNativeType, "Array")(
props,
propName,
componentName,
location,
propFullName
)
if (error instanceof Error) return error
const propValue = props[propName]
for (let i = 0; i < propValue.length; i++) {
error = (typeChecker as React.Validator<any>)(
propValue,
i as any,
componentName,
location,
propFullName + "[" + i + "]",
...rest
)
if (error instanceof Error) return error
}
return null
}
})
}
)
}
const observableArray = createObservableTypeCheckerCreator(false, "Array")
const observableArrayOf = createObservableArrayOfTypeChecker.bind(null, false)
const observableMap = createObservableTypeCheckerCreator(false, "Map")
const observableObject = createObservableTypeCheckerCreator(false, "Object")
const arrayOrObservableArray = createObservableTypeCheckerCreator(true, "Array")
const arrayOrObservableArrayOf = createObservableArrayOfTypeChecker.bind(null, true)
const objectOrObservableObject = createObservableTypeCheckerCreator(true, "Object")
export const PropTypes = {
observableArray,
observableArrayOf,
observableMap,
observableObject,
arrayOrObservableArray,
arrayOrObservableArrayOf,
objectOrObservableObject
}