json-as 1.2.1 → 1.2.2-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/package.json +1 -1
  2. package/.github/FUNDING.yml +0 -1
  3. package/.github/dependabot.yml +0 -11
  4. package/.github/workflows/benchmark.yml +0 -72
  5. package/.github/workflows/release-package.yml +0 -47
  6. package/.github/workflows/tests.yml +0 -25
  7. package/.prettierignore +0 -9
  8. package/.prettierrc.json +0 -7
  9. package/.trunk/configs/.markdownlint.yaml +0 -2
  10. package/.trunk/configs/.shellcheckrc +0 -7
  11. package/.trunk/configs/.yamllint.yaml +0 -7
  12. package/.trunk/trunk.yaml +0 -37
  13. package/CHANGELOG.md +0 -334
  14. package/SECURITY.md +0 -32
  15. package/asconfig.json +0 -7
  16. package/assembly/__benches__/abc.bench.ts +0 -28
  17. package/assembly/__benches__/large.bench.ts +0 -238
  18. package/assembly/__benches__/lib/bench.ts +0 -85
  19. package/assembly/__benches__/medium.bench.ts +0 -128
  20. package/assembly/__benches__/small.bench.ts +0 -46
  21. package/assembly/__benches__/throughput.ts +0 -172
  22. package/assembly/__benches__/vec3.bench.ts +0 -37
  23. package/assembly/__tests__/arbitrary.spec.ts +0 -35
  24. package/assembly/__tests__/array.spec.ts +0 -145
  25. package/assembly/__tests__/bool.spec.ts +0 -12
  26. package/assembly/__tests__/box.spec.ts +0 -27
  27. package/assembly/__tests__/custom.spec.ts +0 -56
  28. package/assembly/__tests__/date.spec.ts +0 -36
  29. package/assembly/__tests__/enum.spec.ts +0 -35
  30. package/assembly/__tests__/float.spec.ts +0 -42
  31. package/assembly/__tests__/generics.spec.ts +0 -49
  32. package/assembly/__tests__/hierarchy.spec.ts +0 -61
  33. package/assembly/__tests__/integer.spec.ts +0 -26
  34. package/assembly/__tests__/lib/index.ts +0 -41
  35. package/assembly/__tests__/map.spec.ts +0 -7
  36. package/assembly/__tests__/namespace.spec.ts +0 -63
  37. package/assembly/__tests__/null.spec.ts +0 -12
  38. package/assembly/__tests__/raw.spec.ts +0 -23
  39. package/assembly/__tests__/resolving.spec.ts +0 -55
  40. package/assembly/__tests__/staticarray.spec.ts +0 -12
  41. package/assembly/__tests__/string.spec.ts +0 -30
  42. package/assembly/__tests__/struct.spec.ts +0 -163
  43. package/assembly/__tests__/test.spec.ts +0 -3
  44. package/assembly/__tests__/types.spec.ts +0 -27
  45. package/assembly/__tests__/types.ts +0 -98
  46. package/assembly/test.tmp.ts +0 -133
  47. package/bench/abc.bench.ts +0 -25
  48. package/bench/large.bench.ts +0 -127
  49. package/bench/lib/bench.d.ts +0 -27
  50. package/bench/lib/bench.js +0 -53
  51. package/bench/lib/chart.ts +0 -217
  52. package/bench/medium.bench.ts +0 -68
  53. package/bench/runners/assemblyscript.js +0 -34
  54. package/bench/small.bench.ts +0 -34
  55. package/bench/throughput.ts +0 -87
  56. package/bench/tsconfig.json +0 -13
  57. package/bench/vec3.bench.ts +0 -30
  58. package/bench.ts +0 -18
  59. package/ci/bench/lib/bench.ts +0 -42
  60. package/ci/bench/runners/assemblyscript.js +0 -29
  61. package/ci/run-bench.as.sh +0 -63
  62. package/publish.sh +0 -78
  63. package/run-bench.as.sh +0 -60
  64. package/run-bench.js.sh +0 -36
  65. package/run-tests.sh +0 -51
  66. package/scripts/build-chart01.ts +0 -38
  67. package/scripts/build-chart02.ts +0 -38
  68. package/scripts/build-chart03.ts +0 -139
  69. package/scripts/build-chart05.ts +0 -47
  70. package/scripts/generate-as-class.ts +0 -50
  71. package/scripts/lib/bench-utils.ts +0 -308
  72. package/transform/src/builder.ts +0 -1375
  73. package/transform/src/index.ts +0 -1486
  74. package/transform/src/linkers/alias.ts +0 -58
  75. package/transform/src/linkers/custom.ts +0 -32
  76. package/transform/src/linkers/imports.ts +0 -22
  77. package/transform/src/types.ts +0 -300
  78. package/transform/src/util.ts +0 -128
  79. package/transform/src/visitor.ts +0 -530
