Debian Codegraph @

packages/babel-generator/src/generators/flow.ts

raw symbols SCIP
1 import type Printer from "../printer";
2 import { isDeclareExportDeclaration, isStatement } from "@babel/types";
3 import type * as t from "@babel/types";
4 import { ExportAllDeclaration } from "./modules";
5
6 export function AnyTypeAnnotation(this: Printer) {
7 this.word("any");
8 }
9
10 export function ArrayTypeAnnotation(
11 this: Printer,
12 node: t.ArrayTypeAnnotation,
13 ) {
14 this.print(node.elementType, node, true);
15 this.token("[");
16 this.token("]");
17 }
18
19 export function BooleanTypeAnnotation(this: Printer) {
20 this.word("boolean");
21 }
22
23 export function BooleanLiteralTypeAnnotation(
24 this: Printer,
25 node: t.BooleanLiteralTypeAnnotation,
26 ) {
27 this.word(node.value ? "true" : "false");
28 }
29
30 export function NullLiteralTypeAnnotation(this: Printer) {
31 this.word("null");
32 }
33
34 export function DeclareClass(
35 this: Printer,
36 node: t.DeclareClass,
37 parent: t.Node,
38 ) {
39 if (!isDeclareExportDeclaration(parent)) {
40 this.word("declare");
41 this.space();
42 }
43 this.word("class");
44 this.space();
45 this._interfaceish(node);
46 }
47
48 export function DeclareFunction(
49 this: Printer,
50 node: t.DeclareFunction,
51 parent: t.Node,
52 ) {
53 if (!isDeclareExportDeclaration(parent)) {
54 this.word("declare");
55 this.space();
56 }
57 this.word("function");
58 this.space();
59 this.print(node.id, node);
60 // @ts-ignore(Babel 7 vs Babel 8) TODO(Babel 8) Remove this comment, since we'll remove the Noop node
61 this.print(node.id.typeAnnotation.typeAnnotation, node);
62
63 if (node.predicate) {
64 this.space();
65 this.print(node.predicate, node);
66 }
67
68 this.semicolon();
69 }
70
71 export function InferredPredicate(this: Printer) {
72 this.token("%");
73 this.word("checks");
74 }
75
76 export function DeclaredPredicate(this: Printer, node: t.DeclaredPredicate) {
77 this.token("%");
78 this.word("checks");
79 this.token("(");
80 this.print(node.value, node);
81 this.token(")");
82 }
83
84 export function DeclareInterface(this: Printer, node: t.DeclareInterface) {
85 this.word("declare");
86 this.space();
87 this.InterfaceDeclaration(node);
88 }
89
90 export function DeclareModule(this: Printer, node: t.DeclareModule) {
91 this.word("declare");
92 this.space();
93 this.word("module");
94 this.space();
95 this.print(node.id, node);
96 this.space();
97 this.print(node.body, node);
98 }
99
100 export function DeclareModuleExports(
101 this: Printer,
102 node: t.DeclareModuleExports,
103 ) {
104 this.word("declare");
105 this.space();
106 this.word("module");
107 this.token(".");
108 this.word("exports");
109 this.print(node.typeAnnotation, node);
110 }
111
112 export function DeclareTypeAlias(this: Printer, node: t.DeclareTypeAlias) {
113 this.word("declare");
114 this.space();
115 this.TypeAlias(node);
116 }
117
118 export function DeclareOpaqueType(
119 this: Printer,
120 node: t.DeclareOpaqueType,
121 parent: t.Node,
122 ) {
123 if (!isDeclareExportDeclaration(parent)) {
124 this.word("declare");
125 this.space();
126 }
127 this.OpaqueType(node);
128 }
129
130 export function DeclareVariable(
131 this: Printer,
132 node: t.DeclareVariable,
133 parent: t.Node,
134 ) {
135 if (!isDeclareExportDeclaration(parent)) {
136 this.word("declare");
137 this.space();
138 }
139 this.word("var");
140 this.space();
141 this.print(node.id, node);
142 this.print(node.id.typeAnnotation, node);
143 this.semicolon();
144 }
145
146 export function DeclareExportDeclaration(
147 this: Printer,
148 node: t.DeclareExportDeclaration,
149 ) {
150 this.word("declare");
151 this.space();
152 this.word("export");
153 this.space();
154 if (node.default) {
155 this.word("default");
156 this.space();
157 }
158
159 FlowExportDeclaration.call(this, node);
160 }
161
162 export function DeclareExportAllDeclaration(
163 this: Printer,
164 node: t.DeclareExportAllDeclaration,
165 ) {
166 this.word("declare");
167 this.space();
168 ExportAllDeclaration.call(this, node);
169 }
170
171 export function EnumDeclaration(this: Printer, node: t.EnumDeclaration) {
172 const { id, body } = node;
173 this.word("enum");
174 this.space();
175 this.print(id, node);
176 this.print(body, node);
177 }
178
179 function enumExplicitType(
180 context: Printer,
181 name: string,
182 hasExplicitType: boolean,
183 ) {
184 if (hasExplicitType) {
185 context.space();
186 context.word("of");
187 context.space();
188 context.word(name);
189 }
190 context.space();
191 }
192
193 function enumBody(context: Printer, node: t.EnumBody) {
194 const { members } = node;
195 context.token("{");
196 context.indent();
197 context.newline();
198 for (const member of members) {
199 context.print(member, node);
200 context.newline();
201 }
202 if (node.hasUnknownMembers) {
203 context.token("...");
204 context.newline();
205 }
206 context.dedent();
207 context.token("}");
208 }
209
210 export function EnumBooleanBody(this: Printer, node: t.EnumBooleanBody) {
211 const { explicitType } = node;
212 enumExplicitType(this, "boolean", explicitType);
213 enumBody(this, node);
214 }
215
216 export function EnumNumberBody(this: Printer, node: t.EnumNumberBody) {
217 const { explicitType } = node;
218 enumExplicitType(this, "number", explicitType);
219 enumBody(this, node);
220 }
221
222 export function EnumStringBody(this: Printer, node: t.EnumStringBody) {
223 const { explicitType } = node;
224 enumExplicitType(this, "string", explicitType);
225 enumBody(this, node);
226 }
227
228 export function EnumSymbolBody(this: Printer, node: t.EnumSymbolBody) {
229 enumExplicitType(this, "symbol", true);
230 enumBody(this, node);
231 }
232
233 export function EnumDefaultedMember(
234 this: Printer,
235 node: t.EnumDefaultedMember,
236 ) {
237 const { id } = node;
238 this.print(id, node);
239 this.token(",");
240 }
241
242 function enumInitializedMember(
243 context: Printer,
244 node: t.EnumBooleanMember | t.EnumNumberMember | t.EnumStringMember,
245 ) {
246 const { id, init } = node;
247 context.print(id, node);
248 context.space();
249 context.token("=");
250 context.space();
251 context.print(init, node);
252 context.token(",");
253 }
254
255 export function EnumBooleanMember(this: Printer, node: t.EnumBooleanMember) {
256 enumInitializedMember(this, node);
257 }
258
259 export function EnumNumberMember(this: Printer, node: t.EnumNumberMember) {
260 enumInitializedMember(this, node);
261 }
262
263 export function EnumStringMember(this: Printer, node: t.EnumStringMember) {
264 enumInitializedMember(this, node);
265 }
266
267 function FlowExportDeclaration(
268 this: Printer,
269 node: t.DeclareExportDeclaration,
270 ) {
271 if (node.declaration) {
272 const declar = node.declaration;
273 this.print(declar, node);
274 if (!isStatement(declar)) this.semicolon();
275 } else {
276 this.token("{");
277 if (node.specifiers.length) {
278 this.space();
279 this.printList(node.specifiers, node);
280 this.space();
281 }
282 this.token("}");
283
284 if (node.source) {
285 this.space();
286 this.word("from");
287 this.space();
288 this.print(node.source, node);
289 }
290
291 this.semicolon();
292 }
293 }
294
295 export function ExistsTypeAnnotation(this: Printer) {
296 this.token("*");
297 }
298
299 export function FunctionTypeAnnotation(
300 this: Printer,
301 node: t.FunctionTypeAnnotation,
302 parent: t.Node | void,
303 ) {
304 this.print(node.typeParameters, node);
305 this.token("(");
306
307 if (node.this) {
308 this.word("this");
309 this.token(":");
310 this.space();
311 this.print(node.this.typeAnnotation, node);
312 if (node.params.length || node.rest) {
313 this.token(",");
314 this.space();
315 }
316 }
317
318 this.printList(node.params, node);
319
320 if (node.rest) {
321 if (node.params.length) {
322 this.token(",");
323 this.space();
324 }
325 this.token("...");
326 this.print(node.rest, node);
327 }
328
329 this.token(")");
330
331 // this node type is overloaded, not sure why but it makes it EXTREMELY annoying
332 if (
333 parent &&
334 (parent.type === "ObjectTypeCallProperty" ||
335 parent.type === "ObjectTypeInternalSlot" ||
336 parent.type === "DeclareFunction" ||
337 (parent.type === "ObjectTypeProperty" && parent.method))
338 ) {
339 this.token(":");
340 } else {
341 this.space();
342 this.token("=>");
343 }
344
345 this.space();
346 this.print(node.returnType, node);
347 }
348
349 export function FunctionTypeParam(this: Printer, node: t.FunctionTypeParam) {
350 this.print(node.name, node);
351 if (node.optional) this.token("?");
352 if (node.name) {
353 this.token(":");
354 this.space();
355 }
356 this.print(node.typeAnnotation, node);
357 }
358
359 export function InterfaceExtends(this: Printer, node: t.InterfaceExtends) {
360 this.print(node.id, node);
361 this.print(node.typeParameters, node, true);
362 }
363
364 export {
365 InterfaceExtends as ClassImplements,
366 InterfaceExtends as GenericTypeAnnotation,
367 };
368
369 export function _interfaceish(
370 this: Printer,
371 node: t.InterfaceDeclaration | t.DeclareInterface | t.DeclareClass,
372 ) {
373 this.print(node.id, node);
374 this.print(node.typeParameters, node);
375 if (node.extends?.length) {
376 this.space();
377 this.word("extends");
378 this.space();
379 this.printList(node.extends, node);
380 }
381 if (node.mixins && node.mixins.length) {
382 this.space();
383 this.word("mixins");
384 this.space();
385 this.printList(node.mixins, node);
386 }
387 if (node.implements && node.implements.length) {
388 this.space();
389 this.word("implements");
390 this.space();
391 this.printList(node.implements, node);
392 }
393 this.space();
394 this.print(node.body, node);
395 }
396
397 export function _variance(
398 this: Printer,
399 node:
400 | t.TypeParameter
401 | t.ObjectTypeIndexer
402 | t.ObjectTypeProperty
403 | t.ClassProperty
404 | t.ClassPrivateProperty
405 | t.ClassAccessorProperty,
406 ) {
407 if (node.variance) {
408 if (node.variance.kind === "plus") {
409 this.token("+");
410 } else if (node.variance.kind === "minus") {
411 this.token("-");
412 }
413 }
414 }
415
416 export function InterfaceDeclaration(
417 this: Printer,
418 node: t.InterfaceDeclaration | t.DeclareInterface,
419 ) {
420 this.word("interface");
421 this.space();
422 this._interfaceish(node);
423 }
424
425 function andSeparator(this: Printer) {
426 this.space();
427 this.token("&");
428 this.space();
429 }
430
431 export function InterfaceTypeAnnotation(
432 this: Printer,
433 node: t.InterfaceTypeAnnotation,
434 ) {
435 this.word("interface");
436 if (node.extends && node.extends.length) {
437 this.space();
438 this.word("extends");
439 this.space();
440 this.printList(node.extends, node);
441 }
442 this.space();
443 this.print(node.body, node);
444 }
445
446 export function IntersectionTypeAnnotation(
447 this: Printer,
448 node: t.IntersectionTypeAnnotation,
449 ) {
450 this.printJoin(node.types, node, { separator: andSeparator });
451 }
452
453 export function MixedTypeAnnotation(this: Printer) {
454 this.word("mixed");
455 }
456
457 export function EmptyTypeAnnotation(this: Printer) {
458 this.word("empty");
459 }
460
461 export function NullableTypeAnnotation(
462 this: Printer,
463 node: t.NullableTypeAnnotation,
464 ) {
465 this.token("?");
466 this.print(node.typeAnnotation, node);
467 }
468
469 export {
470 NumericLiteral as NumberLiteralTypeAnnotation,
471 StringLiteral as StringLiteralTypeAnnotation,
472 } from "./types";
473
474 export function NumberTypeAnnotation(this: Printer) {
475 this.word("number");
476 }
477
478 export function StringTypeAnnotation(this: Printer) {
479 this.word("string");
480 }
481
482 export function ThisTypeAnnotation(this: Printer) {
483 this.word("this");
484 }
485
486 export function TupleTypeAnnotation(
487 this: Printer,
488 node: t.TupleTypeAnnotation,
489 ) {
490 this.token("[");
491 this.printList(node.types, node);
492 this.token("]");
493 }
494
495 export function TypeofTypeAnnotation(
496 this: Printer,
497 node: t.TypeofTypeAnnotation,
498 ) {
499 this.word("typeof");
500 this.space();
501 this.print(node.argument, node);
502 }
503
504 export function TypeAlias(
505 this: Printer,
506 node: t.TypeAlias | t.DeclareTypeAlias,
507 ) {
508 this.word("type");
509 this.space();
510 this.print(node.id, node);
511 this.print(node.typeParameters, node);
512 this.space();
513 this.token("=");
514 this.space();
515 this.print(node.right, node);
516 this.semicolon();
517 }
518
519 export function TypeAnnotation(this: Printer, node: t.TypeAnnotation) {
520 this.token(":");
521 this.space();
522 // @ts-expect-error todo(flow->ts) can this be removed? `.optional` looks to be not existing property
523 if (node.optional) this.token("?");
524 this.print(node.typeAnnotation, node);
525 }
526
527 export function TypeParameterInstantiation(
528 this: Printer,
529 node: t.TypeParameterInstantiation,
530 ): void {
531 this.token("<");
532 this.printList(node.params, node, {});
533 this.token(">");
534 }
535
536 export { TypeParameterInstantiation as TypeParameterDeclaration };
537
538 export function TypeParameter(this: Printer, node: t.TypeParameter) {
539 this._variance(node);
540
541 this.word(node.name);
542
543 if (node.bound) {
544 this.print(node.bound, node);
545 }
546
547 if (node.default) {
548 this.space();
549 this.token("=");
550 this.space();
551 this.print(node.default, node);
552 }
553 }
554
555 export function OpaqueType(
556 this: Printer,
557 node: t.OpaqueType | t.DeclareOpaqueType,
558 ) {
559 this.word("opaque");
560 this.space();
561 this.word("type");
562 this.space();
563 this.print(node.id, node);
564 this.print(node.typeParameters, node);
565 if (node.supertype) {
566 this.token(":");
567 this.space();
568 this.print(node.supertype, node);
569 }
570
571 if (node.impltype) {
572 this.space();
573 this.token("=");
574 this.space();
575 this.print(node.impltype, node);
576 }
577 this.semicolon();
578 }
579
580 export function ObjectTypeAnnotation(
581 this: Printer,
582 node: t.ObjectTypeAnnotation,
583 ) {
584 if (node.exact) {
585 this.token("{|");
586 } else {
587 this.token("{");
588 }
589
590 // TODO: remove the array fallbacks and instead enforce the types to require an array
591 const props = [
592 ...node.properties,
593 ...(node.callProperties || []),
594 ...(node.indexers || []),
595 ...(node.internalSlots || []),
596 ];
597
598 if (props.length) {
599 this.newline();
600
601 this.space();
602
603 this.printJoin(props, node, {
604 addNewlines(leading) {
605 if (leading && !props[0]) return 1;
606 },
607 indent: true,
608 statement: true,
609 iterator: () => {
610 if (props.length !== 1 || node.inexact) {
611 this.token(",");
612 this.space();
613 }
614 },
615 });
616
617 this.space();
618 }
619
620 if (node.inexact) {
621 this.indent();
622 this.token("...");
623 if (props.length) {
624 this.newline();
625 }
626 this.dedent();
627 }
628
629 if (node.exact) {
630 this.token("|}");
631 } else {
632 this.token("}");
633 }
634 }
635
636 export function ObjectTypeInternalSlot(
637 this: Printer,
638 node: t.ObjectTypeInternalSlot,
639 ) {
640 if (node.static) {
641 this.word("static");
642 this.space();
643 }
644 this.token("[");
645 this.token("[");
646 this.print(node.id, node);
647 this.token("]");
648 this.token("]");
649 if (node.optional) this.token("?");
650 if (!node.method) {
651 this.token(":");
652 this.space();
653 }
654 this.print(node.value, node);
655 }
656
657 export function ObjectTypeCallProperty(
658 this: Printer,
659 node: t.ObjectTypeCallProperty,
660 ) {
661 if (node.static) {
662 this.word("static");
663 this.space();
664 }
665 this.print(node.value, node);
666 }
667
668 export function ObjectTypeIndexer(this: Printer, node: t.ObjectTypeIndexer) {
669 if (node.static) {
670 this.word("static");
671 this.space();
672 }
673 this._variance(node);
674 this.token("[");
675 if (node.id) {
676 this.print(node.id, node);
677 this.token(":");
678 this.space();
679 }
680 this.print(node.key, node);
681 this.token("]");
682 this.token(":");
683 this.space();
684 this.print(node.value, node);
685 }
686
687 export function ObjectTypeProperty(this: Printer, node: t.ObjectTypeProperty) {
688 if (node.proto) {
689 this.word("proto");
690 this.space();
691 }
692 if (node.static) {
693 this.word("static");
694 this.space();
695 }
696 if (node.kind === "get" || node.kind === "set") {
697 this.word(node.kind);
698 this.space();
699 }
700 this._variance(node);
701 this.print(node.key, node);
702 if (node.optional) this.token("?");
703 if (!node.method) {
704 this.token(":");
705 this.space();
706 }
707 this.print(node.value, node);
708 }
709
710 export function ObjectTypeSpreadProperty(
711 this: Printer,
712 node: t.ObjectTypeSpreadProperty,
713 ) {
714 this.token("...");
715 this.print(node.argument, node);
716 }
717
718 export function QualifiedTypeIdentifier(
719 this: Printer,
720 node: t.QualifiedTypeIdentifier,
721 ) {
722 this.print(node.qualification, node);
723 this.token(".");
724 this.print(node.id, node);
725 }
726
727 export function SymbolTypeAnnotation(this: Printer) {
728 this.word("symbol");
729 }
730
731 function orSeparator(this: Printer) {
732 this.space();
733 this.token("|");
734 this.space();
735 }
736
737 export function UnionTypeAnnotation(
738 this: Printer,
739 node: t.UnionTypeAnnotation,
740 ) {
741 this.printJoin(node.types, node, { separator: orSeparator });
742 }
743
744 export function TypeCastExpression(this: Printer, node: t.TypeCastExpression) {
745 this.token("(");
746 this.print(node.expression, node);
747 this.print(node.typeAnnotation, node);
748 this.token(")");
749 }
750
751 export function Variance(this: Printer, node: t.Variance) {
752 if (node.kind === "plus") {
753 this.token("+");
754 } else {
755 this.token("-");
756 }
757 }
758
759 export function VoidTypeAnnotation(this: Printer) {
760 this.word("void");
761 }
762
763 export function IndexedAccessType(this: Printer, node: t.IndexedAccessType) {
764 this.print(node.objectType, node, true);
765 this.token("[");
766 this.print(node.indexType, node);
767 this.token("]");
768 }
769
770 export function OptionalIndexedAccessType(
771 this: Printer,
772 node: t.OptionalIndexedAccessType,
773 ) {
774 this.print(node.objectType, node);
775 if (node.optional) {
776 this.token("?.");
777 }
778 this.token("[");
779 this.print(node.indexType, node);
780 this.token("]");
781 }