@sdkgen/parser 1.6.2 → 2.0.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.
- package/dist/spec/lexer.spec.js +1 -3
- package/dist/spec/parser.spec.js +226 -30
- package/dist/src/ast.d.ts +17 -15
- package/dist/src/ast.js +23 -21
- package/dist/src/compatibility/index.js +17 -7
- package/dist/src/json.d.ts +9 -17
- package/dist/src/json.js +97 -60
- package/dist/src/lexer.js +1 -7
- package/dist/src/parser.js +92 -40
- package/dist/src/semantic/{06_give_struct_and_enum_names.d.ts → 02_give_struct_and_enum_names.d.ts} +0 -0
- package/dist/src/semantic/{06_give_struct_and_enum_names.js → 02_give_struct_and_enum_names.js} +6 -1
- package/dist/src/semantic/{02_match_type_definitions.d.ts → 03_match_type_definitions.d.ts} +0 -0
- package/dist/src/semantic/{02_match_type_definitions.js → 03_match_type_definitions.js} +0 -0
- package/dist/src/semantic/04_check_dont_return_secret.d.ts +1 -0
- package/dist/src/semantic/04_check_dont_return_secret.js +6 -1
- package/dist/src/semantic/05_expand_spreads.d.ts +9 -0
- package/dist/src/semantic/05_expand_spreads.js +60 -0
- package/dist/src/semantic/{07_check_empty_struct_or_enum.d.ts → 06_check_empty_struct_or_enum.d.ts} +0 -0
- package/dist/src/semantic/{07_check_empty_struct_or_enum.js → 06_check_empty_struct_or_enum.js} +1 -1
- package/dist/src/semantic/07_validate_recursive_types.d.ts +8 -0
- package/dist/src/semantic/07_validate_recursive_types.js +49 -0
- package/dist/src/semantic/{10_validate_annotations.d.ts → 09_validate_annotations.d.ts} +0 -0
- package/dist/src/semantic/{10_validate_annotations.js → 09_validate_annotations.js} +1 -1
- package/dist/src/semantic/analyser.js +12 -14
- package/dist/src/semantic/visitor.js +19 -3
- package/dist/src/token.d.ts +1 -4
- package/dist/src/token.js +4 -10
- package/dist/tsconfig.tsbuildinfo +1 -2191
- package/package.json +6 -6
- package/dist/src/semantic/03_check_no_recursive_types.d.ts +0 -7
- package/dist/src/semantic/03_check_no_recursive_types.js +0 -36
- package/dist/src/semantic/05_check_naming_for_getters_returning_bool.d.ts +0 -5
- package/dist/src/semantic/05_check_naming_for_getters_returning_bool.js +0 -21
- package/dist/src/semantic/09_apply_struct_spreads.d.ts +0 -7
- package/dist/src/semantic/09_apply_struct_spreads.js +0 -42
package/dist/spec/lexer.spec.js
CHANGED
|
@@ -41,10 +41,8 @@ describe(lexer_1.Lexer, () => {
|
|
|
41
41
|
itLexes("enum", [new token_1.EnumKeywordToken()]);
|
|
42
42
|
itLexes("error", [new token_1.ErrorKeywordToken()]);
|
|
43
43
|
itLexes("import", [new token_1.ImportKeywordToken()]);
|
|
44
|
-
itLexes("get", [new token_1.GetKeywordToken()]);
|
|
45
44
|
itLexes("Get", [new token_1.IdentifierToken("Get")]);
|
|
46
|
-
itLexes("
|
|
47
|
-
itLexes("fn", [new token_1.FunctionKeywordToken("fn")]);
|
|
45
|
+
itLexes("fn", [new token_1.FnKeywordToken()]);
|
|
48
46
|
itLexes("enuma", [new token_1.IdentifierToken("enuma")]);
|
|
49
47
|
itLexes("errorh", [new token_1.IdentifierToken("errorh")]);
|
|
50
48
|
for (const kw of ["enum", "type", "error", "import", "get", "function", "fn", "true", "false"]) {
|
package/dist/spec/parser.spec.js
CHANGED
|
@@ -7,8 +7,8 @@ function expectParses(source, json, warnings = []) {
|
|
|
7
7
|
const parser = new parser_1.Parser(new lexer_1.Lexer(source));
|
|
8
8
|
const ast = parser.parse();
|
|
9
9
|
expect(ast.warnings).toEqual(warnings);
|
|
10
|
-
expect(json_1.astToJson(ast)).toEqual(json);
|
|
11
|
-
expect(json_1.astToJson(ast)).toEqual(json_1.astToJson(json_1.jsonToAst(json_1.astToJson(ast))));
|
|
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
12
|
}
|
|
13
13
|
function expectDoesntParse(source, message) {
|
|
14
14
|
const parser = new parser_1.Parser(new lexer_1.Lexer(source));
|
|
@@ -32,31 +32,6 @@ describe(parser_1.Parser, () => {
|
|
|
32
32
|
},
|
|
33
33
|
});
|
|
34
34
|
});
|
|
35
|
-
test(`handles simple get operations for primitive type '${p}'`, () => {
|
|
36
|
-
expectParses(`
|
|
37
|
-
get ${p === "bool" ? "isFoo" : "foo"}(): ${p}
|
|
38
|
-
get bar(): ${p}?
|
|
39
|
-
fn getBaz(): ${p}[]
|
|
40
|
-
`, {
|
|
41
|
-
annotations: {},
|
|
42
|
-
errors: ["Fatal"],
|
|
43
|
-
functionTable: {
|
|
44
|
-
[p === "bool" ? "isFoo" : "getFoo"]: {
|
|
45
|
-
args: {},
|
|
46
|
-
ret: p,
|
|
47
|
-
},
|
|
48
|
-
getBar: {
|
|
49
|
-
args: {},
|
|
50
|
-
ret: `${p}?`,
|
|
51
|
-
},
|
|
52
|
-
getBaz: {
|
|
53
|
-
args: {},
|
|
54
|
-
ret: `${p}[]`,
|
|
55
|
-
},
|
|
56
|
-
},
|
|
57
|
-
typeTable: {},
|
|
58
|
-
}, ["Keyword 'get' is deprecated at -:2:11. Use 'fn' instead.", "Keyword 'get' is deprecated at -:3:11. Use 'fn' instead."]);
|
|
59
|
-
});
|
|
60
35
|
}
|
|
61
36
|
for (const kw of lexer_1.Lexer.KEYWORDS) {
|
|
62
37
|
test(`handles '${kw}' on the name of a field`, () => {
|
|
@@ -221,6 +196,51 @@ describe(parser_1.Parser, () => {
|
|
|
221
196
|
},
|
|
222
197
|
},
|
|
223
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
|
+
});
|
|
224
244
|
});
|
|
225
245
|
test("handles functions with arguments", () => {
|
|
226
246
|
expectParses(`
|
|
@@ -228,7 +248,7 @@ describe(parser_1.Parser, () => {
|
|
|
228
248
|
aa: string
|
|
229
249
|
}
|
|
230
250
|
|
|
231
|
-
|
|
251
|
+
fn doIt(foo: int, bar: Bar): string
|
|
232
252
|
`, {
|
|
233
253
|
annotations: {},
|
|
234
254
|
errors: ["Fatal"],
|
|
@@ -246,7 +266,33 @@ describe(parser_1.Parser, () => {
|
|
|
246
266
|
aa: "string",
|
|
247
267
|
},
|
|
248
268
|
},
|
|
249
|
-
}
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
test("handles spreads in function arguments", () => {
|
|
272
|
+
expectParses(`
|
|
273
|
+
type Bar {
|
|
274
|
+
aa: string
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
fn doIt(foo: int, ...Bar): string
|
|
278
|
+
`, {
|
|
279
|
+
annotations: {},
|
|
280
|
+
errors: ["Fatal"],
|
|
281
|
+
functionTable: {
|
|
282
|
+
doIt: {
|
|
283
|
+
args: {
|
|
284
|
+
aa: "string",
|
|
285
|
+
foo: "int",
|
|
286
|
+
},
|
|
287
|
+
ret: "string",
|
|
288
|
+
},
|
|
289
|
+
},
|
|
290
|
+
typeTable: {
|
|
291
|
+
Bar: {
|
|
292
|
+
aa: "string",
|
|
293
|
+
},
|
|
294
|
+
},
|
|
295
|
+
});
|
|
250
296
|
});
|
|
251
297
|
test("handles functions with annotations", () => {
|
|
252
298
|
expectParses(`
|
|
@@ -443,8 +489,8 @@ describe(parser_1.Parser, () => {
|
|
|
443
489
|
value: {
|
|
444
490
|
bodyVariable: null,
|
|
445
491
|
headers: [
|
|
446
|
-
["user-agent", "userAgent"],
|
|
447
492
|
["accept-language", "lang"],
|
|
493
|
+
["user-agent", "userAgent"],
|
|
448
494
|
["x-token", "token"],
|
|
449
495
|
],
|
|
450
496
|
method: "GET",
|
|
@@ -644,4 +690,154 @@ describe(parser_1.Parser, () => {
|
|
|
644
690
|
typeTable: {},
|
|
645
691
|
});
|
|
646
692
|
});
|
|
693
|
+
test("handles recursive types", () => {
|
|
694
|
+
expectParses(`
|
|
695
|
+
type Item {
|
|
696
|
+
children: Item[]
|
|
697
|
+
}
|
|
698
|
+
`, {
|
|
699
|
+
annotations: {},
|
|
700
|
+
errors: ["Fatal"],
|
|
701
|
+
functionTable: {},
|
|
702
|
+
typeTable: { Item: { children: "Item[]" } },
|
|
703
|
+
});
|
|
704
|
+
expectParses(`
|
|
705
|
+
type Item enum {
|
|
706
|
+
first(a: Item[])
|
|
707
|
+
second(b: Item)
|
|
708
|
+
}
|
|
709
|
+
`, {
|
|
710
|
+
annotations: {},
|
|
711
|
+
errors: ["Fatal"],
|
|
712
|
+
functionTable: {},
|
|
713
|
+
typeTable: {
|
|
714
|
+
Item: [
|
|
715
|
+
["first", "ItemFirst"],
|
|
716
|
+
["second", "ItemSecond"],
|
|
717
|
+
],
|
|
718
|
+
ItemFirst: {
|
|
719
|
+
a: "Item[]",
|
|
720
|
+
},
|
|
721
|
+
ItemSecond: {
|
|
722
|
+
b: "Item",
|
|
723
|
+
},
|
|
724
|
+
},
|
|
725
|
+
});
|
|
726
|
+
expectDoesntParse(`
|
|
727
|
+
type Item Item[]
|
|
728
|
+
`, "Type 'Item' at -:2:9 is recursive but is not an struct");
|
|
729
|
+
expectDoesntParse(`
|
|
730
|
+
type Item Item[][]?
|
|
731
|
+
`, "Type 'Item' at -:2:9 is recursive but is not an struct");
|
|
732
|
+
expectDoesntParse(`
|
|
733
|
+
type Item Item?
|
|
734
|
+
`, "Type 'Item' at -:2:9 is recursive but is not an struct");
|
|
735
|
+
expectDoesntParse(`
|
|
736
|
+
type Item Item
|
|
737
|
+
`, "Type 'Item' at -:2:9 is recursive but is not an struct");
|
|
738
|
+
expectDoesntParse(`
|
|
739
|
+
type Item {
|
|
740
|
+
value: Item
|
|
741
|
+
}
|
|
742
|
+
`, "Type 'Item' at -:2:9 is infinitely recursive");
|
|
743
|
+
expectDoesntParse(`
|
|
744
|
+
type Item enum {
|
|
745
|
+
first(a: Item)
|
|
746
|
+
second(b: Item)
|
|
747
|
+
}
|
|
748
|
+
`, "Type 'Item' at -:2:9 is infinitely recursive");
|
|
749
|
+
});
|
|
750
|
+
test("handles enum with fields", () => {
|
|
751
|
+
expectParses(`
|
|
752
|
+
type Shape enum {
|
|
753
|
+
point
|
|
754
|
+
circle(radius: float)
|
|
755
|
+
box(width: float, height: float)
|
|
756
|
+
}
|
|
757
|
+
`, {
|
|
758
|
+
annotations: {},
|
|
759
|
+
errors: ["Fatal"],
|
|
760
|
+
functionTable: {},
|
|
761
|
+
typeTable: {
|
|
762
|
+
Shape: ["point", ["circle", "ShapeCircle"], ["box", "ShapeBox"]],
|
|
763
|
+
ShapeBox: {
|
|
764
|
+
height: "float",
|
|
765
|
+
width: "float",
|
|
766
|
+
},
|
|
767
|
+
ShapeCircle: {
|
|
768
|
+
radius: "float",
|
|
769
|
+
},
|
|
770
|
+
},
|
|
771
|
+
});
|
|
772
|
+
});
|
|
773
|
+
test("merges identical types with the same name", () => {
|
|
774
|
+
expectParses(`
|
|
775
|
+
type Test {
|
|
776
|
+
foo: string
|
|
777
|
+
bar: int
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
type Test {
|
|
781
|
+
foo: string
|
|
782
|
+
bar: int
|
|
783
|
+
}
|
|
784
|
+
`, {
|
|
785
|
+
annotations: {},
|
|
786
|
+
errors: ["Fatal"],
|
|
787
|
+
functionTable: {},
|
|
788
|
+
typeTable: {
|
|
789
|
+
Test: {
|
|
790
|
+
bar: "int",
|
|
791
|
+
foo: "string",
|
|
792
|
+
},
|
|
793
|
+
},
|
|
794
|
+
});
|
|
795
|
+
expectDoesntParse(`
|
|
796
|
+
type Test {
|
|
797
|
+
foo: string
|
|
798
|
+
bar: int
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
type Test {
|
|
802
|
+
foo: string
|
|
803
|
+
bar: uint
|
|
804
|
+
}
|
|
805
|
+
`, "Type 'Test' at -:7:9 is defined multiple times (also at -:2:19)");
|
|
806
|
+
});
|
|
807
|
+
test("merges identical types with automatic name", () => {
|
|
808
|
+
expectParses(`
|
|
809
|
+
type Test {
|
|
810
|
+
foo: {
|
|
811
|
+
bar: int
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
type TestFoo {
|
|
816
|
+
bar: int
|
|
817
|
+
}
|
|
818
|
+
`, {
|
|
819
|
+
annotations: {},
|
|
820
|
+
errors: ["Fatal"],
|
|
821
|
+
functionTable: {},
|
|
822
|
+
typeTable: {
|
|
823
|
+
Test: {
|
|
824
|
+
foo: "TestFoo",
|
|
825
|
+
},
|
|
826
|
+
TestFoo: {
|
|
827
|
+
bar: "int",
|
|
828
|
+
},
|
|
829
|
+
},
|
|
830
|
+
});
|
|
831
|
+
expectDoesntParse(`
|
|
832
|
+
type Test {
|
|
833
|
+
foo: {
|
|
834
|
+
bar: int
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
type TestFoo {
|
|
839
|
+
bar: uint
|
|
840
|
+
}
|
|
841
|
+
`, "The name of the type 'TestFoo' at -:8:22 will conflict with 'Test.foo' at -:3:16");
|
|
842
|
+
});
|
|
647
843
|
});
|
package/dist/src/ast.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ export declare abstract class AstNode {
|
|
|
8
8
|
}
|
|
9
9
|
export declare abstract class Type extends AstNode {
|
|
10
10
|
abstract get name(): string;
|
|
11
|
-
toJSON():
|
|
11
|
+
toJSON(): unknown;
|
|
12
12
|
}
|
|
13
13
|
export declare class ErrorNode extends AstNode {
|
|
14
14
|
name: string;
|
|
@@ -42,6 +42,7 @@ export declare class RestAnnotation extends Annotation {
|
|
|
42
42
|
export declare class HiddenAnnotation extends Annotation {
|
|
43
43
|
}
|
|
44
44
|
export declare abstract class PrimitiveType extends Type {
|
|
45
|
+
toJSON(): string;
|
|
45
46
|
}
|
|
46
47
|
export declare class StringPrimitiveType extends PrimitiveType {
|
|
47
48
|
name: string;
|
|
@@ -119,12 +120,14 @@ export declare class ArrayType extends Type {
|
|
|
119
120
|
export declare class EnumValue extends AstNode {
|
|
120
121
|
value: string;
|
|
121
122
|
annotations: Annotation[];
|
|
123
|
+
struct: StructType | null;
|
|
122
124
|
constructor(value: string);
|
|
123
125
|
}
|
|
124
126
|
export declare class EnumType extends Type {
|
|
125
127
|
values: EnumValue[];
|
|
126
128
|
name: string;
|
|
127
129
|
constructor(values: EnumValue[]);
|
|
130
|
+
get hasStructValues(): boolean;
|
|
128
131
|
}
|
|
129
132
|
export declare class Field extends AstNode {
|
|
130
133
|
name: string;
|
|
@@ -133,16 +136,20 @@ export declare class Field extends AstNode {
|
|
|
133
136
|
annotations: Annotation[];
|
|
134
137
|
constructor(name: string, type: Type, secret?: boolean);
|
|
135
138
|
}
|
|
139
|
+
export declare class Spread extends AstNode {
|
|
140
|
+
typeReference: TypeReference;
|
|
141
|
+
constructor(typeReference: TypeReference);
|
|
142
|
+
}
|
|
136
143
|
export declare class TypeReference extends Type {
|
|
137
144
|
name: string;
|
|
138
145
|
type: Type;
|
|
139
146
|
constructor(name: string);
|
|
140
147
|
}
|
|
141
148
|
export declare class StructType extends Type {
|
|
142
|
-
|
|
143
|
-
spreads: TypeReference[];
|
|
149
|
+
fieldsAndSpreads: Array<Field | Spread>;
|
|
144
150
|
name: string;
|
|
145
|
-
|
|
151
|
+
fields: Field[];
|
|
152
|
+
constructor(fieldsAndSpreads: Array<Field | Spread>);
|
|
146
153
|
}
|
|
147
154
|
export declare class TypeDefinition extends AstNode {
|
|
148
155
|
name: string;
|
|
@@ -150,25 +157,20 @@ export declare class TypeDefinition extends AstNode {
|
|
|
150
157
|
annotations: Annotation[];
|
|
151
158
|
constructor(name: string, type: Type);
|
|
152
159
|
}
|
|
153
|
-
export declare
|
|
160
|
+
export declare class FunctionOperation extends AstNode {
|
|
154
161
|
name: string;
|
|
155
|
-
|
|
162
|
+
fieldsAndSpreads: Array<Field | Spread>;
|
|
156
163
|
returnType: Type;
|
|
157
164
|
annotations: Annotation[];
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
export declare class GetOperation extends Operation {
|
|
162
|
-
get prettyName(): string;
|
|
163
|
-
}
|
|
164
|
-
export declare class FunctionOperation extends Operation {
|
|
165
|
+
args: Field[];
|
|
166
|
+
constructor(name: string, fieldsAndSpreads: Array<Field | Spread>, returnType: Type);
|
|
165
167
|
}
|
|
166
168
|
export declare class AstRoot {
|
|
167
169
|
typeDefinitions: TypeDefinition[];
|
|
168
|
-
operations:
|
|
170
|
+
operations: FunctionOperation[];
|
|
169
171
|
errors: ErrorNode[];
|
|
170
172
|
structTypes: StructType[];
|
|
171
173
|
enumTypes: EnumType[];
|
|
172
174
|
warnings: string[];
|
|
173
|
-
constructor(typeDefinitions?: TypeDefinition[], operations?:
|
|
175
|
+
constructor(typeDefinitions?: TypeDefinition[], operations?: FunctionOperation[], errors?: ErrorNode[]);
|
|
174
176
|
}
|
package/dist/src/ast.js
CHANGED
|
@@ -11,7 +11,7 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
11
11
|
return t;
|
|
12
12
|
};
|
|
13
13
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
-
exports.AstRoot = exports.FunctionOperation = exports.
|
|
14
|
+
exports.AstRoot = exports.FunctionOperation = exports.TypeDefinition = exports.StructType = exports.TypeReference = exports.Spread = exports.Field = exports.EnumType = exports.EnumValue = exports.ArrayType = exports.OptionalType = exports.JsonPrimitiveType = exports.XmlPrimitiveType = exports.Base64PrimitiveType = exports.HtmlPrimitiveType = exports.HexPrimitiveType = exports.UuidPrimitiveType = exports.UrlPrimitiveType = exports.EmailPrimitiveType = exports.CnpjPrimitiveType = exports.CpfPrimitiveType = exports.MoneyPrimitiveType = exports.VoidPrimitiveType = exports.BytesPrimitiveType = exports.BoolPrimitiveType = exports.DateTimePrimitiveType = exports.DatePrimitiveType = exports.BigIntPrimitiveType = exports.FloatPrimitiveType = exports.UIntPrimitiveType = exports.IntPrimitiveType = exports.StringPrimitiveType = exports.PrimitiveType = exports.HiddenAnnotation = exports.RestAnnotation = exports.ArgDescriptionAnnotation = exports.ThrowsAnnotation = exports.DescriptionAnnotation = exports.Annotation = exports.ErrorNode = exports.Type = exports.AstNode = void 0;
|
|
15
15
|
const token_1 = require("./token");
|
|
16
16
|
class AstNode {
|
|
17
17
|
constructor() {
|
|
@@ -30,8 +30,7 @@ class AstNode {
|
|
|
30
30
|
exports.AstNode = AstNode;
|
|
31
31
|
class Type extends AstNode {
|
|
32
32
|
toJSON() {
|
|
33
|
-
|
|
34
|
-
const _a = Object.assign({}, this), { name } = _a, rest = __rest(_a, ["name"]);
|
|
33
|
+
const _a = Object.assign({}, this), { name: _name } = _a, rest = __rest(_a, ["name"]);
|
|
35
34
|
return rest;
|
|
36
35
|
}
|
|
37
36
|
}
|
|
@@ -85,6 +84,9 @@ class HiddenAnnotation extends Annotation {
|
|
|
85
84
|
}
|
|
86
85
|
exports.HiddenAnnotation = HiddenAnnotation;
|
|
87
86
|
class PrimitiveType extends Type {
|
|
87
|
+
toJSON() {
|
|
88
|
+
return this.name;
|
|
89
|
+
}
|
|
88
90
|
}
|
|
89
91
|
exports.PrimitiveType = PrimitiveType;
|
|
90
92
|
class StringPrimitiveType extends PrimitiveType {
|
|
@@ -259,6 +261,7 @@ class EnumValue extends AstNode {
|
|
|
259
261
|
super();
|
|
260
262
|
this.value = value;
|
|
261
263
|
this.annotations = [];
|
|
264
|
+
this.struct = null;
|
|
262
265
|
}
|
|
263
266
|
}
|
|
264
267
|
exports.EnumValue = EnumValue;
|
|
@@ -267,6 +270,9 @@ class EnumType extends Type {
|
|
|
267
270
|
super();
|
|
268
271
|
this.values = values;
|
|
269
272
|
}
|
|
273
|
+
get hasStructValues() {
|
|
274
|
+
return this.values.some(v => v.struct !== null);
|
|
275
|
+
}
|
|
270
276
|
}
|
|
271
277
|
exports.EnumType = EnumType;
|
|
272
278
|
class Field extends AstNode {
|
|
@@ -279,6 +285,13 @@ class Field extends AstNode {
|
|
|
279
285
|
}
|
|
280
286
|
}
|
|
281
287
|
exports.Field = Field;
|
|
288
|
+
class Spread extends AstNode {
|
|
289
|
+
constructor(typeReference) {
|
|
290
|
+
super();
|
|
291
|
+
this.typeReference = typeReference;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
exports.Spread = Spread;
|
|
282
295
|
class TypeReference extends Type {
|
|
283
296
|
constructor(name) {
|
|
284
297
|
super();
|
|
@@ -287,10 +300,10 @@ class TypeReference extends Type {
|
|
|
287
300
|
}
|
|
288
301
|
exports.TypeReference = TypeReference;
|
|
289
302
|
class StructType extends Type {
|
|
290
|
-
constructor(
|
|
303
|
+
constructor(fieldsAndSpreads) {
|
|
291
304
|
super();
|
|
292
|
-
this.
|
|
293
|
-
this.
|
|
305
|
+
this.fieldsAndSpreads = fieldsAndSpreads;
|
|
306
|
+
this.fields = [];
|
|
294
307
|
}
|
|
295
308
|
}
|
|
296
309
|
exports.StructType = StructType;
|
|
@@ -303,26 +316,15 @@ class TypeDefinition extends AstNode {
|
|
|
303
316
|
}
|
|
304
317
|
}
|
|
305
318
|
exports.TypeDefinition = TypeDefinition;
|
|
306
|
-
class
|
|
307
|
-
constructor(name,
|
|
319
|
+
class FunctionOperation extends AstNode {
|
|
320
|
+
constructor(name, fieldsAndSpreads, returnType) {
|
|
308
321
|
super();
|
|
309
322
|
this.name = name;
|
|
310
|
-
this.
|
|
323
|
+
this.fieldsAndSpreads = fieldsAndSpreads;
|
|
311
324
|
this.returnType = returnType;
|
|
312
325
|
this.annotations = [];
|
|
326
|
+
this.args = [];
|
|
313
327
|
}
|
|
314
|
-
get prettyName() {
|
|
315
|
-
return this.name;
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
exports.Operation = Operation;
|
|
319
|
-
class GetOperation extends Operation {
|
|
320
|
-
get prettyName() {
|
|
321
|
-
return this.returnType instanceof BoolPrimitiveType ? this.name : `get${this.name[0].toUpperCase()}${this.name.slice(1)}`;
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
exports.GetOperation = GetOperation;
|
|
325
|
-
class FunctionOperation extends Operation {
|
|
326
328
|
}
|
|
327
329
|
exports.FunctionOperation = FunctionOperation;
|
|
328
330
|
class AstRoot {
|
|
@@ -7,6 +7,7 @@ const ast_1 = require("../ast");
|
|
|
7
7
|
* 2 -> New version
|
|
8
8
|
*/
|
|
9
9
|
function checkClientToServer(path, issues, t1, t2) {
|
|
10
|
+
var _a, _b;
|
|
10
11
|
if (t1 instanceof ast_1.TypeReference) {
|
|
11
12
|
checkClientToServer(path, issues, t1.type, t2);
|
|
12
13
|
return;
|
|
@@ -74,7 +75,11 @@ function checkClientToServer(path, issues, t1, t2) {
|
|
|
74
75
|
}
|
|
75
76
|
if (t1 instanceof ast_1.EnumType && t2 instanceof ast_1.EnumType) {
|
|
76
77
|
for (const value of t1.values) {
|
|
77
|
-
|
|
78
|
+
const other = t2.values.find(v => v.value === value.value);
|
|
79
|
+
if (other) {
|
|
80
|
+
checkClientToServer(`${path}.${value.value}`, issues, (_a = value.struct) !== null && _a !== void 0 ? _a : new ast_1.StructType([]), (_b = other.struct) !== null && _b !== void 0 ? _b : new ast_1.StructType([]));
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
78
83
|
issues.push(`The enum at ${path} used to accept the value "${value.value}" that doesn't exist now. Clients that send it will fail.`);
|
|
79
84
|
}
|
|
80
85
|
}
|
|
@@ -86,6 +91,7 @@ function checkClientToServer(path, issues, t1, t2) {
|
|
|
86
91
|
issues.push(`${path} was ${t1.name} and now it is ${t2.name}. They are not compatible.`);
|
|
87
92
|
}
|
|
88
93
|
function checkServerToClient(path, issues, t1, t2) {
|
|
94
|
+
var _a, _b;
|
|
89
95
|
if (t1 instanceof ast_1.TypeReference) {
|
|
90
96
|
checkServerToClient(path, issues, t1.type, t2);
|
|
91
97
|
return;
|
|
@@ -153,7 +159,11 @@ function checkServerToClient(path, issues, t1, t2) {
|
|
|
153
159
|
}
|
|
154
160
|
if (t1 instanceof ast_1.EnumType && t2 instanceof ast_1.EnumType) {
|
|
155
161
|
for (const value of t2.values) {
|
|
156
|
-
|
|
162
|
+
const other = t1.values.find(v => v.value === value.value);
|
|
163
|
+
if (other) {
|
|
164
|
+
checkServerToClient(`${path}.${value.value}`, issues, (_a = other.struct) !== null && _a !== void 0 ? _a : new ast_1.StructType([]), (_b = value.struct) !== null && _b !== void 0 ? _b : new ast_1.StructType([]));
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
157
167
|
issues.push(`The enum at ${path} now has the value "${value.value}" that didn't exist before. Client will crash if it receives it`);
|
|
158
168
|
}
|
|
159
169
|
}
|
|
@@ -167,12 +177,12 @@ function checkServerToClient(path, issues, t1, t2) {
|
|
|
167
177
|
function compatibilityIssues(ast1, ast2) {
|
|
168
178
|
const issues = [];
|
|
169
179
|
for (const op1 of ast1.operations) {
|
|
170
|
-
const op2 = ast2.operations.find(x => x.
|
|
180
|
+
const op2 = ast2.operations.find(x => x.name === op1.name);
|
|
171
181
|
if (!op2) {
|
|
172
|
-
issues.push(`function ${op1.
|
|
182
|
+
issues.push(`function ${op1.name} used to exist, but it's now missing. Add it back.`);
|
|
173
183
|
continue;
|
|
174
184
|
}
|
|
175
|
-
checkServerToClient(`${op1.
|
|
185
|
+
checkServerToClient(`${op1.name}.ret`, issues, op1.returnType, op2.returnType);
|
|
176
186
|
for (const arg2 of op2.args) {
|
|
177
187
|
const arg1 = op1.args.find(x => x.name === arg2.name);
|
|
178
188
|
if (!arg1) {
|
|
@@ -180,11 +190,11 @@ function compatibilityIssues(ast1, ast2) {
|
|
|
180
190
|
continue;
|
|
181
191
|
}
|
|
182
192
|
else {
|
|
183
|
-
issues.push(`${op1.
|
|
193
|
+
issues.push(`${op1.name}.args.${arg2.name} didn't exist before and isn't optional. Make it optional.`);
|
|
184
194
|
continue;
|
|
185
195
|
}
|
|
186
196
|
}
|
|
187
|
-
checkClientToServer(`${op1.
|
|
197
|
+
checkClientToServer(`${op1.name}.args.${arg1.name}`, issues, arg1.type, arg2.type);
|
|
188
198
|
}
|
|
189
199
|
}
|
|
190
200
|
for (const err1 of ast1.errors) {
|
package/dist/src/json.d.ts
CHANGED
|
@@ -1,19 +1,11 @@
|
|
|
1
1
|
import { AstRoot } from "./ast";
|
|
2
2
|
import type { DeepReadonly } from "./utils";
|
|
3
|
-
export declare type TypeDescription = string | string[] |
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
interface FunctionTable {
|
|
10
|
-
[name: string]: {
|
|
11
|
-
args: {
|
|
12
|
-
[arg: string]: TypeDescription;
|
|
13
|
-
};
|
|
14
|
-
ret: TypeDescription;
|
|
15
|
-
} | undefined;
|
|
16
|
-
}
|
|
3
|
+
export declare type TypeDescription = string | Array<string | [string, string]> | Record<string, string>;
|
|
4
|
+
declare type TypeTable = Record<string, TypeDescription | undefined>;
|
|
5
|
+
declare type FunctionTable = Record<string, {
|
|
6
|
+
args: Record<string, string>;
|
|
7
|
+
ret: TypeDescription;
|
|
8
|
+
} | undefined>;
|
|
17
9
|
declare type AnnotationJson = {
|
|
18
10
|
type: "description";
|
|
19
11
|
value: string;
|
|
@@ -27,11 +19,11 @@ declare type AnnotationJson = {
|
|
|
27
19
|
type: "rest";
|
|
28
20
|
value: {
|
|
29
21
|
bodyVariable: string | null;
|
|
30
|
-
headers:
|
|
22
|
+
headers: ReadonlyArray<[string, string]>;
|
|
31
23
|
method: string;
|
|
32
24
|
path: string;
|
|
33
|
-
pathVariables: string[];
|
|
34
|
-
queryVariables: string[];
|
|
25
|
+
pathVariables: readonly string[];
|
|
26
|
+
queryVariables: readonly string[];
|
|
35
27
|
};
|
|
36
28
|
};
|
|
37
29
|
export interface AstJson {
|