@sdkgen/parser 2.3.1 → 2.4.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.js → ast.js} +2 -12
  2. package/dist/compatibility/index.js +210 -0
  3. package/dist/helpers.js +24 -0
  4. package/dist/index.js +17 -0
  5. package/dist/json.js +230 -0
  6. package/dist/{src/lexer.js → lexer.js} +26 -25
  7. package/dist/parser.js +424 -0
  8. package/dist/restparser.js +63 -0
  9. package/dist/semantic/01_check_multiple_declaration.js +23 -0
  10. package/dist/semantic/02_give_struct_and_enum_names.js +62 -0
  11. package/dist/semantic/03_match_type_definitions.js +19 -0
  12. package/dist/semantic/04_check_dont_return_secret.js +43 -0
  13. package/dist/semantic/05_expand_spreads.js +61 -0
  14. package/dist/semantic/06_check_empty_struct_or_enum.js +22 -0
  15. package/dist/semantic/07_validate_recursive_types.js +50 -0
  16. package/dist/semantic/08_collect_struct_and_enum_types.js +26 -0
  17. package/dist/{src/semantic → semantic}/09_validate_annotations.js +1 -0
  18. package/dist/semantic/10_check_duplicate_members_on_enum.js +17 -0
  19. package/dist/{src/semantic → semantic}/analyser.js +1 -0
  20. package/dist/semantic/transformer.js +51 -0
  21. package/dist/semantic/visitor.js +66 -0
  22. package/dist/{src/token.js → token.js} +1 -0
  23. package/dist/utils.js +28 -0
  24. package/package.json +24 -8
  25. package/.eslintrc.json +0 -3
  26. package/.prettierrc +0 -12
  27. package/dist/spec/lexer.spec.d.ts +0 -1
  28. package/dist/spec/lexer.spec.js +0 -152
  29. package/dist/spec/parser.spec.d.ts +0 -1
  30. package/dist/spec/parser.spec.js +0 -967
  31. package/dist/src/compatibility/index.js +0 -211
  32. package/dist/src/helpers.js +0 -23
  33. package/dist/src/index.js +0 -29
  34. package/dist/src/json.js +0 -234
  35. package/dist/src/parser.js +0 -423
  36. package/dist/src/restparser.js +0 -65
  37. package/dist/src/semantic/01_check_multiple_declaration.js +0 -22
  38. package/dist/src/semantic/02_give_struct_and_enum_names.js +0 -61
  39. package/dist/src/semantic/03_match_type_definitions.js +0 -18
  40. package/dist/src/semantic/04_check_dont_return_secret.js +0 -42
  41. package/dist/src/semantic/05_expand_spreads.js +0 -60
  42. package/dist/src/semantic/06_check_empty_struct_or_enum.js +0 -21
  43. package/dist/src/semantic/07_validate_recursive_types.js +0 -49
  44. package/dist/src/semantic/08_collect_struct_and_enum_types.js +0 -25
  45. package/dist/src/semantic/10_check_duplicate_members_on_enum.js +0 -16
  46. package/dist/src/semantic/transformer.js +0 -50
  47. package/dist/src/semantic/visitor.js +0 -65
  48. package/dist/src/utils.js +0 -27
  49. package/dist/tsconfig.tsbuildinfo +0 -1
  50. /package/dist/{src/ast.d.ts → ast.d.ts} +0 -0
  51. /package/dist/{src/compatibility → compatibility}/index.d.ts +0 -0
  52. /package/dist/{src/helpers.d.ts → helpers.d.ts} +0 -0
  53. /package/dist/{src/index.d.ts → index.d.ts} +0 -0
  54. /package/dist/{src/json.d.ts → json.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,211 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.compatibilityIssues = void 0;
4
- const ast_1 = require("../ast");
5
- /*
6
- * 1 -> Old version
7
- * 2 -> New version
8
- */
9
- function checkClientToServer(path, issues, t1, t2) {
10
- var _a, _b;
11
- if (t1 instanceof ast_1.TypeReference) {
12
- checkClientToServer(path, issues, t1.type, t2);
13
- return;
14
- }
15
- if (t2 instanceof ast_1.TypeReference) {
16
- checkClientToServer(path, issues, t1, t2.type);
17
- return;
18
- }
19
- if (!(t1 instanceof ast_1.OptionalType) && t2 instanceof ast_1.OptionalType) {
20
- checkClientToServer(path, issues, t1, t2.base);
21
- return;
22
- }
23
- if (t1 instanceof ast_1.OptionalType && t2 instanceof ast_1.OptionalType) {
24
- checkClientToServer(path, issues, t1.base, t2.base);
25
- return;
26
- }
27
- if (t1 instanceof ast_1.OptionalType && !(t2 instanceof ast_1.OptionalType)) {
28
- issues.push(`${path} was optional, but now it isn't. If the client sends a null, it will be invalid. Add the optional annotation back.`);
29
- checkClientToServer(path, issues, t1.base, t2);
30
- return;
31
- }
32
- if (t1 instanceof ast_1.ArrayType && t2 instanceof ast_1.ArrayType) {
33
- checkClientToServer(path, issues, t1.base, t2.base);
34
- return;
35
- }
36
- if (t1 instanceof ast_1.StructType && t2 instanceof ast_1.StructType) {
37
- for (const field2 of t2.fields) {
38
- const field1 = t1.fields.find(x => x.name === field2.name);
39
- if (!field1) {
40
- if (field2.type instanceof ast_1.OptionalType) {
41
- continue;
42
- }
43
- else {
44
- issues.push(`${path}.${field2.name} didn't exist before and isn't optional. Make it optional.`);
45
- continue;
46
- }
47
- }
48
- checkClientToServer(`${path}.${field1.name}`, issues, field1.type, field2.type);
49
- }
50
- return;
51
- }
52
- if ((t1 instanceof ast_1.UIntPrimitiveType && t2 instanceof ast_1.IntPrimitiveType) ||
53
- (t1 instanceof ast_1.IntPrimitiveType && t2 instanceof ast_1.FloatPrimitiveType) ||
54
- (t1 instanceof ast_1.MoneyPrimitiveType && t2 instanceof ast_1.IntPrimitiveType) ||
55
- (t1 instanceof ast_1.MoneyPrimitiveType && t2 instanceof ast_1.UIntPrimitiveType) ||
56
- (t1 instanceof ast_1.UIntPrimitiveType && t2 instanceof ast_1.BigIntPrimitiveType) ||
57
- (t1 instanceof ast_1.IntPrimitiveType && t2 instanceof ast_1.BigIntPrimitiveType) ||
58
- (t1 instanceof ast_1.MoneyPrimitiveType && t2 instanceof ast_1.BigIntPrimitiveType) ||
59
- (t1 instanceof ast_1.BigIntPrimitiveType && t2 instanceof ast_1.StringPrimitiveType) ||
60
- (t1 instanceof ast_1.UuidPrimitiveType && t2 instanceof ast_1.StringPrimitiveType) ||
61
- (t1 instanceof ast_1.XmlPrimitiveType && t2 instanceof ast_1.StringPrimitiveType) ||
62
- (t1 instanceof ast_1.DatePrimitiveType && t2 instanceof ast_1.StringPrimitiveType) ||
63
- (t1 instanceof ast_1.DateTimePrimitiveType && t2 instanceof ast_1.StringPrimitiveType) ||
64
- (t1 instanceof ast_1.CpfPrimitiveType && t2 instanceof ast_1.StringPrimitiveType) ||
65
- (t1 instanceof ast_1.CnpjPrimitiveType && t2 instanceof ast_1.StringPrimitiveType) ||
66
- (t1 instanceof ast_1.HexPrimitiveType && t2 instanceof ast_1.StringPrimitiveType) ||
67
- (t1 instanceof ast_1.HtmlPrimitiveType && t2 instanceof ast_1.StringPrimitiveType) ||
68
- (t1 instanceof ast_1.Base64PrimitiveType && t2 instanceof ast_1.StringPrimitiveType) ||
69
- (t1 instanceof ast_1.BytesPrimitiveType && t2 instanceof ast_1.StringPrimitiveType) ||
70
- (t1 instanceof ast_1.BytesPrimitiveType && t2 instanceof ast_1.Base64PrimitiveType) ||
71
- (t1 instanceof ast_1.UrlPrimitiveType && t2 instanceof ast_1.StringPrimitiveType) ||
72
- (t1 instanceof ast_1.EmailPrimitiveType && t2 instanceof ast_1.StringPrimitiveType) ||
73
- (t1 instanceof ast_1.DecimalPrimitiveType && t2 instanceof ast_1.StringPrimitiveType) ||
74
- (t1 instanceof ast_1.EnumType && t2 instanceof ast_1.StringPrimitiveType)) {
75
- return;
76
- }
77
- if (t1 instanceof ast_1.EnumType && t2 instanceof ast_1.EnumType) {
78
- for (const value of t1.values) {
79
- const other = t2.values.find(v => v.value === value.value);
80
- if (other) {
81
- 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([]));
82
- }
83
- else {
84
- issues.push(`The enum at ${path} used to accept the value "${value.value}" that doesn't exist now. Clients that send it will fail.`);
85
- }
86
- }
87
- return;
88
- }
89
- if (t1.constructor.name === t2.constructor.name) {
90
- return;
91
- }
92
- issues.push(`${path} was ${t1.name} and now it is ${t2.name}. They are not compatible.`);
93
- }
94
- function checkServerToClient(path, issues, t1, t2) {
95
- var _a, _b;
96
- if (t1 instanceof ast_1.TypeReference) {
97
- checkServerToClient(path, issues, t1.type, t2);
98
- return;
99
- }
100
- if (t2 instanceof ast_1.TypeReference) {
101
- checkServerToClient(path, issues, t1, t2.type);
102
- return;
103
- }
104
- if (t1 instanceof ast_1.OptionalType && !(t2 instanceof ast_1.OptionalType)) {
105
- checkServerToClient(path, issues, t1.base, t2);
106
- return;
107
- }
108
- if (t1 instanceof ast_1.OptionalType && t2 instanceof ast_1.OptionalType) {
109
- checkServerToClient(path, issues, t1.base, t2.base);
110
- return;
111
- }
112
- if (!(t1 instanceof ast_1.OptionalType) && t2 instanceof ast_1.OptionalType) {
113
- issues.push(`${path} wasn't optional, but now it is. If the client receives a null, it will crash. Remove the optional annotation.`);
114
- checkServerToClient(path, issues, t1, t2.base);
115
- return;
116
- }
117
- if (t1 instanceof ast_1.ArrayType && t2 instanceof ast_1.ArrayType) {
118
- checkServerToClient(path, issues, t1.base, t2.base);
119
- return;
120
- }
121
- if (t1 instanceof ast_1.StructType && t2 instanceof ast_1.StructType) {
122
- for (const field1 of t1.fields) {
123
- const field2 = t2.fields.find(x => x.name === field1.name);
124
- if (!field2) {
125
- if (field1.type instanceof ast_1.OptionalType) {
126
- continue;
127
- }
128
- else {
129
- issues.push(`${path}.${field1.name} used to exist with type ${field1.type.name}, but it's now missing. Add it back.`);
130
- continue;
131
- }
132
- }
133
- checkServerToClient(`${path}.${field1.name}`, issues, field1.type, field2.type);
134
- }
135
- return;
136
- }
137
- if ((t1 instanceof ast_1.IntPrimitiveType && t2 instanceof ast_1.UIntPrimitiveType) ||
138
- (t1 instanceof ast_1.FloatPrimitiveType && t2 instanceof ast_1.IntPrimitiveType) ||
139
- (t1 instanceof ast_1.IntPrimitiveType && t2 instanceof ast_1.MoneyPrimitiveType) ||
140
- (t1 instanceof ast_1.UIntPrimitiveType && t2 instanceof ast_1.MoneyPrimitiveType) ||
141
- (t1 instanceof ast_1.BigIntPrimitiveType && t2 instanceof ast_1.UIntPrimitiveType) ||
142
- (t1 instanceof ast_1.BigIntPrimitiveType && t2 instanceof ast_1.IntPrimitiveType) ||
143
- (t1 instanceof ast_1.BigIntPrimitiveType && t2 instanceof ast_1.MoneyPrimitiveType) ||
144
- (t1 instanceof ast_1.StringPrimitiveType && t2 instanceof ast_1.BigIntPrimitiveType) ||
145
- (t1 instanceof ast_1.StringPrimitiveType && t2 instanceof ast_1.UuidPrimitiveType) ||
146
- (t1 instanceof ast_1.StringPrimitiveType && t2 instanceof ast_1.XmlPrimitiveType) ||
147
- (t1 instanceof ast_1.StringPrimitiveType && t2 instanceof ast_1.DatePrimitiveType) ||
148
- (t1 instanceof ast_1.StringPrimitiveType && t2 instanceof ast_1.DateTimePrimitiveType) ||
149
- (t1 instanceof ast_1.StringPrimitiveType && t2 instanceof ast_1.CpfPrimitiveType) ||
150
- (t1 instanceof ast_1.StringPrimitiveType && t2 instanceof ast_1.CnpjPrimitiveType) ||
151
- (t1 instanceof ast_1.StringPrimitiveType && t2 instanceof ast_1.HexPrimitiveType) ||
152
- (t1 instanceof ast_1.StringPrimitiveType && t2 instanceof ast_1.HtmlPrimitiveType) ||
153
- (t1 instanceof ast_1.StringPrimitiveType && t2 instanceof ast_1.Base64PrimitiveType) ||
154
- (t1 instanceof ast_1.StringPrimitiveType && t2 instanceof ast_1.BytesPrimitiveType) ||
155
- (t1 instanceof ast_1.Base64PrimitiveType && t2 instanceof ast_1.BytesPrimitiveType) ||
156
- (t1 instanceof ast_1.StringPrimitiveType && t2 instanceof ast_1.UrlPrimitiveType) ||
157
- (t1 instanceof ast_1.StringPrimitiveType && t2 instanceof ast_1.EmailPrimitiveType) ||
158
- (t1 instanceof ast_1.StringPrimitiveType && t2 instanceof ast_1.DecimalPrimitiveType) ||
159
- (t1 instanceof ast_1.StringPrimitiveType && t2 instanceof ast_1.EnumType)) {
160
- return;
161
- }
162
- if (t1 instanceof ast_1.EnumType && t2 instanceof ast_1.EnumType) {
163
- for (const value of t2.values) {
164
- const other = t1.values.find(v => v.value === value.value);
165
- if (other) {
166
- 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([]));
167
- }
168
- else {
169
- issues.push(`The enum at ${path} now has the value "${value.value}" that didn't exist before. Client will crash if it receives it`);
170
- }
171
- }
172
- return;
173
- }
174
- if (t1.constructor.name === t2.constructor.name) {
175
- return;
176
- }
177
- issues.push(`${path} was ${t1.name} and now it is ${t2.name}. They are not compatible.`);
178
- }
179
- function compatibilityIssues(ast1, ast2) {
180
- const issues = [];
181
- for (const op1 of ast1.operations) {
182
- const op2 = ast2.operations.find(x => x.name === op1.name);
183
- if (!op2) {
184
- issues.push(`function ${op1.name} used to exist, but it's now missing. Add it back.`);
185
- continue;
186
- }
187
- checkServerToClient(`${op1.name}.ret`, issues, op1.returnType, op2.returnType);
188
- for (const arg2 of op2.args) {
189
- const arg1 = op1.args.find(x => x.name === arg2.name);
190
- if (!arg1) {
191
- if (arg2.type instanceof ast_1.OptionalType) {
192
- continue;
193
- }
194
- else {
195
- issues.push(`${op1.name}.args.${arg2.name} didn't exist before and isn't optional. Make it optional.`);
196
- continue;
197
- }
198
- }
199
- checkClientToServer(`${op1.name}.args.${arg1.name}`, issues, arg1.type, arg2.type);
200
- }
201
- }
202
- for (const err1 of ast1.errors) {
203
- const err2 = ast2.errors.find(x => x.name === err1.name);
204
- if (!err2) {
205
- continue;
206
- }
207
- checkServerToClient(`${err1.name}.data`, issues, err1.dataType, err2.dataType);
208
- }
209
- return issues;
210
- }
211
- exports.compatibilityIssues = compatibilityIssues;
@@ -1,23 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.hasType = void 0;
4
- const visitor_1 = require("./semantic/visitor");
5
- class HasTypeVisitor extends visitor_1.Visitor {
6
- constructor(root, type) {
7
- super(root);
8
- this.type = type;
9
- this.found = false;
10
- }
11
- visit(node) {
12
- if (node.constructor === this.type) {
13
- this.found = true;
14
- }
15
- super.visit(node);
16
- }
17
- }
18
- function hasType(root, type) {
19
- const visitor = new HasTypeVisitor(root, type);
20
- visitor.process();
21
- return visitor.found;
22
- }
23
- exports.hasType = hasType;
package/dist/src/index.js DELETED
@@ -1,29 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.hasType = exports.Visitor = exports.jsonToAst = exports.astToJson = void 0;
18
- __exportStar(require("./ast"), exports);
19
- __exportStar(require("./compatibility"), exports);
20
- var json_1 = require("./json");
21
- Object.defineProperty(exports, "astToJson", { enumerable: true, get: function () { return json_1.astToJson; } });
22
- Object.defineProperty(exports, "jsonToAst", { enumerable: true, get: function () { return json_1.jsonToAst; } });
23
- __exportStar(require("./lexer"), exports);
24
- __exportStar(require("./parser"), exports);
25
- __exportStar(require("./token"), exports);
26
- var visitor_1 = require("./semantic/visitor");
27
- Object.defineProperty(exports, "Visitor", { enumerable: true, get: function () { return visitor_1.Visitor; } });
28
- var helpers_1 = require("./helpers");
29
- Object.defineProperty(exports, "hasType", { enumerable: true, get: function () { return helpers_1.hasType; } });
package/dist/src/json.js DELETED
@@ -1,234 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.jsonToAst = exports.astToJson = void 0;
4
- const ast_1 = require("./ast");
5
- const analyser_1 = require("./semantic/analyser");
6
- const utils_1 = require("./utils");
7
- function annotationToJson(ann) {
8
- if (ann instanceof ast_1.DescriptionAnnotation) {
9
- return { type: "description", value: ann.text };
10
- }
11
- else if (ann instanceof ast_1.ThrowsAnnotation) {
12
- return { type: "throws", value: ann.error };
13
- }
14
- else if (ann instanceof ast_1.RestAnnotation) {
15
- return {
16
- type: "rest",
17
- value: {
18
- bodyVariable: ann.bodyVariable,
19
- headers: [...ann.headers.entries()].sort(([a], [b]) => a.localeCompare(b)),
20
- method: ann.method,
21
- path: ann.path,
22
- pathVariables: ann.pathVariables,
23
- queryVariables: [...ann.queryVariables].sort((a, b) => a.localeCompare(b)),
24
- },
25
- };
26
- }
27
- else if (ann instanceof ast_1.HiddenAnnotation) {
28
- return { type: "hidden", value: null };
29
- }
30
- else if (ann instanceof ast_1.StatusCodeAnnotation) {
31
- return { type: "statusCode", value: ann.statusCode };
32
- }
33
- throw new Error(`BUG: annotationToJson with ${ann.constructor.name}`);
34
- }
35
- function annotationFromJson(json) {
36
- switch (json.type) {
37
- case "description":
38
- return new ast_1.DescriptionAnnotation(json.value);
39
- case "throws":
40
- return new ast_1.ThrowsAnnotation(json.value);
41
- case "rest": {
42
- const { method, path, pathVariables, queryVariables, headers, bodyVariable } = json.value;
43
- return new ast_1.RestAnnotation(method, path, pathVariables, queryVariables, new Map(headers), bodyVariable);
44
- }
45
- case "hidden":
46
- return new ast_1.HiddenAnnotation();
47
- case "statusCode":
48
- return new ast_1.StatusCodeAnnotation(json.value);
49
- default:
50
- throw new Error(`BUG: annotationFromJson with ${json.type}`);
51
- }
52
- }
53
- function astToJson(ast) {
54
- var _a, _b, _c, _d;
55
- const annotations = {};
56
- const typeTable = {};
57
- function processType(type) {
58
- if (type instanceof ast_1.ArrayType) {
59
- return `${processType(type.base)}[]`;
60
- }
61
- else if (type instanceof ast_1.OptionalType) {
62
- return `${processType(type.base)}?`;
63
- }
64
- return type.name;
65
- }
66
- for (const { name, fields } of ast.structTypes) {
67
- if (name in typeTable) {
68
- throw new Error(`Duplicate struct type ${name}`);
69
- }
70
- typeTable[name] = {};
71
- const obj = typeTable[name];
72
- for (const field of fields) {
73
- obj[field.name] = processType(field.type);
74
- for (const ann of field.annotations) {
75
- if (ann instanceof ast_1.DescriptionAnnotation) {
76
- const target = `type.${name}.${field.name}`;
77
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
78
- (_a = annotations[target]) !== null && _a !== void 0 ? _a : (annotations[target] = []);
79
- annotations[target].push(annotationToJson(ann));
80
- }
81
- }
82
- }
83
- }
84
- for (const { name, values } of ast.enumTypes) {
85
- if (name in typeTable) {
86
- throw new Error(`Duplicate enum type ${name}`);
87
- }
88
- typeTable[name] = values.map(v => {
89
- if (!v.struct) {
90
- return v.value;
91
- }
92
- return [v.value, v.struct.name];
93
- });
94
- }
95
- for (const { name, type } of ast.typeDefinitions) {
96
- if (type instanceof ast_1.StructType || type instanceof ast_1.EnumType) {
97
- continue;
98
- }
99
- typeTable[name] = processType(type);
100
- }
101
- const functionTable = {};
102
- for (const op of ast.operations) {
103
- const args = {};
104
- for (const arg of op.args) {
105
- args[arg.name] = processType(arg.type);
106
- for (const ann of arg.annotations) {
107
- if (ann instanceof ast_1.DescriptionAnnotation) {
108
- const target = `fn.${op.name}.${arg.name}`;
109
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
110
- (_b = annotations[target]) !== null && _b !== void 0 ? _b : (annotations[target] = []);
111
- annotations[target].push(annotationToJson(ann));
112
- }
113
- }
114
- }
115
- functionTable[op.name] = {
116
- args,
117
- ret: processType(op.returnType),
118
- };
119
- for (const ann of op.annotations) {
120
- const target = `fn.${op.name}`;
121
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
122
- (_c = annotations[target]) !== null && _c !== void 0 ? _c : (annotations[target] = []);
123
- annotations[target].push(annotationToJson(ann));
124
- }
125
- }
126
- const errors = ast.errors.map(error => error.dataType instanceof ast_1.VoidPrimitiveType ? error.name : [error.name, error.dataType.name]);
127
- for (const error of ast.errors) {
128
- for (const ann of error.annotations) {
129
- const target = `error.${error.name}`;
130
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
131
- (_d = annotations[target]) !== null && _d !== void 0 ? _d : (annotations[target] = []);
132
- annotations[target].push(annotationToJson(ann));
133
- }
134
- }
135
- return {
136
- annotations,
137
- errors,
138
- functionTable,
139
- typeTable,
140
- };
141
- }
142
- exports.astToJson = astToJson;
143
- function jsonToAst(json) {
144
- var _a, _b;
145
- const operations = [];
146
- const typeDefinition = [];
147
- const solveEnumValueStructRef = [];
148
- function processType(description, typeName) {
149
- var _a;
150
- if (typeof description === "string") {
151
- const primitiveClass = utils_1.primitiveToAstClass.get(description);
152
- if (primitiveClass) {
153
- return new primitiveClass();
154
- }
155
- else if (description.endsWith("?")) {
156
- return new ast_1.OptionalType(processType(description.slice(0, description.length - 1)));
157
- }
158
- else if (description.endsWith("[]")) {
159
- return new ast_1.ArrayType(processType(description.slice(0, description.length - 2)));
160
- }
161
- return new ast_1.TypeReference(description);
162
- }
163
- else if (Array.isArray(description)) {
164
- return new ast_1.EnumType(description.map(v => {
165
- if (Array.isArray(v)) {
166
- const [value, structName] = v;
167
- const enumValue = new ast_1.EnumValue(value);
168
- solveEnumValueStructRef.push([enumValue, structName]);
169
- return enumValue;
170
- }
171
- return new ast_1.EnumValue(v);
172
- }));
173
- }
174
- const fields = [];
175
- for (const fieldName of Object.keys(description)) {
176
- const field = new ast_1.Field(fieldName, processType(description[fieldName]));
177
- if (typeName) {
178
- const target = `type.${typeName}.${fieldName}`;
179
- for (const annotationJson of (_a = json.annotations[target]) !== null && _a !== void 0 ? _a : []) {
180
- field.annotations.push(annotationFromJson(annotationJson));
181
- }
182
- }
183
- fields.push(field);
184
- }
185
- return new ast_1.StructType(fields);
186
- }
187
- for (const [typeName, description] of Object.entries(json.typeTable)) {
188
- const type = processType(description, typeName);
189
- typeDefinition.push(new ast_1.TypeDefinition(typeName, type));
190
- }
191
- for (const [functionName, func] of Object.entries(json.functionTable)) {
192
- const args = Object.keys(func.args).map(argName => {
193
- var _a;
194
- const field = new ast_1.Field(argName, processType(func.args[argName]));
195
- const target = `fn.${functionName}.${argName}`;
196
- for (const annotationJson of (_a = json.annotations[target]) !== null && _a !== void 0 ? _a : []) {
197
- field.annotations.push(annotationFromJson(annotationJson));
198
- }
199
- return field;
200
- });
201
- const op = new ast_1.FunctionOperation(functionName, args, processType(func.ret));
202
- const target = `fn.${functionName}`;
203
- for (const annotationJson of (_a = json.annotations[target]) !== null && _a !== void 0 ? _a : []) {
204
- op.annotations.push(annotationFromJson(annotationJson));
205
- }
206
- operations.push(op);
207
- }
208
- for (const [enumValue, structName] of solveEnumValueStructRef) {
209
- const struct = (_b = typeDefinition.find(def => def.name === structName)) === null || _b === void 0 ? void 0 : _b.type;
210
- if (struct instanceof ast_1.StructType) {
211
- enumValue.struct = struct;
212
- }
213
- }
214
- const errors = json.errors.map(error => {
215
- var _a;
216
- let errorNode;
217
- if (Array.isArray(error)) {
218
- const [name, type] = error;
219
- errorNode = new ast_1.ErrorNode(name, processType(type));
220
- }
221
- else {
222
- errorNode = new ast_1.ErrorNode(error, new ast_1.VoidPrimitiveType());
223
- }
224
- const target = `error.${errorNode.name}`;
225
- for (const annotationJson of (_a = json.annotations[target]) !== null && _a !== void 0 ? _a : []) {
226
- errorNode.annotations.push(annotationFromJson(annotationJson));
227
- }
228
- return errorNode;
229
- });
230
- const ast = new ast_1.AstRoot(typeDefinition, operations, errors);
231
- (0, analyser_1.analyse)(ast);
232
- return ast;
233
- }
234
- exports.jsonToAst = jsonToAst;