@@ -1,1375 +0,0 @@
1
- // Taken from https://github.com/as-pect/visitor-as/blob/master/src/astBuilder.ts
2
- // tslint:disable: as-internal-case
3
-
4
- import { CommonFlags, TypeNode, Node, NodeKind, Source, NamedTypeNode, FunctionTypeNode, TypeParameterNode, IdentifierExpression, CallExpression, ClassExpression, ElementAccessExpression, FunctionExpression, InstanceOfExpression, LiteralExpression, NewExpression, ParenthesizedExpression, PropertyAccessExpression, TernaryExpression, UnaryPostfixExpression, UnaryPrefixExpression, BlockStatement, BreakStatement, ContinueStatement, DoStatement, EmptyStatement, ExportStatement, ExportDefaultStatement, ExportImportStatement, ExpressionStatement, ForStatement, IfStatement, ImportStatement, ReturnStatement, SwitchStatement, ThrowStatement, TryStatement, VariableStatement, WhileStatement, ClassDeclaration, EnumDeclaration, EnumValueDeclaration, FieldDeclaration, FunctionDeclaration, ImportDeclaration, InterfaceDeclaration, MethodDeclaration, NamespaceDeclaration, TypeDeclaration, VariableDeclaration, DecoratorNode, ExportMember, ParameterNode, SwitchCase, TypeName, ArrayLiteralExpression, Expression, ObjectLiteralExpression, AssertionKind, LiteralKind, FloatLiteralExpression, StringLiteralExpression, RegexpLiteralExpression, UnaryExpression, ArrowKind, ParameterKind, DeclarationStatement, AssertionExpression, BinaryExpression, CommaExpression, IntegerLiteralExpression, isTypeOmitted, operatorTokenToString, ForOfStatement, IndexSignatureNode, TemplateLiteralExpression, util, FalseExpression, NullExpression, TrueExpression } from "assemblyscript/dist/assemblyscript.js";
5
- import { Visitor } from "./visitor.js";
6
- import { ThisExpression } from "types:assemblyscript/src/ast";
7
-
8
- function assert<T>(isTruish: T, message: string = "assertion error"): T {
9
- if (!isTruish) throw new Error(message);
10
- return isTruish;
11
- }
12
-
13
- /** An AST builder. */
14
- export class ASTBuilder extends Visitor {
15
- /** Rebuilds the textual source from the specified AST, as far as possible. */
16
- static build(node: Node): string {
17
- var builder = new ASTBuilder();
18
- builder.visitNode(node);
19
- return builder.finish();
20
- }
21
-
22
- private sb: string[] = [];
23
- private indentLevel: number = 0;
24
- visitNode(node: Node) {
25
- return this.visit(node);
26
- }
27
-
28
- visitSource(source: Source): void {
29
- var statements = source.statements;
30
- for (let i = 0, k = statements.length; i < k; ++i) {
31
- this.visitNodeAndTerminate(statements[i]);
32
- }
33
- }
34
-
35
- // types
36
-
37
- visitTypeNode(node: TypeNode): void {
38
- switch (node.kind) {
39
- case NodeKind.NamedType: {
40
- this.visitNamedTypeNode(<NamedTypeNode>node);
41
- break;
42
- }
43
- case NodeKind.FunctionType: {
44
- this.visitFunctionTypeNode(<FunctionTypeNode>node);
45
- break;
46
- }
47
- default:
48
- assert(false);
49
- }
50
- }
51
-
52
- visitThisExpression(node: ThisExpression, ref?: Node | null): void {
53
- this.sb.push("this");
54
- }
55
- visitTypeName(node: TypeName): void {
56
- this.visitIdentifierExpression(node.identifier);
57
- var sb = this.sb;
58
- var current = node.next;
59
- while (current) {
60
- sb.push(".");
61
- this.visitIdentifierExpression(current.identifier);
62
- current = current.next;
63
- }
64
- }
65
-
66
- visitNamedTypeNode(node: NamedTypeNode): void {
67
- this.visitTypeName(node.name);
68
- var typeArguments = node.typeArguments;
69
- if (typeArguments) {
70
- let numTypeArguments = typeArguments.length;
71
- let sb = this.sb;
72
- if (numTypeArguments) {
73
- sb.push("<");
74
- this.visitTypeNode(typeArguments[0]);
75
- for (let i = 1; i < numTypeArguments; ++i) {
76
- sb.push(", ");
77
- this.visitTypeNode(typeArguments[i]);
78
- }
79
- sb.push(">");
80
- }
81
- if (node.isNullable) sb.push(" | null");
82
- }
83
- }
84
-
85
- visitFunctionTypeNode(node: FunctionTypeNode): void {
86
- var isNullable = node.isNullable;
87
- var sb = this.sb;
88
- sb.push(isNullable ? "((" : "(");
89
- var explicitThisType = node.explicitThisType;
90
- if (explicitThisType) {
91
- sb.push("this: ");
92
- this.visitTypeNode(explicitThisType);
93
- }
94
- var parameters = node.parameters;
95
- var numParameters = parameters.length;
96
- if (numParameters) {
97
- if (explicitThisType) sb.push(", ");
98
- this.serializeParameter(parameters[0]);
99
- for (let i = 1; i < numParameters; ++i) {
100
- sb.push(", ");
101
- this.serializeParameter(parameters[i]);
102
- }
103
- }
104
- var returnType = node.returnType;
105
- if (returnType) {
106
- sb.push(") => ");
107
- this.visitTypeNode(returnType);
108
- } else {
109
- sb.push(") => void");
110
- }
111
- if (isNullable) sb.push(") | null");
112
- }
113
-
114
- visitTypeParameter(node: TypeParameterNode): void {
115
- this.visitIdentifierExpression(node.name);
116
- var extendsType = node.extendsType;
117
- if (extendsType) {
118
- this.sb.push(" extends ");
119
- this.visitTypeNode(extendsType);
120
- }
121
- var defaultType = node.defaultType;
122
- if (defaultType) {
123
- this.sb.push("=");
124
- this.visitTypeNode(defaultType);
125
- }
126
- }
127
-
128
- // expressions
129
-
130
- visitIdentifierExpression(node: IdentifierExpression): void {
131
- if (node.isQuoted) this.visitStringLiteral(node.text);
132
- else this.sb.push(node.text);
133
- }
134
-
135
- visitArrayLiteralExpression(node: ArrayLiteralExpression): void {
136
- var sb = this.sb;
137
- sb.push("[");
138
- var elements = node.elementExpressions;
139
- var numElements = elements.length;
140
- if (numElements) {
141
- let element = elements[0];
142
- if (element) this.visitNode(element);
143
- for (let i = 1; i < numElements; ++i) {
144
- element = elements[i];
145
- sb.push(", ");
146
- if (element) this.visitNode(element);
147
- }
148
- }
149
- sb.push("]");
150
- }
151
-
152
- visitObjectLiteralExpression(node: ObjectLiteralExpression): void {
153
- var sb = this.sb;
154
- var names = node.names;
155
- var values = node.values;
156
- var numElements = names.length;
157
- assert(numElements == values.length);
158
- if (numElements) {
159
- sb.push("{\n");
160
- util.indent(sb, ++this.indentLevel);
161
- this.visitNode(names[0]);
162
- sb.push(": ");
163
- this.visitNode(values[0]);
164
- for (let i = 1; i < numElements; ++i) {
165
- sb.push(",\n");
166
- util.indent(sb, this.indentLevel);
167
- let name = names[i];
168
- let value = values[i];
169
- if (name == value) {
170
- this.visitNode(name);
171
- } else {
172
- this.visitNode(name);
173
- sb.push(": ");
174
- this.visitNode(value);
175
- }
176
- }
177
- sb.push("\n");
178
- util.indent(sb, --this.indentLevel);
179
- sb.push("}");
180
- } else {
181
- sb.push("{}");
182
- }
183
- }
184
-
185
- visitAssertionExpression(node: AssertionExpression): void {
186
- var sb = this.sb;
187
- switch (node.assertionKind) {
188
- case AssertionKind.Prefix: {
189
- sb.push("<");
190
- if (node.toType) this.visitTypeNode(node.toType);
191
- sb.push(">");
192
- this.visitNode(node.expression);
193
- break;
194
- }
195
- case AssertionKind.As: {
196
- this.visitNode(node.expression);
197
- sb.push(" as ");
198
- if (node.toType) this.visitTypeNode(node.toType);
199
- break;
200
- }
201
- case AssertionKind.NonNull: {
202
- this.visitNode(node.expression);
203
- sb.push("!");
204
- break;
205
- }
206
- case AssertionKind.Const: {
207
- this.visitNode(node.expression);
208
- sb.push(" as const");
209
- break;
210
- }
211
- default:
212
- assert(false);
213
- }
214
- }
215
-
216
- visitBinaryExpression(node: BinaryExpression): void {
217
- var sb = this.sb;
218
- this.visitNode(node.left);
219
- sb.push(" ");
220
- sb.push(operatorTokenToString(node.operator));
221
- sb.push(" ");
222
- this.visitNode(node.right);
223
- }
224
-
225
- visitCallExpression(node: CallExpression): void {
226
- this.visitNode(node.expression);
227
- this.visitArguments(node.typeArguments, node.args);
228
- }
229
-
230
- visitArguments(typeArguments: TypeNode[] | null, args: Expression[]): void {
231
- var sb = this.sb;
232
- if (typeArguments) {
233
- let numTypeArguments = typeArguments.length;
234
- if (numTypeArguments) {
235
- sb.push("<");
236
- this.visitTypeNode(typeArguments[0]);
237
- for (let i = 1; i < numTypeArguments; ++i) {
238
- sb.push(", ");
239
- this.visitTypeNode(typeArguments[i]);
240
- }
241
- sb.push(">(");
242
- }
243
- } else {
244
- sb.push("(");
245
- }
246
- var numArgs = args.length;
247
- if (numArgs) {
248
- this.visitNode(args[0]);
249
- for (let i = 1; i < numArgs; ++i) {
250
- sb.push(", ");
251
- this.visitNode(args[i]);
252
- }
253
- }
254
- sb.push(")");
255
- }
256
-
257
- visitClassExpression(node: ClassExpression): void {
258
- var declaration = node.declaration;
259
- this.visitClassDeclaration(declaration);
260
- }
261
-
262
- visitCommaExpression(node: CommaExpression): void {
263
- var expressions = node.expressions;
264
- var numExpressions = expressions.length;
265
- this.visitNode(expressions[0]);
266
- var sb = this.sb;
267
- for (let i = 1; i < numExpressions; ++i) {
268
- sb.push(",");
269
- this.visitNode(expressions[i]);
270
- }
271
- }
272
-
273
- visitElementAccessExpression(node: ElementAccessExpression): void {
274
- var sb = this.sb;
275
- this.visitNode(node.expression);
276
- sb.push("[");
277
- this.visitNode(node.elementExpression);
278
- sb.push("]");
279
- }
280
-
281
- visitFunctionExpression(node: FunctionExpression): void {
282
- var declaration = node.declaration;
283
- if (!declaration.arrowKind) {
284
- if (declaration.name.text.length) {
285
- this.sb.push("function ");
286
- } else {
287
- this.sb.push("function");
288
- }
289
- } else {
290
- assert(declaration.name.text.length == 0);
291
- }
292
- this.visitFunctionCommon(declaration);
293
- }
294
-
295
- visitLiteralExpression(node: LiteralExpression): void {
296
- switch (node.literalKind) {
297
- case LiteralKind.Float: {
298
- this.visitFloatLiteralExpression(<FloatLiteralExpression>node);
299
- break;
300
- }
301
- case LiteralKind.Integer: {
302
- this.visitIntegerLiteralExpression(<IntegerLiteralExpression>node);
303
- break;
304
- }
305
- case LiteralKind.String: {
306
- this.visitStringLiteralExpression(<StringLiteralExpression>node);
307
- break;
308
- }
309
- case LiteralKind.Template: {
310
- this.visitTemplateLiteralExpression(<TemplateLiteralExpression>node);
311
- break;
312
- }
313
- case LiteralKind.RegExp: {
314
- this.visitRegexpLiteralExpression(<RegexpLiteralExpression>node);
315
- break;
316
- }
317
- case LiteralKind.Array: {
318
- this.visitArrayLiteralExpression(<ArrayLiteralExpression>node);
319
- break;
320
- }
321
- case LiteralKind.Object: {
322
- this.visitObjectLiteralExpression(<ObjectLiteralExpression>node);
323
- break;
324
- }
325
- default: {
326
- assert(false);
327
- break;
328
- }
329
- }
330
- }
331
-
332
- visitFloatLiteralExpression(node: FloatLiteralExpression): void {
333
- this.sb.push(node.value.toString());
334
- }
335
-
336
- visitInstanceOfExpression(node: InstanceOfExpression): void {
337
- this.visitNode(node.expression);
338
- this.sb.push(" instanceof ");
339
- this.visitTypeNode(node.isType);
340
- }
341
-
342
- visitIntegerLiteralExpression(node: IntegerLiteralExpression): void {
343
- this.sb.push(i64_to_string(node.value));
344
- }
345
-
346
- visitStringLiteral(str: string): void {
347
- var sb = this.sb;
348
- sb.push('"');
349
- this.visitRawString(str, util.CharCode.DoubleQuote);
350
- sb.push('"');
351
- }
352
-
353
- private visitRawString(str: string, quote: util.CharCode): void {
354
- var sb = this.sb;
355
- var off = 0;
356
- var i = 0;
357
- for (let k = str.length; i < k; ) {
358
- switch (str.charCodeAt(i)) {
359
- case util.CharCode.Null: {
360
- if (i > off) sb.push(str.substring(off, (off = i + 1)));
361
- sb.push("\\0");
362
- off = ++i;
363
- break;
364
- }
365
- case util.CharCode.Backslash: {
366
- if (i > off) sb.push(str.substring(off, i));
367
- off = ++i;
368
- sb.push("\\b");
369
- break;
370
- }
371
- case util.CharCode.Tab: {
372
- if (i > off) sb.push(str.substring(off, i));
373
- off = ++i;
374
- sb.push("\\t");
375
- break;
376
- }
377
- case util.CharCode.LineFeed: {
378
- if (i > off) sb.push(str.substring(off, i));
379
- off = ++i;
380
- sb.push("\\n");
381
- break;
382
- }
383
- case util.CharCode.VerticalTab: {
384
- if (i > off) sb.push(str.substring(off, i));
385
- off = ++i;
386
- sb.push("\\v");
387
- break;
388
- }
389
- case util.CharCode.FormFeed: {
390
- if (i > off) sb.push(str.substring(off, i));
391
- off = ++i;
392
- sb.push("\\f");
393
- break;
394
- }
395
- case util.CharCode.CarriageReturn: {
396
- if (i > off) sb.push(str.substring(off, i));
397
- sb.push("\\r");
398
- off = ++i;
399
- break;
400
- }
401
- case util.CharCode.DoubleQuote: {
402
- if (quote == util.CharCode.DoubleQuote) {
403
- if (i > off) sb.push(str.substring(off, i));
404
- sb.push('\\"');
405
- off = ++i;
406
- } else {
407
- ++i;
408
- }
409
- break;
410
- }
411
- case util.CharCode.SingleQuote: {
412
- if (quote == util.CharCode.SingleQuote) {
413
- if (i > off) sb.push(str.substring(off, i));
414
- sb.push("\\'");
415
- off = ++i;
416
- } else {
417
- ++i;
418
- }
419
- break;
420
- }
421
- case util.CharCode.Backslash: {
422
- if (i > off) sb.push(str.substring(off, i));
423
- sb.push("\\\\");
424
- off = ++i;
425
- break;
426
- }
427
- case util.CharCode.Backtick: {
428
- if (quote == util.CharCode.Backtick) {
429
- if (i > off) sb.push(str.substring(off, i));
430
- sb.push("\\`");
431
- off = ++i;
432
- } else {
433
- ++i;
434
- }
435
- break;
436
- }
437
- default: {
438
- ++i;
439
- break;
440
- }
441
- }
442
- }
443
- if (i > off) sb.push(str.substring(off, i));
444
- }
445
-
446
- visitStringLiteralExpression(node: StringLiteralExpression): void {
447
- this.visitStringLiteral(node.value);
448
- }
449
-
450
- visitTemplateLiteralExpression(node: TemplateLiteralExpression): void {
451
- var sb = this.sb;
452
- var tag = node.tag;
453
- var parts = node.parts;
454
- var expressions = node.expressions;
455
- if (tag) this.visitNode(tag);
456
- sb.push("`");
457
- this.visitRawString(parts[0], util.CharCode.Backtick);
458
- assert(parts.length == expressions.length + 1);
459
- for (let i = 0, k = expressions.length; i < k; ++i) {
460
- sb.push("${");
461
- this.visitNode(expressions[i]);
462
- sb.push("}");
463
- this.visitRawString(parts[i + 1], util.CharCode.Backtick);
464
- }
465
- sb.push("`");
466
- }
467
-
468
- visitRegexpLiteralExpression(node: RegexpLiteralExpression): void {
469
- var sb = this.sb;
470
- sb.push("/");
471
- sb.push(node.pattern);
472
- sb.push("/");
473
- sb.push(node.patternFlags);
474
- }
475
-
476
- visitNewExpression(node: NewExpression): void {
477
- this.sb.push("new ");
478
- this.visitTypeName(node.typeName);
479
- this.visitArguments(node.typeArguments, node.args);
480
- }
481
-
482
- visitParenthesizedExpression(node: ParenthesizedExpression): void {
483
- var sb = this.sb;
484
- sb.push("(");
485
- this.visitNode(node.expression);
486
- sb.push(")");
487
- }
488
-
489
- visitPropertyAccessExpression(node: PropertyAccessExpression): void {
490
- this.visitNode(node.expression);
491
- this.sb.push(".");
492
- this.visitIdentifierExpression(node.property);
493
- }
494
-
495
- visitTernaryExpression(node: TernaryExpression): void {
496
- var sb = this.sb;
497
- this.visitNode(node.condition);
498
- sb.push(" ? ");
499
- this.visitNode(node.ifThen);
500
- sb.push(" : ");
501
- this.visitNode(node.ifElse);
502
- }
503
-
504
- visitUnaryExpression(node: UnaryExpression): void {
505
- switch (node.kind) {
506
- case NodeKind.UnaryPostfix: {
507
- this.visitUnaryPostfixExpression(<UnaryPostfixExpression>node);
508
- break;
509
- }
510
- case NodeKind.UnaryPrefix: {
511
- this.visitUnaryPrefixExpression(<UnaryPrefixExpression>node);
512
- break;
513
- }
514
- default:
515
- assert(false);
516
- }
517
- }
518
-
519
- visitUnaryPostfixExpression(node: UnaryPostfixExpression): void {
520
- this.visitNode(node.operand);
521
- this.sb.push(operatorTokenToString(node.operator));
522
- }
523
-
524
- visitUnaryPrefixExpression(node: UnaryPrefixExpression): void {
525
- this.sb.push(operatorTokenToString(node.operator));
526
- this.visitNode(node.operand);
527
- }
528
-
529
- // statements
530
-
531
- visitNodeAndTerminate(node: Node): void {
532
- this.visitNode(node);
533
- var sb = this.sb;
534
- if (
535
- !sb.length || // leading EmptyStatement
536
- node.kind == NodeKind.Variable || // potentially assigns a FunctionExpression
537
- node.kind == NodeKind.Expression // potentially assigns a FunctionExpression
538
- ) {
539
- sb.push(";\n");
540
- } else {
541
- let last = sb[sb.length - 1];
542
- let lastCharPos = last.length - 1;
543
- if (lastCharPos >= 0 && (last.charCodeAt(lastCharPos) == util.CharCode.CloseBrace || last.charCodeAt(lastCharPos) == util.CharCode.Semicolon)) {
544
- sb.push("\n");
545
- } else {
546
- sb.push(";\n");
547
- }
548
- }
549
- }
550
-
551
- visitBlockStatement(node: BlockStatement): void {
552
- var sb = this.sb;
553
- var statements = node.statements;
554
- var numStatements = statements.length;
555
- if (numStatements) {
556
- sb.push("{\n");
557
- let indentLevel = ++this.indentLevel;
558
- for (let i = 0; i < numStatements; ++i) {
559
- util.indent(sb, indentLevel);
560
- this.visitNodeAndTerminate(statements[i]);
561
- }
562
- util.indent(sb, --this.indentLevel);
563
- sb.push("}");
564
- } else {
565
- sb.push("{}");
566
- }
567
- }
568
-
569
- visitBreakStatement(node: BreakStatement): void {
570
- var label = node.label;
571
- if (label) {
572
- this.sb.push("break ");
573
- this.visitIdentifierExpression(label);
574
- } else {
575
- this.sb.push("break");
576
- }
577
- }
578
-
579
- visitContinueStatement(node: ContinueStatement): void {
580
- var label = node.label;
581
- if (label) {
582
- this.sb.push("continue ");
583
- this.visitIdentifierExpression(label);
584
- } else {
585
- this.sb.push("continue");
586
- }
587
- }
588
-
589
- visitClassDeclaration(node: ClassDeclaration, isDefault = false): void {
590
- var decorators = node.decorators;
591
- if (decorators) {
592
- for (let i = 0, k = decorators.length; i < k; ++i) {
593
- this.serializeDecorator(decorators[i]);
594
- }
595
- }
596
- var sb = this.sb;
597
- if (isDefault) {
598
- sb.push("export default ");
599
- } else {
600
- this.serializeExternalModifiers(node);
601
- }
602
- if (node.is(CommonFlags.Abstract)) sb.push("abstract ");
603
- if (node.name.text.length) {
604
- sb.push("class ");
605
- this.visitIdentifierExpression(node.name);
606
- } else {
607
- sb.push("class");
608
- }
609
- var typeParameters = node.typeParameters;
610
- if (typeParameters != null && typeParameters.length > 0) {
611
- sb.push("<");
612
- this.visitTypeParameter(typeParameters[0]);
613
- for (let i = 1, k = typeParameters.length; i < k; ++i) {
614
- sb.push(", ");
615
- this.visitTypeParameter(typeParameters[i]);
616
- }
617
- sb.push(">");
618
- }
619
- var extendsType = node.extendsType;
620
- if (extendsType) {
621
- sb.push(" extends ");
622
- this.visitTypeNode(extendsType);
623
- }
624
- var implementsTypes = node.implementsTypes;
625
- if (implementsTypes) {
626
- let numImplementsTypes = implementsTypes.length;
627
- if (numImplementsTypes) {
628
- sb.push(" implements ");
629
- this.visitTypeNode(implementsTypes[0]);
630
- for (let i = 1; i < numImplementsTypes; ++i) {
631
- sb.push(", ");
632
- this.visitTypeNode(implementsTypes[i]);
633
- }
634
- }
635
- }
636
- var indexSignature = node.indexSignature;
637
- var members = node.members;
638
- var numMembers = members.length;
639
- if (indexSignature !== null || numMembers) {
640
- sb.push(" {\n");
641
- let indentLevel = ++this.indentLevel;
642
- if (indexSignature) {
643
- util.indent(sb, indentLevel);
644
- this.visitNodeAndTerminate(indexSignature);
645
- }
646
- for (let i = 0, k = members.length; i < k; ++i) {
647
- let member = members[i];
648
- if (member.kind != NodeKind.FieldDeclaration || (<FieldDeclaration>member).parameterIndex < 0) {
649
- util.indent(sb, indentLevel);
650
- this.visitNodeAndTerminate(member);
651
- }
652
- }
653
- util.indent(sb, --this.indentLevel);
654
- sb.push("}");
655
- } else {
656
- sb.push(" {}");
657
- }
658
- }
659
-
660
- visitDoStatement(node: DoStatement): void {
661
- var sb = this.sb;
662
- sb.push("do ");
663
- this.visitNode(node.body);
664
- if (node.body.kind == NodeKind.Block) {
665
- sb.push(" while (");
666
- } else {
667
- util.indent(sb, this.indentLevel);
668
- sb.push("while (");
669
- }
670
- this.visitNode(node.condition);
671
- sb.push(")");
672
- }
673
-
674
- visitEmptyStatement(node: EmptyStatement): void {
675
- /* nop */
676
- }
677
-
678
- visitEnumDeclaration(node: EnumDeclaration, isDefault = false): void {
679
- var sb = this.sb;
680
- if (isDefault) {
681
- sb.push("export default ");
682
- } else {
683
- this.serializeExternalModifiers(node);
684
- }
685
- if (node.is(CommonFlags.Const)) sb.push("const ");
686
- sb.push("enum ");
687
- this.visitIdentifierExpression(node.name);
688
- var values = node.values;
689
- var numValues = values.length;
690
- if (numValues) {
691
- sb.push(" {\n");
692
- let indentLevel = ++this.indentLevel;
693
- util.indent(sb, indentLevel);
694
- this.visitEnumValueDeclaration(node.values[0]);
695
- for (let i = 1; i < numValues; ++i) {
696
- sb.push(",\n");
697
- util.indent(sb, indentLevel);
698
- this.visitEnumValueDeclaration(node.values[i]);
699
- }
700
- sb.push("\n");
701
- util.indent(sb, --this.indentLevel);
702
- sb.push("}");
703
- } else {
704
- sb.push(" {}");
705
- }
706
- }
707
-
708
- visitEnumValueDeclaration(node: EnumValueDeclaration): void {
709
- this.visitIdentifierExpression(node.name);
710
- var initializer = node.initializer;
711
- if (initializer) {
712
- this.sb.push(" = ");
713
- this.visitNode(initializer);
714
- }
715
- }
716
-
717
- visitExportImportStatement(node: ExportImportStatement): void {
718
- var sb = this.sb;
719
- sb.push("export import ");
720
- this.visitIdentifierExpression(node.externalName);
721
- sb.push(" = ");
722
- this.visitIdentifierExpression(node.name);
723
- }
724
-
725
- visitExportMember(node: ExportMember): void {
726
- this.visitIdentifierExpression(node.localName);
727
- if (node.exportedName.text != node.localName.text) {
728
- this.sb.push(" as ");
729
- this.visitIdentifierExpression(node.exportedName);
730
- }
731
- }
732
-
733
- visitExportStatement(node: ExportStatement): void {
734
- var sb = this.sb;
735
- if (node.isDeclare) {
736
- sb.push("declare ");
737
- }
738
- var members = node.members;
739
- if (members == null) {
740
- sb.push("export *");
741
- } else if (members.length > 0) {
742
- let numMembers = members.length;
743
- sb.push("export {\n");
744
- let indentLevel = ++this.indentLevel;
745
- util.indent(sb, indentLevel);
746
- this.visitExportMember(members[0]);
747
- for (let i = 1; i < numMembers; ++i) {
748
- sb.push(",\n");
749
- util.indent(sb, indentLevel);
750
- this.visitExportMember(members[i]);
751
- }
752
- --this.indentLevel;
753
- sb.push("\n}");
754
- } else {
755
- sb.push("export {}");
756
- }
757
- var path = node.path;
758
- if (path) {
759
- sb.push(" from ");
760
- this.visitStringLiteralExpression(path);
761
- }
762
- sb.push(";");
763
- }
764
-
765
- visitExportDefaultStatement(node: ExportDefaultStatement): void {
766
- var declaration = node.declaration;
767
- switch (declaration.kind) {
768
- case NodeKind.EnumDeclaration: {
769
- this.visitEnumDeclaration(<EnumDeclaration>declaration, true);
770
- break;
771
- }
772
- case NodeKind.FunctionDeclaration: {
773
- this.visitFunctionDeclaration(<FunctionDeclaration>declaration, true);
774
- break;
775
- }
776
- case NodeKind.ClassDeclaration: {
777
- this.visitClassDeclaration(<ClassDeclaration>declaration, true);
778
- break;
779
- }
780
- case NodeKind.InterfaceDeclaration: {
781
- this.visitInterfaceDeclaration(<InterfaceDeclaration>declaration, true);
782
- break;
783
- }
784
- case NodeKind.NamespaceDeclaration: {
785
- this.visitNamespaceDeclaration(<NamespaceDeclaration>declaration, true);
786
- break;
787
- }
788
- default:
789
- assert(false);
790
- }
791
- }
792
-
793
- visitExpressionStatement(node: ExpressionStatement): void {
794
- this.visitNode(node.expression);
795
- }
796
-
797
- visitFieldDeclaration(node: FieldDeclaration): void {
798
- var decorators = node.decorators;
799
- if (decorators) {
800
- for (let i = 0, k = decorators.length; i < k; ++i) {
801
- this.serializeDecorator(decorators[i]);
802
- }
803
- }
804
- this.serializeAccessModifiers(node);
805
- this.visitIdentifierExpression(node.name);
806
- var sb = this.sb;
807
- if (node.flags & CommonFlags.DefinitelyAssigned) {
808
- sb.push("!");
809
- }
810
- var type = node.type;
811
- if (type) {
812
- sb.push(": ");
813
- this.visitTypeNode(type);
814
- }
815
- var initializer = node.initializer;
816
- if (initializer) {
817
- sb.push(" = ");
818
- this.visitNode(initializer);
819
- }
820
- }
821
-
822
- visitForStatement(node: ForStatement): void {
823
- var sb = this.sb;
824
- sb.push("for (");
825
- var initializer = node.initializer;
826
- if (initializer) {
827
- this.visitNode(initializer);
828
- }
829
- var condition = node.condition;
830
- if (condition) {
831
- sb.push("; ");
832
- this.visitNode(condition);
833
- } else {
834
- sb.push(";");
835
- }
836
- var incrementor = node.incrementor;
837
- if (incrementor) {
838
- sb.push("; ");
839
- this.visitNode(incrementor);
840
- } else {
841
- sb.push(";");
842
- }
843
- sb.push(") ");
844
- this.visitNode(node.body);
845
- }
846
-
847
- visitForOfStatement(node: ForOfStatement): void {
848
- var sb = this.sb;
849
- sb.push("for (");
850
- this.visitNode(node.variable);
851
- sb.push(" of ");
852
- this.visitNode(node.iterable);
853
- sb.push(") ");
854
- this.visitNode(node.body);
855
- }
856
-
857
- visitFunctionDeclaration(node: FunctionDeclaration, isDefault = false): void {
858
- var sb = this.sb;
859
- var decorators = node.decorators;
860
- if (decorators) {
861
- for (let i = 0, k = decorators.length; i < k; ++i) {
862
- this.serializeDecorator(decorators[i]);
863
- }
864
- }
865
- if (isDefault) {
866
- sb.push("export default ");
867
- } else {
868
- this.serializeExternalModifiers(node);
869
- this.serializeAccessModifiers(node);
870
- }
871
- if (node.name.text.length) {
872
- sb.push("function ");
873
- } else {
874
- sb.push("function");
875
- }
876
- this.visitFunctionCommon(node);
877
- }
878
-
879
- visitFunctionCommon(node: FunctionDeclaration): void {
880
- var sb = this.sb;
881
- this.visitIdentifierExpression(node.name);
882
- var signature = node.signature;
883
- var typeParameters = node.typeParameters;
884
- if (typeParameters) {
885
- let numTypeParameters = typeParameters.length;
886
- if (numTypeParameters) {
887
- sb.push("<");
888
- this.visitTypeParameter(typeParameters[0]);
889
- for (let i = 1; i < numTypeParameters; ++i) {
890
- sb.push(", ");
891
- this.visitTypeParameter(typeParameters[i]);
892
- }
893
- sb.push(">");
894
- }
895
- }
896
- if (node.arrowKind == ArrowKind.Single) {
897
- let parameters = signature.parameters;
898
- assert(parameters.length == 1);
899
- assert(!signature.explicitThisType);
900
- this.serializeParameter(parameters[0]);
901
- } else {
902
- sb.push("(");
903
- let parameters = signature.parameters;
904
- let numParameters = parameters.length;
905
- let explicitThisType = signature.explicitThisType;
906
- if (explicitThisType) {
907
- sb.push("this: ");
908
- this.visitTypeNode(explicitThisType);
909
- }
910
- if (numParameters) {
911
- if (explicitThisType) sb.push(", ");
912
- this.serializeParameter(parameters[0]);
913
- for (let i = 1; i < numParameters; ++i) {
914
- sb.push(", ");
915
- this.serializeParameter(parameters[i]);
916
- }
917
- }
918
- }
919
- var body = node.body;
920
- var returnType = signature.returnType;
921
- if (node.arrowKind) {
922
- if (body) {
923
- if (node.arrowKind == ArrowKind.Single) {
924
- assert(isTypeOmitted(returnType));
925
- } else {
926
- if (isTypeOmitted(returnType)) {
927
- sb.push(")");
928
- } else {
929
- sb.push("): ");
930
- this.visitTypeNode(returnType);
931
- }
932
- }
933
- sb.push(" => ");
934
- this.visitNode(body);
935
- } else {
936
- assert(!isTypeOmitted(returnType));
937
- sb.push(" => ");
938
- this.visitTypeNode(returnType);
939
- }
940
- } else {
941
- if (!isTypeOmitted(returnType) && !node.isAny(CommonFlags.Constructor | CommonFlags.Set)) {
942
- sb.push("): ");
943
- this.visitTypeNode(returnType);
944
- } else {
945
- sb.push(")");
946
- }
947
- if (body) {
948
- sb.push(" ");
949
- this.visitNode(body);
950
- }
951
- }
952
- }
953
-
954
- visitIfStatement(node: IfStatement): void {
955
- var sb = this.sb;
956
- sb.push("if (");
957
- this.visitNode(node.condition);
958
- sb.push(") ");
959
- var ifTrue = node.ifTrue;
960
- this.visitNode(ifTrue);
961
- if (ifTrue.kind != NodeKind.Block) {
962
- sb.push(";\n");
963
- }
964
- var ifFalse = node.ifFalse;
965
- if (ifFalse) {
966
- if (ifTrue.kind == NodeKind.Block) {
967
- sb.push(" else ");
968
- } else {
969
- sb.push("else ");
970
- }
971
- this.visitNode(ifFalse);
972
- }
973
- }
974
-
975
- visitImportDeclaration(node: ImportDeclaration): void {
976
- var externalName = node.foreignName;
977
- var name = node.name;
978
- this.visitIdentifierExpression(externalName);
979
- if (externalName.text != name.text) {
980
- this.sb.push(" as ");
981
- this.visitIdentifierExpression(name);
982
- }
983
- }
984
-
985
- visitImportStatement(node: ImportStatement): void {
986
- var sb = this.sb;
987
- sb.push("import ");
988
- var declarations = node.declarations;
989
- var namespaceName = node.namespaceName;
990
- if (declarations) {
991
- let numDeclarations = declarations.length;
992
- if (numDeclarations) {
993
- sb.push("{\n");
994
- let indentLevel = ++this.indentLevel;
995
- util.indent(sb, indentLevel);
996
- this.visitImportDeclaration(declarations[0]);
997
- for (let i = 1; i < numDeclarations; ++i) {
998
- sb.push(",\n");
999
- util.indent(sb, indentLevel);
1000
- this.visitImportDeclaration(declarations[i]);
1001
- }
1002
- --this.indentLevel;
1003
- sb.push("\n} from ");
1004
- } else {
1005
- sb.push("{} from ");
1006
- }
1007
- } else if (namespaceName) {
1008
- sb.push("* as ");
1009
- this.visitIdentifierExpression(namespaceName);
1010
- sb.push(" from ");
1011
- }
1012
- this.visitStringLiteralExpression(node.path);
1013
- }
1014
-
1015
- visitIndexSignature(node: IndexSignatureNode): void {
1016
- var sb = this.sb;
1017
- sb.push("[key: ");
1018
- this.visitTypeNode(node.keyType);
1019
- sb.push("]: ");
1020
- this.visitTypeNode(node.valueType);
1021
- }
1022
-
1023
- visitInterfaceDeclaration(node: InterfaceDeclaration, isDefault = false): void {
1024
- var decorators = node.decorators;
1025
- if (decorators) {
1026
- for (let i = 0, k = decorators.length; i < k; ++i) {
1027
- this.serializeDecorator(decorators[i]);
1028
- }
1029
- }
1030
- var sb = this.sb;
1031
- if (isDefault) {
1032
- sb.push("export default ");
1033
- } else {
1034
- this.serializeExternalModifiers(node);
1035
- }
1036
- sb.push("interface ");
1037
- this.visitIdentifierExpression(node.name);
1038
- var typeParameters = node.typeParameters;
1039
- if (typeParameters != null && typeParameters.length > 0) {
1040
- sb.push("<");
1041
- this.visitTypeParameter(typeParameters[0]);
1042
- for (let i = 1, k = typeParameters.length; i < k; ++i) {
1043
- sb.push(", ");
1044
- this.visitTypeParameter(typeParameters[i]);
1045
- }
1046
- sb.push(">");
1047
- }
1048
- var extendsType = node.extendsType;
1049
- if (extendsType) {
1050
- sb.push(" extends ");
1051
- this.visitTypeNode(extendsType);
1052
- }
1053
- // must not have implementsTypes
1054
- sb.push(" {\n");
1055
- var indentLevel = ++this.indentLevel;
1056
- var members = node.members;
1057
- for (let i = 0, k = members.length; i < k; ++i) {
1058
- util.indent(sb, indentLevel);
1059
- this.visitNodeAndTerminate(members[i]);
1060
- }
1061
- --this.indentLevel;
1062
- sb.push("}");
1063
- }
1064
-
1065
- visitMethodDeclaration(node: MethodDeclaration): void {
1066
- var decorators = node.decorators;
1067
- if (decorators) {
1068
- for (let i = 0, k = decorators.length; i < k; ++i) {
1069
- this.serializeDecorator(decorators[i]);
1070
- }
1071
- }
1072
- this.serializeAccessModifiers(node);
1073
- if (node.is(CommonFlags.Get)) {
1074
- this.sb.push("get ");
1075
- } else if (node.is(CommonFlags.Set)) {
1076
- this.sb.push("set ");
1077
- }
1078
- this.visitFunctionCommon(node);
1079
- }
1080
-
1081
- visitNamespaceDeclaration(node: NamespaceDeclaration, isDefault = false): void {
1082
- var decorators = node.decorators;
1083
- if (decorators) {
1084
- for (let i = 0, k = decorators.length; i < k; ++i) {
1085
- this.serializeDecorator(decorators[i]);
1086
- }
1087
- }
1088
- var sb = this.sb;
1089
- if (isDefault) {
1090
- sb.push("export default ");
1091
- } else {
1092
- this.serializeExternalModifiers(node);
1093
- }
1094
- sb.push("namespace ");
1095
- this.visitIdentifierExpression(node.name);
1096
- var members = node.members;
1097
- var numMembers = members.length;
1098
- if (numMembers) {
1099
- sb.push(" {\n");
1100
- let indentLevel = ++this.indentLevel;
1101
- for (let i = 0, k = members.length; i < k; ++i) {
1102
- util.indent(sb, indentLevel);
1103
- this.visitNodeAndTerminate(members[i]);
1104
- }
1105
- util.indent(sb, --this.indentLevel);
1106
- sb.push("}");
1107
- } else {
1108
- sb.push(" {}");
1109
- }
1110
- }
1111
-
1112
- visitReturnStatement(node: ReturnStatement): void {
1113
- var value = node.value;
1114
- if (value) {
1115
- this.sb.push("return ");
1116
- this.visitNode(value);
1117
- } else {
1118
- this.sb.push("return");
1119
- }
1120
- }
1121
-
1122
- visitTrueExpression(node: TrueExpression): void {
1123
- this.sb.push("true");
1124
- }
1125
-
1126
- visitFalseExpression(node: FalseExpression): void {
1127
- this.sb.push("false");
1128
- }
1129
- visitNullExpression(node: NullExpression): void {
1130
- this.sb.push("null");
1131
- }
1132
- visitSwitchCase(node: SwitchCase): void {
1133
- var sb = this.sb;
1134
- var label = node.label;
1135
- if (label) {
1136
- sb.push("case ");
1137
- this.visitNode(label);
1138
- sb.push(":\n");
1139
- } else {
1140
- sb.push("default:\n");
1141
- }
1142
- var statements = node.statements;
1143
- var numStatements = statements.length;
1144
- if (numStatements) {
1145
- let indentLevel = ++this.indentLevel;
1146
- util.indent(sb, indentLevel);
1147
- this.visitNodeAndTerminate(statements[0]);
1148
- for (let i = 1; i < numStatements; ++i) {
1149
- util.indent(sb, indentLevel);
1150
- this.visitNodeAndTerminate(statements[i]);
1151
- }
1152
- --this.indentLevel;
1153
- }
1154
- }
1155
-
1156
- visitSwitchStatement(node: SwitchStatement): void {
1157
- var sb = this.sb;
1158
- sb.push("switch (");
1159
- this.visitNode(node.condition);
1160
- sb.push(") {\n");
1161
- var indentLevel = ++this.indentLevel;
1162
- var cases = node.cases;
1163
- for (let i = 0, k = cases.length; i < k; ++i) {
1164
- util.indent(sb, indentLevel);
1165
- this.visitSwitchCase(cases[i]);
1166
- sb.push("\n");
1167
- }
1168
- --this.indentLevel;
1169
- sb.push("}");
1170
- }
1171
-
1172
- visitThrowStatement(node: ThrowStatement): void {
1173
- this.sb.push("throw ");
1174
- this.visitNode(node.value);
1175
- }
1176
-
1177
- visitTryStatement(node: TryStatement): void {
1178
- var sb = this.sb;
1179
- sb.push("try {\n");
1180
- var indentLevel = ++this.indentLevel;
1181
- var statements = node.bodyStatements;
1182
- for (let i = 0, k = statements.length; i < k; ++i) {
1183
- util.indent(sb, indentLevel);
1184
- this.visitNodeAndTerminate(statements[i]);
1185
- }
1186
- var catchVariable = node.catchVariable;
1187
- if (catchVariable) {
1188
- util.indent(sb, indentLevel - 1);
1189
- sb.push("} catch (");
1190
- this.visitIdentifierExpression(catchVariable);
1191
- sb.push(") {\n");
1192
- let catchStatements = node.catchStatements;
1193
- if (catchStatements) {
1194
- for (let i = 0, k = catchStatements.length; i < k; ++i) {
1195
- util.indent(sb, indentLevel);
1196
- this.visitNodeAndTerminate(catchStatements[i]);
1197
- }
1198
- }
1199
- }
1200
- var finallyStatements = node.finallyStatements;
1201
- if (finallyStatements) {
1202
- util.indent(sb, indentLevel - 1);
1203
- sb.push("} finally {\n");
1204
- for (let i = 0, k = finallyStatements.length; i < k; ++i) {
1205
- util.indent(sb, indentLevel);
1206
- this.visitNodeAndTerminate(finallyStatements[i]);
1207
- }
1208
- }
1209
- util.indent(sb, indentLevel - 1);
1210
- sb.push("}");
1211
- }
1212
-
1213
- visitTypeDeclaration(node: TypeDeclaration): void {
1214
- var decorators = node.decorators;
1215
- if (decorators) {
1216
- for (let i = 0, k = decorators.length; i < k; ++i) {
1217
- this.serializeDecorator(decorators[i]);
1218
- }
1219
- }
1220
- var sb = this.sb;
1221
- this.serializeExternalModifiers(node);
1222
- sb.push("type ");
1223
- this.visitIdentifierExpression(node.name);
1224
- var typeParameters = node.typeParameters;
1225
- if (typeParameters) {
1226
- let numTypeParameters = typeParameters.length;
1227
- if (numTypeParameters) {
1228
- sb.push("<");
1229
- for (let i = 0; i < numTypeParameters; ++i) {
1230
- this.visitTypeParameter(typeParameters[i]);
1231
- }
1232
- sb.push(">");
1233
- }
1234
- }
1235
- sb.push(" = ");
1236
- this.visitTypeNode(node.type);
1237
- }
1238
-
1239
- visitVariableDeclaration(node: VariableDeclaration): void {
1240
- this.visitIdentifierExpression(node.name);
1241
- var type = node.type;
1242
- var sb = this.sb;
1243
- if (node.flags & CommonFlags.DefinitelyAssigned) {
1244
- sb.push("!");
1245
- }
1246
- if (type) {
1247
- sb.push(": ");
1248
- this.visitTypeNode(type);
1249
- }
1250
- var initializer = node.initializer;
1251
- if (initializer) {
1252
- sb.push(" = ");
1253
- this.visitNode(initializer);
1254
- }
1255
- }
1256
-
1257
- visitVariableStatement(node: VariableStatement): void {
1258
- var decorators = node.decorators;
1259
- if (decorators) {
1260
- for (let i = 0, k = decorators.length; i < k; ++i) {
1261
- this.serializeDecorator(decorators[i]);
1262
- }
1263
- }
1264
- var sb = this.sb;
1265
- var declarations = node.declarations;
1266
- var numDeclarations = declarations.length;
1267
- var firstDeclaration = declarations[0];
1268
- this.serializeExternalModifiers(firstDeclaration);
1269
- sb.push(firstDeclaration.is(CommonFlags.Const) ? "const " : firstDeclaration.is(CommonFlags.Let) ? "let " : "var ");
1270
- this.visitVariableDeclaration(node.declarations[0]);
1271
- for (let i = 1; i < numDeclarations; ++i) {
1272
- sb.push(", ");
1273
- this.visitVariableDeclaration(node.declarations[i]);
1274
- }
1275
- }
1276
-
1277
- visitWhileStatement(node: WhileStatement): void {
1278
- var sb = this.sb;
1279
- sb.push("while (");
1280
- this.visitNode(node.condition);
1281
- var statement = node.body;
1282
- if (statement.kind == NodeKind.Empty) {
1283
- sb.push(")");
1284
- } else {
1285
- sb.push(") ");
1286
- this.visitNode(node.body);
1287
- }
1288
- }
1289
-
1290
- // other
1291
-
1292
- serializeDecorator(node: DecoratorNode): void {
1293
- var sb = this.sb;
1294
- sb.push("@");
1295
- this.visitNode(node.name);
1296
- var args = node.args;
1297
- if (args) {
1298
- sb.push("(");
1299
- let numArgs = args.length;
1300
- if (numArgs) {
1301
- this.visitNode(args[0]);
1302
- for (let i = 1; i < numArgs; ++i) {
1303
- sb.push(", ");
1304
- this.visitNode(args[i]);
1305
- }
1306
- }
1307
- sb.push(")\n");
1308
- } else {
1309
- sb.push("\n");
1310
- }
1311
- util.indent(sb, this.indentLevel);
1312
- }
1313
-
1314
- serializeParameter(node: ParameterNode): void {
1315
- var sb = this.sb;
1316
- var kind = node.parameterKind;
1317
- var implicitFieldDeclaration = node.implicitFieldDeclaration;
1318
- if (implicitFieldDeclaration) {
1319
- this.serializeAccessModifiers(implicitFieldDeclaration);
1320
- }
1321
- if (kind == ParameterKind.Rest) {
1322
- sb.push("...");
1323
- }
1324
- this.visitIdentifierExpression(node.name);
1325
- var type = node.type;
1326
- var initializer = node.initializer;
1327
- if (type) {
1328
- if (kind == ParameterKind.Optional && !initializer) sb.push("?");
1329
- if (!isTypeOmitted(type)) {
1330
- sb.push(": ");
1331
- this.visitTypeNode(type);
1332
- }
1333
- }
1334
- if (initializer) {
1335
- sb.push(" = ");
1336
- this.visitNode(initializer);
1337
- }
1338
- }
1339
-
1340
- serializeExternalModifiers(node: DeclarationStatement): void {
1341
- var sb = this.sb;
1342
- if (node.is(CommonFlags.Export)) {
1343
- sb.push("export ");
1344
- } else if (node.is(CommonFlags.Import)) {
1345
- sb.push("import ");
1346
- } else if (node.is(CommonFlags.Declare)) {
1347
- sb.push("declare ");
1348
- }
1349
- }
1350
-
1351
- serializeAccessModifiers(node: DeclarationStatement): void {
1352
- var sb = this.sb;
1353
- if (node.is(CommonFlags.Public)) {
1354
- sb.push("public ");
1355
- } else if (node.is(CommonFlags.Private)) {
1356
- sb.push("private ");
1357
- } else if (node.is(CommonFlags.Protected)) {
1358
- sb.push("protected ");
1359
- }
1360
- if (node.is(CommonFlags.Static)) {
1361
- sb.push("static ");
1362
- } else if (node.is(CommonFlags.Abstract)) {
1363
- sb.push("abstract ");
1364
- }
1365
- if (node.is(CommonFlags.Readonly)) {
1366
- sb.push("readonly ");
1367
- }
1368
- }
1369
-
1370
- finish(): string {
1371
- var ret = this.sb.join("");
1372
- this.sb = [];
1373
- return ret;
1374
- }
1375
- }