1 | import getErrorMessage from "./getErrorMessage";
|
2 |
|
3 | import invariant from './invariant';
|
4 |
|
5 | import { Type, TypeParameterApplication, GenericType, ObjectType } from './types';
|
6 |
|
7 |
|
8 | export default function registerBuiltinTypeConstructors(t) {
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | t.declareTypeConstructor({
|
16 | name: 'Class',
|
17 | typeName: 'ClassType',
|
18 | collectErrors(validation, path, input, instanceType) {
|
19 | if (typeof input !== 'function') {
|
20 | validation.addError(path, this, getErrorMessage('ERR_EXPECT_CLASS', instanceType.toString()));
|
21 | return true;
|
22 | }
|
23 | var expectedType = instanceType.unwrap();
|
24 | if (expectedType instanceof GenericType && typeof expectedType.impl === 'function') {
|
25 | if (input === expectedType.impl) {
|
26 | return false;
|
27 | } else if (expectedType.impl.prototype.isPrototypeOf(input.prototype)) {
|
28 | return false;
|
29 | } else {
|
30 | validation.addError(path, this, getErrorMessage('ERR_EXPECT_CLASS', instanceType.toString()));
|
31 | return true;
|
32 | }
|
33 | }
|
34 | var annotation = t.getAnnotation(input);
|
35 | if (annotation) {
|
36 | return expectedType.acceptsType(annotation);
|
37 | }
|
38 | var matches = void 0;
|
39 |
|
40 | switch (input.typeName) {
|
41 | case 'NumberType':
|
42 | case 'NumericLiteralType':
|
43 | matches = input === Number;
|
44 | break;
|
45 | case 'BooleanType':
|
46 | case 'BooleanLiteralType':
|
47 | matches = input === Boolean;
|
48 | break;
|
49 | case 'StringType':
|
50 | case 'StringLiteralType':
|
51 | matches = input === String;
|
52 | break;
|
53 | case 'ArrayType':
|
54 | case 'TupleType':
|
55 | matches = input === Array;
|
56 | break;
|
57 | default:
|
58 | return false;
|
59 | }
|
60 | if (matches) {
|
61 | return false;
|
62 | } else {
|
63 | validation.addError(path, this, getErrorMessage('ERR_EXPECT_CLASS', instanceType.toString()));
|
64 | return true;
|
65 | }
|
66 | },
|
67 | accepts(input, instanceType) {
|
68 | if (typeof input !== 'function') {
|
69 | return false;
|
70 | }
|
71 | var expectedType = instanceType.unwrap();
|
72 | if (expectedType instanceof GenericType && typeof expectedType.impl === 'function') {
|
73 | if (input === expectedType.impl) {
|
74 | return true;
|
75 | } else if (typeof expectedType.impl === 'function') {
|
76 | if (expectedType.impl.prototype.isPrototypeOf(input.prototype)) {
|
77 | return true;
|
78 | } else {
|
79 | return false;
|
80 | }
|
81 | }
|
82 | }
|
83 |
|
84 | var annotation = t.getAnnotation(input);
|
85 |
|
86 | if (annotation) {
|
87 | return expectedType.acceptsType(annotation);
|
88 | } else if (expectedType instanceof TypeParameterApplication) {
|
89 | expectedType = expectedType.parent;
|
90 | }
|
91 |
|
92 | if (expectedType instanceof GenericType && typeof expectedType.impl === 'function') {
|
93 | if (expectedType.impl.prototype.isPrototypeOf(input.prototype)) {
|
94 | return true;
|
95 | } else {
|
96 | return false;
|
97 | }
|
98 | }
|
99 |
|
100 |
|
101 | switch (input.typeName) {
|
102 | case 'NumberType':
|
103 | case 'NumericLiteralType':
|
104 | return input === Number;
|
105 | case 'BooleanType':
|
106 | case 'BooleanLiteralType':
|
107 | return input === Boolean;
|
108 | case 'StringType':
|
109 | case 'StringLiteralType':
|
110 | return input === String;
|
111 | case 'ArrayType':
|
112 | case 'TupleType':
|
113 | return input === Array;
|
114 | default:
|
115 | return false;
|
116 | }
|
117 | },
|
118 | inferTypeParameters(input) {
|
119 | return [];
|
120 | }
|
121 | });
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | t.declareTypeConstructor({
|
127 | name: '$Diff',
|
128 | typeName: '$DiffType',
|
129 | collectErrors(validation, path, input, aType, bType) {
|
130 | if (input === null || typeof input !== "object" && typeof input !== "function") {
|
131 | validation.addError(path, this, getErrorMessage('ERR_EXPECT_OBJECT'));
|
132 | return true;
|
133 | }
|
134 | aType = aType.unwrap();
|
135 | invariant(bType, "Must specify two type parameters.");
|
136 | bType = bType.unwrap();
|
137 | invariant(aType instanceof ObjectType && bType instanceof ObjectType, "Can only $Diff object types.");
|
138 | var hasErrors = false;
|
139 | var properties = aType.properties;
|
140 | for (var i = 0; i < properties.length; i++) {
|
141 | var property = properties[i];
|
142 | if (bType.hasProperty(property.key)) {
|
143 | continue;
|
144 | }
|
145 | if (property.collectErrors(validation, path.concat(property.key), input)) {
|
146 | hasErrors = true;
|
147 | }
|
148 | }
|
149 | return hasErrors;
|
150 | },
|
151 | accepts(input, aType, bType) {
|
152 | if (input === null || typeof input !== "object" && typeof input !== "function") {
|
153 | return false;
|
154 | }
|
155 | aType = aType.unwrap();
|
156 | bType = bType.unwrap();
|
157 | invariant(aType instanceof ObjectType && bType instanceof ObjectType, "Can only $Diff object types.");
|
158 | var properties = aType.properties;
|
159 | for (var i = 0; i < properties.length; i++) {
|
160 | var property = properties[i];
|
161 | if (bType.hasProperty(property.key)) {
|
162 | continue;
|
163 | }
|
164 | if (!property.accepts(input)) {
|
165 | return false;
|
166 | }
|
167 | }
|
168 | return true;
|
169 | },
|
170 | inferTypeParameters(input) {
|
171 | return [];
|
172 | }
|
173 | });
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | t.declareTypeConstructor({
|
179 | name: '$Shape',
|
180 | typeName: '$ShapeType',
|
181 | collectErrors(validation, path, input, shapeType) {
|
182 | if (input === null || typeof input !== "object" && typeof input !== "function") {
|
183 | validation.addError(path, this, getErrorMessage('ERR_EXPECT_OBJECT'));
|
184 | return true;
|
185 | }
|
186 | shapeType = shapeType.unwrap();
|
187 | invariant(typeof shapeType.getProperty === 'function', "Can only $Shape<T> object types.");
|
188 |
|
189 | var hasErrors = false;
|
190 | for (var key in input) {
|
191 |
|
192 | var property = shapeType.getProperty(key);
|
193 | if (!property) {
|
194 | continue;
|
195 | }
|
196 | if (property.collectErrors(validation, path, input)) {
|
197 | hasErrors = true;
|
198 | }
|
199 | }
|
200 |
|
201 | return hasErrors;
|
202 | },
|
203 | accepts(input, shapeType) {
|
204 | if (input === null || typeof input !== "object" && typeof input !== "function") {
|
205 | return false;
|
206 | }
|
207 | shapeType = shapeType.unwrap();
|
208 | invariant(typeof shapeType.getProperty === 'function', "Can only $Shape<T> object types.");
|
209 | for (var key in input) {
|
210 |
|
211 | var property = shapeType.getProperty(key);
|
212 | if (!property || !property.accepts(input)) {
|
213 | return false;
|
214 | }
|
215 | }
|
216 | return true;
|
217 | },
|
218 | inferTypeParameters(input) {
|
219 | return [];
|
220 | }
|
221 | });
|
222 |
|
223 |
|
224 | t.declareTypeConstructor({
|
225 | name: '$SuperType',
|
226 | typeName: '$SuperType',
|
227 | collectErrors(validation, path, input, superType) {
|
228 | return superType.collectErrors(validation, path, input);
|
229 | },
|
230 | accepts(input, superType) {
|
231 | return superType.accepts(input);
|
232 | },
|
233 | inferTypeParameters(input) {
|
234 | return [];
|
235 | }
|
236 | });
|
237 |
|
238 |
|
239 | t.declareTypeConstructor({
|
240 | name: '$SubType',
|
241 | typeName: '$SubType',
|
242 | collectErrors(validation, path, input, subType) {
|
243 | return subType.collectErrors(validation, path, input);
|
244 | },
|
245 | accepts(input, subType) {
|
246 | return subType.accepts(input);
|
247 | },
|
248 | inferTypeParameters(input) {
|
249 | return [];
|
250 | }
|
251 | });
|
252 |
|
253 |
|
254 | t.declareTypeConstructor({
|
255 | name: '$ObjMap',
|
256 | typeName: '$ObjMap',
|
257 | collectErrors(validation, path, input, object, mapper) {
|
258 | var target = object.unwrap();
|
259 | invariant(mapper, 'Must specify at least two type parameters.');
|
260 | invariant(Array.isArray(target.properties), 'Target must be an object type.');
|
261 |
|
262 | var hasErrors = false;
|
263 | var _iteratorNormalCompletion = true;
|
264 | var _didIteratorError = false;
|
265 | var _iteratorError = undefined;
|
266 |
|
267 | try {
|
268 | for (var _iterator = target.properties[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
269 | var prop = _step.value;
|
270 |
|
271 | prop;
|
272 | var applied = mapper.unwrap(prop.value.unwrap());
|
273 | applied;
|
274 |
|
275 | var returnType = applied.returnType.unwrap();
|
276 | var value = input[prop.key];
|
277 | if (returnType.collectErrors(validation, path.concat(prop.key), value)) {
|
278 | hasErrors = true;
|
279 | }
|
280 | }
|
281 | } catch (err) {
|
282 | _didIteratorError = true;
|
283 | _iteratorError = err;
|
284 | } finally {
|
285 | try {
|
286 | if (!_iteratorNormalCompletion && _iterator.return) {
|
287 | _iterator.return();
|
288 | }
|
289 | } finally {
|
290 | if (_didIteratorError) {
|
291 | throw _iteratorError;
|
292 | }
|
293 | }
|
294 | }
|
295 |
|
296 | return hasErrors;
|
297 | },
|
298 | accepts(input, object, mapper) {
|
299 | var target = object.unwrap();
|
300 | invariant(Array.isArray(target.properties), 'Target must be an object type.');
|
301 |
|
302 | var _iteratorNormalCompletion2 = true;
|
303 | var _didIteratorError2 = false;
|
304 | var _iteratorError2 = undefined;
|
305 |
|
306 | try {
|
307 | for (var _iterator2 = target.properties[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
|
308 | var prop = _step2.value;
|
309 |
|
310 | prop;
|
311 | var applied = mapper.unwrap(prop.value.unwrap());
|
312 | applied;
|
313 |
|
314 | var returnType = applied.returnType.unwrap();
|
315 | if (!returnType.accepts(input[prop.key])) {
|
316 | return false;
|
317 | }
|
318 | }
|
319 | } catch (err) {
|
320 | _didIteratorError2 = true;
|
321 | _iteratorError2 = err;
|
322 | } finally {
|
323 | try {
|
324 | if (!_iteratorNormalCompletion2 && _iterator2.return) {
|
325 | _iterator2.return();
|
326 | }
|
327 | } finally {
|
328 | if (_didIteratorError2) {
|
329 | throw _iteratorError2;
|
330 | }
|
331 | }
|
332 | }
|
333 |
|
334 | return true;
|
335 | },
|
336 | inferTypeParameters(input) {
|
337 | return [];
|
338 | }
|
339 | });
|
340 |
|
341 |
|
342 | t.declareTypeConstructor({
|
343 | name: '$ObjMapi',
|
344 | typeName: '$ObjMapi',
|
345 | collectErrors(validation, path, input, object, mapper) {
|
346 | var target = object.unwrap();
|
347 | invariant(mapper, 'Must specify at least two type parameters.');
|
348 | invariant(Array.isArray(target.properties), 'Target must be an object type.');
|
349 |
|
350 | var hasErrors = false;
|
351 | var _iteratorNormalCompletion3 = true;
|
352 | var _didIteratorError3 = false;
|
353 | var _iteratorError3 = undefined;
|
354 |
|
355 | try {
|
356 | for (var _iterator3 = target.properties[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
|
357 | var prop = _step3.value;
|
358 |
|
359 | prop;
|
360 | var applied = mapper.unwrap(this.context.string(prop.key), prop.value.unwrap());
|
361 | applied;
|
362 |
|
363 | var value = input[prop.key];
|
364 | var returnType = applied.returnType.unwrap();
|
365 | if (returnType.collectErrors(validation, path.concat(prop.key), value)) {
|
366 | hasErrors = true;
|
367 | }
|
368 | }
|
369 | } catch (err) {
|
370 | _didIteratorError3 = true;
|
371 | _iteratorError3 = err;
|
372 | } finally {
|
373 | try {
|
374 | if (!_iteratorNormalCompletion3 && _iterator3.return) {
|
375 | _iterator3.return();
|
376 | }
|
377 | } finally {
|
378 | if (_didIteratorError3) {
|
379 | throw _iteratorError3;
|
380 | }
|
381 | }
|
382 | }
|
383 |
|
384 | return hasErrors;
|
385 | },
|
386 | accepts(input, object, mapper) {
|
387 | var target = object.unwrap();
|
388 | invariant(Array.isArray(target.properties), 'Target must be an object type.');
|
389 |
|
390 | var _iteratorNormalCompletion4 = true;
|
391 | var _didIteratorError4 = false;
|
392 | var _iteratorError4 = undefined;
|
393 |
|
394 | try {
|
395 | for (var _iterator4 = target.properties[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
|
396 | var prop = _step4.value;
|
397 |
|
398 | prop;
|
399 | var applied = mapper.unwrap(this.context.string(prop.key), prop.value.unwrap());
|
400 | applied;
|
401 |
|
402 | var value = input[prop.key];
|
403 | var returnType = applied.returnType.unwrap();
|
404 | if (!returnType.accepts(value)) {
|
405 | return false;
|
406 | }
|
407 | }
|
408 | } catch (err) {
|
409 | _didIteratorError4 = true;
|
410 | _iteratorError4 = err;
|
411 | } finally {
|
412 | try {
|
413 | if (!_iteratorNormalCompletion4 && _iterator4.return) {
|
414 | _iterator4.return();
|
415 | }
|
416 | } finally {
|
417 | if (_didIteratorError4) {
|
418 | throw _iteratorError4;
|
419 | }
|
420 | }
|
421 | }
|
422 |
|
423 | return true;
|
424 | },
|
425 | inferTypeParameters(input) {
|
426 | return [];
|
427 | }
|
428 | });
|
429 |
|
430 |
|
431 | t.declareTypeConstructor({
|
432 | name: '$Keys',
|
433 | typeName: '$KeysType',
|
434 | collectErrors(validation, path, input, subject) {
|
435 | subject = subject.unwrap();
|
436 | invariant(subject instanceof ObjectType, '$Keys<T> - T must be an ObjectType.');
|
437 | var properties = subject.properties;
|
438 | var length = properties.length;
|
439 | for (var i = 0; i < length; i++) {
|
440 | var property = properties[i];
|
441 | if (input === property.key) {
|
442 | return false;
|
443 | }
|
444 | }
|
445 | var keys = new Array(length);
|
446 | for (var _i = 0; _i < length; _i++) {
|
447 | keys[_i] = properties[_i].key;
|
448 | }
|
449 | validation.addError(path, this, getErrorMessage('ERR_NO_UNION', keys.join(' | ')));
|
450 | return true;
|
451 | },
|
452 | accepts(input, subject) {
|
453 | subject = subject.unwrap();
|
454 | invariant(subject instanceof ObjectType, '$Keys<T> - T must be an ObjectType.');
|
455 | var properties = subject.properties;
|
456 | for (var i = 0; i < properties.length; i++) {
|
457 | var property = properties[i];
|
458 | if (input === property.key) {
|
459 | return true;
|
460 | }
|
461 | }
|
462 | return false;
|
463 | },
|
464 | inferTypeParameters(input) {
|
465 | return [];
|
466 | }
|
467 | });
|
468 |
|
469 | t.declareTypeConstructor({
|
470 | name: 'Date',
|
471 | impl: Date,
|
472 | typeName: 'DateType',
|
473 | collectErrors(validation, path, input) {
|
474 | if (!(input instanceof Date)) {
|
475 | validation.addError(path, this, getErrorMessage('ERR_EXPECT_INSTANCEOF', Date));
|
476 | return true;
|
477 | } else if (isNaN(input.getTime())) {
|
478 | validation.addError(path, this, getErrorMessage('ERR_INVALID_DATE'));
|
479 | return true;
|
480 | } else {
|
481 | return false;
|
482 | }
|
483 | },
|
484 | accepts(input) {
|
485 | return input instanceof Date && !isNaN(input.getTime());
|
486 | },
|
487 | inferTypeParameters(input) {
|
488 | return [];
|
489 | }
|
490 | });
|
491 |
|
492 | t.declareTypeConstructor({
|
493 | name: 'Iterable',
|
494 | typeName: 'IterableType',
|
495 | collectErrors(validation, path, input, keyType) {
|
496 | if (!input) {
|
497 | validation.addError(path, this, getErrorMessage('ERR_EXPECT_OBJECT'));
|
498 | return true;
|
499 | } else if (typeof input[Symbol.iterator] !== 'function') {
|
500 | validation.addError(path, this, getErrorMessage('ERR_EXPECT_ITERABLE'));
|
501 | return true;
|
502 | }
|
503 | return false;
|
504 | },
|
505 | accepts(input, keyType) {
|
506 | if (!input || typeof input[Symbol.iterator] !== 'function') {
|
507 | return false;
|
508 | }
|
509 | return true;
|
510 | },
|
511 | inferTypeParameters(input) {
|
512 | return [];
|
513 | }
|
514 | });
|
515 |
|
516 | t.declareTypeConstructor({
|
517 | name: 'Promise',
|
518 | impl: Promise,
|
519 | typeName: 'PromiseType',
|
520 | collectErrors(validation, path, input, futureType) {
|
521 | var context = this.context;
|
522 |
|
523 | if (!context.checkPredicate('Promise', input)) {
|
524 | validation.addError(path, this, getErrorMessage('ERR_EXPECT_PROMISE', futureType));
|
525 | return true;
|
526 | }
|
527 | return false;
|
528 | },
|
529 | accepts(input) {
|
530 | var context = this.context;
|
531 |
|
532 | return context.checkPredicate('Promise', input);
|
533 | },
|
534 | inferTypeParameters(input) {
|
535 | return [];
|
536 | }
|
537 | });
|
538 |
|
539 | t.declareTypeConstructor({
|
540 | name: 'Map',
|
541 | impl: Map,
|
542 | typeName: 'MapType',
|
543 | collectErrors(validation, path, input, keyType, valueType) {
|
544 | invariant(valueType, "Must specify two type parameters.");
|
545 | var context = this.context;
|
546 |
|
547 | if (!context.checkPredicate('Map', input)) {
|
548 | validation.addError(path, this, getErrorMessage('ERR_EXPECT_INSTANCEOF', 'Map'));
|
549 | return true;
|
550 | }
|
551 | var hasErrors = false;
|
552 | var _iteratorNormalCompletion5 = true;
|
553 | var _didIteratorError5 = false;
|
554 | var _iteratorError5 = undefined;
|
555 |
|
556 | try {
|
557 | for (var _iterator5 = input[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
|
558 | var _ref = _step5.value;
|
559 |
|
560 | var _ref2 = babelHelpers.slicedToArray(_ref, 2);
|
561 |
|
562 | var key = _ref2[0];
|
563 | var value = _ref2[1];
|
564 |
|
565 | if (!keyType.accepts(key)) {
|
566 | validation.addError(path, this, getErrorMessage('ERR_EXPECT_KEY_TYPE', keyType));
|
567 | hasErrors = true;
|
568 | }
|
569 | if (valueType.collectErrors(validation, path.concat(key), value)) {
|
570 | hasErrors = true;
|
571 | }
|
572 | }
|
573 | } catch (err) {
|
574 | _didIteratorError5 = true;
|
575 | _iteratorError5 = err;
|
576 | } finally {
|
577 | try {
|
578 | if (!_iteratorNormalCompletion5 && _iterator5.return) {
|
579 | _iterator5.return();
|
580 | }
|
581 | } finally {
|
582 | if (_didIteratorError5) {
|
583 | throw _iteratorError5;
|
584 | }
|
585 | }
|
586 | }
|
587 |
|
588 | return hasErrors;
|
589 | },
|
590 | accepts(input, keyType, valueType) {
|
591 | var context = this.context;
|
592 |
|
593 | if (!context.checkPredicate('Map', input)) {
|
594 | return false;
|
595 | }
|
596 | var _iteratorNormalCompletion6 = true;
|
597 | var _didIteratorError6 = false;
|
598 | var _iteratorError6 = undefined;
|
599 |
|
600 | try {
|
601 | for (var _iterator6 = input[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) {
|
602 | var _ref3 = _step6.value;
|
603 |
|
604 | var _ref4 = babelHelpers.slicedToArray(_ref3, 2);
|
605 |
|
606 | var key = _ref4[0];
|
607 | var value = _ref4[1];
|
608 |
|
609 | if (!keyType.accepts(key) || !valueType.accepts(value)) {
|
610 | return false;
|
611 | }
|
612 | }
|
613 | } catch (err) {
|
614 | _didIteratorError6 = true;
|
615 | _iteratorError6 = err;
|
616 | } finally {
|
617 | try {
|
618 | if (!_iteratorNormalCompletion6 && _iterator6.return) {
|
619 | _iterator6.return();
|
620 | }
|
621 | } finally {
|
622 | if (_didIteratorError6) {
|
623 | throw _iteratorError6;
|
624 | }
|
625 | }
|
626 | }
|
627 |
|
628 | return true;
|
629 | },
|
630 | inferTypeParameters(input) {
|
631 | var keyTypes = [];
|
632 | var valueTypes = [];
|
633 | var _iteratorNormalCompletion7 = true;
|
634 | var _didIteratorError7 = false;
|
635 | var _iteratorError7 = undefined;
|
636 |
|
637 | try {
|
638 | loop: for (var _iterator7 = input[Symbol.iterator](), _step7; !(_iteratorNormalCompletion7 = (_step7 = _iterator7.next()).done); _iteratorNormalCompletion7 = true) {
|
639 | var _ref5 = _step7.value;
|
640 |
|
641 | var _ref6 = babelHelpers.slicedToArray(_ref5, 2);
|
642 |
|
643 | var key = _ref6[0];
|
644 | var value = _ref6[1];
|
645 |
|
646 | findKey: {
|
647 | for (var i = 0; i < keyTypes.length; i++) {
|
648 | var type = keyTypes[i];
|
649 | if (type.accepts(key)) {
|
650 | break findKey;
|
651 | }
|
652 | }
|
653 | keyTypes.push(t.typeOf(key));
|
654 | }
|
655 |
|
656 | for (var _i2 = 0; _i2 < valueTypes.length; _i2++) {
|
657 | var _type = valueTypes[_i2];
|
658 | if (_type.accepts(value)) {
|
659 | continue loop;
|
660 | }
|
661 | }
|
662 | valueTypes.push(t.typeOf(value));
|
663 | }
|
664 | } catch (err) {
|
665 | _didIteratorError7 = true;
|
666 | _iteratorError7 = err;
|
667 | } finally {
|
668 | try {
|
669 | if (!_iteratorNormalCompletion7 && _iterator7.return) {
|
670 | _iterator7.return();
|
671 | }
|
672 | } finally {
|
673 | if (_didIteratorError7) {
|
674 | throw _iteratorError7;
|
675 | }
|
676 | }
|
677 | }
|
678 |
|
679 | var typeInstances = [];
|
680 |
|
681 | if (keyTypes.length === 0) {
|
682 | typeInstances.push(t.existential());
|
683 | } else if (keyTypes.length === 1) {
|
684 | typeInstances.push(keyTypes[0]);
|
685 | } else {
|
686 | typeInstances.push(t.union.apply(t, keyTypes));
|
687 | }
|
688 |
|
689 | if (valueTypes.length === 0) {
|
690 | typeInstances.push(t.existential());
|
691 | } else if (valueTypes.length === 1) {
|
692 | typeInstances.push(valueTypes[0]);
|
693 | } else {
|
694 | typeInstances.push(t.union.apply(t, valueTypes));
|
695 | }
|
696 |
|
697 | return typeInstances;
|
698 | }
|
699 | });
|
700 |
|
701 | t.declareTypeConstructor({
|
702 | name: 'Set',
|
703 | impl: Set,
|
704 | typeName: 'SetType',
|
705 | collectErrors(validation, path, input, valueType) {
|
706 | var context = this.context;
|
707 |
|
708 | if (!context.checkPredicate('Set', input)) {
|
709 | validation.addError(path, this, getErrorMessage('ERR_EXPECT_INSTANCEOF', 'Set'));
|
710 | return true;
|
711 | }
|
712 | var hasErrors = false;
|
713 | var _iteratorNormalCompletion8 = true;
|
714 | var _didIteratorError8 = false;
|
715 | var _iteratorError8 = undefined;
|
716 |
|
717 | try {
|
718 | for (var _iterator8 = input[Symbol.iterator](), _step8; !(_iteratorNormalCompletion8 = (_step8 = _iterator8.next()).done); _iteratorNormalCompletion8 = true) {
|
719 | var value = _step8.value;
|
720 |
|
721 | if (valueType.collectErrors(validation, path, value)) {
|
722 | hasErrors = true;
|
723 | }
|
724 | }
|
725 | } catch (err) {
|
726 | _didIteratorError8 = true;
|
727 | _iteratorError8 = err;
|
728 | } finally {
|
729 | try {
|
730 | if (!_iteratorNormalCompletion8 && _iterator8.return) {
|
731 | _iterator8.return();
|
732 | }
|
733 | } finally {
|
734 | if (_didIteratorError8) {
|
735 | throw _iteratorError8;
|
736 | }
|
737 | }
|
738 | }
|
739 |
|
740 | return hasErrors;
|
741 | },
|
742 | accepts(input, valueType) {
|
743 | var context = this.context;
|
744 |
|
745 | if (!context.checkPredicate('Set', input)) {
|
746 | return false;
|
747 | }
|
748 | var _iteratorNormalCompletion9 = true;
|
749 | var _didIteratorError9 = false;
|
750 | var _iteratorError9 = undefined;
|
751 |
|
752 | try {
|
753 | for (var _iterator9 = input[Symbol.iterator](), _step9; !(_iteratorNormalCompletion9 = (_step9 = _iterator9.next()).done); _iteratorNormalCompletion9 = true) {
|
754 | var value = _step9.value;
|
755 |
|
756 | if (!valueType.accepts(value)) {
|
757 | return false;
|
758 | }
|
759 | }
|
760 | } catch (err) {
|
761 | _didIteratorError9 = true;
|
762 | _iteratorError9 = err;
|
763 | } finally {
|
764 | try {
|
765 | if (!_iteratorNormalCompletion9 && _iterator9.return) {
|
766 | _iterator9.return();
|
767 | }
|
768 | } finally {
|
769 | if (_didIteratorError9) {
|
770 | throw _iteratorError9;
|
771 | }
|
772 | }
|
773 | }
|
774 |
|
775 | return true;
|
776 | },
|
777 | inferTypeParameters(input) {
|
778 | var valueTypes = [];
|
779 | var _iteratorNormalCompletion10 = true;
|
780 | var _didIteratorError10 = false;
|
781 | var _iteratorError10 = undefined;
|
782 |
|
783 | try {
|
784 | loop: for (var _iterator10 = input[Symbol.iterator](), _step10; !(_iteratorNormalCompletion10 = (_step10 = _iterator10.next()).done); _iteratorNormalCompletion10 = true) {
|
785 | var value = _step10.value;
|
786 |
|
787 | for (var i = 0; i < valueTypes.length; i++) {
|
788 | var type = valueTypes[i];
|
789 | if (type.accepts(value)) {
|
790 | continue loop;
|
791 | }
|
792 | }
|
793 | valueTypes.push(t.typeOf(value));
|
794 | }
|
795 | } catch (err) {
|
796 | _didIteratorError10 = true;
|
797 | _iteratorError10 = err;
|
798 | } finally {
|
799 | try {
|
800 | if (!_iteratorNormalCompletion10 && _iterator10.return) {
|
801 | _iterator10.return();
|
802 | }
|
803 | } finally {
|
804 | if (_didIteratorError10) {
|
805 | throw _iteratorError10;
|
806 | }
|
807 | }
|
808 | }
|
809 |
|
810 | if (valueTypes.length === 0) {
|
811 | return [t.existential()];
|
812 | } else if (valueTypes.length === 1) {
|
813 | return [valueTypes[0]];
|
814 | } else {
|
815 | return [t.union.apply(t, valueTypes)];
|
816 | }
|
817 | }
|
818 | });
|
819 |
|
820 |
|
821 | t.declareTypeConstructor({
|
822 | name: '$FlowIgnore',
|
823 | typeName: '$FlowIgnore',
|
824 | collectErrors(validation, path, input) {
|
825 | return false;
|
826 | },
|
827 | accepts(input) {
|
828 | return true;
|
829 | },
|
830 | inferTypeParameters(input) {
|
831 | return [];
|
832 | }
|
833 | });
|
834 |
|
835 | return t;
|
836 | } |
\ | No newline at end of file |