Debian Codegraph @

packages/babel-types/src/definitions/typescript.ts

raw
1 import {
2 defineAliasedType,
3 arrayOfType,
4 assertEach,
5 assertNodeType,
6 assertOneOf,
7 assertValueType,
8 chain,
9 validate,
10 validateArrayOfType,
11 validateOptional,
12 validateOptionalType,
13 validateType,
14 } from "./utils";
15 import {
16 functionDeclarationCommon,
17 classMethodOrDeclareMethodCommon,
18 } from "./core";
19 import is from "../validators/is";
20
21 const defineType = defineAliasedType("TypeScript");
22
23 const bool = assertValueType("boolean");
24
25 const tSFunctionTypeAnnotationCommon = () => ({
26 returnType: {
27 validate: process.env.BABEL_8_BREAKING
28 ? assertNodeType("TSTypeAnnotation")
29 : // @ts-ignore(Babel 7 vs Babel 8) Babel 7 AST
30 assertNodeType("TSTypeAnnotation", "Noop"),
31 optional: true,
32 },
33 typeParameters: {
34 validate: process.env.BABEL_8_BREAKING
35 ? assertNodeType("TSTypeParameterDeclaration")
36 : // @ts-ignore(Babel 7 vs Babel 8) Babel 7 AST
37 assertNodeType("TSTypeParameterDeclaration", "Noop"),
38 optional: true,
39 },
40 });
41
42 defineType("TSParameterProperty", {
43 aliases: ["LVal"], // TODO: This isn't usable in general as an LVal. Should have a "Parameter" alias.
44 visitor: ["parameter"],
45 fields: {
46 accessibility: {
47 validate: assertOneOf("public", "private", "protected"),
48 optional: true,
49 },
50 readonly: {
51 validate: assertValueType("boolean"),
52 optional: true,
53 },
54 parameter: {
55 validate: assertNodeType("Identifier", "AssignmentPattern"),
56 },
57 override: {
58 validate: assertValueType("boolean"),
59 optional: true,
60 },
61 decorators: {
62 validate: chain(
63 assertValueType("array"),
64 assertEach(assertNodeType("Decorator")),
65 ),
66 optional: true,
67 },
68 },
69 });
70
71 defineType("TSDeclareFunction", {
72 aliases: ["Statement", "Declaration"],
73 visitor: ["id", "typeParameters", "params", "returnType"],
74 fields: {
75 ...functionDeclarationCommon(),
76 ...tSFunctionTypeAnnotationCommon(),
77 },
78 });
79
80 defineType("TSDeclareMethod", {
81 visitor: ["decorators", "key", "typeParameters", "params", "returnType"],
82 fields: {
83 ...classMethodOrDeclareMethodCommon(),
84 ...tSFunctionTypeAnnotationCommon(),
85 },
86 });
87
88 defineType("TSQualifiedName", {
89 aliases: ["TSEntityName"],
90 visitor: ["left", "right"],
91 fields: {
92 left: validateType("TSEntityName"),
93 right: validateType("Identifier"),
94 },
95 });
96
97 const signatureDeclarationCommon = () => ({
98 typeParameters: validateOptionalType("TSTypeParameterDeclaration"),
99 [process.env.BABEL_8_BREAKING ? "params" : "parameters"]: validateArrayOfType(
100 ["Identifier", "RestElement"],
101 ),
102 [process.env.BABEL_8_BREAKING ? "returnType" : "typeAnnotation"]:
103 validateOptionalType("TSTypeAnnotation"),
104 });
105
106 const callConstructSignatureDeclaration = {
107 aliases: ["TSTypeElement"],
108 visitor: [
109 "typeParameters",
110 process.env.BABEL_8_BREAKING ? "params" : "parameters",
111 process.env.BABEL_8_BREAKING ? "returnType" : "typeAnnotation",
112 ],
113 fields: signatureDeclarationCommon(),
114 };
115
116 defineType("TSCallSignatureDeclaration", callConstructSignatureDeclaration);
117 defineType(
118 "TSConstructSignatureDeclaration",
119 callConstructSignatureDeclaration,
120 );
121
122 const namedTypeElementCommon = () => ({
123 key: validateType("Expression"),
124 computed: { default: false },
125 optional: validateOptional(bool),
126 });
127
128 defineType("TSPropertySignature", {
129 aliases: ["TSTypeElement"],
130 visitor: ["key", "typeAnnotation", "initializer"],
131 fields: {
132 ...namedTypeElementCommon(),
133 readonly: validateOptional(bool),
134 typeAnnotation: validateOptionalType("TSTypeAnnotation"),
135 initializer: validateOptionalType("Expression"),
136 kind: {
137 validate: assertOneOf("get", "set"),
138 },
139 },
140 });
141
142 defineType("TSMethodSignature", {
143 aliases: ["TSTypeElement"],
144 visitor: [
145 "key",
146 "typeParameters",
147 process.env.BABEL_8_BREAKING ? "params" : "parameters",
148 process.env.BABEL_8_BREAKING ? "returnType" : "typeAnnotation",
149 ],
150 fields: {
151 ...signatureDeclarationCommon(),
152 ...namedTypeElementCommon(),
153 kind: {
154 validate: assertOneOf("method", "get", "set"),
155 },
156 },
157 });
158
159 defineType("TSIndexSignature", {
160 aliases: ["TSTypeElement"],
161 visitor: ["parameters", "typeAnnotation"],
162 fields: {
163 readonly: validateOptional(bool),
164 static: validateOptional(bool),
165 parameters: validateArrayOfType("Identifier"), // Length must be 1
166 typeAnnotation: validateOptionalType("TSTypeAnnotation"),
167 },
168 });
169
170 const tsKeywordTypes = [
171 "TSAnyKeyword",
172 "TSBooleanKeyword",
173 "TSBigIntKeyword",
174 "TSIntrinsicKeyword",
175 "TSNeverKeyword",
176 "TSNullKeyword",
177 "TSNumberKeyword",
178 "TSObjectKeyword",
179 "TSStringKeyword",
180 "TSSymbolKeyword",
181 "TSUndefinedKeyword",
182 "TSUnknownKeyword",
183 "TSVoidKeyword",
184 ] as const;
185
186 for (const type of tsKeywordTypes) {
187 defineType(type, {
188 aliases: ["TSType", "TSBaseType"],
189 visitor: [],
190 fields: {},
191 });
192 }
193
194 defineType("TSThisType", {
195 aliases: ["TSType", "TSBaseType"],
196 visitor: [],
197 fields: {},
198 });
199
200 const fnOrCtrBase = {
201 aliases: ["TSType"],
202 visitor: [
203 "typeParameters",
204 process.env.BABEL_8_BREAKING ? "params" : "parameters",
205 process.env.BABEL_8_BREAKING ? "returnType" : "typeAnnotation",
206 ],
207 };
208
209 defineType("TSFunctionType", {
210 ...fnOrCtrBase,
211 fields: signatureDeclarationCommon(),
212 });
213 defineType("TSConstructorType", {
214 ...fnOrCtrBase,
215 fields: {
216 ...signatureDeclarationCommon(),
217 abstract: validateOptional(bool),
218 },
219 });
220
221 defineType("TSTypeReference", {
222 aliases: ["TSType"],
223 visitor: ["typeName", "typeParameters"],
224 fields: {
225 typeName: validateType("TSEntityName"),
226 typeParameters: validateOptionalType("TSTypeParameterInstantiation"),
227 },
228 });
229
230 defineType("TSTypePredicate", {
231 aliases: ["TSType"],
232 visitor: ["parameterName", "typeAnnotation"],
233 builder: ["parameterName", "typeAnnotation", "asserts"],
234 fields: {
235 parameterName: validateType(["Identifier", "TSThisType"]),
236 typeAnnotation: validateOptionalType("TSTypeAnnotation"),
237 asserts: validateOptional(bool),
238 },
239 });
240
241 defineType("TSTypeQuery", {
242 aliases: ["TSType"],
243 visitor: ["exprName", "typeParameters"],
244 fields: {
245 exprName: validateType(["TSEntityName", "TSImportType"]),
246 typeParameters: validateOptionalType("TSTypeParameterInstantiation"),
247 },
248 });
249
250 defineType("TSTypeLiteral", {
251 aliases: ["TSType"],
252 visitor: ["members"],
253 fields: {
254 members: validateArrayOfType("TSTypeElement"),
255 },
256 });
257
258 defineType("TSArrayType", {
259 aliases: ["TSType"],
260 visitor: ["elementType"],
261 fields: {
262 elementType: validateType("TSType"),
263 },
264 });
265
266 defineType("TSTupleType", {
267 aliases: ["TSType"],
268 visitor: ["elementTypes"],
269 fields: {
270 elementTypes: validateArrayOfType(["TSType", "TSNamedTupleMember"]),
271 },
272 });
273
274 defineType("TSOptionalType", {
275 aliases: ["TSType"],
276 visitor: ["typeAnnotation"],
277 fields: {
278 typeAnnotation: validateType("TSType"),
279 },
280 });
281
282 defineType("TSRestType", {
283 aliases: ["TSType"],
284 visitor: ["typeAnnotation"],
285 fields: {
286 typeAnnotation: validateType("TSType"),
287 },
288 });
289
290 defineType("TSNamedTupleMember", {
291 visitor: ["label", "elementType"],
292 builder: ["label", "elementType", "optional"],
293 fields: {
294 label: validateType("Identifier"),
295 optional: {
296 validate: bool,
297 default: false,
298 },
299 elementType: validateType("TSType"),
300 },
301 });
302
303 const unionOrIntersection = {
304 aliases: ["TSType"],
305 visitor: ["types"],
306 fields: {
307 types: validateArrayOfType("TSType"),
308 },
309 };
310
311 defineType("TSUnionType", unionOrIntersection);
312 defineType("TSIntersectionType", unionOrIntersection);
313
314 defineType("TSConditionalType", {
315 aliases: ["TSType"],
316 visitor: ["checkType", "extendsType", "trueType", "falseType"],
317 fields: {
318 checkType: validateType("TSType"),
319 extendsType: validateType("TSType"),
320 trueType: validateType("TSType"),
321 falseType: validateType("TSType"),
322 },
323 });
324
325 defineType("TSInferType", {
326 aliases: ["TSType"],
327 visitor: ["typeParameter"],
328 fields: {
329 typeParameter: validateType("TSTypeParameter"),
330 },
331 });
332
333 defineType("TSParenthesizedType", {
334 aliases: ["TSType"],
335 visitor: ["typeAnnotation"],
336 fields: {
337 typeAnnotation: validateType("TSType"),
338 },
339 });
340
341 defineType("TSTypeOperator", {
342 aliases: ["TSType"],
343 visitor: ["typeAnnotation"],
344 fields: {
345 operator: validate(assertValueType("string")),
346 typeAnnotation: validateType("TSType"),
347 },
348 });
349
350 defineType("TSIndexedAccessType", {
351 aliases: ["TSType"],
352 visitor: ["objectType", "indexType"],
353 fields: {
354 objectType: validateType("TSType"),
355 indexType: validateType("TSType"),
356 },
357 });
358
359 defineType("TSMappedType", {
360 aliases: ["TSType"],
361 visitor: ["typeParameter", "typeAnnotation", "nameType"],
362 fields: {
363 readonly: validateOptional(assertOneOf(true, false, "+", "-")),
364 typeParameter: validateType("TSTypeParameter"),
365 optional: validateOptional(assertOneOf(true, false, "+", "-")),
366 typeAnnotation: validateOptionalType("TSType"),
367 nameType: validateOptionalType("TSType"),
368 },
369 });
370
371 defineType("TSLiteralType", {
372 aliases: ["TSType", "TSBaseType"],
373 visitor: ["literal"],
374 fields: {
375 literal: {
376 validate: (function () {
377 const unaryExpression = assertNodeType(
378 "NumericLiteral",
379 "BigIntLiteral",
380 );
381 const unaryOperator = assertOneOf("-");
382
383 const literal = assertNodeType(
384 "NumericLiteral",
385 "StringLiteral",
386 "BooleanLiteral",
387 "BigIntLiteral",
388 "TemplateLiteral",
389 );
390 function validator(parent: any, key: string, node: any) {
391 // type A = -1 | 1;
392 if (is("UnaryExpression", node)) {
393 // check operator first
394 unaryOperator(node, "operator", node.operator);
395 unaryExpression(node, "argument", node.argument);
396 } else {
397 // type A = 'foo' | 'bar' | false | 1;
398 literal(parent, key, node);
399 }
400 }
401
402 validator.oneOfNodeTypes = [
403 "NumericLiteral",
404 "StringLiteral",
405 "BooleanLiteral",
406 "BigIntLiteral",
407 "TemplateLiteral",
408 "UnaryExpression",
409 ];
410
411 return validator;
412 })(),
413 },
414 },
415 });
416
417 defineType("TSExpressionWithTypeArguments", {
418 aliases: ["TSType"],
419 visitor: ["expression", "typeParameters"],
420 fields: {
421 expression: validateType("TSEntityName"),
422 typeParameters: validateOptionalType("TSTypeParameterInstantiation"),
423 },
424 });
425
426 defineType("TSInterfaceDeclaration", {
427 // "Statement" alias prevents a semicolon from appearing after it in an export declaration.
428 aliases: ["Statement", "Declaration"],
429 visitor: ["id", "typeParameters", "extends", "body"],
430 fields: {
431 declare: validateOptional(bool),
432 id: validateType("Identifier"),
433 typeParameters: validateOptionalType("TSTypeParameterDeclaration"),
434 extends: validateOptional(arrayOfType("TSExpressionWithTypeArguments")),
435 body: validateType("TSInterfaceBody"),
436 },
437 });
438
439 defineType("TSInterfaceBody", {
440 visitor: ["body"],
441 fields: {
442 body: validateArrayOfType("TSTypeElement"),
443 },
444 });
445
446 defineType("TSTypeAliasDeclaration", {
447 aliases: ["Statement", "Declaration"],
448 visitor: ["id", "typeParameters", "typeAnnotation"],
449 fields: {
450 declare: validateOptional(bool),
451 id: validateType("Identifier"),
452 typeParameters: validateOptionalType("TSTypeParameterDeclaration"),
453 typeAnnotation: validateType("TSType"),
454 },
455 });
456
457 defineType("TSInstantiationExpression", {
458 aliases: ["Expression"],
459 visitor: ["expression", "typeParameters"],
460 fields: {
461 expression: validateType("Expression"),
462 typeParameters: validateOptionalType("TSTypeParameterInstantiation"),
463 },
464 });
465
466 const TSTypeExpression = {
467 aliases: ["Expression", "LVal", "PatternLike"],
468 visitor: ["expression", "typeAnnotation"],
469 fields: {
470 expression: validateType("Expression"),
471 typeAnnotation: validateType("TSType"),
472 },
473 };
474
475 defineType("TSAsExpression", TSTypeExpression);
476 defineType("TSSatisfiesExpression", TSTypeExpression);
477
478 defineType("TSTypeAssertion", {
479 aliases: ["Expression", "LVal", "PatternLike"],
480 visitor: ["typeAnnotation", "expression"],
481 fields: {
482 typeAnnotation: validateType("TSType"),
483 expression: validateType("Expression"),
484 },
485 });
486
487 defineType("TSEnumDeclaration", {
488 // "Statement" alias prevents a semicolon from appearing after it in an export declaration.
489 aliases: ["Statement", "Declaration"],
490 visitor: ["id", "members"],
491 fields: {
492 declare: validateOptional(bool),
493 const: validateOptional(bool),
494 id: validateType("Identifier"),
495 members: validateArrayOfType("TSEnumMember"),
496 initializer: validateOptionalType("Expression"),
497 },
498 });
499
500 defineType("TSEnumMember", {
501 visitor: ["id", "initializer"],
502 fields: {
503 id: validateType(["Identifier", "StringLiteral"]),
504 initializer: validateOptionalType("Expression"),
505 },
506 });
507
508 defineType("TSModuleDeclaration", {
509 aliases: ["Statement", "Declaration"],
510 visitor: ["id", "body"],
511 fields: {
512 declare: validateOptional(bool),
513 global: validateOptional(bool),
514 id: validateType(["Identifier", "StringLiteral"]),
515 body: validateType(["TSModuleBlock", "TSModuleDeclaration"]),
516 },
517 });
518
519 defineType("TSModuleBlock", {
520 aliases: ["Scopable", "Block", "BlockParent", "FunctionParent"],
521 visitor: ["body"],
522 fields: {
523 body: validateArrayOfType("Statement"),
524 },
525 });
526
527 defineType("TSImportType", {
528 aliases: ["TSType"],
529 visitor: ["argument", "qualifier", "typeParameters"],
530 fields: {
531 argument: validateType("StringLiteral"),
532 qualifier: validateOptionalType("TSEntityName"),
533 typeParameters: validateOptionalType("TSTypeParameterInstantiation"),
534 },
535 });
536
537 defineType("TSImportEqualsDeclaration", {
538 aliases: ["Statement"],
539 visitor: ["id", "moduleReference"],
540 fields: {
541 isExport: validate(bool),
542 id: validateType("Identifier"),
543 moduleReference: validateType([
544 "TSEntityName",
545 "TSExternalModuleReference",
546 ]),
547 importKind: {
548 validate: assertOneOf("type", "value"),
549 optional: true,
550 },
551 },
552 });
553
554 defineType("TSExternalModuleReference", {
555 visitor: ["expression"],
556 fields: {
557 expression: validateType("StringLiteral"),
558 },
559 });
560
561 defineType("TSNonNullExpression", {
562 aliases: ["Expression", "LVal", "PatternLike"],
563 visitor: ["expression"],
564 fields: {
565 expression: validateType("Expression"),
566 },
567 });
568
569 defineType("TSExportAssignment", {
570 aliases: ["Statement"],
571 visitor: ["expression"],
572 fields: {
573 expression: validateType("Expression"),
574 },
575 });
576
577 defineType("TSNamespaceExportDeclaration", {
578 aliases: ["Statement"],
579 visitor: ["id"],
580 fields: {
581 id: validateType("Identifier"),
582 },
583 });
584
585 defineType("TSTypeAnnotation", {
586 visitor: ["typeAnnotation"],
587 fields: {
588 typeAnnotation: {
589 validate: assertNodeType("TSType"),
590 },
591 },
592 });
593
594 defineType("TSTypeParameterInstantiation", {
595 visitor: ["params"],
596 fields: {
597 params: {
598 validate: chain(
599 assertValueType("array"),
600 assertEach(assertNodeType("TSType")),
601 ),
602 },
603 },
604 });
605
606 defineType("TSTypeParameterDeclaration", {
607 visitor: ["params"],
608 fields: {
609 params: {
610 validate: chain(
611 assertValueType("array"),
612 assertEach(assertNodeType("TSTypeParameter")),
613 ),
614 },
615 },
616 });
617
618 defineType("TSTypeParameter", {
619 builder: ["constraint", "default", "name"],
620 visitor: ["constraint", "default"],
621 fields: {
622 name: {
623 validate: !process.env.BABEL_8_BREAKING
624 ? assertValueType("string")
625 : assertNodeType("Identifier"),
626 },
627 in: {
628 validate: assertValueType("boolean"),
629 optional: true,
630 },
631 out: {
632 validate: assertValueType("boolean"),
633 optional: true,
634 },
635 constraint: {
636 validate: assertNodeType("TSType"),
637 optional: true,
638 },
639 default: {
640 validate: assertNodeType("TSType"),
641 optional: true,
642 },
643 },
644 });