UNPKG

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