UNPKG

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