@sdkgen/parser 2.3.2 → 2.5.0

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 (72) hide show
  1. package/dist/{src/ast.d.ts → ast.d.ts} +4 -0
  2. package/dist/{src/ast.js → ast.js} +10 -13
  3. package/dist/compatibility/index.js +210 -0
  4. package/dist/helpers.js +24 -0
  5. package/dist/index.js +17 -0
  6. package/dist/{src/json.d.ts → json.d.ts} +3 -0
  7. package/dist/json.js +235 -0
  8. package/dist/{src/lexer.js → lexer.js} +26 -25
  9. package/dist/parser.js +430 -0
  10. package/dist/restparser.js +63 -0
  11. package/dist/semantic/01_check_multiple_declaration.js +23 -0
  12. package/dist/semantic/02_give_struct_and_enum_names.js +62 -0
  13. package/dist/semantic/03_match_type_definitions.js +19 -0
  14. package/dist/semantic/04_check_dont_return_secret.js +43 -0
  15. package/dist/semantic/05_expand_spreads.js +61 -0
  16. package/dist/semantic/06_check_empty_struct_or_enum.js +22 -0
  17. package/dist/semantic/07_validate_recursive_types.js +50 -0
  18. package/dist/semantic/08_collect_struct_and_enum_types.js +26 -0
  19. package/dist/{src/semantic → semantic}/09_validate_annotations.js +4 -0
  20. package/dist/semantic/10_check_duplicate_members_on_enum.js +17 -0
  21. package/dist/{src/semantic → semantic}/analyser.js +1 -0
  22. package/dist/semantic/transformer.js +51 -0
  23. package/dist/semantic/visitor.js +66 -0
  24. package/dist/{src/token.js → token.js} +1 -0
  25. package/dist/utils.js +28 -0
  26. package/package.json +24 -8
  27. package/.eslintrc.json +0 -3
  28. package/.prettierrc +0 -12
  29. package/dist/spec/lexer.spec.d.ts +0 -1
  30. package/dist/spec/lexer.spec.js +0 -152
  31. package/dist/spec/parser.spec.d.ts +0 -1
  32. package/dist/spec/parser.spec.js +0 -967
  33. package/dist/src/compatibility/index.js +0 -211
  34. package/dist/src/helpers.js +0 -23
  35. package/dist/src/index.js +0 -29
  36. package/dist/src/json.js +0 -234
  37. package/dist/src/parser.js +0 -423
  38. package/dist/src/restparser.js +0 -65
  39. package/dist/src/semantic/01_check_multiple_declaration.js +0 -22
  40. package/dist/src/semantic/02_give_struct_and_enum_names.js +0 -61
  41. package/dist/src/semantic/03_match_type_definitions.js +0 -18
  42. package/dist/src/semantic/04_check_dont_return_secret.js +0 -42
  43. package/dist/src/semantic/05_expand_spreads.js +0 -60
  44. package/dist/src/semantic/06_check_empty_struct_or_enum.js +0 -21
  45. package/dist/src/semantic/07_validate_recursive_types.js +0 -49
  46. package/dist/src/semantic/08_collect_struct_and_enum_types.js +0 -25
  47. package/dist/src/semantic/10_check_duplicate_members_on_enum.js +0 -16
  48. package/dist/src/semantic/transformer.js +0 -50
  49. package/dist/src/semantic/visitor.js +0 -65
  50. package/dist/src/utils.js +0 -27
  51. package/dist/tsconfig.tsbuildinfo +0 -1
  52. /package/dist/{src/compatibility → compatibility}/index.d.ts +0 -0
  53. /package/dist/{src/helpers.d.ts → helpers.d.ts} +0 -0
  54. /package/dist/{src/index.d.ts → index.d.ts} +0 -0
  55. /package/dist/{src/lexer.d.ts → lexer.d.ts} +0 -0
  56. /package/dist/{src/parser.d.ts → parser.d.ts} +0 -0
  57. /package/dist/{src/restparser.d.ts → restparser.d.ts} +0 -0
  58. /package/dist/{src/semantic → semantic}/01_check_multiple_declaration.d.ts +0 -0
  59. /package/dist/{src/semantic → semantic}/02_give_struct_and_enum_names.d.ts +0 -0
  60. /package/dist/{src/semantic → semantic}/03_match_type_definitions.d.ts +0 -0
  61. /package/dist/{src/semantic → semantic}/04_check_dont_return_secret.d.ts +0 -0
  62. /package/dist/{src/semantic → semantic}/05_expand_spreads.d.ts +0 -0
  63. /package/dist/{src/semantic → semantic}/06_check_empty_struct_or_enum.d.ts +0 -0
  64. /package/dist/{src/semantic → semantic}/07_validate_recursive_types.d.ts +0 -0
  65. /package/dist/{src/semantic → semantic}/08_collect_struct_and_enum_types.d.ts +0 -0
  66. /package/dist/{src/semantic → semantic}/09_validate_annotations.d.ts +0 -0
  67. /package/dist/{src/semantic → semantic}/10_check_duplicate_members_on_enum.d.ts +0 -0
  68. /package/dist/{src/semantic → semantic}/analyser.d.ts +0 -0
  69. /package/dist/{src/semantic → semantic}/transformer.d.ts +0 -0
  70. /package/dist/{src/semantic → semantic}/visitor.d.ts +0 -0
  71. /package/dist/{src/token.d.ts → token.d.ts} +0 -0
  72. /package/dist/{src/utils.d.ts → utils.d.ts} +0 -0
