@aeriajs/compiler 0.0.9 → 0.0.11
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/codegen/generateContracts.js +12 -7
- package/dist/codegen/generateContracts.mjs +12 -7
- package/dist/codegen/utils.d.ts +1 -1
- package/dist/codegen/utils.js +6 -7
- package/dist/codegen/utils.mjs +5 -5
- package/dist/compile.d.ts +1 -0
- package/dist/compile.js +15 -4
- package/dist/compile.mjs +12 -3
- package/dist/parser.js +21 -8
- package/dist/parser.mjs +21 -8
- package/package.json +1 -1
|
@@ -31,8 +31,9 @@ const makeJSContractsCode = (contractAst) => {
|
|
|
31
31
|
};
|
|
32
32
|
const declarations = contractAst.map((contractNode) => {
|
|
33
33
|
const { name, kind, roles, response, ...contractProperty } = contractNode;
|
|
34
|
-
let responseString
|
|
34
|
+
let responseString;
|
|
35
35
|
if (response) {
|
|
36
|
+
responseString = '';
|
|
36
37
|
if (Array.isArray(response)) {
|
|
37
38
|
const responseArray = [];
|
|
38
39
|
for (const responseElement of response) {
|
|
@@ -47,10 +48,12 @@ const makeJSContractsCode = (contractAst) => {
|
|
|
47
48
|
}
|
|
48
49
|
}
|
|
49
50
|
const contractSchema = (0, utils_js_1.getProperties)(contractProperty);
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
51
|
+
if (responseString) {
|
|
52
|
+
contractSchema.response = {
|
|
53
|
+
[utils_js_1.UnquotedSymbol]: responseString,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
return `export const ${name} = defineContract(${(0, utils_js_1.stringify)(contractSchema)})`;
|
|
54
57
|
}).join('\n\n');
|
|
55
58
|
return `import { ${Array.from(imports).join(', ')} } from \'aeria\'\n\n` + declarations;
|
|
56
59
|
};
|
|
@@ -76,9 +79,11 @@ const makeTSContractsCode = (contractAst) => {
|
|
|
76
79
|
}
|
|
77
80
|
}
|
|
78
81
|
const contractProperties = (0, utils_js_1.getProperties)(contractSchema);
|
|
79
|
-
return `export declare const ${contractNode.name}
|
|
82
|
+
return `export declare const ${contractNode.name}: ${(0, utils_js_1.stringify)({
|
|
80
83
|
...contractProperties,
|
|
81
|
-
|
|
84
|
+
...(responseSchema && {
|
|
85
|
+
response: responseSchema,
|
|
86
|
+
}),
|
|
82
87
|
})}`;
|
|
83
88
|
}).join('\n\n');
|
|
84
89
|
};
|
|
@@ -26,8 +26,9 @@ const makeJSContractsCode = (contractAst) => {
|
|
|
26
26
|
};
|
|
27
27
|
const declarations = contractAst.map((contractNode) => {
|
|
28
28
|
const { name, kind, roles, response, ...contractProperty } = contractNode;
|
|
29
|
-
let responseString
|
|
29
|
+
let responseString;
|
|
30
30
|
if (response) {
|
|
31
|
+
responseString = "";
|
|
31
32
|
if (Array.isArray(response)) {
|
|
32
33
|
const responseArray = [];
|
|
33
34
|
for (const responseElement of response) {
|
|
@@ -41,10 +42,12 @@ const makeJSContractsCode = (contractAst) => {
|
|
|
41
42
|
}
|
|
42
43
|
}
|
|
43
44
|
const contractSchema = getProperties(contractProperty);
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
if (responseString) {
|
|
46
|
+
contractSchema.response = {
|
|
47
|
+
[UnquotedSymbol]: responseString
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
return `export const ${name} = defineContract(${stringify(contractSchema)})`;
|
|
48
51
|
}).join("\n\n");
|
|
49
52
|
return `import { ${Array.from(imports).join(", ")} } from 'aeria'
|
|
50
53
|
|
|
@@ -69,9 +72,11 @@ const makeTSContractsCode = (contractAst) => {
|
|
|
69
72
|
}
|
|
70
73
|
}
|
|
71
74
|
const contractProperties = getProperties(contractSchema);
|
|
72
|
-
return `export declare const ${contractNode.name}
|
|
75
|
+
return `export declare const ${contractNode.name}: ${stringify({
|
|
73
76
|
...contractProperties,
|
|
74
|
-
|
|
77
|
+
...responseSchema && {
|
|
78
|
+
response: responseSchema
|
|
79
|
+
}
|
|
75
80
|
})}`;
|
|
76
81
|
}).join("\n\n");
|
|
77
82
|
};
|
package/dist/codegen/utils.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ export declare const makeASTImports: (ast: AST.Node[], initialImports?: Record<s
|
|
|
13
13
|
code: string[];
|
|
14
14
|
modifiedSymbols: Record<string, string>;
|
|
15
15
|
};
|
|
16
|
-
export declare const propertyToSchema: (
|
|
16
|
+
export declare const propertyToSchema: ({ property, nestedProperties }: Pick<AST.PropertyNode, "property" | "nestedProperties">) => Property;
|
|
17
17
|
/** Transforms the AST properties to the format of aeria schema properties */
|
|
18
18
|
export declare const getProperties: <TProperties extends Record<string, AST.PropertyNode | AST.PropertyNode[]>, TReturnType = TProperties[keyof TProperties] extends unknown[] ? Record<string, Property[]> : Record<string, Property>>(properties: TProperties) => TReturnType;
|
|
19
19
|
export declare const UnquotedSymbol: unique symbol;
|
package/dist/codegen/utils.js
CHANGED
|
@@ -14,8 +14,7 @@ exports.DEFAULT_FUNCTIONS = [
|
|
|
14
14
|
];
|
|
15
15
|
exports.ArraySymbol = Symbol('array');
|
|
16
16
|
const getExposedFunctions = (astFunctions) => {
|
|
17
|
-
return Object.fromEntries(Object.entries(astFunctions)
|
|
18
|
-
.map(([key, value]) => [
|
|
17
|
+
return Object.fromEntries(Object.entries(astFunctions).map(([key, value]) => [
|
|
19
18
|
key,
|
|
20
19
|
value.accessCondition,
|
|
21
20
|
]));
|
|
@@ -56,22 +55,22 @@ const makeASTImports = (ast, initialImports) => {
|
|
|
56
55
|
};
|
|
57
56
|
};
|
|
58
57
|
exports.makeASTImports = makeASTImports;
|
|
59
|
-
const propertyToSchema = (
|
|
60
|
-
const propertySchema =
|
|
58
|
+
const propertyToSchema = ({ property, nestedProperties }) => {
|
|
59
|
+
const propertySchema = property;
|
|
61
60
|
if ('$ref' in propertySchema) {
|
|
62
61
|
propertySchema.$ref = (0, exports.getCollectionId)(propertySchema.$ref);
|
|
63
62
|
}
|
|
64
63
|
else if ('items' in propertySchema && '$ref' in propertySchema.items) {
|
|
65
64
|
propertySchema.items.$ref = (0, exports.getCollectionId)(propertySchema.items.$ref);
|
|
66
65
|
}
|
|
67
|
-
if (
|
|
66
|
+
if (nestedProperties && 'type' in propertySchema) {
|
|
68
67
|
if (propertySchema.type === 'object' && 'properties' in propertySchema) {
|
|
69
|
-
propertySchema.properties = (0, exports.getProperties)(
|
|
68
|
+
propertySchema.properties = (0, exports.getProperties)(nestedProperties);
|
|
70
69
|
}
|
|
71
70
|
else if (propertySchema.type === 'array') {
|
|
72
71
|
propertySchema.items = {
|
|
73
72
|
type: 'object',
|
|
74
|
-
properties: (0, exports.getProperties)(
|
|
73
|
+
properties: (0, exports.getProperties)(nestedProperties),
|
|
75
74
|
};
|
|
76
75
|
}
|
|
77
76
|
}
|
package/dist/codegen/utils.mjs
CHANGED
|
@@ -48,20 +48,20 @@ export const makeASTImports = (ast, initialImports) => {
|
|
|
48
48
|
modifiedSymbols
|
|
49
49
|
};
|
|
50
50
|
};
|
|
51
|
-
export const propertyToSchema = (
|
|
52
|
-
const propertySchema =
|
|
51
|
+
export const propertyToSchema = ({ property, nestedProperties }) => {
|
|
52
|
+
const propertySchema = property;
|
|
53
53
|
if ("$ref" in propertySchema) {
|
|
54
54
|
propertySchema.$ref = getCollectionId(propertySchema.$ref);
|
|
55
55
|
} else if ("items" in propertySchema && "$ref" in propertySchema.items) {
|
|
56
56
|
propertySchema.items.$ref = getCollectionId(propertySchema.items.$ref);
|
|
57
57
|
}
|
|
58
|
-
if (
|
|
58
|
+
if (nestedProperties && "type" in propertySchema) {
|
|
59
59
|
if (propertySchema.type === "object" && "properties" in propertySchema) {
|
|
60
|
-
propertySchema.properties = getProperties(
|
|
60
|
+
propertySchema.properties = getProperties(nestedProperties);
|
|
61
61
|
} else if (propertySchema.type === "array") {
|
|
62
62
|
propertySchema.items = {
|
|
63
63
|
type: "object",
|
|
64
|
-
properties: getProperties(
|
|
64
|
+
properties: getProperties(nestedProperties)
|
|
65
65
|
};
|
|
66
66
|
}
|
|
67
67
|
}
|
package/dist/compile.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { CompilationOptions, CompilationResult } from './types.js';
|
|
2
2
|
import { Diagnostic } from './diagnostic.js';
|
|
3
|
+
export declare const FILE_PRECEDENCE: string[];
|
|
3
4
|
export declare const parseAndCheck: (sources: Record<string, string>, options?: Pick<CompilationOptions, "languageServer">) => Promise<CompilationResult>;
|
|
4
5
|
export declare const generateScaffolding: (options: CompilationOptions) => Promise<string[]>;
|
|
5
6
|
export declare const compileFromFiles: (schemaDir: string, options: CompilationOptions) => Promise<CompilationResult | {
|
package/dist/compile.js
CHANGED
|
@@ -33,7 +33,7 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.compileFromFiles = exports.generateScaffolding = exports.parseAndCheck = void 0;
|
|
36
|
+
exports.compileFromFiles = exports.generateScaffolding = exports.parseAndCheck = exports.FILE_PRECEDENCE = void 0;
|
|
37
37
|
const diagnostic_js_1 = require("./diagnostic.js");
|
|
38
38
|
const lexer_js_1 = require("./lexer.js");
|
|
39
39
|
const parser_js_1 = require("./parser.js");
|
|
@@ -41,6 +41,7 @@ const semantic_js_1 = require("./semantic.js");
|
|
|
41
41
|
const codegen_js_1 = require("./codegen.js");
|
|
42
42
|
const path = __importStar(require("node:path"));
|
|
43
43
|
const fs = __importStar(require("node:fs"));
|
|
44
|
+
exports.FILE_PRECEDENCE = ['contract'];
|
|
44
45
|
const parseAndCheck = async (sources, options = {}) => {
|
|
45
46
|
const errors = [];
|
|
46
47
|
let errorCount = 0;
|
|
@@ -80,10 +81,20 @@ const generateScaffolding = async (options) => {
|
|
|
80
81
|
};
|
|
81
82
|
exports.generateScaffolding = generateScaffolding;
|
|
82
83
|
const compileFromFiles = async (schemaDir, options) => {
|
|
83
|
-
const fileList = await fs.promises.
|
|
84
|
+
const fileList = await Array.fromAsync(fs.promises.glob(`${schemaDir}/*.aeria`));
|
|
85
|
+
const sortedFileList = fileList.sort((a, b) => {
|
|
86
|
+
const aIndex = exports.FILE_PRECEDENCE.findIndex((file) => a.split('/').at(-1).startsWith(file));
|
|
87
|
+
const bIndex = exports.FILE_PRECEDENCE.findIndex((file) => b.split('/').at(-1).startsWith(file));
|
|
88
|
+
if (!~aIndex && !~bIndex) {
|
|
89
|
+
return 1;
|
|
90
|
+
}
|
|
91
|
+
return aIndex > bIndex
|
|
92
|
+
? 1
|
|
93
|
+
: -1;
|
|
94
|
+
});
|
|
84
95
|
const sources = {};
|
|
85
|
-
for (const file of
|
|
86
|
-
sources[file] = await fs.promises.readFile(
|
|
96
|
+
for (const file of sortedFileList) {
|
|
97
|
+
sources[file] = await fs.promises.readFile(file, {
|
|
87
98
|
encoding: 'utf-8',
|
|
88
99
|
});
|
|
89
100
|
}
|
package/dist/compile.mjs
CHANGED
|
@@ -6,6 +6,7 @@ import { analyze } from "./semantic.mjs";
|
|
|
6
6
|
import { generateCode } from "./codegen.mjs";
|
|
7
7
|
import * as path from "node:path";
|
|
8
8
|
import * as fs from "node:fs";
|
|
9
|
+
export const FILE_PRECEDENCE = ["contract"];
|
|
9
10
|
export const parseAndCheck = async (sources, options = {}) => {
|
|
10
11
|
const errors = [];
|
|
11
12
|
let errorCount = 0;
|
|
@@ -42,10 +43,18 @@ export const generateScaffolding = async (options) => {
|
|
|
42
43
|
return directories;
|
|
43
44
|
};
|
|
44
45
|
export const compileFromFiles = async (schemaDir, options) => {
|
|
45
|
-
const fileList = await fs.promises.
|
|
46
|
+
const fileList = await Array.fromAsync(fs.promises.glob(`${schemaDir}/*.aeria`));
|
|
47
|
+
const sortedFileList = fileList.sort((a, b) => {
|
|
48
|
+
const aIndex = FILE_PRECEDENCE.findIndex((file) => a.split("/").at(-1).startsWith(file));
|
|
49
|
+
const bIndex = FILE_PRECEDENCE.findIndex((file) => b.split("/").at(-1).startsWith(file));
|
|
50
|
+
if (!~aIndex && !~bIndex) {
|
|
51
|
+
return 1;
|
|
52
|
+
}
|
|
53
|
+
return aIndex > bIndex ? 1 : -1;
|
|
54
|
+
});
|
|
46
55
|
const sources = {};
|
|
47
|
-
for (const file of
|
|
48
|
-
sources[file] = await fs.promises.readFile(
|
|
56
|
+
for (const file of sortedFileList) {
|
|
57
|
+
sources[file] = await fs.promises.readFile(file, {
|
|
49
58
|
encoding: "utf-8"
|
|
50
59
|
});
|
|
51
60
|
}
|
package/dist/parser.js
CHANGED
|
@@ -126,9 +126,19 @@ const parse = (tokens) => {
|
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
128
|
};
|
|
129
|
-
const parseArray = (
|
|
130
|
-
consume(token_js_1.TokenType.LeftSquareBracket);
|
|
129
|
+
const parseArray = (types) => {
|
|
130
|
+
const { location } = consume(token_js_1.TokenType.LeftSquareBracket);
|
|
131
131
|
const array = [];
|
|
132
|
+
let type;
|
|
133
|
+
for (const typeCandidate of types) {
|
|
134
|
+
if (match(typeCandidate)) {
|
|
135
|
+
type = typeCandidate;
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (!type) {
|
|
140
|
+
throw new diagnostic_js_1.Diagnostic(`array got an invalid type, accepted ones are: ${types.join(' | ')}`, location);
|
|
141
|
+
}
|
|
132
142
|
while (!match(token_js_1.TokenType.RightSquareBracket)) {
|
|
133
143
|
const { value } = consume(type);
|
|
134
144
|
array.push(value);
|
|
@@ -189,7 +199,10 @@ const parse = (tokens) => {
|
|
|
189
199
|
return true;
|
|
190
200
|
};
|
|
191
201
|
if ('enum' in property && attributeName === 'values') {
|
|
192
|
-
property.enum = parseArray(
|
|
202
|
+
property.enum = parseArray([
|
|
203
|
+
token_js_1.TokenType.QuotedString,
|
|
204
|
+
token_js_1.TokenType.Number,
|
|
205
|
+
]);
|
|
193
206
|
return;
|
|
194
207
|
}
|
|
195
208
|
switch (attributeName) {
|
|
@@ -216,7 +229,7 @@ const parse = (tokens) => {
|
|
|
216
229
|
case 'form':
|
|
217
230
|
case 'populate':
|
|
218
231
|
case 'indexes': {
|
|
219
|
-
property[attributeName] = parseArray(token_js_1.TokenType.Identifier);
|
|
232
|
+
property[attributeName] = parseArray([token_js_1.TokenType.Identifier]);
|
|
220
233
|
return;
|
|
221
234
|
}
|
|
222
235
|
case 'populateDepth': {
|
|
@@ -229,7 +242,7 @@ const parse = (tokens) => {
|
|
|
229
242
|
switch (attributeName) {
|
|
230
243
|
case 'extensions':
|
|
231
244
|
case 'accept': {
|
|
232
|
-
property[attributeName] = parseArray(token_js_1.TokenType.QuotedString);
|
|
245
|
+
property[attributeName] = parseArray([token_js_1.TokenType.QuotedString]);
|
|
233
246
|
return;
|
|
234
247
|
}
|
|
235
248
|
}
|
|
@@ -246,7 +259,7 @@ const parse = (tokens) => {
|
|
|
246
259
|
}
|
|
247
260
|
case 'mask': {
|
|
248
261
|
if (match(token_js_1.TokenType.LeftSquareBracket)) {
|
|
249
|
-
property[attributeName] = parseArray(token_js_1.TokenType.QuotedString);
|
|
262
|
+
property[attributeName] = parseArray([token_js_1.TokenType.QuotedString]);
|
|
250
263
|
return;
|
|
251
264
|
}
|
|
252
265
|
else {
|
|
@@ -716,7 +729,7 @@ const parse = (tokens) => {
|
|
|
716
729
|
};
|
|
717
730
|
}
|
|
718
731
|
else {
|
|
719
|
-
const value = parseArray(token_js_1.TokenType.QuotedString);
|
|
732
|
+
const value = parseArray([token_js_1.TokenType.QuotedString]);
|
|
720
733
|
functions[functionName] = {
|
|
721
734
|
accessCondition: value,
|
|
722
735
|
};
|
|
@@ -782,7 +795,7 @@ const parse = (tokens) => {
|
|
|
782
795
|
}
|
|
783
796
|
case 'roles':
|
|
784
797
|
case 'requires': {
|
|
785
|
-
const value = parseArray(token_js_1.TokenType.Identifier);
|
|
798
|
+
const value = parseArray([token_js_1.TokenType.Identifier]);
|
|
786
799
|
baseSlots[keyword] = value;
|
|
787
800
|
break;
|
|
788
801
|
}
|
package/dist/parser.mjs
CHANGED
|
@@ -88,9 +88,19 @@ export const parse = (tokens) => {
|
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
90
|
};
|
|
91
|
-
const parseArray = (
|
|
92
|
-
consume(TokenType.LeftSquareBracket);
|
|
91
|
+
const parseArray = (types) => {
|
|
92
|
+
const { location } = consume(TokenType.LeftSquareBracket);
|
|
93
93
|
const array = [];
|
|
94
|
+
let type;
|
|
95
|
+
for (const typeCandidate of types) {
|
|
96
|
+
if (match(typeCandidate)) {
|
|
97
|
+
type = typeCandidate;
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (!type) {
|
|
102
|
+
throw new Diagnostic(`array got an invalid type, accepted ones are: ${types.join(" | ")}`, location);
|
|
103
|
+
}
|
|
94
104
|
while (!match(TokenType.RightSquareBracket)) {
|
|
95
105
|
const { value } = consume(type);
|
|
96
106
|
array.push(value);
|
|
@@ -148,7 +158,10 @@ export const parse = (tokens) => {
|
|
|
148
158
|
return true;
|
|
149
159
|
};
|
|
150
160
|
if ("enum" in property && attributeName === "values") {
|
|
151
|
-
property.enum = parseArray(
|
|
161
|
+
property.enum = parseArray([
|
|
162
|
+
TokenType.QuotedString,
|
|
163
|
+
TokenType.Number
|
|
164
|
+
]);
|
|
152
165
|
return;
|
|
153
166
|
}
|
|
154
167
|
switch (attributeName) {
|
|
@@ -175,7 +188,7 @@ export const parse = (tokens) => {
|
|
|
175
188
|
case "form":
|
|
176
189
|
case "populate":
|
|
177
190
|
case "indexes": {
|
|
178
|
-
property[attributeName] = parseArray(TokenType.Identifier);
|
|
191
|
+
property[attributeName] = parseArray([TokenType.Identifier]);
|
|
179
192
|
return;
|
|
180
193
|
}
|
|
181
194
|
case "populateDepth": {
|
|
@@ -188,7 +201,7 @@ export const parse = (tokens) => {
|
|
|
188
201
|
switch (attributeName) {
|
|
189
202
|
case "extensions":
|
|
190
203
|
case "accept": {
|
|
191
|
-
property[attributeName] = parseArray(TokenType.QuotedString);
|
|
204
|
+
property[attributeName] = parseArray([TokenType.QuotedString]);
|
|
192
205
|
return;
|
|
193
206
|
}
|
|
194
207
|
}
|
|
@@ -205,7 +218,7 @@ export const parse = (tokens) => {
|
|
|
205
218
|
}
|
|
206
219
|
case "mask": {
|
|
207
220
|
if (match(TokenType.LeftSquareBracket)) {
|
|
208
|
-
property[attributeName] = parseArray(TokenType.QuotedString);
|
|
221
|
+
property[attributeName] = parseArray([TokenType.QuotedString]);
|
|
209
222
|
return;
|
|
210
223
|
} else {
|
|
211
224
|
const { value } = consume(TokenType.QuotedString);
|
|
@@ -664,7 +677,7 @@ export const parse = (tokens) => {
|
|
|
664
677
|
accessCondition: value
|
|
665
678
|
};
|
|
666
679
|
} else {
|
|
667
|
-
const value = parseArray(TokenType.QuotedString);
|
|
680
|
+
const value = parseArray([TokenType.QuotedString]);
|
|
668
681
|
functions[functionName] = {
|
|
669
682
|
accessCondition: value
|
|
670
683
|
};
|
|
@@ -729,7 +742,7 @@ export const parse = (tokens) => {
|
|
|
729
742
|
}
|
|
730
743
|
case "roles":
|
|
731
744
|
case "requires": {
|
|
732
|
-
const value = parseArray(TokenType.Identifier);
|
|
745
|
+
const value = parseArray([TokenType.Identifier]);
|
|
733
746
|
baseSlots[keyword] = value;
|
|
734
747
|
break;
|
|
735
748
|
}
|