source: trip-planner-front/node_modules/@babel/types/lib/definitions/utils.js@ 6a80231

Last change on this file since 6a80231 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 8.3 KB
RevLine 
[6a3a178]1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.validate = validate;
7exports.typeIs = typeIs;
8exports.validateType = validateType;
9exports.validateOptional = validateOptional;
10exports.validateOptionalType = validateOptionalType;
11exports.arrayOf = arrayOf;
12exports.arrayOfType = arrayOfType;
13exports.validateArrayOfType = validateArrayOfType;
14exports.assertEach = assertEach;
15exports.assertOneOf = assertOneOf;
16exports.assertNodeType = assertNodeType;
17exports.assertNodeOrValueType = assertNodeOrValueType;
18exports.assertValueType = assertValueType;
19exports.assertShape = assertShape;
20exports.assertOptionalChainStart = assertOptionalChainStart;
21exports.chain = chain;
22exports.default = defineType;
23exports.NODE_PARENT_VALIDATIONS = exports.DEPRECATED_KEYS = exports.BUILDER_KEYS = exports.NODE_FIELDS = exports.FLIPPED_ALIAS_KEYS = exports.ALIAS_KEYS = exports.VISITOR_KEYS = void 0;
24
25var _is = require("../validators/is");
26
27var _validate = require("../validators/validate");
28
29const VISITOR_KEYS = {};
30exports.VISITOR_KEYS = VISITOR_KEYS;
31const ALIAS_KEYS = {};
32exports.ALIAS_KEYS = ALIAS_KEYS;
33const FLIPPED_ALIAS_KEYS = {};
34exports.FLIPPED_ALIAS_KEYS = FLIPPED_ALIAS_KEYS;
35const NODE_FIELDS = {};
36exports.NODE_FIELDS = NODE_FIELDS;
37const BUILDER_KEYS = {};
38exports.BUILDER_KEYS = BUILDER_KEYS;
39const DEPRECATED_KEYS = {};
40exports.DEPRECATED_KEYS = DEPRECATED_KEYS;
41const NODE_PARENT_VALIDATIONS = {};
42exports.NODE_PARENT_VALIDATIONS = NODE_PARENT_VALIDATIONS;
43
44function getType(val) {
45 if (Array.isArray(val)) {
46 return "array";
47 } else if (val === null) {
48 return "null";
49 } else {
50 return typeof val;
51 }
52}
53
54function validate(validate) {
55 return {
56 validate
57 };
58}
59
60function typeIs(typeName) {
61 return typeof typeName === "string" ? assertNodeType(typeName) : assertNodeType(...typeName);
62}
63
64function validateType(typeName) {
65 return validate(typeIs(typeName));
66}
67
68function validateOptional(validate) {
69 return {
70 validate,
71 optional: true
72 };
73}
74
75function validateOptionalType(typeName) {
76 return {
77 validate: typeIs(typeName),
78 optional: true
79 };
80}
81
82function arrayOf(elementType) {
83 return chain(assertValueType("array"), assertEach(elementType));
84}
85
86function arrayOfType(typeName) {
87 return arrayOf(typeIs(typeName));
88}
89
90function validateArrayOfType(typeName) {
91 return validate(arrayOfType(typeName));
92}
93
94function assertEach(callback) {
95 function validator(node, key, val) {
96 if (!Array.isArray(val)) return;
97
98 for (let i = 0; i < val.length; i++) {
99 const subkey = `${key}[${i}]`;
100 const v = val[i];
101 callback(node, subkey, v);
102 if (process.env.BABEL_TYPES_8_BREAKING) (0, _validate.validateChild)(node, subkey, v);
103 }
104 }
105
106 validator.each = callback;
107 return validator;
108}
109
110function assertOneOf(...values) {
111 function validate(node, key, val) {
112 if (values.indexOf(val) < 0) {
113 throw new TypeError(`Property ${key} expected value to be one of ${JSON.stringify(values)} but got ${JSON.stringify(val)}`);
114 }
115 }
116
117 validate.oneOf = values;
118 return validate;
119}
120
121function assertNodeType(...types) {
122 function validate(node, key, val) {
123 for (const type of types) {
124 if ((0, _is.default)(type, val)) {
125 (0, _validate.validateChild)(node, key, val);
126 return;
127 }
128 }
129
130 throw new TypeError(`Property ${key} of ${node.type} expected node to be of a type ${JSON.stringify(types)} but instead got ${JSON.stringify(val == null ? void 0 : val.type)}`);
131 }
132
133 validate.oneOfNodeTypes = types;
134 return validate;
135}
136
137function assertNodeOrValueType(...types) {
138 function validate(node, key, val) {
139 for (const type of types) {
140 if (getType(val) === type || (0, _is.default)(type, val)) {
141 (0, _validate.validateChild)(node, key, val);
142 return;
143 }
144 }
145
146 throw new TypeError(`Property ${key} of ${node.type} expected node to be of a type ${JSON.stringify(types)} but instead got ${JSON.stringify(val == null ? void 0 : val.type)}`);
147 }
148
149 validate.oneOfNodeOrValueTypes = types;
150 return validate;
151}
152
153function assertValueType(type) {
154 function validate(node, key, val) {
155 const valid = getType(val) === type;
156
157 if (!valid) {
158 throw new TypeError(`Property ${key} expected type of ${type} but got ${getType(val)}`);
159 }
160 }
161
162 validate.type = type;
163 return validate;
164}
165
166function assertShape(shape) {
167 function validate(node, key, val) {
168 const errors = [];
169
170 for (const property of Object.keys(shape)) {
171 try {
172 (0, _validate.validateField)(node, property, val[property], shape[property]);
173 } catch (error) {
174 if (error instanceof TypeError) {
175 errors.push(error.message);
176 continue;
177 }
178
179 throw error;
180 }
181 }
182
183 if (errors.length) {
184 throw new TypeError(`Property ${key} of ${node.type} expected to have the following:\n${errors.join("\n")}`);
185 }
186 }
187
188 validate.shapeOf = shape;
189 return validate;
190}
191
192function assertOptionalChainStart() {
193 function validate(node) {
194 var _current;
195
196 let current = node;
197
198 while (node) {
199 const {
200 type
201 } = current;
202
203 if (type === "OptionalCallExpression") {
204 if (current.optional) return;
205 current = current.callee;
206 continue;
207 }
208
209 if (type === "OptionalMemberExpression") {
210 if (current.optional) return;
211 current = current.object;
212 continue;
213 }
214
215 break;
216 }
217
218 throw new TypeError(`Non-optional ${node.type} must chain from an optional OptionalMemberExpression or OptionalCallExpression. Found chain from ${(_current = current) == null ? void 0 : _current.type}`);
219 }
220
221 return validate;
222}
223
224function chain(...fns) {
225 function validate(...args) {
226 for (const fn of fns) {
227 fn(...args);
228 }
229 }
230
231 validate.chainOf = fns;
232
233 if (fns.length >= 2 && "type" in fns[0] && fns[0].type === "array" && !("each" in fns[1])) {
234 throw new Error(`An assertValueType("array") validator can only be followed by an assertEach(...) validator.`);
235 }
236
237 return validate;
238}
239
240const validTypeOpts = ["aliases", "builder", "deprecatedAlias", "fields", "inherits", "visitor", "validate"];
241const validFieldKeys = ["default", "optional", "validate"];
242
243function defineType(type, opts = {}) {
244 const inherits = opts.inherits && store[opts.inherits] || {};
245 let fields = opts.fields;
246
247 if (!fields) {
248 fields = {};
249
250 if (inherits.fields) {
251 const keys = Object.getOwnPropertyNames(inherits.fields);
252
253 for (const key of keys) {
254 const field = inherits.fields[key];
255 const def = field.default;
256
257 if (Array.isArray(def) ? def.length > 0 : def && typeof def === "object") {
258 throw new Error("field defaults can only be primitives or empty arrays currently");
259 }
260
261 fields[key] = {
262 default: Array.isArray(def) ? [] : def,
263 optional: field.optional,
264 validate: field.validate
265 };
266 }
267 }
268 }
269
270 const visitor = opts.visitor || inherits.visitor || [];
271 const aliases = opts.aliases || inherits.aliases || [];
272 const builder = opts.builder || inherits.builder || opts.visitor || [];
273
274 for (const k of Object.keys(opts)) {
275 if (validTypeOpts.indexOf(k) === -1) {
276 throw new Error(`Unknown type option "${k}" on ${type}`);
277 }
278 }
279
280 if (opts.deprecatedAlias) {
281 DEPRECATED_KEYS[opts.deprecatedAlias] = type;
282 }
283
284 for (const key of visitor.concat(builder)) {
285 fields[key] = fields[key] || {};
286 }
287
288 for (const key of Object.keys(fields)) {
289 const field = fields[key];
290
291 if (field.default !== undefined && builder.indexOf(key) === -1) {
292 field.optional = true;
293 }
294
295 if (field.default === undefined) {
296 field.default = null;
297 } else if (!field.validate && field.default != null) {
298 field.validate = assertValueType(getType(field.default));
299 }
300
301 for (const k of Object.keys(field)) {
302 if (validFieldKeys.indexOf(k) === -1) {
303 throw new Error(`Unknown field key "${k}" on ${type}.${key}`);
304 }
305 }
306 }
307
308 VISITOR_KEYS[type] = opts.visitor = visitor;
309 BUILDER_KEYS[type] = opts.builder = builder;
310 NODE_FIELDS[type] = opts.fields = fields;
311 ALIAS_KEYS[type] = opts.aliases = aliases;
312 aliases.forEach(alias => {
313 FLIPPED_ALIAS_KEYS[alias] = FLIPPED_ALIAS_KEYS[alias] || [];
314 FLIPPED_ALIAS_KEYS[alias].push(type);
315 });
316
317 if (opts.validate) {
318 NODE_PARENT_VALIDATIONS[type] = opts.validate;
319 }
320
321 store[type] = opts;
322}
323
324const store = {};
Note: See TracBrowser for help on using the repository browser.