@@ -1,967 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const json_1 = require("../src/json");
4
- const lexer_1 = require("../src/lexer");
5
- const parser_1 = require("../src/parser");
6
- function expectParses(source, json, warnings = []) {
7
- const parser = new parser_1.Parser(new lexer_1.Lexer(source));
8
- const ast = parser.parse();
9
- expect(ast.warnings).toEqual(warnings);
10
- expect((0, json_1.astToJson)(ast)).toEqual(json);
11
- expect((0, json_1.astToJson)(ast)).toEqual((0, json_1.astToJson)((0, json_1.jsonToAst)((0, json_1.astToJson)(ast))));
12
- }
13
- function expectDoesntParse(source, message) {
14
- const parser = new parser_1.Parser(new lexer_1.Lexer(source));
15
- expect(() => parser.parse()).toThrowError(message);
16
- }
17
- describe(parser_1.Parser, () => {
18
- for (const p of lexer_1.Lexer.PRIMITIVES) {
19
- test(`handles primitive type '${p}'`, () => {
20
- expectParses(`
21
- type Foo {
22
- foo: ${p}
23
- }
24
- `, {
25
- annotations: {},
26
- errors: ["Fatal"],
27
- functionTable: {},
28
- typeTable: {
29
- Foo: {
30
- foo: p,
31
- },
32
- },
33
- });
34
- });
35
- }
36
- for (const kw of lexer_1.Lexer.KEYWORDS) {
37
- test(`handles '${kw}' on the name of a field`, () => {
38
- expectParses(`
39
- type Foo {
40
- ${kw}: int
41
- }
42
- `, {
43
- annotations: {},
44
- errors: ["Fatal"],
45
- functionTable: {},
46
- typeTable: {
47
- Foo: {
48
- [kw]: "int",
49
- },
50
- },
51
- });
52
- });
53
- }
54
- test("handles arrays and optionals", () => {
55
- expectParses(`
56
- type Foo {
57
- aa: string[]
58
- bbb: int?[]??
59
- cccc: int[][][]
60
- ddddd: uint[][][]??[]???[][]
61
- }
62
- `, {
63
- annotations: {},
64
- errors: ["Fatal"],
65
- functionTable: {},
66
- typeTable: {
67
- Foo: {
68
- aa: "string[]",
69
- bbb: "int?[]??",
70
- cccc: "int[][][]",
71
- ddddd: "uint[][][]??[]???[][]",
72
- },
73
- },
74
- });
75
- });
76
- test("handles enums", () => {
77
- expectParses(`
78
- type Foo {
79
- a: int
80
- status: enum {
81
- c a zzz
82
- }
83
- }
84
-
85
- type Other enum { aa bb }
86
- `, {
87
- annotations: {},
88
- errors: ["Fatal"],
89
- functionTable: {},
90
- typeTable: {
91
- Foo: {
92
- a: "int",
93
- status: "FooStatus",
94
- },
95
- FooStatus: ["c", "a", "zzz"],
96
- Other: ["aa", "bb"],
97
- },
98
- });
99
- });
100
- test("handles errors", () => {
101
- expectParses(`
102
- error Foo
103
- error Bar {
104
- foo: string
105
- }
106
- error FooBar int
107
- `, {
108
- annotations: {},
109
- errors: ["Foo", ["Bar", "BarData"], ["FooBar", "int"], "Fatal"],
110
- functionTable: {},
111
- typeTable: {
112
- BarData: {
113
- foo: "string",
114
- },
115
- },
116
- });
117
- });
118
- test("handles combinations of all part", () => {
119
- expectParses(`
120
- error Foo
121
- error Bar
122
-
123
- type Baz {
124
- a: string?
125
- b: int
126
- }
127
-
128
- fn getBaz(): Baz
129
- `, {
130
- annotations: {},
131
- errors: ["Foo", "Bar", "Fatal"],
132
- functionTable: {
133
- getBaz: {
134
- args: {},
135
- ret: "Baz",
136
- },
137
- },
138
- typeTable: {
139
- Baz: {
140
- a: "string?",
141
- b: "int",
142
- },
143
- },
144
- });
145
- });
146
- test("fails when struct or enum is empty", () => {
147
- expectDoesntParse(`
148
- type Baz {
149
- }
150
- `, "empty");
151
- expectDoesntParse(`
152
- type Baaz enum {
153
- }
154
- `, "empty");
155
- });
156
- test("fails when field happens twice", () => {
157
- expectDoesntParse(`
158
- type Baz {
159
- a: int
160
- b: bool
161
- a: int
162
- }
163
- `, "redeclare");
164
- expectDoesntParse(`
165
- type Baz {
166
- b: int
167
- xx: bool
168
- xx: int
169
- }
170
- `, "redeclare");
171
- expectDoesntParse(`
172
- fn foo(a: string, a: int)
173
- `, "redeclare");
174
- });
175
- test("handles spreads in structs", () => {
176
- expectParses(`
177
- type Bar {
178
- aa: string
179
- }
180
-
181
- type Foo {
182
- ...Bar
183
- bb: int
184
- }
185
- `, {
186
- annotations: {},
187
- errors: ["Fatal"],
188
- functionTable: {},
189
- typeTable: {
190
- Bar: {
191
- aa: "string",
192
- },
193
- Foo: {
194
- aa: "string",
195
- bb: "int",
196
- },
197
- },
198
- });
199
- expectParses(`
200
- type Bar {
201
- aa: string
202
- }
203
-
204
- type Foo {
205
- ...Bar
206
- aa: int
207
- }
208
- `, {
209
- annotations: {},
210
- errors: ["Fatal"],
211
- functionTable: {},
212
- typeTable: {
213
- Bar: {
214
- aa: "string",
215
- },
216
- Foo: {
217
- aa: "int",
218
- },
219
- },
220
- });
221
- expectParses(`
222
- type Bar {
223
- aa: string
224
- }
225
-
226
- type Foo {
227
- ...Bar
228
- aa: int
229
- ...Bar
230
- }
231
- `, {
232
- annotations: {},
233
- errors: ["Fatal"],
234
- functionTable: {},
235
- typeTable: {
236
- Bar: {
237
- aa: "string",
238
- },
239
- Foo: {
240
- aa: "string",
241
- },
242
- },
243
- });
244
- expectParses(`
245
- type Test {
246
- foo: enum { bar baz }
247
- }
248
-
249
- type Test2 {
250
- ...Test
251
- }
252
- `, {
253
- annotations: {},
254
- errors: ["Fatal"],
255
- functionTable: {},
256
- typeTable: {
257
- Test: {
258
- foo: "TestFoo",
259
- },
260
- Test2: {
261
- foo: "TestFoo",
262
- },
263
- TestFoo: ["bar", "baz"],
264
- },
265
- });
266
- });
267
- test("handles functions with arguments", () => {
268
- expectParses(`
269
- type Bar {
270
- aa: string
271
- }
272
-
273
- fn doIt(foo: int, bar: Bar): string
274
- `, {
275
- annotations: {},
276
- errors: ["Fatal"],
277
- functionTable: {
278
- doIt: {
279
- args: {
280
- bar: "Bar",
281
- foo: "int",
282
- },
283
- ret: "string",
284
- },
285
- },
286
- typeTable: {
287
- Bar: {
288
- aa: "string",
289
- },
290
- },
291
- });
292
- });
293
- test("handles spreads in function arguments", () => {
294
- expectParses(`
295
- type Bar {
296
- aa: string
297
- }
298
-
299
- fn doIt(foo: int, ...Bar): string
300
- `, {
301
- annotations: {},
302
- errors: ["Fatal"],
303
- functionTable: {
304
- doIt: {
305
- args: {
306
- aa: "string",
307
- foo: "int",
308
- },
309
- ret: "string",
310
- },
311
- },
312
- typeTable: {
313
- Bar: {
314
- aa: "string",
315
- },
316
- },
317
- });
318
- expectDoesntParse(`
319
- type Foo {
320
- a: int !secret
321
- }
322
-
323
- type Bar {
324
- ...Foo
325
- }
326
-
327
- fn Poo(): Bar
328
- `, "secret");
329
- });
330
- test("handles functions with annotations", () => {
331
- expectParses(`
332
- @description does it
333
- @description and does another \\
334
- thing too
335
- @arg bar Represents the number of things
336
- fn doIt(foo: int, bar: float): string
337
- `, {
338
- annotations: {
339
- "fn.doIt": [
340
- { type: "description", value: "does it" },
341
- { type: "description", value: "and does another thing too" },
342
- ],
343
- "fn.doIt.bar": [{ type: "description", value: "Represents the number of things" }],
344
- },
345
- errors: ["Fatal"],
346
- functionTable: {
347
- doIt: {
348
- args: {
349
- bar: "float",
350
- foo: "int",
351
- },
352
- ret: "string",
353
- },
354
- },
355
- typeTable: {},
356
- });
357
- expectParses(`
358
- error NotFound
359
- error InvalidArgument
360
-
361
- @throws NotFound
362
- @throws InvalidArgument
363
- fn doIt(): string
364
- fn doIt2(): int
365
- `, {
366
- annotations: {
367
- "fn.doIt": [
368
- { type: "throws", value: "NotFound" },
369
- { type: "throws", value: "InvalidArgument" },
370
- ],
371
- },
372
- errors: ["NotFound", "InvalidArgument", "Fatal"],
373
- functionTable: {
374
- doIt: {
375
- args: {},
376
- ret: "string",
377
- },
378
- doIt2: {
379
- args: {},
380
- ret: "int",
381
- },
382
- },
383
- typeTable: {},
384
- });
385
- });
386
- test("handles descriptions inside structs", () => {
387
- expectParses(`
388
- type Foo {
389
- @description foobar
390
- x: int
391
- y: string
392
- }
393
- `, {
394
- annotations: {
395
- "type.Foo.x": [{ type: "description", value: "foobar" }],
396
- },
397
- errors: ["Fatal"],
398
- functionTable: {},
399
- typeTable: {
400
- Foo: {
401
- x: "int",
402
- y: "string",
403
- },
404
- },
405
- });
406
- });
407
- test("handles rest annotations", () => {
408
- expectParses(`
409
- @rest GET /foo
410
- fn foo(): string
411
-
412
- @rest GET /users/{id}/name
413
- fn name(id: string): string
414
-
415
- @rest GET /users/count?{since}&{until}
416
- fn userCount(since: date?, until: date?): uint
417
-
418
- @rest GET /users/{userId}/addresses/{addressId}
419
- fn userAddress(userId: string, addressId: string): string
420
- `, {
421
- annotations: {
422
- "fn.foo": [
423
- {
424
- type: "rest",
425
- value: {
426
- bodyVariable: null,
427
- headers: [],
428
- method: "GET",
429
- path: "/foo",
430
- pathVariables: [],
431
- queryVariables: [],
432
- },
433
- },
434
- ],
435
- "fn.name": [
436
- {
437
- type: "rest",
438
- value: {
439
- bodyVariable: null,
440
- headers: [],
441
- method: "GET",
442
- path: "/users/{id}/name",
443
- pathVariables: ["id"],
444
- queryVariables: [],
445
- },
446
- },
447
- ],
448
- "fn.userCount": [
449
- {
450
- type: "rest",
451
- value: {
452
- bodyVariable: null,
453
- headers: [],
454
- method: "GET",
455
- path: "/users/count",
456
- pathVariables: [],
457
- queryVariables: ["since", "until"],
458
- },
459
- },
460
- ],
461
- "fn.userAddress": [
462
- {
463
- type: "rest",
464
- value: {
465
- bodyVariable: null,
466
- headers: [],
467
- method: "GET",
468
- path: "/users/{userId}/addresses/{addressId}",
469
- pathVariables: ["userId", "addressId"],
470
- queryVariables: [],
471
- },
472
- },
473
- ],
474
- },
475
- errors: ["Fatal"],
476
- functionTable: {
477
- foo: {
478
- args: {},
479
- ret: "string",
480
- },
481
- name: {
482
- args: {
483
- id: "string",
484
- },
485
- ret: "string",
486
- },
487
- userCount: {
488
- args: {
489
- since: "date?",
490
- until: "date?",
491
- },
492
- ret: "uint",
493
- },
494
- userAddress: {
495
- args: {
496
- userId: "string",
497
- addressId: "string",
498
- },
499
- ret: "string",
500
- },
501
- },
502
- typeTable: {},
503
- });
504
- expectParses(`
505
- @rest GET /chats/{chatId}/messages?{since}&{until} [header x-token: {token}]
506
- fn getMessages(token: base64, chatId: uuid, since: datetime?, until: datetime?): string[]
507
- `, {
508
- annotations: {
509
- "fn.getMessages": [
510
- {
511
- type: "rest",
512
- value: {
513
- bodyVariable: null,
514
- headers: [["x-token", "token"]],
515
- method: "GET",
516
- path: "/chats/{chatId}/messages",
517
- pathVariables: ["chatId"],
518
- queryVariables: ["since", "until"],
519
- },
520
- },
521
- ],
522
- },
523
- errors: ["Fatal"],
524
- functionTable: {
525
- getMessages: {
526
- args: {
527
- chatId: "uuid",
528
- since: "datetime?",
529
- token: "base64",
530
- until: "datetime?",
531
- },
532
- ret: "string[]",
533
- },
534
- },
535
- typeTable: {},
536
- });
537
- expectParses(`
538
- @rest GET /posts [header user-agent: {userAgent}] [header accept-language: {lang}] [header x-token: {token}]
539
- fn getPosts(userAgent: string, lang: string, token: base64): uuid
540
- `, {
541
- annotations: {
542
- "fn.getPosts": [
543
- {
544
- type: "rest",
545
- value: {
546
- bodyVariable: null,
547
- headers: [
548
- ["accept-language", "lang"],
549
- ["user-agent", "userAgent"],
550
- ["x-token", "token"],
551
- ],
552
- method: "GET",
553
- path: "/posts",
554
- pathVariables: [],
555
- queryVariables: [],
556
- },
557
- },
558
- ],
559
- },
560
- errors: ["Fatal"],
561
- functionTable: {
562
- getPosts: {
563
- args: {
564
- lang: "string",
565
- token: "base64",
566
- userAgent: "string",
567
- },
568
- ret: "uuid",
569
- },
570
- },
571
- typeTable: {},
572
- });
573
- expectParses(`
574
- @rest POST /insertCustomerLead [header api_key: {apiKey}] [header api_secret: {apiSecret}] [body {customerLead}]
575
- fn insertCustomerLead(customerLead: string, apiKey: bigint, apiSecret: string)
576
- `, {
577
- annotations: {
578
- "fn.insertCustomerLead": [
579
- {
580
- type: "rest",
581
- value: {
582
- bodyVariable: "customerLead",
583
- headers: [
584
- ["api_key", "apiKey"],
585
- ["api_secret", "apiSecret"],
586
- ],
587
- method: "POST",
588
- path: "/insertCustomerLead",
589
- pathVariables: [],
590
- queryVariables: [],
591
- },
592
- },
593
- ],
594
- },
595
- errors: ["Fatal"],
596
- functionTable: {
597
- insertCustomerLead: {
598
- args: {
599
- customerLead: "string",
600
- apiKey: "bigint",
601
- apiSecret: "string",
602
- },
603
- ret: "void",
604
- },
605
- },
606
- typeTable: {},
607
- });
608
- expectParses(`
609
- type NewUser {
610
- name: string
611
- }
612
-
613
- type User {
614
- id: uuid
615
- }
616
-
617
- @rest POST /users [body {user}]
618
- fn createNewUser(user: NewUser): User
619
- `, {
620
- annotations: {
621
- "fn.createNewUser": [
622
- {
623
- type: "rest",
624
- value: {
625
- bodyVariable: "user",
626
- headers: [],
627
- method: "POST",
628
- path: "/users",
629
- pathVariables: [],
630
- queryVariables: [],
631
- },
632
- },
633
- ],
634
- },
635
- errors: ["Fatal"],
636
- functionTable: {
637
- createNewUser: {
638
- args: {
639
- user: "NewUser",
640
- },
641
- ret: "User",
642
- },
643
- },
644
- typeTable: {
645
- NewUser: {
646
- name: "string",
647
- },
648
- User: {
649
- id: "uuid",
650
- },
651
- },
652
- });
653
- expectParses(`
654
- type Kind enum {
655
- first
656
- second
657
- third
658
- }
659
-
660
- @rest GET /things/{kind}
661
- fn countThings(kind: Kind): uint
662
- `, {
663
- annotations: {
664
- "fn.countThings": [
665
- {
666
- type: "rest",
667
- value: {
668
- bodyVariable: null,
669
- headers: [],
670
- method: "GET",
671
- path: "/things/{kind}",
672
- pathVariables: ["kind"],
673
- queryVariables: [],
674
- },
675
- },
676
- ],
677
- },
678
- errors: ["Fatal"],
679
- functionTable: {
680
- countThings: {
681
- args: {
682
- kind: "Kind",
683
- },
684
- ret: "uint",
685
- },
686
- },
687
- typeTable: {
688
- Kind: ["first", "second", "third"],
689
- },
690
- });
691
- expectParses(`
692
- @rest POST /foo/{bar}
693
- fn foo(bar: string !secret): string
694
- `, {
695
- annotations: {
696
- "fn.foo": [
697
- {
698
- type: "rest",
699
- value: {
700
- bodyVariable: null,
701
- headers: [],
702
- method: "POST",
703
- path: "/foo/{bar}",
704
- pathVariables: ["bar"],
705
- queryVariables: [],
706
- },
707
- },
708
- ],
709
- },
710
- errors: ["Fatal"],
711
- functionTable: {
712
- foo: {
713
- args: {
714
- bar: "string",
715
- },
716
- ret: "string",
717
- },
718
- },
719
- typeTable: {},
720
- });
721
- expectParses(`
722
- @rest POST /foo?{bar}
723
- fn foo(bar: string !secret): string
724
- `, {
725
- annotations: {
726
- "fn.foo": [
727
- {
728
- type: "rest",
729
- value: {
730
- bodyVariable: null,
731
- headers: [],
732
- method: "POST",
733
- path: "/foo",
734
- pathVariables: [],
735
- queryVariables: ["bar"],
736
- },
737
- },
738
- ],
739
- },
740
- errors: ["Fatal"],
741
- functionTable: {
742
- foo: {
743
- args: {
744
- bar: "string",
745
- },
746
- ret: "string",
747
- },
748
- },
749
- typeTable: {},
750
- });
751
- expectDoesntParse(`
752
- @rest HEAD /foo
753
- fn foo(): string
754
- `, "Unsupported method 'HEAD'");
755
- expectDoesntParse(`
756
- @rest GET /foo/{id}
757
- fn foo(): string
758
- `, "Argument 'id' not found");
759
- expectDoesntParse(`
760
- @rest GET /foo?{id}
761
- fn foo(): string
762
- `, "Argument 'id' not found");
763
- expectDoesntParse(`
764
- @rest GET aaa
765
- fn foo(): string
766
- `, "Invalid path");
767
- expectDoesntParse(`
768
- @rest GET /aaa?oug
769
- fn foo(): string
770
- `, "Invalid querystring on path");
771
- expectDoesntParse(`
772
- @rest GET /aaa/{arg}
773
- fn foo(arg: string?): string
774
- `, "path argument 'arg' can't be nullable");
775
- expectDoesntParse(`
776
- @rest GET /aaa/{arg}
777
- fn foo(arg: string[]): string
778
- `, "Argument 'arg' can't have type 'string[]' for rest annotation");
779
- expectDoesntParse(`
780
- @rest GET /aaa
781
- fn foo(arg: string): string
782
- `, "is missing");
783
- expectDoesntParse(`
784
- @rest GET /aaa
785
- fn foo(): void
786
- `, "GET rest endpoint must return something");
787
- expectDoesntParse(`
788
- @rest GET /bobobobo/{bar}
789
- fn foo(bar: string !secret): bool
790
- `, "marked as secret");
791
- expectDoesntParse(`
792
- @rest GET /bobobobo?{bar}
793
- fn foo(bar: string !secret): bool
794
- `, "marked as secret");
795
- });
796
- test("handles functions with @hidden", () => {
797
- expectParses(`
798
- @hidden
799
- fn doIt(foo: int, bar: float): string
800
- `, {
801
- annotations: {
802
- "fn.doIt": [{ type: "hidden", value: null }],
803
- },
804
- errors: ["Fatal"],
805
- functionTable: {
806
- doIt: {
807
- args: {
808
- bar: "float",
809
- foo: "int",
810
- },
811
- ret: "string",
812
- },
813
- },
814
- typeTable: {},
815
- });
816
- });
817
- test("handles recursive types", () => {
818
- expectParses(`
819
- type Item {
820
- children: Item[]
821
- }
822
- `, {
823
- annotations: {},
824
- errors: ["Fatal"],
825
- functionTable: {},
826
- typeTable: { Item: { children: "Item[]" } },
827
- });
828
- expectParses(`
829
- type Item enum {
830
- first(a: Item[])
831
- second(b: Item)
832
- }
833
- `, {
834
- annotations: {},
835
- errors: ["Fatal"],
836
- functionTable: {},
837
- typeTable: {
838
- Item: [
839
- ["first", "ItemFirst"],
840
- ["second", "ItemSecond"],
841
- ],
842
- ItemFirst: {
843
- a: "Item[]",
844
- },
845
- ItemSecond: {
846
- b: "Item",
847
- },
848
- },
849
- });
850
- expectDoesntParse(`
851
- type Item Item[]
852
- `, "Type 'Item' at -:2:9 is recursive but is not an struct");
853
- expectDoesntParse(`
854
- type Item Item[][]?
855
- `, "Type 'Item' at -:2:9 is recursive but is not an struct");
856
- expectDoesntParse(`
857
- type Item Item?
858
- `, "Type 'Item' at -:2:9 is recursive but is not an struct");
859
- expectDoesntParse(`
860
- type Item Item
861
- `, "Type 'Item' at -:2:9 is recursive but is not an struct");
862
- expectDoesntParse(`
863
- type Item {
864
- value: Item
865
- }
866
- `, "Type 'Item' at -:2:9 is infinitely recursive");
867
- expectDoesntParse(`
868
- type Item enum {
869
- first(a: Item)
870
- second(b: Item)
871
- }
872
- `, "Type 'Item' at -:2:9 is infinitely recursive");
873
- });
874
- test("handles enum with fields", () => {
875
- expectParses(`
876
- type Shape enum {
877
- point
878
- circle(radius: float)
879
- box(width: float, height: float)
880
- }
881
- `, {
882
- annotations: {},
883
- errors: ["Fatal"],
884
- functionTable: {},
885
- typeTable: {
886
- Shape: ["point", ["circle", "ShapeCircle"], ["box", "ShapeBox"]],
887
- ShapeBox: {
888
- height: "float",
889
- width: "float",
890
- },
891
- ShapeCircle: {
892
- radius: "float",
893
- },
894
- },
895
- });
896
- });
897
- test("merges identical types with the same name", () => {
898
- expectParses(`
899
- type Test {
900
- foo: string
901
- bar: int
902
- }
903
-
904
- type Test {
905
- foo: string
906
- bar: int
907
- }
908
- `, {
909
- annotations: {},
910
- errors: ["Fatal"],
911
- functionTable: {},
912
- typeTable: {
913
- Test: {
914
- bar: "int",
915
- foo: "string",
916
- },
917
- },
918
- });
919
- expectDoesntParse(`
920
- type Test {
921
- foo: string
922
- bar: int
923
- }
924
-
925
- type Test {
926
- foo: string
927
- bar: uint
928
- }
929
- `, "Type 'Test' at -:7:9 is defined multiple times (also at -:2:19)");
930
- });
931
- test("merges identical types with automatic name", () => {
932
- expectParses(`
933
- type Test {
934
- foo: {
935
- bar: int
936
- }
937
- }
938
-
939
- type TestFoo {
940
- bar: int
941
- }
942
- `, {
943
- annotations: {},
944
- errors: ["Fatal"],
945
- functionTable: {},
946
- typeTable: {
947
- Test: {
948
- foo: "TestFoo",
949
- },
950
- TestFoo: {
951
- bar: "int",
952
- },
953
- },
954
- });
955
- expectDoesntParse(`
956
- type Test {
957
- foo: {
958
- bar: int
959
- }
960
- }
961
-
962
- type TestFoo {
963
- bar: uint
964
- }
965
- `, "The name of the type 'TestFoo' at -:8:22 will conflict with 'Test.foo' at -:3:16");
966
- });
967
- });