1
|
"use strict";
|
2
|
|
3
|
exports.__esModule = true;
|
4
|
exports.TypeParameterDeclaration = exports.StringLiteralTypeAnnotation = exports.NumericLiteralTypeAnnotation = exports.GenericTypeAnnotation = exports.ClassImplements = undefined;
|
5
|
exports.AnyTypeAnnotation = AnyTypeAnnotation;
|
6
|
exports.ArrayTypeAnnotation = ArrayTypeAnnotation;
|
7
|
exports.BooleanTypeAnnotation = BooleanTypeAnnotation;
|
8
|
exports.BooleanLiteralTypeAnnotation = BooleanLiteralTypeAnnotation;
|
9
|
exports.NullLiteralTypeAnnotation = NullLiteralTypeAnnotation;
|
10
|
exports.DeclareClass = DeclareClass;
|
11
|
exports.DeclareFunction = DeclareFunction;
|
12
|
exports.DeclareInterface = DeclareInterface;
|
13
|
exports.DeclareModule = DeclareModule;
|
14
|
exports.DeclareModuleExports = DeclareModuleExports;
|
15
|
exports.DeclareTypeAlias = DeclareTypeAlias;
|
16
|
exports.DeclareOpaqueType = DeclareOpaqueType;
|
17
|
exports.DeclareVariable = DeclareVariable;
|
18
|
exports.DeclareExportDeclaration = DeclareExportDeclaration;
|
19
|
exports.ExistentialTypeParam = ExistentialTypeParam;
|
20
|
exports.FunctionTypeAnnotation = FunctionTypeAnnotation;
|
21
|
exports.FunctionTypeParam = FunctionTypeParam;
|
22
|
exports.InterfaceExtends = InterfaceExtends;
|
23
|
exports._interfaceish = _interfaceish;
|
24
|
exports._variance = _variance;
|
25
|
exports.InterfaceDeclaration = InterfaceDeclaration;
|
26
|
exports.IntersectionTypeAnnotation = IntersectionTypeAnnotation;
|
27
|
exports.MixedTypeAnnotation = MixedTypeAnnotation;
|
28
|
exports.EmptyTypeAnnotation = EmptyTypeAnnotation;
|
29
|
exports.NullableTypeAnnotation = NullableTypeAnnotation;
|
30
|
|
31
|
var _types = require("./types");
|
32
|
|
33
|
Object.defineProperty(exports, "NumericLiteralTypeAnnotation", {
|
34
|
enumerable: true,
|
35
|
get: function get() {
|
36
|
return _types.NumericLiteral;
|
37
|
}
|
38
|
});
|
39
|
Object.defineProperty(exports, "StringLiteralTypeAnnotation", {
|
40
|
enumerable: true,
|
41
|
get: function get() {
|
42
|
return _types.StringLiteral;
|
43
|
}
|
44
|
});
|
45
|
exports.NumberTypeAnnotation = NumberTypeAnnotation;
|
46
|
exports.StringTypeAnnotation = StringTypeAnnotation;
|
47
|
exports.ThisTypeAnnotation = ThisTypeAnnotation;
|
48
|
exports.TupleTypeAnnotation = TupleTypeAnnotation;
|
49
|
exports.TypeofTypeAnnotation = TypeofTypeAnnotation;
|
50
|
exports.TypeAlias = TypeAlias;
|
51
|
exports.OpaqueType = OpaqueType;
|
52
|
exports.TypeAnnotation = TypeAnnotation;
|
53
|
exports.TypeParameter = TypeParameter;
|
54
|
exports.TypeParameterInstantiation = TypeParameterInstantiation;
|
55
|
exports.ObjectTypeAnnotation = ObjectTypeAnnotation;
|
56
|
exports.ObjectTypeCallProperty = ObjectTypeCallProperty;
|
57
|
exports.ObjectTypeIndexer = ObjectTypeIndexer;
|
58
|
exports.ObjectTypeProperty = ObjectTypeProperty;
|
59
|
exports.ObjectTypeSpreadProperty = ObjectTypeSpreadProperty;
|
60
|
exports.QualifiedTypeIdentifier = QualifiedTypeIdentifier;
|
61
|
exports.UnionTypeAnnotation = UnionTypeAnnotation;
|
62
|
exports.TypeCastExpression = TypeCastExpression;
|
63
|
exports.VoidTypeAnnotation = VoidTypeAnnotation;
|
64
|
|
65
|
var _babelTypes = require("babel-types");
|
66
|
|
67
|
var t = _interopRequireWildcard(_babelTypes);
|
68
|
|
69
|
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
|
70
|
|
71
|
function AnyTypeAnnotation() {
|
72
|
this.word("any");
|
73
|
}
|
74
|
|
75
|
function ArrayTypeAnnotation(node) {
|
76
|
this.print(node.elementType, node);
|
77
|
this.token("[");
|
78
|
this.token("]");
|
79
|
}
|
80
|
|
81
|
function BooleanTypeAnnotation() {
|
82
|
this.word("boolean");
|
83
|
}
|
84
|
|
85
|
function BooleanLiteralTypeAnnotation(node) {
|
86
|
this.word(node.value ? "true" : "false");
|
87
|
}
|
88
|
|
89
|
function NullLiteralTypeAnnotation() {
|
90
|
this.word("null");
|
91
|
}
|
92
|
|
93
|
function DeclareClass(node, parent) {
|
94
|
if (!t.isDeclareExportDeclaration(parent)) {
|
95
|
this.word("declare");
|
96
|
this.space();
|
97
|
}
|
98
|
this.word("class");
|
99
|
this.space();
|
100
|
this._interfaceish(node);
|
101
|
}
|
102
|
|
103
|
function DeclareFunction(node, parent) {
|
104
|
if (!t.isDeclareExportDeclaration(parent)) {
|
105
|
this.word("declare");
|
106
|
this.space();
|
107
|
}
|
108
|
this.word("function");
|
109
|
this.space();
|
110
|
this.print(node.id, node);
|
111
|
this.print(node.id.typeAnnotation.typeAnnotation, node);
|
112
|
this.semicolon();
|
113
|
}
|
114
|
|
115
|
function DeclareInterface(node) {
|
116
|
this.word("declare");
|
117
|
this.space();
|
118
|
this.InterfaceDeclaration(node);
|
119
|
}
|
120
|
|
121
|
function DeclareModule(node) {
|
122
|
this.word("declare");
|
123
|
this.space();
|
124
|
this.word("module");
|
125
|
this.space();
|
126
|
this.print(node.id, node);
|
127
|
this.space();
|
128
|
this.print(node.body, node);
|
129
|
}
|
130
|
|
131
|
function DeclareModuleExports(node) {
|
132
|
this.word("declare");
|
133
|
this.space();
|
134
|
this.word("module");
|
135
|
this.token(".");
|
136
|
this.word("exports");
|
137
|
this.print(node.typeAnnotation, node);
|
138
|
}
|
139
|
|
140
|
function DeclareTypeAlias(node) {
|
141
|
this.word("declare");
|
142
|
this.space();
|
143
|
this.TypeAlias(node);
|
144
|
}
|
145
|
|
146
|
function DeclareOpaqueType(node, parent) {
|
147
|
if (!t.isDeclareExportDeclaration(parent)) {
|
148
|
this.word("declare");
|
149
|
this.space();
|
150
|
}
|
151
|
this.OpaqueType(node);
|
152
|
}
|
153
|
|
154
|
function DeclareVariable(node, parent) {
|
155
|
if (!t.isDeclareExportDeclaration(parent)) {
|
156
|
this.word("declare");
|
157
|
this.space();
|
158
|
}
|
159
|
this.word("var");
|
160
|
this.space();
|
161
|
this.print(node.id, node);
|
162
|
this.print(node.id.typeAnnotation, node);
|
163
|
this.semicolon();
|
164
|
}
|
165
|
|
166
|
function DeclareExportDeclaration(node) {
|
167
|
this.word("declare");
|
168
|
this.space();
|
169
|
this.word("export");
|
170
|
this.space();
|
171
|
if (node.default) {
|
172
|
this.word("default");
|
173
|
this.space();
|
174
|
}
|
175
|
|
176
|
FlowExportDeclaration.apply(this, arguments);
|
177
|
}
|
178
|
|
179
|
function FlowExportDeclaration(node) {
|
180
|
if (node.declaration) {
|
181
|
var declar = node.declaration;
|
182
|
this.print(declar, node);
|
183
|
if (!t.isStatement(declar)) this.semicolon();
|
184
|
} else {
|
185
|
this.token("{");
|
186
|
if (node.specifiers.length) {
|
187
|
this.space();
|
188
|
this.printList(node.specifiers, node);
|
189
|
this.space();
|
190
|
}
|
191
|
this.token("}");
|
192
|
|
193
|
if (node.source) {
|
194
|
this.space();
|
195
|
this.word("from");
|
196
|
this.space();
|
197
|
this.print(node.source, node);
|
198
|
}
|
199
|
|
200
|
this.semicolon();
|
201
|
}
|
202
|
}
|
203
|
|
204
|
function ExistentialTypeParam() {
|
205
|
this.token("*");
|
206
|
}
|
207
|
|
208
|
function FunctionTypeAnnotation(node, parent) {
|
209
|
this.print(node.typeParameters, node);
|
210
|
this.token("(");
|
211
|
this.printList(node.params, node);
|
212
|
|
213
|
if (node.rest) {
|
214
|
if (node.params.length) {
|
215
|
this.token(",");
|
216
|
this.space();
|
217
|
}
|
218
|
this.token("...");
|
219
|
this.print(node.rest, node);
|
220
|
}
|
221
|
|
222
|
this.token(")");
|
223
|
|
224
|
if (parent.type === "ObjectTypeCallProperty" || parent.type === "DeclareFunction") {
|
225
|
this.token(":");
|
226
|
} else {
|
227
|
this.space();
|
228
|
this.token("=>");
|
229
|
}
|
230
|
|
231
|
this.space();
|
232
|
this.print(node.returnType, node);
|
233
|
}
|
234
|
|
235
|
function FunctionTypeParam(node) {
|
236
|
this.print(node.name, node);
|
237
|
if (node.optional) this.token("?");
|
238
|
this.token(":");
|
239
|
this.space();
|
240
|
this.print(node.typeAnnotation, node);
|
241
|
}
|
242
|
|
243
|
function InterfaceExtends(node) {
|
244
|
this.print(node.id, node);
|
245
|
this.print(node.typeParameters, node);
|
246
|
}
|
247
|
|
248
|
exports.ClassImplements = InterfaceExtends;
|
249
|
exports.GenericTypeAnnotation = InterfaceExtends;
|
250
|
function _interfaceish(node) {
|
251
|
this.print(node.id, node);
|
252
|
this.print(node.typeParameters, node);
|
253
|
if (node.extends.length) {
|
254
|
this.space();
|
255
|
this.word("extends");
|
256
|
this.space();
|
257
|
this.printList(node.extends, node);
|
258
|
}
|
259
|
if (node.mixins && node.mixins.length) {
|
260
|
this.space();
|
261
|
this.word("mixins");
|
262
|
this.space();
|
263
|
this.printList(node.mixins, node);
|
264
|
}
|
265
|
this.space();
|
266
|
this.print(node.body, node);
|
267
|
}
|
268
|
|
269
|
function _variance(node) {
|
270
|
if (node.variance === "plus") {
|
271
|
this.token("+");
|
272
|
} else if (node.variance === "minus") {
|
273
|
this.token("-");
|
274
|
}
|
275
|
}
|
276
|
|
277
|
function InterfaceDeclaration(node) {
|
278
|
this.word("interface");
|
279
|
this.space();
|
280
|
this._interfaceish(node);
|
281
|
}
|
282
|
|
283
|
function andSeparator() {
|
284
|
this.space();
|
285
|
this.token("&");
|
286
|
this.space();
|
287
|
}
|
288
|
|
289
|
function IntersectionTypeAnnotation(node) {
|
290
|
this.printJoin(node.types, node, { separator: andSeparator });
|
291
|
}
|
292
|
|
293
|
function MixedTypeAnnotation() {
|
294
|
this.word("mixed");
|
295
|
}
|
296
|
|
297
|
function EmptyTypeAnnotation() {
|
298
|
this.word("empty");
|
299
|
}
|
300
|
|
301
|
function NullableTypeAnnotation(node) {
|
302
|
this.token("?");
|
303
|
this.print(node.typeAnnotation, node);
|
304
|
}
|
305
|
|
306
|
function NumberTypeAnnotation() {
|
307
|
this.word("number");
|
308
|
}
|
309
|
|
310
|
function StringTypeAnnotation() {
|
311
|
this.word("string");
|
312
|
}
|
313
|
|
314
|
function ThisTypeAnnotation() {
|
315
|
this.word("this");
|
316
|
}
|
317
|
|
318
|
function TupleTypeAnnotation(node) {
|
319
|
this.token("[");
|
320
|
this.printList(node.types, node);
|
321
|
this.token("]");
|
322
|
}
|
323
|
|
324
|
function TypeofTypeAnnotation(node) {
|
325
|
this.word("typeof");
|
326
|
this.space();
|
327
|
this.print(node.argument, node);
|
328
|
}
|
329
|
|
330
|
function TypeAlias(node) {
|
331
|
this.word("type");
|
332
|
this.space();
|
333
|
this.print(node.id, node);
|
334
|
this.print(node.typeParameters, node);
|
335
|
this.space();
|
336
|
this.token("=");
|
337
|
this.space();
|
338
|
this.print(node.right, node);
|
339
|
this.semicolon();
|
340
|
}
|
341
|
function OpaqueType(node) {
|
342
|
this.word("opaque");
|
343
|
this.space();
|
344
|
this.word("type");
|
345
|
this.space();
|
346
|
this.print(node.id, node);
|
347
|
this.print(node.typeParameters, node);
|
348
|
if (node.supertype) {
|
349
|
this.token(":");
|
350
|
this.space();
|
351
|
this.print(node.supertype, node);
|
352
|
}
|
353
|
if (node.impltype) {
|
354
|
this.space();
|
355
|
this.token("=");
|
356
|
this.space();
|
357
|
this.print(node.impltype, node);
|
358
|
}
|
359
|
this.semicolon();
|
360
|
}
|
361
|
|
362
|
function TypeAnnotation(node) {
|
363
|
this.token(":");
|
364
|
this.space();
|
365
|
if (node.optional) this.token("?");
|
366
|
this.print(node.typeAnnotation, node);
|
367
|
}
|
368
|
|
369
|
function TypeParameter(node) {
|
370
|
this._variance(node);
|
371
|
|
372
|
this.word(node.name);
|
373
|
|
374
|
if (node.bound) {
|
375
|
this.print(node.bound, node);
|
376
|
}
|
377
|
|
378
|
if (node.default) {
|
379
|
this.space();
|
380
|
this.token("=");
|
381
|
this.space();
|
382
|
this.print(node.default, node);
|
383
|
}
|
384
|
}
|
385
|
|
386
|
function TypeParameterInstantiation(node) {
|
387
|
this.token("<");
|
388
|
this.printList(node.params, node, {});
|
389
|
this.token(">");
|
390
|
}
|
391
|
|
392
|
exports.TypeParameterDeclaration = TypeParameterInstantiation;
|
393
|
function ObjectTypeAnnotation(node) {
|
394
|
var _this = this;
|
395
|
|
396
|
if (node.exact) {
|
397
|
this.token("{|");
|
398
|
} else {
|
399
|
this.token("{");
|
400
|
}
|
401
|
|
402
|
var props = node.properties.concat(node.callProperties, node.indexers);
|
403
|
|
404
|
if (props.length) {
|
405
|
this.space();
|
406
|
|
407
|
this.printJoin(props, node, {
|
408
|
addNewlines: function addNewlines(leading) {
|
409
|
if (leading && !props[0]) return 1;
|
410
|
},
|
411
|
|
412
|
indent: true,
|
413
|
statement: true,
|
414
|
iterator: function iterator() {
|
415
|
if (props.length !== 1) {
|
416
|
if (_this.format.flowCommaSeparator) {
|
417
|
_this.token(",");
|
418
|
} else {
|
419
|
_this.semicolon();
|
420
|
}
|
421
|
_this.space();
|
422
|
}
|
423
|
}
|
424
|
});
|
425
|
|
426
|
this.space();
|
427
|
}
|
428
|
|
429
|
if (node.exact) {
|
430
|
this.token("|}");
|
431
|
} else {
|
432
|
this.token("}");
|
433
|
}
|
434
|
}
|
435
|
|
436
|
function ObjectTypeCallProperty(node) {
|
437
|
if (node.static) {
|
438
|
this.word("static");
|
439
|
this.space();
|
440
|
}
|
441
|
this.print(node.value, node);
|
442
|
}
|
443
|
|
444
|
function ObjectTypeIndexer(node) {
|
445
|
if (node.static) {
|
446
|
this.word("static");
|
447
|
this.space();
|
448
|
}
|
449
|
this._variance(node);
|
450
|
this.token("[");
|
451
|
this.print(node.id, node);
|
452
|
this.token(":");
|
453
|
this.space();
|
454
|
this.print(node.key, node);
|
455
|
this.token("]");
|
456
|
this.token(":");
|
457
|
this.space();
|
458
|
this.print(node.value, node);
|
459
|
}
|
460
|
|
461
|
function ObjectTypeProperty(node) {
|
462
|
if (node.static) {
|
463
|
this.word("static");
|
464
|
this.space();
|
465
|
}
|
466
|
this._variance(node);
|
467
|
this.print(node.key, node);
|
468
|
if (node.optional) this.token("?");
|
469
|
this.token(":");
|
470
|
this.space();
|
471
|
this.print(node.value, node);
|
472
|
}
|
473
|
|
474
|
function ObjectTypeSpreadProperty(node) {
|
475
|
this.token("...");
|
476
|
this.print(node.argument, node);
|
477
|
}
|
478
|
|
479
|
function QualifiedTypeIdentifier(node) {
|
480
|
this.print(node.qualification, node);
|
481
|
this.token(".");
|
482
|
this.print(node.id, node);
|
483
|
}
|
484
|
|
485
|
function orSeparator() {
|
486
|
this.space();
|
487
|
this.token("|");
|
488
|
this.space();
|
489
|
}
|
490
|
|
491
|
function UnionTypeAnnotation(node) {
|
492
|
this.printJoin(node.types, node, { separator: orSeparator });
|
493
|
}
|
494
|
|
495
|
function TypeCastExpression(node) {
|
496
|
this.token("(");
|
497
|
this.print(node.expression, node);
|
498
|
this.print(node.typeAnnotation, node);
|
499
|
this.token(")");
|
500
|
}
|
501
|
|
502
|
function VoidTypeAnnotation() {
|
503
|
this.word("void");
|
504
|
}
|