@sdkgen/parser 1.5.5 → 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 +261 -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.d.ts +1 -1
- package/dist/src/parser.js +107 -49
- package/dist/src/restparser.js +9 -12
- 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 -1537
- package/package.json +6 -7
- 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/src/json.js
CHANGED
|
@@ -4,71 +4,109 @@ exports.jsonToAst = exports.astToJson = void 0;
|
|
|
4
4
|
const ast_1 = require("./ast");
|
|
5
5
|
const analyser_1 = require("./semantic/analyser");
|
|
6
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].sort((a, b) => a.localeCompare(b)),
|
|
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
|
+
throw new Error(`BUG: annotationToJson with ${ann.constructor.name}`);
|
|
31
|
+
}
|
|
32
|
+
function annotationFromJson(json) {
|
|
33
|
+
switch (json.type) {
|
|
34
|
+
case "description":
|
|
35
|
+
return new ast_1.DescriptionAnnotation(json.value);
|
|
36
|
+
case "throws":
|
|
37
|
+
return new ast_1.ThrowsAnnotation(json.value);
|
|
38
|
+
case "rest": {
|
|
39
|
+
const { method, path, pathVariables, queryVariables, headers, bodyVariable } = json.value;
|
|
40
|
+
return new ast_1.RestAnnotation(method, path, pathVariables, queryVariables, new Map(headers), bodyVariable);
|
|
41
|
+
}
|
|
42
|
+
case "hidden":
|
|
43
|
+
return new ast_1.HiddenAnnotation();
|
|
44
|
+
default:
|
|
45
|
+
throw new Error(`BUG: annotationFromJson with ${json.type}`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
7
48
|
function astToJson(ast) {
|
|
49
|
+
var _a, _b, _c;
|
|
8
50
|
const annotations = {};
|
|
9
51
|
const typeTable = {};
|
|
52
|
+
function processType(type) {
|
|
53
|
+
if (type instanceof ast_1.ArrayType) {
|
|
54
|
+
return `${processType(type.base)}[]`;
|
|
55
|
+
}
|
|
56
|
+
else if (type instanceof ast_1.OptionalType) {
|
|
57
|
+
return `${processType(type.base)}?`;
|
|
58
|
+
}
|
|
59
|
+
return type.name;
|
|
60
|
+
}
|
|
10
61
|
for (const { name, fields } of ast.structTypes) {
|
|
11
62
|
typeTable[name] = {};
|
|
12
63
|
const obj = typeTable[name];
|
|
13
64
|
for (const field of fields) {
|
|
14
|
-
obj[field.name] = field.type
|
|
65
|
+
obj[field.name] = processType(field.type);
|
|
15
66
|
for (const ann of field.annotations) {
|
|
16
67
|
if (ann instanceof ast_1.DescriptionAnnotation) {
|
|
17
68
|
const target = `type.${name}.${field.name}`;
|
|
18
|
-
annotations[target]
|
|
19
|
-
|
|
20
|
-
list.push({ type: "description", value: ann.text });
|
|
69
|
+
(_a = annotations[target]) !== null && _a !== void 0 ? _a : (annotations[target] = []);
|
|
70
|
+
annotations[target].push(annotationToJson(ann));
|
|
21
71
|
}
|
|
22
72
|
}
|
|
23
73
|
}
|
|
24
74
|
}
|
|
25
75
|
for (const { name, values } of ast.enumTypes) {
|
|
26
|
-
typeTable[name] = values.map(v =>
|
|
76
|
+
typeTable[name] = values.map(v => {
|
|
77
|
+
if (!v.struct) {
|
|
78
|
+
return v.value;
|
|
79
|
+
}
|
|
80
|
+
return [v.value, v.struct.name];
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
for (const { name, type } of ast.typeDefinitions) {
|
|
84
|
+
if (type instanceof ast_1.StructType || type instanceof ast_1.EnumType) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
typeTable[name] = processType(type);
|
|
27
88
|
}
|
|
28
89
|
const functionTable = {};
|
|
29
90
|
for (const op of ast.operations) {
|
|
30
91
|
const args = {};
|
|
31
92
|
for (const arg of op.args) {
|
|
32
|
-
args[arg.name] = arg.type
|
|
93
|
+
args[arg.name] = processType(arg.type);
|
|
33
94
|
for (const ann of arg.annotations) {
|
|
34
95
|
if (ann instanceof ast_1.DescriptionAnnotation) {
|
|
35
|
-
const target = `fn.${op.
|
|
36
|
-
annotations[target]
|
|
37
|
-
|
|
38
|
-
list.push({ type: "description", value: ann.text });
|
|
96
|
+
const target = `fn.${op.name}.${arg.name}`;
|
|
97
|
+
(_b = annotations[target]) !== null && _b !== void 0 ? _b : (annotations[target] = []);
|
|
98
|
+
annotations[target].push(annotationToJson(ann));
|
|
39
99
|
}
|
|
40
100
|
}
|
|
41
101
|
}
|
|
42
|
-
functionTable[op.
|
|
102
|
+
functionTable[op.name] = {
|
|
43
103
|
args,
|
|
44
|
-
ret: op.returnType
|
|
104
|
+
ret: processType(op.returnType),
|
|
45
105
|
};
|
|
46
106
|
for (const ann of op.annotations) {
|
|
47
|
-
const target = `fn.${op.
|
|
48
|
-
annotations[target]
|
|
49
|
-
|
|
50
|
-
if (ann instanceof ast_1.DescriptionAnnotation) {
|
|
51
|
-
list.push({ type: "description", value: ann.text });
|
|
52
|
-
}
|
|
53
|
-
if (ann instanceof ast_1.ThrowsAnnotation) {
|
|
54
|
-
list.push({ type: "throws", value: ann.error });
|
|
55
|
-
}
|
|
56
|
-
if (ann instanceof ast_1.RestAnnotation) {
|
|
57
|
-
list.push({
|
|
58
|
-
type: "rest",
|
|
59
|
-
value: {
|
|
60
|
-
bodyVariable: ann.bodyVariable,
|
|
61
|
-
headers: [...ann.headers.entries()],
|
|
62
|
-
method: ann.method,
|
|
63
|
-
path: ann.path,
|
|
64
|
-
pathVariables: ann.pathVariables,
|
|
65
|
-
queryVariables: ann.queryVariables,
|
|
66
|
-
},
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
if (ann instanceof ast_1.HiddenAnnotation) {
|
|
70
|
-
list.push({ type: "hidden", value: null });
|
|
71
|
-
}
|
|
107
|
+
const target = `fn.${op.name}`;
|
|
108
|
+
(_c = annotations[target]) !== null && _c !== void 0 ? _c : (annotations[target] = []);
|
|
109
|
+
annotations[target].push(annotationToJson(ann));
|
|
72
110
|
}
|
|
73
111
|
}
|
|
74
112
|
const errors = ast.errors.map(error => (error.dataType instanceof ast_1.VoidPrimitiveType ? error.name : [error.name, error.dataType.name]));
|
|
@@ -81,9 +119,10 @@ function astToJson(ast) {
|
|
|
81
119
|
}
|
|
82
120
|
exports.astToJson = astToJson;
|
|
83
121
|
function jsonToAst(json) {
|
|
84
|
-
var _a;
|
|
122
|
+
var _a, _b;
|
|
85
123
|
const operations = [];
|
|
86
124
|
const typeDefinition = [];
|
|
125
|
+
const solveEnumValueStructRef = [];
|
|
87
126
|
function processType(description, typeName) {
|
|
88
127
|
var _a;
|
|
89
128
|
if (typeof description === "string") {
|
|
@@ -100,7 +139,15 @@ function jsonToAst(json) {
|
|
|
100
139
|
return new ast_1.TypeReference(description);
|
|
101
140
|
}
|
|
102
141
|
else if (Array.isArray(description)) {
|
|
103
|
-
return new ast_1.EnumType(description.map(v =>
|
|
142
|
+
return new ast_1.EnumType(description.map(v => {
|
|
143
|
+
if (Array.isArray(v)) {
|
|
144
|
+
const [value, structName] = v;
|
|
145
|
+
const enumValue = new ast_1.EnumValue(value);
|
|
146
|
+
solveEnumValueStructRef.push([enumValue, structName]);
|
|
147
|
+
return enumValue;
|
|
148
|
+
}
|
|
149
|
+
return new ast_1.EnumValue(v);
|
|
150
|
+
}));
|
|
104
151
|
}
|
|
105
152
|
const fields = [];
|
|
106
153
|
for (const fieldName of Object.keys(description)) {
|
|
@@ -108,14 +155,12 @@ function jsonToAst(json) {
|
|
|
108
155
|
if (typeName) {
|
|
109
156
|
const target = `type.${typeName}.${fieldName}`;
|
|
110
157
|
for (const annotationJson of (_a = json.annotations[target]) !== null && _a !== void 0 ? _a : []) {
|
|
111
|
-
|
|
112
|
-
field.annotations.push(new ast_1.DescriptionAnnotation(annotationJson.value));
|
|
113
|
-
}
|
|
158
|
+
field.annotations.push(annotationFromJson(annotationJson));
|
|
114
159
|
}
|
|
115
160
|
}
|
|
116
161
|
fields.push(field);
|
|
117
162
|
}
|
|
118
|
-
return new ast_1.StructType(fields
|
|
163
|
+
return new ast_1.StructType(fields);
|
|
119
164
|
}
|
|
120
165
|
for (const [typeName, description] of Object.entries(json.typeTable)) {
|
|
121
166
|
const type = processType(description, typeName);
|
|
@@ -127,31 +172,23 @@ function jsonToAst(json) {
|
|
|
127
172
|
const field = new ast_1.Field(argName, processType(func.args[argName]));
|
|
128
173
|
const target = `fn.${functionName}.${argName}`;
|
|
129
174
|
for (const annotationJson of (_a = json.annotations[target]) !== null && _a !== void 0 ? _a : []) {
|
|
130
|
-
|
|
131
|
-
field.annotations.push(new ast_1.DescriptionAnnotation(annotationJson.value));
|
|
132
|
-
}
|
|
175
|
+
field.annotations.push(annotationFromJson(annotationJson));
|
|
133
176
|
}
|
|
134
177
|
return field;
|
|
135
178
|
});
|
|
136
179
|
const op = new ast_1.FunctionOperation(functionName, args, processType(func.ret));
|
|
137
180
|
const target = `fn.${functionName}`;
|
|
138
181
|
for (const annotationJson of (_a = json.annotations[target]) !== null && _a !== void 0 ? _a : []) {
|
|
139
|
-
|
|
140
|
-
op.annotations.push(new ast_1.DescriptionAnnotation(annotationJson.value));
|
|
141
|
-
}
|
|
142
|
-
else if (annotationJson.type === "throws") {
|
|
143
|
-
op.annotations.push(new ast_1.ThrowsAnnotation(annotationJson.value));
|
|
144
|
-
}
|
|
145
|
-
else if (annotationJson.type === "rest" && typeof annotationJson.value === "object") {
|
|
146
|
-
const { method, path, pathVariables, queryVariables, headers, bodyVariable } = annotationJson.value;
|
|
147
|
-
op.annotations.push(new ast_1.RestAnnotation(method, path, pathVariables, queryVariables, new Map(headers), bodyVariable));
|
|
148
|
-
}
|
|
149
|
-
else if (annotationJson.type === "hidden") {
|
|
150
|
-
op.annotations.push(new ast_1.HiddenAnnotation());
|
|
151
|
-
}
|
|
182
|
+
op.annotations.push(annotationFromJson(annotationJson));
|
|
152
183
|
}
|
|
153
184
|
operations.push(op);
|
|
154
185
|
}
|
|
186
|
+
for (const [enumValue, structName] of solveEnumValueStructRef) {
|
|
187
|
+
const struct = (_b = typeDefinition.find(def => def.name === structName)) === null || _b === void 0 ? void 0 : _b.type;
|
|
188
|
+
if (struct instanceof ast_1.StructType) {
|
|
189
|
+
enumValue.struct = struct;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
155
192
|
const errors = json.errors.map(error => {
|
|
156
193
|
if (Array.isArray(error)) {
|
|
157
194
|
return new ast_1.ErrorNode(error[0], processType(error[1]));
|
|
@@ -159,7 +196,7 @@ function jsonToAst(json) {
|
|
|
159
196
|
return new ast_1.ErrorNode(error, new ast_1.VoidPrimitiveType());
|
|
160
197
|
});
|
|
161
198
|
const ast = new ast_1.AstRoot(typeDefinition, operations, errors);
|
|
162
|
-
analyser_1.analyse(ast);
|
|
199
|
+
(0, analyser_1.analyse)(ast);
|
|
163
200
|
return ast;
|
|
164
201
|
}
|
|
165
202
|
exports.jsonToAst = jsonToAst;
|
package/dist/src/lexer.js
CHANGED
|
@@ -230,14 +230,8 @@ class Lexer {
|
|
|
230
230
|
case "import":
|
|
231
231
|
token = new token_1.ImportKeywordToken();
|
|
232
232
|
break;
|
|
233
|
-
case "get":
|
|
234
|
-
token = new token_1.GetKeywordToken();
|
|
235
|
-
break;
|
|
236
|
-
case "function":
|
|
237
|
-
token = new token_1.FunctionKeywordToken("function");
|
|
238
|
-
break;
|
|
239
233
|
case "fn":
|
|
240
|
-
token = new token_1.
|
|
234
|
+
token = new token_1.FnKeywordToken();
|
|
241
235
|
break;
|
|
242
236
|
case "true":
|
|
243
237
|
token = new token_1.TrueKeywordToken();
|
package/dist/src/parser.d.ts
CHANGED
|
@@ -7,9 +7,9 @@ export declare class Parser {
|
|
|
7
7
|
private token;
|
|
8
8
|
private annotations;
|
|
9
9
|
private warnings;
|
|
10
|
+
private readFileSync;
|
|
10
11
|
constructor(source: Lexer | string);
|
|
11
12
|
private nextToken;
|
|
12
|
-
private get currentFileName();
|
|
13
13
|
private multiExpect;
|
|
14
14
|
private expect;
|
|
15
15
|
parse(): AstRoot;
|
package/dist/src/parser.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/* eslint-disable no-sync */
|
|
3
|
+
/* eslint-disable no-loop-func */
|
|
2
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
5
|
exports.Parser = exports.ParserError = void 0;
|
|
4
|
-
/* eslint-disable no-loop-func */
|
|
5
|
-
const fs_1 = require("fs");
|
|
6
|
-
const path_1 = require("path");
|
|
7
6
|
const ast_1 = require("./ast");
|
|
8
7
|
const lexer_1 = require("./lexer");
|
|
9
8
|
const restparser_1 = require("./restparser");
|
|
@@ -18,11 +17,21 @@ class Parser {
|
|
|
18
17
|
this.token = null;
|
|
19
18
|
this.annotations = [];
|
|
20
19
|
this.warnings = [];
|
|
20
|
+
this.readFileSync = () => {
|
|
21
|
+
throw "Not implemented";
|
|
22
|
+
};
|
|
23
|
+
try {
|
|
24
|
+
// eslint-disable-next-line
|
|
25
|
+
this.readFileSync = require("fs").readFileSync;
|
|
26
|
+
}
|
|
27
|
+
catch (e) {
|
|
28
|
+
// do nothing
|
|
29
|
+
}
|
|
21
30
|
if (source instanceof lexer_1.Lexer) {
|
|
22
31
|
this.lexers = [source];
|
|
23
32
|
}
|
|
24
33
|
else {
|
|
25
|
-
this.lexers = [new lexer_1.Lexer(
|
|
34
|
+
this.lexers = [new lexer_1.Lexer(this.readFileSync(source).toString(), source)];
|
|
26
35
|
}
|
|
27
36
|
this.nextToken();
|
|
28
37
|
}
|
|
@@ -35,9 +44,6 @@ class Parser {
|
|
|
35
44
|
this.lexers.pop();
|
|
36
45
|
}
|
|
37
46
|
}
|
|
38
|
-
get currentFileName() {
|
|
39
|
-
return this.token === null ? null : this.token.location.filename;
|
|
40
|
-
}
|
|
41
47
|
multiExpect(matcher) {
|
|
42
48
|
if (!this.token) {
|
|
43
49
|
throw new ParserError(`Expected ${Object.keys(matcher)
|
|
@@ -86,18 +92,15 @@ class Parser {
|
|
|
86
92
|
ErrorKeywordToken: () => {
|
|
87
93
|
errors.push(this.parseError());
|
|
88
94
|
},
|
|
89
|
-
|
|
90
|
-
operations.push(this.parseOperation());
|
|
91
|
-
},
|
|
92
|
-
GetKeywordToken: () => {
|
|
95
|
+
FnKeywordToken: () => {
|
|
93
96
|
operations.push(this.parseOperation());
|
|
94
97
|
},
|
|
95
98
|
ImportKeywordToken: () => {
|
|
96
99
|
this.checkCannotHaveAnnotationsHere();
|
|
97
100
|
this.nextToken();
|
|
98
101
|
const pathToken = this.expect(token_1.StringLiteralToken);
|
|
99
|
-
const resolvedPath =
|
|
100
|
-
this.lexers.push(new lexer_1.Lexer(
|
|
102
|
+
const resolvedPath = pathToken.location.filename.replace(/[^/\\]+$/u, `${pathToken.value}.sdkgen`);
|
|
103
|
+
this.lexers.push(new lexer_1.Lexer(this.readFileSync(resolvedPath).toString(), resolvedPath));
|
|
101
104
|
this.nextToken();
|
|
102
105
|
},
|
|
103
106
|
TypeKeywordToken: () => {
|
|
@@ -107,7 +110,7 @@ class Parser {
|
|
|
107
110
|
}
|
|
108
111
|
const ast = new ast_1.AstRoot(typeDefinition, operations, errors);
|
|
109
112
|
ast.warnings = this.warnings;
|
|
110
|
-
analyser_1.analyse(ast);
|
|
113
|
+
(0, analyser_1.analyse)(ast);
|
|
111
114
|
return ast;
|
|
112
115
|
}
|
|
113
116
|
acceptAnnotations() {
|
|
@@ -126,7 +129,7 @@ class Parser {
|
|
|
126
129
|
break;
|
|
127
130
|
case "rest":
|
|
128
131
|
try {
|
|
129
|
-
this.annotations.push(restparser_1.parseRestAnnotation(body).at(this.token));
|
|
132
|
+
this.annotations.push((0, restparser_1.parseRestAnnotation)(body).at(this.token));
|
|
130
133
|
}
|
|
131
134
|
catch (error) {
|
|
132
135
|
throw new ParserError(`${error instanceof Error ? error.message : error} at ${this.token.location}`);
|
|
@@ -155,7 +158,7 @@ class Parser {
|
|
|
155
158
|
const nameToken = this.expect(token_1.IdentifierToken);
|
|
156
159
|
const name = nameToken.value;
|
|
157
160
|
if (!/[A-Z]/u.test(name[0])) {
|
|
158
|
-
throw new ParserError(`The custom type name must start with an uppercase letter, but found
|
|
161
|
+
throw new ParserError(`The custom type name must start with an uppercase letter, but found ${JSON.stringify(name)} at ${nameToken.location}`);
|
|
159
162
|
}
|
|
160
163
|
this.nextToken();
|
|
161
164
|
const { annotations } = this;
|
|
@@ -172,7 +175,7 @@ class Parser {
|
|
|
172
175
|
const nameToken = this.expect(token_1.IdentifierToken);
|
|
173
176
|
const name = nameToken.value;
|
|
174
177
|
if (!/[A-Z]/u.test(name[0])) {
|
|
175
|
-
throw new ParserError(`Error name must start with an uppercase letter, but found
|
|
178
|
+
throw new ParserError(`Error name must start with an uppercase letter, but found ${JSON.stringify(name)} at ${nameToken.location}`);
|
|
176
179
|
}
|
|
177
180
|
this.nextToken();
|
|
178
181
|
let type = new ast_1.VoidPrimitiveType();
|
|
@@ -187,37 +190,67 @@ class Parser {
|
|
|
187
190
|
parseOperation() {
|
|
188
191
|
let { annotations } = this;
|
|
189
192
|
this.annotations = [];
|
|
190
|
-
|
|
191
|
-
FunctionKeywordToken: token => token,
|
|
192
|
-
GetKeywordToken: token => token,
|
|
193
|
-
});
|
|
193
|
+
this.expect(token_1.FnKeywordToken);
|
|
194
194
|
this.nextToken();
|
|
195
|
-
if (["get", "function"].includes(openingToken.maybeAsIdentifier().value)) {
|
|
196
|
-
this.warnings.push(`Keyword '${openingToken.maybeAsIdentifier().value}' is deprecated at ${openingToken.location}. Use 'fn' instead.`);
|
|
197
|
-
}
|
|
198
195
|
const name = this.expect(token_1.IdentifierToken).value;
|
|
199
196
|
this.nextToken();
|
|
200
197
|
this.expect(token_1.ParensOpenSymbolToken);
|
|
201
198
|
this.nextToken();
|
|
202
199
|
const argNames = new Set();
|
|
203
|
-
const
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
200
|
+
const fieldsAndSpreads = [];
|
|
201
|
+
let finished = false;
|
|
202
|
+
let parensCloseToken;
|
|
203
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
204
|
+
while (!finished) {
|
|
205
|
+
this.acceptAnnotations();
|
|
206
|
+
this.multiExpect({
|
|
207
|
+
ParensCloseSymbolToken: token => {
|
|
208
|
+
this.checkCannotHaveAnnotationsHere();
|
|
209
|
+
this.nextToken();
|
|
210
|
+
finished = true;
|
|
211
|
+
parensCloseToken = token;
|
|
212
|
+
},
|
|
213
|
+
IdentifierToken: () => {
|
|
214
|
+
const field = this.parseField();
|
|
215
|
+
if (argNames.has(field.name)) {
|
|
216
|
+
throw new ParserError(`Cannot redeclare argument '${field.name}'`);
|
|
217
|
+
}
|
|
218
|
+
argNames.add(field.name);
|
|
219
|
+
fieldsAndSpreads.push(field);
|
|
220
|
+
this.multiExpect({
|
|
221
|
+
ParensCloseSymbolToken: token => {
|
|
222
|
+
this.checkCannotHaveAnnotationsHere();
|
|
223
|
+
this.nextToken();
|
|
224
|
+
finished = true;
|
|
225
|
+
parensCloseToken = token;
|
|
226
|
+
},
|
|
227
|
+
CommaSymbolToken: () => this.nextToken(),
|
|
228
|
+
});
|
|
229
|
+
},
|
|
230
|
+
SpreadSymbolToken: () => {
|
|
231
|
+
this.checkCannotHaveAnnotationsHere();
|
|
232
|
+
this.nextToken();
|
|
233
|
+
const identToken = this.expect(token_1.IdentifierToken);
|
|
234
|
+
this.nextToken();
|
|
235
|
+
if (!/[A-Z]/u.test(identToken.value[0])) {
|
|
236
|
+
throw new ParserError(`Expected a type but found ${JSON.stringify(identToken.value)} at ${identToken.location}`);
|
|
237
|
+
}
|
|
238
|
+
fieldsAndSpreads.push(new ast_1.Spread(new ast_1.TypeReference(identToken.value).at(identToken)).at(identToken));
|
|
239
|
+
this.multiExpect({
|
|
240
|
+
ParensCloseSymbolToken: token => {
|
|
241
|
+
this.checkCannotHaveAnnotationsHere();
|
|
242
|
+
this.nextToken();
|
|
243
|
+
finished = true;
|
|
244
|
+
parensCloseToken = token;
|
|
245
|
+
},
|
|
246
|
+
CommaSymbolToken: () => this.nextToken(),
|
|
247
|
+
});
|
|
248
|
+
},
|
|
249
|
+
});
|
|
217
250
|
}
|
|
218
251
|
for (const annotation of annotations) {
|
|
219
252
|
if (annotation instanceof ast_1.ArgDescriptionAnnotation) {
|
|
220
|
-
const arg =
|
|
253
|
+
const arg = fieldsAndSpreads.find(x => x instanceof ast_1.Field && x.name === annotation.argName);
|
|
221
254
|
if (!arg) {
|
|
222
255
|
throw new ParserError(`Argument '${annotation.argName}' not found, at ${annotation.location}`);
|
|
223
256
|
}
|
|
@@ -225,14 +258,12 @@ class Parser {
|
|
|
225
258
|
}
|
|
226
259
|
}
|
|
227
260
|
annotations = annotations.filter(ann => !(ann instanceof ast_1.ArgDescriptionAnnotation));
|
|
228
|
-
const parensCloseToken = this.expect(token_1.ParensCloseSymbolToken);
|
|
229
|
-
this.nextToken();
|
|
230
261
|
let returnType = new ast_1.VoidPrimitiveType().at(parensCloseToken);
|
|
231
262
|
if (this.token instanceof token_1.ColonSymbolToken) {
|
|
232
263
|
this.nextToken();
|
|
233
264
|
returnType = this.parseType();
|
|
234
265
|
}
|
|
235
|
-
const op =
|
|
266
|
+
const op = new ast_1.FunctionOperation(name, fieldsAndSpreads, returnType);
|
|
236
267
|
op.annotations = annotations;
|
|
237
268
|
return op;
|
|
238
269
|
}
|
|
@@ -259,6 +290,34 @@ class Parser {
|
|
|
259
290
|
this.annotations = [];
|
|
260
291
|
enumType.values.push(enumValue);
|
|
261
292
|
this.nextToken();
|
|
293
|
+
if (!(this.token instanceof token_1.ParensOpenSymbolToken)) {
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
this.nextToken();
|
|
297
|
+
const fieldNames = new Set();
|
|
298
|
+
const fields = [];
|
|
299
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
300
|
+
while (this.token && this.token.maybeAsIdentifier() instanceof token_1.IdentifierToken) {
|
|
301
|
+
const field = this.parseField();
|
|
302
|
+
// "tag" is a reserved name on tagged enum fields and can't be used.
|
|
303
|
+
if (field.name === "tag") {
|
|
304
|
+
field.name = "_tag";
|
|
305
|
+
}
|
|
306
|
+
if (fieldNames.has(field.name)) {
|
|
307
|
+
throw new ParserError(`Cannot redeclare argument '${field.name}'`);
|
|
308
|
+
}
|
|
309
|
+
fieldNames.add(field.name);
|
|
310
|
+
fields.push(field);
|
|
311
|
+
if (this.token instanceof token_1.CommaSymbolToken) {
|
|
312
|
+
this.nextToken();
|
|
313
|
+
}
|
|
314
|
+
else {
|
|
315
|
+
break;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
enumValue.struct = new ast_1.StructType(fields).atLocation(enumValue.location);
|
|
319
|
+
this.expect(token_1.ParensCloseSymbolToken);
|
|
320
|
+
this.nextToken();
|
|
262
321
|
},
|
|
263
322
|
});
|
|
264
323
|
}
|
|
@@ -290,8 +349,7 @@ class Parser {
|
|
|
290
349
|
parseStruct() {
|
|
291
350
|
const openingToken = this.expect(token_1.CurlyOpenSymbolToken);
|
|
292
351
|
this.nextToken();
|
|
293
|
-
const
|
|
294
|
-
const spreads = [];
|
|
352
|
+
const fieldsAndSpreads = [];
|
|
295
353
|
const fieldNames = new Set();
|
|
296
354
|
let finished = false;
|
|
297
355
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
@@ -309,7 +367,7 @@ class Parser {
|
|
|
309
367
|
throw new ParserError(`Cannot redeclare field '${field.name}'`);
|
|
310
368
|
}
|
|
311
369
|
fieldNames.add(field.name);
|
|
312
|
-
|
|
370
|
+
fieldsAndSpreads.push(field);
|
|
313
371
|
},
|
|
314
372
|
SpreadSymbolToken: () => {
|
|
315
373
|
this.checkCannotHaveAnnotationsHere();
|
|
@@ -317,13 +375,13 @@ class Parser {
|
|
|
317
375
|
const identToken = this.expect(token_1.IdentifierToken);
|
|
318
376
|
this.nextToken();
|
|
319
377
|
if (!/[A-Z]/u.test(identToken.value[0])) {
|
|
320
|
-
throw new ParserError(`Expected a type but found
|
|
378
|
+
throw new ParserError(`Expected a type but found ${JSON.stringify(identToken.value)} at ${identToken.location}`);
|
|
321
379
|
}
|
|
322
|
-
|
|
380
|
+
fieldsAndSpreads.push(new ast_1.Spread(new ast_1.TypeReference(identToken.value).at(identToken)).at(identToken));
|
|
323
381
|
},
|
|
324
382
|
});
|
|
325
383
|
}
|
|
326
|
-
return new ast_1.StructType(
|
|
384
|
+
return new ast_1.StructType(fieldsAndSpreads).at(openingToken);
|
|
327
385
|
}
|
|
328
386
|
parseType() {
|
|
329
387
|
this.checkCannotHaveAnnotationsHere();
|
|
@@ -333,7 +391,7 @@ class Parser {
|
|
|
333
391
|
IdentifierToken: token => {
|
|
334
392
|
this.nextToken();
|
|
335
393
|
if (!/[A-Z]/u.test(token.value[0])) {
|
|
336
|
-
throw new ParserError(`Expected a type but found
|
|
394
|
+
throw new ParserError(`Expected a type but found ${JSON.stringify(token.value)} at ${token.location}`);
|
|
337
395
|
}
|
|
338
396
|
return new ast_1.TypeReference(token.value).at(token);
|
|
339
397
|
},
|
package/dist/src/restparser.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.parseRestAnnotation = void 0;
|
|
4
|
-
const path_1 = require("path");
|
|
5
4
|
const ast_1 = require("./ast");
|
|
6
5
|
function scanHeaders(text) {
|
|
7
6
|
var _a;
|
|
8
|
-
|
|
7
|
+
// Header name: https://tools.ietf.org/html/rfc2616#section-4.2
|
|
8
|
+
const headerRegex = /\[header (?<header>[^()<>@,;:\\"/[\]?={}\s\t]+): \{(?<name>\w+)\}\]/gu;
|
|
9
9
|
const headers = new Map();
|
|
10
10
|
let match;
|
|
11
11
|
while ((match = headerRegex.exec(text)) !== null) {
|
|
12
12
|
if (((_a = match.groups) === null || _a === void 0 ? void 0 : _a.header) && match.groups.name) {
|
|
13
|
-
headers.set(match.groups.header, match.groups.name);
|
|
13
|
+
headers.set(match.groups.header.toLowerCase(), match.groups.name);
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
return headers;
|
|
@@ -41,24 +41,21 @@ function parseRestAnnotation(text) {
|
|
|
41
41
|
if (!["GET", "POST", "PUT", "DELETE", "PATCH"].includes(method)) {
|
|
42
42
|
throw new Error(`Unsupported method '${method}'`);
|
|
43
43
|
}
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
// eslint-disable-next-line prefer-destructuring
|
|
45
|
+
let path = fragments[1];
|
|
46
|
+
if (!path.startsWith("/")) {
|
|
46
47
|
throw new Error(`Invalid path`);
|
|
47
48
|
}
|
|
48
|
-
if (parsedPath.dir === "/") {
|
|
49
|
-
parsedPath.dir = "";
|
|
50
|
-
}
|
|
51
49
|
let queryVariables = [];
|
|
52
|
-
if (
|
|
53
|
-
const [base, ...queryArray] =
|
|
54
|
-
|
|
50
|
+
if (path.includes("?")) {
|
|
51
|
+
const [base, ...queryArray] = path.split("?");
|
|
52
|
+
path = base;
|
|
55
53
|
const query = queryArray.join("?");
|
|
56
54
|
if (!/^\{\w+\}(?:&\{\w+\})*$/u.test(query)) {
|
|
57
55
|
throw new Error(`Invalid querystring on path`);
|
|
58
56
|
}
|
|
59
57
|
queryVariables = scanVariables(query);
|
|
60
58
|
}
|
|
61
|
-
const path = `${parsedPath.dir}/${parsedPath.base}`;
|
|
62
59
|
const pathVariables = scanVariables(path);
|
|
63
60
|
const remaining = fragments.slice(2).join(" ");
|
|
64
61
|
const headers = scanHeaders(remaining);
|
package/dist/src/semantic/{06_give_struct_and_enum_names.d.ts → 02_give_struct_and_enum_names.d.ts}
RENAMED
|
File without changes
|
package/dist/src/semantic/{06_give_struct_and_enum_names.js → 02_give_struct_and_enum_names.js}
RENAMED
|
@@ -18,7 +18,7 @@ class GiveStructAndEnumNamesVisitor extends visitor_1.Visitor {
|
|
|
18
18
|
this.path = [`${node.name}Data`];
|
|
19
19
|
super.visit(node);
|
|
20
20
|
}
|
|
21
|
-
else if (node instanceof ast_1.
|
|
21
|
+
else if (node instanceof ast_1.FunctionOperation) {
|
|
22
22
|
this.path = [node.name];
|
|
23
23
|
super.visit(node);
|
|
24
24
|
}
|
|
@@ -27,6 +27,11 @@ class GiveStructAndEnumNamesVisitor extends visitor_1.Visitor {
|
|
|
27
27
|
super.visit(node);
|
|
28
28
|
this.path.pop();
|
|
29
29
|
}
|
|
30
|
+
else if (node instanceof ast_1.EnumValue) {
|
|
31
|
+
this.path.push(node.value);
|
|
32
|
+
super.visit(node);
|
|
33
|
+
this.path.pop();
|
|
34
|
+
}
|
|
30
35
|
else if (node instanceof ast_1.StructType || node instanceof ast_1.EnumType) {
|
|
31
36
|
node.name = this.path.map(s => s[0].toUpperCase() + s.slice(1)).join("");
|
|
32
37
|
const previous = this.names.get(node.name);
|
|
File without changes
|
|
File without changes
|
|
@@ -8,9 +8,14 @@ class CheckDontReturnSecretVisitor extends visitor_1.Visitor {
|
|
|
8
8
|
super(...arguments);
|
|
9
9
|
this.isInReturn = false;
|
|
10
10
|
this.path = [];
|
|
11
|
+
this.visited = new Set();
|
|
11
12
|
}
|
|
12
13
|
visit(node) {
|
|
13
|
-
if (node
|
|
14
|
+
if (this.visited.has(node)) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
this.visited.add(node);
|
|
18
|
+
if (node instanceof ast_1.FunctionOperation) {
|
|
14
19
|
this.isInReturn = true;
|
|
15
20
|
this.path.push(`${node.name}(...)`);
|
|
16
21
|
this.visit(node.returnType);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Spread } from "..";
|
|
2
|
+
import type { AstNode } from "../ast";
|
|
3
|
+
import { Field, StructType } from "../ast";
|
|
4
|
+
import { Visitor } from "./visitor";
|
|
5
|
+
export declare class ExpandSpreadsVisitor extends Visitor {
|
|
6
|
+
processed: Set<StructType>;
|
|
7
|
+
expandSpread(fields: Field[], fieldsAndSpreads: Array<Field | Spread>): void;
|
|
8
|
+
visit(node: AstNode): void;
|
|
9
|
+
}
|