/
utils.ts
379 lines (319 loc) 路 9.85 KB
/
utils.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import is from "../validators/is";
import { validateField, validateChild } from "../validators/validate";
import type * as t from "..";
export const VISITOR_KEYS: Record<string, string[]> = {};
export const ALIAS_KEYS: Partial<Record<NodeTypesWithoutComment, string[]>> =
{};
export const FLIPPED_ALIAS_KEYS: Record<string, NodeTypesWithoutComment[]> = {};
export const NODE_FIELDS: Record<string, FieldDefinitions> = {};
export const BUILDER_KEYS: Record<string, string[]> = {};
export const DEPRECATED_KEYS: Record<string, NodeTypesWithoutComment> = {};
export const NODE_PARENT_VALIDATIONS: Record<string, Validator> = {};
function getType(val: any) {
if (Array.isArray(val)) {
return "array";
} else if (val === null) {
return "null";
} else {
return typeof val;
}
}
type NodeTypesWithoutComment = t.Node["type"] | keyof t.Aliases;
type NodeTypes = NodeTypesWithoutComment | t.Comment["type"];
type PrimitiveTypes = ReturnType<typeof getType>;
type FieldDefinitions = {
[x: string]: FieldOptions;
};
type DefineTypeOpts = {
fields?: FieldDefinitions;
visitor?: Array<string>;
aliases?: Array<string>;
builder?: Array<string>;
inherits?: NodeTypes;
deprecatedAlias?: string;
validate?: Validator;
};
export type Validator = (
| { type: PrimitiveTypes }
| { each: Validator }
| { chainOf: Validator[] }
| { oneOf: any[] }
| { oneOfNodeTypes: NodeTypes[] }
| { oneOfNodeOrValueTypes: (NodeTypes | PrimitiveTypes)[] }
| { shapeOf: { [x: string]: FieldOptions } }
| {}
) &
((node: t.Node, key: string, val: any) => void);
export type FieldOptions = {
default?: string | number | boolean | [];
optional?: boolean;
validate?: Validator;
};
export function validate(validate: Validator): FieldOptions {
return { validate };
}
export function typeIs(typeName: NodeTypes | NodeTypes[]) {
return typeof typeName === "string"
? assertNodeType(typeName)
: assertNodeType(...typeName);
}
export function validateType(typeName: NodeTypes | NodeTypes[]) {
return validate(typeIs(typeName));
}
export function validateOptional(validate: Validator): FieldOptions {
return { validate, optional: true };
}
export function validateOptionalType(
typeName: NodeTypes | NodeTypes[],
): FieldOptions {
return { validate: typeIs(typeName), optional: true };
}
export function arrayOf(elementType: Validator): Validator {
return chain(assertValueType("array"), assertEach(elementType));
}
export function arrayOfType(typeName: NodeTypes | NodeTypes[]) {
return arrayOf(typeIs(typeName));
}
export function validateArrayOfType(typeName: NodeTypes | NodeTypes[]) {
return validate(arrayOfType(typeName));
}
export function assertEach(callback: Validator): Validator {
function validator(node: t.Node, key: string, val: any) {
if (!Array.isArray(val)) return;
for (let i = 0; i < val.length; i++) {
const subkey = `${key}[${i}]`;
const v = val[i];
callback(node, subkey, v);
if (process.env.BABEL_TYPES_8_BREAKING) validateChild(node, subkey, v);
}
}
validator.each = callback;
return validator;
}
export function assertOneOf(...values: Array<any>): Validator {
function validate(node: any, key: string, val: any) {
if (values.indexOf(val) < 0) {
throw new TypeError(
`Property ${key} expected value to be one of ${JSON.stringify(
values,
)} but got ${JSON.stringify(val)}`,
);
}
}
validate.oneOf = values;
return validate;
}
export function assertNodeType(...types: NodeTypes[]): Validator {
function validate(node: t.Node, key: string, val: any) {
for (const type of types) {
if (is(type, val)) {
validateChild(node, key, val);
return;
}
}
throw new TypeError(
`Property ${key} of ${
node.type
} expected node to be of a type ${JSON.stringify(
types,
)} but instead got ${JSON.stringify(val?.type)}`,
);
}
validate.oneOfNodeTypes = types;
return validate;
}
export function assertNodeOrValueType(
...types: (NodeTypes | PrimitiveTypes)[]
): Validator {
function validate(node: t.Node, key: string, val: any) {
for (const type of types) {
if (getType(val) === type || is(type, val)) {
validateChild(node, key, val);
return;
}
}
throw new TypeError(
`Property ${key} of ${
node.type
} expected node to be of a type ${JSON.stringify(
types,
)} but instead got ${JSON.stringify(val?.type)}`,
);
}
validate.oneOfNodeOrValueTypes = types;
return validate;
}
export function assertValueType(type: PrimitiveTypes): Validator {
function validate(node: t.Node, key: string, val: any) {
const valid = getType(val) === type;
if (!valid) {
throw new TypeError(
`Property ${key} expected type of ${type} but got ${getType(val)}`,
);
}
}
validate.type = type;
return validate;
}
export function assertShape(shape: { [x: string]: FieldOptions }): Validator {
function validate(node: t.Node, key: string, val: any) {
const errors = [];
for (const property of Object.keys(shape)) {
try {
validateField(node, property, val[property], shape[property]);
} catch (error) {
if (error instanceof TypeError) {
errors.push(error.message);
continue;
}
throw error;
}
}
if (errors.length) {
throw new TypeError(
`Property ${key} of ${
node.type
} expected to have the following:\n${errors.join("\n")}`,
);
}
}
validate.shapeOf = shape;
return validate;
}
export function assertOptionalChainStart(): Validator {
function validate(node: t.Node) {
let current = node;
while (node) {
const { type } = current;
if (type === "OptionalCallExpression") {
if (current.optional) return;
current = current.callee;
continue;
}
if (type === "OptionalMemberExpression") {
if (current.optional) return;
current = current.object;
continue;
}
break;
}
throw new TypeError(
`Non-optional ${node.type} must chain from an optional OptionalMemberExpression or OptionalCallExpression. Found chain from ${current?.type}`,
);
}
return validate;
}
export function chain(...fns: Array<Validator>): Validator {
function validate(...args: Parameters<Validator>) {
for (const fn of fns) {
fn(...args);
}
}
validate.chainOf = fns;
if (
fns.length >= 2 &&
"type" in fns[0] &&
fns[0].type === "array" &&
!("each" in fns[1])
) {
throw new Error(
`An assertValueType("array") validator can only be followed by an assertEach(...) validator.`,
);
}
return validate;
}
const validTypeOpts = [
"aliases",
"builder",
"deprecatedAlias",
"fields",
"inherits",
"visitor",
"validate",
];
const validFieldKeys = ["default", "optional", "validate"];
// Wraps defineType to ensure these aliases are included.
export function defineAliasedType(...aliases: string[]) {
return (type: string, opts: DefineTypeOpts = {}) => {
let defined = opts.aliases;
if (!defined) {
if (opts.inherits) defined = store[opts.inherits].aliases?.slice();
defined ??= [];
opts.aliases = defined;
}
const additional = aliases.filter(a => !defined.includes(a));
defined.unshift(...additional);
return defineType(type, opts);
};
}
export default function defineType(type: string, opts: DefineTypeOpts = {}) {
const inherits = (opts.inherits && store[opts.inherits]) || {};
let fields = opts.fields;
if (!fields) {
fields = {};
if (inherits.fields) {
const keys = Object.getOwnPropertyNames(inherits.fields);
for (const key of keys) {
const field = inherits.fields[key];
const def = field.default;
if (
Array.isArray(def) ? def.length > 0 : def && typeof def === "object"
) {
throw new Error(
"field defaults can only be primitives or empty arrays currently",
);
}
fields[key] = {
default: Array.isArray(def) ? [] : def,
optional: field.optional,
validate: field.validate,
};
}
}
}
const visitor: Array<string> = opts.visitor || inherits.visitor || [];
const aliases: Array<string> = opts.aliases || inherits.aliases || [];
const builder: Array<string> =
opts.builder || inherits.builder || opts.visitor || [];
for (const k of Object.keys(opts)) {
if (validTypeOpts.indexOf(k) === -1) {
throw new Error(`Unknown type option "${k}" on ${type}`);
}
}
if (opts.deprecatedAlias) {
DEPRECATED_KEYS[opts.deprecatedAlias] = type as NodeTypesWithoutComment;
}
// ensure all field keys are represented in `fields`
for (const key of visitor.concat(builder)) {
fields[key] = fields[key] || {};
}
for (const key of Object.keys(fields)) {
const field = fields[key];
if (field.default !== undefined && builder.indexOf(key) === -1) {
field.optional = true;
}
if (field.default === undefined) {
field.default = null;
} else if (!field.validate && field.default != null) {
field.validate = assertValueType(getType(field.default));
}
for (const k of Object.keys(field)) {
if (validFieldKeys.indexOf(k) === -1) {
throw new Error(`Unknown field key "${k}" on ${type}.${key}`);
}
}
}
VISITOR_KEYS[type] = opts.visitor = visitor;
BUILDER_KEYS[type] = opts.builder = builder;
NODE_FIELDS[type] = opts.fields = fields;
ALIAS_KEYS[type as NodeTypesWithoutComment] = opts.aliases = aliases;
aliases.forEach(alias => {
FLIPPED_ALIAS_KEYS[alias] = FLIPPED_ALIAS_KEYS[alias] || [];
FLIPPED_ALIAS_KEYS[alias].push(type as NodeTypesWithoutComment);
});
if (opts.validate) {
NODE_PARENT_VALIDATIONS[type] = opts.validate;
}
store[type] = opts;
}
const store = {} as Record<string, DefineTypeOpts>;