UNPKG

16.8 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.TSAnyKeyword = TSAnyKeyword;
7exports.TSArrayType = TSArrayType;
8exports.TSSatisfiesExpression = exports.TSAsExpression = TSTypeExpression;
9exports.TSBigIntKeyword = TSBigIntKeyword;
10exports.TSBooleanKeyword = TSBooleanKeyword;
11exports.TSCallSignatureDeclaration = TSCallSignatureDeclaration;
12exports.TSConditionalType = TSConditionalType;
13exports.TSConstructSignatureDeclaration = TSConstructSignatureDeclaration;
14exports.TSConstructorType = TSConstructorType;
15exports.TSDeclareFunction = TSDeclareFunction;
16exports.TSDeclareMethod = TSDeclareMethod;
17exports.TSEnumDeclaration = TSEnumDeclaration;
18exports.TSEnumMember = TSEnumMember;
19exports.TSExportAssignment = TSExportAssignment;
20exports.TSExpressionWithTypeArguments = TSExpressionWithTypeArguments;
21exports.TSExternalModuleReference = TSExternalModuleReference;
22exports.TSFunctionType = TSFunctionType;
23exports.TSImportEqualsDeclaration = TSImportEqualsDeclaration;
24exports.TSImportType = TSImportType;
25exports.TSIndexSignature = TSIndexSignature;
26exports.TSIndexedAccessType = TSIndexedAccessType;
27exports.TSInferType = TSInferType;
28exports.TSInstantiationExpression = TSInstantiationExpression;
29exports.TSInterfaceBody = TSInterfaceBody;
30exports.TSInterfaceDeclaration = TSInterfaceDeclaration;
31exports.TSIntersectionType = TSIntersectionType;
32exports.TSIntrinsicKeyword = TSIntrinsicKeyword;
33exports.TSLiteralType = TSLiteralType;
34exports.TSMappedType = TSMappedType;
35exports.TSMethodSignature = TSMethodSignature;
36exports.TSModuleBlock = TSModuleBlock;
37exports.TSModuleDeclaration = TSModuleDeclaration;
38exports.TSNamedTupleMember = TSNamedTupleMember;
39exports.TSNamespaceExportDeclaration = TSNamespaceExportDeclaration;
40exports.TSNeverKeyword = TSNeverKeyword;
41exports.TSNonNullExpression = TSNonNullExpression;
42exports.TSNullKeyword = TSNullKeyword;
43exports.TSNumberKeyword = TSNumberKeyword;
44exports.TSObjectKeyword = TSObjectKeyword;
45exports.TSOptionalType = TSOptionalType;
46exports.TSParameterProperty = TSParameterProperty;
47exports.TSParenthesizedType = TSParenthesizedType;
48exports.TSPropertySignature = TSPropertySignature;
49exports.TSQualifiedName = TSQualifiedName;
50exports.TSRestType = TSRestType;
51exports.TSStringKeyword = TSStringKeyword;
52exports.TSSymbolKeyword = TSSymbolKeyword;
53exports.TSThisType = TSThisType;
54exports.TSTupleType = TSTupleType;
55exports.TSTypeAliasDeclaration = TSTypeAliasDeclaration;
56exports.TSTypeAnnotation = TSTypeAnnotation;
57exports.TSTypeAssertion = TSTypeAssertion;
58exports.TSTypeLiteral = TSTypeLiteral;
59exports.TSTypeOperator = TSTypeOperator;
60exports.TSTypeParameter = TSTypeParameter;
61exports.TSTypeParameterDeclaration = exports.TSTypeParameterInstantiation = TSTypeParameterInstantiation;
62exports.TSTypePredicate = TSTypePredicate;
63exports.TSTypeQuery = TSTypeQuery;
64exports.TSTypeReference = TSTypeReference;
65exports.TSUndefinedKeyword = TSUndefinedKeyword;
66exports.TSUnionType = TSUnionType;
67exports.TSUnknownKeyword = TSUnknownKeyword;
68exports.TSVoidKeyword = TSVoidKeyword;
69exports.tsPrintClassMemberModifiers = tsPrintClassMemberModifiers;
70exports.tsPrintFunctionOrConstructorType = tsPrintFunctionOrConstructorType;
71exports.tsPrintPropertyOrMethodName = tsPrintPropertyOrMethodName;
72exports.tsPrintSignatureDeclarationBase = tsPrintSignatureDeclarationBase;
73exports.tsPrintTypeLiteralOrInterfaceBody = tsPrintTypeLiteralOrInterfaceBody;
74function TSTypeAnnotation(node) {
75 this.tokenChar(58);
76 this.space();
77 if (node.optional) this.tokenChar(63);
78 this.print(node.typeAnnotation, node);
79}
80function TSTypeParameterInstantiation(node, parent) {
81 this.tokenChar(60);
82 this.printList(node.params, node, {});
83 if (parent.type === "ArrowFunctionExpression" && node.params.length === 1) {
84 this.tokenChar(44);
85 }
86 this.tokenChar(62);
87}
88function TSTypeParameter(node) {
89 if (node.in) {
90 this.word("in");
91 this.space();
92 }
93 if (node.out) {
94 this.word("out");
95 this.space();
96 }
97 this.word(node.name);
98 if (node.constraint) {
99 this.space();
100 this.word("extends");
101 this.space();
102 this.print(node.constraint, node);
103 }
104 if (node.default) {
105 this.space();
106 this.tokenChar(61);
107 this.space();
108 this.print(node.default, node);
109 }
110}
111function TSParameterProperty(node) {
112 if (node.accessibility) {
113 this.word(node.accessibility);
114 this.space();
115 }
116 if (node.readonly) {
117 this.word("readonly");
118 this.space();
119 }
120 this._param(node.parameter);
121}
122function TSDeclareFunction(node, parent) {
123 if (node.declare) {
124 this.word("declare");
125 this.space();
126 }
127 this._functionHead(node, parent);
128 this.tokenChar(59);
129}
130function TSDeclareMethod(node) {
131 this._classMethodHead(node);
132 this.tokenChar(59);
133}
134function TSQualifiedName(node) {
135 this.print(node.left, node);
136 this.tokenChar(46);
137 this.print(node.right, node);
138}
139function TSCallSignatureDeclaration(node) {
140 this.tsPrintSignatureDeclarationBase(node);
141 this.tokenChar(59);
142}
143function TSConstructSignatureDeclaration(node) {
144 this.word("new");
145 this.space();
146 this.tsPrintSignatureDeclarationBase(node);
147 this.tokenChar(59);
148}
149function TSPropertySignature(node) {
150 const {
151 readonly
152 } = node;
153 if (readonly) {
154 this.word("readonly");
155 this.space();
156 }
157 this.tsPrintPropertyOrMethodName(node);
158 this.print(node.typeAnnotation, node);
159 this.tokenChar(59);
160}
161function tsPrintPropertyOrMethodName(node) {
162 if (node.computed) {
163 this.tokenChar(91);
164 }
165 this.print(node.key, node);
166 if (node.computed) {
167 this.tokenChar(93);
168 }
169 if (node.optional) {
170 this.tokenChar(63);
171 }
172}
173function TSMethodSignature(node) {
174 const {
175 kind
176 } = node;
177 if (kind === "set" || kind === "get") {
178 this.word(kind);
179 this.space();
180 }
181 this.tsPrintPropertyOrMethodName(node);
182 this.tsPrintSignatureDeclarationBase(node);
183 this.tokenChar(59);
184}
185function TSIndexSignature(node) {
186 const {
187 readonly,
188 static: isStatic
189 } = node;
190 if (isStatic) {
191 this.word("static");
192 this.space();
193 }
194 if (readonly) {
195 this.word("readonly");
196 this.space();
197 }
198 this.tokenChar(91);
199 this._parameters(node.parameters, node);
200 this.tokenChar(93);
201 this.print(node.typeAnnotation, node);
202 this.tokenChar(59);
203}
204function TSAnyKeyword() {
205 this.word("any");
206}
207function TSBigIntKeyword() {
208 this.word("bigint");
209}
210function TSUnknownKeyword() {
211 this.word("unknown");
212}
213function TSNumberKeyword() {
214 this.word("number");
215}
216function TSObjectKeyword() {
217 this.word("object");
218}
219function TSBooleanKeyword() {
220 this.word("boolean");
221}
222function TSStringKeyword() {
223 this.word("string");
224}
225function TSSymbolKeyword() {
226 this.word("symbol");
227}
228function TSVoidKeyword() {
229 this.word("void");
230}
231function TSUndefinedKeyword() {
232 this.word("undefined");
233}
234function TSNullKeyword() {
235 this.word("null");
236}
237function TSNeverKeyword() {
238 this.word("never");
239}
240function TSIntrinsicKeyword() {
241 this.word("intrinsic");
242}
243function TSThisType() {
244 this.word("this");
245}
246function TSFunctionType(node) {
247 this.tsPrintFunctionOrConstructorType(node);
248}
249function TSConstructorType(node) {
250 if (node.abstract) {
251 this.word("abstract");
252 this.space();
253 }
254 this.word("new");
255 this.space();
256 this.tsPrintFunctionOrConstructorType(node);
257}
258function tsPrintFunctionOrConstructorType(node) {
259 const {
260 typeParameters
261 } = node;
262 const parameters = node.parameters;
263 this.print(typeParameters, node);
264 this.tokenChar(40);
265 this._parameters(parameters, node);
266 this.tokenChar(41);
267 this.space();
268 this.token("=>");
269 this.space();
270 const returnType = node.typeAnnotation;
271 this.print(returnType.typeAnnotation, node);
272}
273function TSTypeReference(node) {
274 this.print(node.typeName, node, true);
275 this.print(node.typeParameters, node, true);
276}
277function TSTypePredicate(node) {
278 if (node.asserts) {
279 this.word("asserts");
280 this.space();
281 }
282 this.print(node.parameterName);
283 if (node.typeAnnotation) {
284 this.space();
285 this.word("is");
286 this.space();
287 this.print(node.typeAnnotation.typeAnnotation);
288 }
289}
290function TSTypeQuery(node) {
291 this.word("typeof");
292 this.space();
293 this.print(node.exprName);
294 if (node.typeParameters) {
295 this.print(node.typeParameters, node);
296 }
297}
298function TSTypeLiteral(node) {
299 this.tsPrintTypeLiteralOrInterfaceBody(node.members, node);
300}
301function tsPrintTypeLiteralOrInterfaceBody(members, node) {
302 tsPrintBraced(this, members, node);
303}
304function tsPrintBraced(printer, members, node) {
305 printer.token("{");
306 if (members.length) {
307 printer.indent();
308 printer.newline();
309 for (const member of members) {
310 printer.print(member, node);
311 printer.newline();
312 }
313 printer.dedent();
314 }
315 printer.rightBrace(node);
316}
317function TSArrayType(node) {
318 this.print(node.elementType, node, true);
319 this.token("[]");
320}
321function TSTupleType(node) {
322 this.tokenChar(91);
323 this.printList(node.elementTypes, node);
324 this.tokenChar(93);
325}
326function TSOptionalType(node) {
327 this.print(node.typeAnnotation, node);
328 this.tokenChar(63);
329}
330function TSRestType(node) {
331 this.token("...");
332 this.print(node.typeAnnotation, node);
333}
334function TSNamedTupleMember(node) {
335 this.print(node.label, node);
336 if (node.optional) this.tokenChar(63);
337 this.tokenChar(58);
338 this.space();
339 this.print(node.elementType, node);
340}
341function TSUnionType(node) {
342 tsPrintUnionOrIntersectionType(this, node, "|");
343}
344function TSIntersectionType(node) {
345 tsPrintUnionOrIntersectionType(this, node, "&");
346}
347function tsPrintUnionOrIntersectionType(printer, node, sep) {
348 printer.printJoin(node.types, node, {
349 separator() {
350 this.space();
351 this.token(sep);
352 this.space();
353 }
354 });
355}
356function TSConditionalType(node) {
357 this.print(node.checkType);
358 this.space();
359 this.word("extends");
360 this.space();
361 this.print(node.extendsType);
362 this.space();
363 this.tokenChar(63);
364 this.space();
365 this.print(node.trueType);
366 this.space();
367 this.tokenChar(58);
368 this.space();
369 this.print(node.falseType);
370}
371function TSInferType(node) {
372 this.token("infer");
373 this.space();
374 this.print(node.typeParameter);
375}
376function TSParenthesizedType(node) {
377 this.tokenChar(40);
378 this.print(node.typeAnnotation, node);
379 this.tokenChar(41);
380}
381function TSTypeOperator(node) {
382 this.word(node.operator);
383 this.space();
384 this.print(node.typeAnnotation, node);
385}
386function TSIndexedAccessType(node) {
387 this.print(node.objectType, node, true);
388 this.tokenChar(91);
389 this.print(node.indexType, node);
390 this.tokenChar(93);
391}
392function TSMappedType(node) {
393 const {
394 nameType,
395 optional,
396 readonly,
397 typeParameter,
398 typeAnnotation
399 } = node;
400 this.tokenChar(123);
401 this.space();
402 if (readonly) {
403 tokenIfPlusMinus(this, readonly);
404 this.word("readonly");
405 this.space();
406 }
407 this.tokenChar(91);
408 this.word(typeParameter.name);
409 this.space();
410 this.word("in");
411 this.space();
412 this.print(typeParameter.constraint, typeParameter);
413 if (nameType) {
414 this.space();
415 this.word("as");
416 this.space();
417 this.print(nameType, node);
418 }
419 this.tokenChar(93);
420 if (optional) {
421 tokenIfPlusMinus(this, optional);
422 this.tokenChar(63);
423 }
424 if (typeAnnotation) {
425 this.tokenChar(58);
426 this.space();
427 this.print(typeAnnotation, node);
428 }
429 this.space();
430 this.tokenChar(125);
431}
432function tokenIfPlusMinus(self, tok) {
433 if (tok !== true) {
434 self.token(tok);
435 }
436}
437function TSLiteralType(node) {
438 this.print(node.literal, node);
439}
440function TSExpressionWithTypeArguments(node) {
441 this.print(node.expression, node);
442 this.print(node.typeParameters, node);
443}
444function TSInterfaceDeclaration(node) {
445 const {
446 declare,
447 id,
448 typeParameters,
449 extends: extendz,
450 body
451 } = node;
452 if (declare) {
453 this.word("declare");
454 this.space();
455 }
456 this.word("interface");
457 this.space();
458 this.print(id, node);
459 this.print(typeParameters, node);
460 if (extendz != null && extendz.length) {
461 this.space();
462 this.word("extends");
463 this.space();
464 this.printList(extendz, node);
465 }
466 this.space();
467 this.print(body, node);
468}
469function TSInterfaceBody(node) {
470 this.tsPrintTypeLiteralOrInterfaceBody(node.body, node);
471}
472function TSTypeAliasDeclaration(node) {
473 const {
474 declare,
475 id,
476 typeParameters,
477 typeAnnotation
478 } = node;
479 if (declare) {
480 this.word("declare");
481 this.space();
482 }
483 this.word("type");
484 this.space();
485 this.print(id, node);
486 this.print(typeParameters, node);
487 this.space();
488 this.tokenChar(61);
489 this.space();
490 this.print(typeAnnotation, node);
491 this.tokenChar(59);
492}
493function TSTypeExpression(node) {
494 var _expression$trailingC;
495 const {
496 type,
497 expression,
498 typeAnnotation
499 } = node;
500 const forceParens = !!((_expression$trailingC = expression.trailingComments) != null && _expression$trailingC.length);
501 this.print(expression, node, true, undefined, forceParens);
502 this.space();
503 this.word(type === "TSAsExpression" ? "as" : "satisfies");
504 this.space();
505 this.print(typeAnnotation, node);
506}
507function TSTypeAssertion(node) {
508 const {
509 typeAnnotation,
510 expression
511 } = node;
512 this.tokenChar(60);
513 this.print(typeAnnotation, node);
514 this.tokenChar(62);
515 this.space();
516 this.print(expression, node);
517}
518function TSInstantiationExpression(node) {
519 this.print(node.expression, node);
520 this.print(node.typeParameters, node);
521}
522function TSEnumDeclaration(node) {
523 const {
524 declare,
525 const: isConst,
526 id,
527 members
528 } = node;
529 if (declare) {
530 this.word("declare");
531 this.space();
532 }
533 if (isConst) {
534 this.word("const");
535 this.space();
536 }
537 this.word("enum");
538 this.space();
539 this.print(id, node);
540 this.space();
541 tsPrintBraced(this, members, node);
542}
543function TSEnumMember(node) {
544 const {
545 id,
546 initializer
547 } = node;
548 this.print(id, node);
549 if (initializer) {
550 this.space();
551 this.tokenChar(61);
552 this.space();
553 this.print(initializer, node);
554 }
555 this.tokenChar(44);
556}
557function TSModuleDeclaration(node) {
558 const {
559 declare,
560 id
561 } = node;
562 if (declare) {
563 this.word("declare");
564 this.space();
565 }
566 if (!node.global) {
567 this.word(id.type === "Identifier" ? "namespace" : "module");
568 this.space();
569 }
570 this.print(id, node);
571 if (!node.body) {
572 this.tokenChar(59);
573 return;
574 }
575 let body = node.body;
576 while (body.type === "TSModuleDeclaration") {
577 this.tokenChar(46);
578 this.print(body.id, body);
579 body = body.body;
580 }
581 this.space();
582 this.print(body, node);
583}
584function TSModuleBlock(node) {
585 tsPrintBraced(this, node.body, node);
586}
587function TSImportType(node) {
588 const {
589 argument,
590 qualifier,
591 typeParameters
592 } = node;
593 this.word("import");
594 this.tokenChar(40);
595 this.print(argument, node);
596 this.tokenChar(41);
597 if (qualifier) {
598 this.tokenChar(46);
599 this.print(qualifier, node);
600 }
601 if (typeParameters) {
602 this.print(typeParameters, node);
603 }
604}
605function TSImportEqualsDeclaration(node) {
606 const {
607 isExport,
608 id,
609 moduleReference
610 } = node;
611 if (isExport) {
612 this.word("export");
613 this.space();
614 }
615 this.word("import");
616 this.space();
617 this.print(id, node);
618 this.space();
619 this.tokenChar(61);
620 this.space();
621 this.print(moduleReference, node);
622 this.tokenChar(59);
623}
624function TSExternalModuleReference(node) {
625 this.token("require(");
626 this.print(node.expression, node);
627 this.tokenChar(41);
628}
629function TSNonNullExpression(node) {
630 this.print(node.expression, node);
631 this.tokenChar(33);
632}
633function TSExportAssignment(node) {
634 this.word("export");
635 this.space();
636 this.tokenChar(61);
637 this.space();
638 this.print(node.expression, node);
639 this.tokenChar(59);
640}
641function TSNamespaceExportDeclaration(node) {
642 this.word("export");
643 this.space();
644 this.word("as");
645 this.space();
646 this.word("namespace");
647 this.space();
648 this.print(node.id, node);
649}
650function tsPrintSignatureDeclarationBase(node) {
651 const {
652 typeParameters
653 } = node;
654 const parameters = node.parameters;
655 this.print(typeParameters, node);
656 this.tokenChar(40);
657 this._parameters(parameters, node);
658 this.tokenChar(41);
659 const returnType = node.typeAnnotation;
660 this.print(returnType, node);
661}
662function tsPrintClassMemberModifiers(node) {
663 const isField = node.type === "ClassAccessorProperty" || node.type === "ClassProperty";
664 if (isField && node.declare) {
665 this.word("declare");
666 this.space();
667 }
668 if (node.accessibility) {
669 this.word(node.accessibility);
670 this.space();
671 }
672 if (node.static) {
673 this.word("static");
674 this.space();
675 }
676 if (node.override) {
677 this.word("override");
678 this.space();
679 }
680 if (node.abstract) {
681 this.word("abstract");
682 this.space();
683 }
684 if (isField && node.readonly) {
685 this.word("readonly");
686 this.space();
687 }
688}
689
690//# sourceMappingURL=typescript.js.map