@aeriajs/compiler 0.0.3 → 0.0.6
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/ast.d.ts +79 -0
- package/dist/ast.js +23 -0
- package/dist/ast.mjs +21 -0
- package/dist/codegen/generateContracts.d.ts +5 -0
- package/dist/codegen/generateContracts.js +84 -0
- package/dist/codegen/generateContracts.mjs +77 -0
- package/dist/codegen/generateExports.d.ts +15 -0
- package/dist/codegen/generateExports.js +41 -0
- package/dist/codegen/generateExports.mjs +32 -0
- package/dist/codegen/generateJSCollections.d.ts +2 -0
- package/dist/codegen/generateJSCollections.js +91 -0
- package/dist/codegen/generateJSCollections.mjs +82 -0
- package/dist/codegen/generateTSCollections.d.ts +2 -0
- package/dist/codegen/generateTSCollections.js +107 -0
- package/dist/codegen/generateTSCollections.mjs +99 -0
- package/dist/codegen/index.d.ts +4 -0
- package/dist/codegen/index.js +20 -0
- package/dist/codegen/index.mjs +5 -0
- package/dist/codegen/utils.d.ts +29 -0
- package/dist/codegen/utils.js +135 -0
- package/dist/codegen/utils.mjs +114 -0
- package/dist/codegen.d.ts +3 -0
- package/dist/codegen.js +102 -0
- package/dist/codegen.mjs +52 -0
- package/dist/compile.d.ts +21 -0
- package/dist/compile.js +96 -0
- package/dist/compile.mjs +57 -0
- package/dist/diagnostic.d.ts +8 -0
- package/dist/diagnostic.js +22 -0
- package/dist/diagnostic.mjs +16 -0
- package/dist/guards.d.ts +3 -0
- package/dist/guards.js +45 -0
- package/dist/guards.mjs +8 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +23 -0
- package/dist/index.mjs +8 -0
- package/dist/lexer.d.ts +14 -0
- package/dist/lexer.js +272 -0
- package/dist/lexer.mjs +270 -0
- package/dist/parser.d.ts +8 -0
- package/dist/parser.js +907 -0
- package/dist/parser.mjs +851 -0
- package/dist/semantic.d.ts +5 -0
- package/dist/semantic.js +149 -0
- package/dist/semantic.mjs +111 -0
- package/dist/token.d.ts +38 -0
- package/dist/token.js +24 -0
- package/dist/token.mjs +22 -0
- package/dist/utils.d.ts +3 -0
- package/dist/utils.js +2 -0
- package/dist/utils.mjs +1 -0
- package/package.json +4 -5
package/dist/ast.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { Property, AccessCondition, CollectionActions, SearchOptions, DescriptionPreset, Icon, OwnershipMode } from '@aeriajs/types';
|
|
2
|
+
import type { ArrayProperties } from './utils.js';
|
|
3
|
+
export declare const LOCATION_SYMBOL: unique symbol;
|
|
4
|
+
export declare const PropertyType: {
|
|
5
|
+
readonly str: "string";
|
|
6
|
+
readonly int: "integer";
|
|
7
|
+
readonly num: "number";
|
|
8
|
+
readonly bool: "boolean";
|
|
9
|
+
readonly enum: "enum";
|
|
10
|
+
readonly date: "string";
|
|
11
|
+
readonly datetime: "string";
|
|
12
|
+
};
|
|
13
|
+
export declare const PropertyModifiers: Record<'Error' | 'Result', ExportSymbol>;
|
|
14
|
+
export type ExportSymbol = {
|
|
15
|
+
packageName: string;
|
|
16
|
+
symbolName: string;
|
|
17
|
+
};
|
|
18
|
+
export type NodeBase<TType> = {
|
|
19
|
+
kind: TType;
|
|
20
|
+
};
|
|
21
|
+
export type PropertyNode = NodeBase<'property'> & {
|
|
22
|
+
modifier?: keyof typeof PropertyModifiers;
|
|
23
|
+
property: Property & {
|
|
24
|
+
[LOCATION_SYMBOL]?: {
|
|
25
|
+
attributes: Record<string, symbol>;
|
|
26
|
+
arrays: {
|
|
27
|
+
[P in ArrayProperties<Extract<Property, {
|
|
28
|
+
properties: unknown;
|
|
29
|
+
}>>]?: symbol[];
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
nestedProperties?: Record<string, PropertyNode>;
|
|
34
|
+
};
|
|
35
|
+
export type CollectionNode = NodeBase<'collection'> & {
|
|
36
|
+
name: string;
|
|
37
|
+
extends?: ExportSymbol;
|
|
38
|
+
owned?: OwnershipMode;
|
|
39
|
+
icon?: Icon;
|
|
40
|
+
actions?: CollectionActions;
|
|
41
|
+
individualActions?: CollectionActions;
|
|
42
|
+
properties: Record<string, PropertyNode>;
|
|
43
|
+
functions?: Record<string, {
|
|
44
|
+
accessCondition: AccessCondition;
|
|
45
|
+
}>;
|
|
46
|
+
required?: Record<string, unknown> | string[];
|
|
47
|
+
indexes?: string[];
|
|
48
|
+
presets?: DescriptionPreset[];
|
|
49
|
+
form?: string[];
|
|
50
|
+
table?: string[];
|
|
51
|
+
filters?: string[];
|
|
52
|
+
search?: SearchOptions<any>;
|
|
53
|
+
[LOCATION_SYMBOL]: {
|
|
54
|
+
arrays: {
|
|
55
|
+
[P in ArrayProperties<CollectionNode>]?: symbol[];
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
export type ContractNode = NodeBase<'contract'> & {
|
|
60
|
+
name: string;
|
|
61
|
+
roles?: AccessCondition;
|
|
62
|
+
query?: PropertyNode;
|
|
63
|
+
payload?: PropertyNode;
|
|
64
|
+
response?: PropertyNode | PropertyNode[];
|
|
65
|
+
};
|
|
66
|
+
export type FunctionSetNode = NodeBase<'functionset'> & {
|
|
67
|
+
name: string;
|
|
68
|
+
functions: Record<string, {
|
|
69
|
+
accessCondition: AccessCondition;
|
|
70
|
+
fromFunctionSet?: true;
|
|
71
|
+
}>;
|
|
72
|
+
};
|
|
73
|
+
export type ProgramNode = NodeBase<'program'> & {
|
|
74
|
+
collections: CollectionNode[];
|
|
75
|
+
contracts: ContractNode[];
|
|
76
|
+
functionsets: FunctionSetNode[];
|
|
77
|
+
};
|
|
78
|
+
export type Node = CollectionNode | ContractNode | FunctionSetNode;
|
|
79
|
+
export type NoteKind = Node['kind'];
|
package/dist/ast.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PropertyModifiers = exports.PropertyType = exports.LOCATION_SYMBOL = void 0;
|
|
4
|
+
exports.LOCATION_SYMBOL = Symbol();
|
|
5
|
+
exports.PropertyType = {
|
|
6
|
+
str: 'string',
|
|
7
|
+
int: 'integer',
|
|
8
|
+
num: 'number',
|
|
9
|
+
bool: 'boolean',
|
|
10
|
+
enum: 'enum',
|
|
11
|
+
date: 'string',
|
|
12
|
+
datetime: 'string',
|
|
13
|
+
};
|
|
14
|
+
exports.PropertyModifiers = {
|
|
15
|
+
Error: {
|
|
16
|
+
packageName: 'aeria',
|
|
17
|
+
symbolName: 'errorSchema',
|
|
18
|
+
},
|
|
19
|
+
Result: {
|
|
20
|
+
packageName: 'aeria',
|
|
21
|
+
symbolName: 'resultSchema',
|
|
22
|
+
},
|
|
23
|
+
};
|
package/dist/ast.mjs
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
export const LOCATION_SYMBOL = Symbol();
|
|
3
|
+
export const PropertyType = {
|
|
4
|
+
str: "string",
|
|
5
|
+
int: "integer",
|
|
6
|
+
num: "number",
|
|
7
|
+
bool: "boolean",
|
|
8
|
+
enum: "enum",
|
|
9
|
+
date: "string",
|
|
10
|
+
datetime: "string"
|
|
11
|
+
};
|
|
12
|
+
export const PropertyModifiers = {
|
|
13
|
+
Error: {
|
|
14
|
+
packageName: "aeria",
|
|
15
|
+
symbolName: "errorSchema"
|
|
16
|
+
},
|
|
17
|
+
Result: {
|
|
18
|
+
packageName: "aeria",
|
|
19
|
+
symbolName: "resultSchema"
|
|
20
|
+
}
|
|
21
|
+
};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateContracts = void 0;
|
|
4
|
+
const types_1 = require("@aeriajs/types");
|
|
5
|
+
const utils_js_1 = require("./utils.js");
|
|
6
|
+
const generateContracts = (ast) => {
|
|
7
|
+
const contractNodes = ast.filter((node) => node.kind === 'contract');
|
|
8
|
+
if (contractNodes.length === 0) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
return {
|
|
12
|
+
js: makeJSContractsCode(contractNodes),
|
|
13
|
+
dTs: makeTSContractsCode(contractNodes),
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
exports.generateContracts = generateContracts;
|
|
17
|
+
const makeJSContractsCode = (contractAst) => {
|
|
18
|
+
const imports = new Set(['defineContract']);
|
|
19
|
+
const getCodeForResponse = (responseProperty) => {
|
|
20
|
+
const { kind, modifier, ...propertyNode } = responseProperty;
|
|
21
|
+
if (!modifier) {
|
|
22
|
+
return (0, utils_js_1.stringify)((0, utils_js_1.propertyToSchema)(propertyNode));
|
|
23
|
+
}
|
|
24
|
+
const modifierSymbol = responseProperty.modifier === 'Result'
|
|
25
|
+
? 'resultSchema'
|
|
26
|
+
: 'errorSchema';
|
|
27
|
+
if (!imports.has(modifierSymbol)) {
|
|
28
|
+
imports.add(modifierSymbol);
|
|
29
|
+
}
|
|
30
|
+
return `${modifierSymbol}(${(0, utils_js_1.stringify)((0, utils_js_1.propertyToSchema)(propertyNode))})`;
|
|
31
|
+
};
|
|
32
|
+
const declarations = contractAst.map((contractNode) => {
|
|
33
|
+
const { name, kind, roles, response, ...contractProperty } = contractNode;
|
|
34
|
+
let responseString = '';
|
|
35
|
+
if (response) {
|
|
36
|
+
if (Array.isArray(response)) {
|
|
37
|
+
const responseArray = [];
|
|
38
|
+
for (const responseElement of response) {
|
|
39
|
+
responseArray.push({
|
|
40
|
+
[utils_js_1.UnquotedSymbol]: getCodeForResponse(responseElement),
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
responseString = (0, utils_js_1.stringify)(responseArray);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
responseString = (0, utils_js_1.stringify)(getCodeForResponse(response));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const contractSchema = (0, utils_js_1.getProperties)(contractProperty);
|
|
50
|
+
contractSchema.response = {
|
|
51
|
+
[utils_js_1.UnquotedSymbol]: responseString,
|
|
52
|
+
};
|
|
53
|
+
return `export const ${name}Contract = defineContract(${(0, utils_js_1.stringify)(contractSchema)})`;
|
|
54
|
+
}).join('\n\n');
|
|
55
|
+
return `import { ${Array.from(imports).join(', ')} } from \'aeria\'\n\n` + declarations;
|
|
56
|
+
};
|
|
57
|
+
const getResponseSchema = (response) => {
|
|
58
|
+
const responseSchema = (0, utils_js_1.propertyToSchema)(response);
|
|
59
|
+
if (!response.modifier) {
|
|
60
|
+
return responseSchema;
|
|
61
|
+
}
|
|
62
|
+
return response.modifier === 'Result' ?
|
|
63
|
+
(0, types_1.resultSchema)(responseSchema) :
|
|
64
|
+
(0, types_1.errorSchema)(responseSchema);
|
|
65
|
+
};
|
|
66
|
+
const makeTSContractsCode = (contractAst) => {
|
|
67
|
+
return contractAst.map((contractNode) => {
|
|
68
|
+
const { name, kind, roles, ...contractSchema } = contractNode;
|
|
69
|
+
let responseSchema = null;
|
|
70
|
+
if (contractSchema.response) {
|
|
71
|
+
if (Array.isArray(contractSchema.response)) {
|
|
72
|
+
responseSchema = contractSchema.response.map(getResponseSchema);
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
responseSchema = getResponseSchema(contractSchema.response);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const contractProperties = (0, utils_js_1.getProperties)(contractSchema);
|
|
79
|
+
return `export declare const ${contractNode.name}Contract: ${(0, utils_js_1.stringify)({
|
|
80
|
+
...contractProperties,
|
|
81
|
+
response: responseSchema,
|
|
82
|
+
})}`;
|
|
83
|
+
}).join('\n\n');
|
|
84
|
+
};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
import { errorSchema, resultSchema } from "@aeriajs/types";
|
|
3
|
+
import { getProperties, propertyToSchema, stringify, UnquotedSymbol } from "./utils.mjs";
|
|
4
|
+
export const generateContracts = (ast) => {
|
|
5
|
+
const contractNodes = ast.filter((node) => node.kind === "contract");
|
|
6
|
+
if (contractNodes.length === 0) {
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
return {
|
|
10
|
+
js: makeJSContractsCode(contractNodes),
|
|
11
|
+
dTs: makeTSContractsCode(contractNodes)
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
const makeJSContractsCode = (contractAst) => {
|
|
15
|
+
const imports = /* @__PURE__ */ new Set(["defineContract"]);
|
|
16
|
+
const getCodeForResponse = (responseProperty) => {
|
|
17
|
+
const { kind, modifier, ...propertyNode } = responseProperty;
|
|
18
|
+
if (!modifier) {
|
|
19
|
+
return stringify(propertyToSchema(propertyNode));
|
|
20
|
+
}
|
|
21
|
+
const modifierSymbol = responseProperty.modifier === "Result" ? "resultSchema" : "errorSchema";
|
|
22
|
+
if (!imports.has(modifierSymbol)) {
|
|
23
|
+
imports.add(modifierSymbol);
|
|
24
|
+
}
|
|
25
|
+
return `${modifierSymbol}(${stringify(propertyToSchema(propertyNode))})`;
|
|
26
|
+
};
|
|
27
|
+
const declarations = contractAst.map((contractNode) => {
|
|
28
|
+
const { name, kind, roles, response, ...contractProperty } = contractNode;
|
|
29
|
+
let responseString = "";
|
|
30
|
+
if (response) {
|
|
31
|
+
if (Array.isArray(response)) {
|
|
32
|
+
const responseArray = [];
|
|
33
|
+
for (const responseElement of response) {
|
|
34
|
+
responseArray.push({
|
|
35
|
+
[UnquotedSymbol]: getCodeForResponse(responseElement)
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
responseString = stringify(responseArray);
|
|
39
|
+
} else {
|
|
40
|
+
responseString = stringify(getCodeForResponse(response));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const contractSchema = getProperties(contractProperty);
|
|
44
|
+
contractSchema.response = {
|
|
45
|
+
[UnquotedSymbol]: responseString
|
|
46
|
+
};
|
|
47
|
+
return `export const ${name}Contract = defineContract(${stringify(contractSchema)})`;
|
|
48
|
+
}).join("\n\n");
|
|
49
|
+
return `import { ${Array.from(imports).join(", ")} } from 'aeria'
|
|
50
|
+
|
|
51
|
+
` + declarations;
|
|
52
|
+
};
|
|
53
|
+
const getResponseSchema = (response) => {
|
|
54
|
+
const responseSchema = propertyToSchema(response);
|
|
55
|
+
if (!response.modifier) {
|
|
56
|
+
return responseSchema;
|
|
57
|
+
}
|
|
58
|
+
return response.modifier === "Result" ? resultSchema(responseSchema) : errorSchema(responseSchema);
|
|
59
|
+
};
|
|
60
|
+
const makeTSContractsCode = (contractAst) => {
|
|
61
|
+
return contractAst.map((contractNode) => {
|
|
62
|
+
const { name, kind, roles, ...contractSchema } = contractNode;
|
|
63
|
+
let responseSchema = null;
|
|
64
|
+
if (contractSchema.response) {
|
|
65
|
+
if (Array.isArray(contractSchema.response)) {
|
|
66
|
+
responseSchema = contractSchema.response.map(getResponseSchema);
|
|
67
|
+
} else {
|
|
68
|
+
responseSchema = getResponseSchema(contractSchema.response);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const contractProperties = getProperties(contractSchema);
|
|
72
|
+
return `export declare const ${contractNode.name}Contract: ${stringify({
|
|
73
|
+
...contractProperties,
|
|
74
|
+
response: responseSchema
|
|
75
|
+
})}`;
|
|
76
|
+
}).join("\n\n");
|
|
77
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type * as AST from '../ast.js';
|
|
2
|
+
export declare const generateExports: (ast: AST.ProgramNode, hasContracts?: boolean) => {
|
|
3
|
+
main: {
|
|
4
|
+
js: string;
|
|
5
|
+
dTs: string;
|
|
6
|
+
};
|
|
7
|
+
collections: {
|
|
8
|
+
js: string;
|
|
9
|
+
dTs: string;
|
|
10
|
+
};
|
|
11
|
+
contracts?: {
|
|
12
|
+
js: string;
|
|
13
|
+
dTs: string;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateExports = void 0;
|
|
4
|
+
const utils_js_1 = require("./utils.js");
|
|
5
|
+
const generateExports = (ast, hasContracts = false) => {
|
|
6
|
+
const symbolsToExport = Object.values(ast.collections.reduce((symbols, node) => {
|
|
7
|
+
const id = (0, utils_js_1.getCollectionId)(node.name);
|
|
8
|
+
symbols[id] = {
|
|
9
|
+
id,
|
|
10
|
+
schema: (0, utils_js_1.resizeFirstChar)(node.name, true),
|
|
11
|
+
extend: (0, utils_js_1.getExtendName)(node.name),
|
|
12
|
+
};
|
|
13
|
+
return symbols;
|
|
14
|
+
}, {}));
|
|
15
|
+
const exports = {
|
|
16
|
+
collections: {
|
|
17
|
+
js: `export { ${symbolsToExport.map((symbol) => `${symbol.id}`).join(', ')} } from './collections.js'`,
|
|
18
|
+
dTs: `export { ${symbolsToExport.map((symbol) => `${symbol.id}`).join(', ')} } from './collections.js'`,
|
|
19
|
+
},
|
|
20
|
+
main: {
|
|
21
|
+
js: (hasContracts
|
|
22
|
+
? 'export * as contracts from \'./contracts/index.js\'\n'
|
|
23
|
+
: '') +
|
|
24
|
+
'export * as collections from \'./collections/index.js\'\n' +
|
|
25
|
+
`export { ${symbolsToExport.map((symbol) => symbol.extend).join(', ')} } from './collections/collections.js'`,
|
|
26
|
+
dTs: (hasContracts
|
|
27
|
+
? 'export * as contracts from \'./contracts/index.js\'\n'
|
|
28
|
+
: '') +
|
|
29
|
+
'export * as collections from \'./collections/index.js\'\n' +
|
|
30
|
+
`export { ${symbolsToExport.map((symbol) => `${symbol.extend}, ${symbol.schema}`).join(', ')} } from './collections/collections.js'`,
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
if (hasContracts) {
|
|
34
|
+
exports.contracts = {
|
|
35
|
+
js: 'export * from \'./contracts.js\'',
|
|
36
|
+
dTs: 'export * from \'./contracts.js\'',
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
return exports;
|
|
40
|
+
};
|
|
41
|
+
exports.generateExports = generateExports;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
import { resizeFirstChar, getExtendName, getCollectionId } from "./utils.mjs";
|
|
3
|
+
export const generateExports = (ast, hasContracts = false) => {
|
|
4
|
+
const symbolsToExport = Object.values(ast.collections.reduce((symbols, node) => {
|
|
5
|
+
const id = getCollectionId(node.name);
|
|
6
|
+
symbols[id] = {
|
|
7
|
+
id,
|
|
8
|
+
schema: resizeFirstChar(node.name, true),
|
|
9
|
+
extend: getExtendName(node.name)
|
|
10
|
+
};
|
|
11
|
+
return symbols;
|
|
12
|
+
}, {}));
|
|
13
|
+
const exports = {
|
|
14
|
+
collections: {
|
|
15
|
+
js: `export { ${symbolsToExport.map((symbol) => `${symbol.id}`).join(", ")} } from './collections.mjs'`,
|
|
16
|
+
dTs: `export { ${symbolsToExport.map((symbol) => `${symbol.id}`).join(", ")} } from './collections.mjs'`
|
|
17
|
+
},
|
|
18
|
+
main: {
|
|
19
|
+
js: (hasContracts ? "export * as contracts from './contracts/index.mjs'\n" : "") + `export * as collections from './collections/index.mjs'
|
|
20
|
+
export { ${symbolsToExport.map((symbol) => symbol.extend).join(", ")} } from './collections/collections.mjs'`,
|
|
21
|
+
dTs: (hasContracts ? "export * as contracts from './contracts/index.mjs'\n" : "") + `export * as collections from './collections/index.mjs'
|
|
22
|
+
export { ${symbolsToExport.map((symbol) => `${symbol.extend}, ${symbol.schema}`).join(", ")} } from './collections/collections.mjs'`
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
if (hasContracts) {
|
|
26
|
+
exports.contracts = {
|
|
27
|
+
js: "export * from './contracts.mjs'",
|
|
28
|
+
dTs: "export * from './contracts.mjs'"
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
return exports;
|
|
32
|
+
};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateJSCollections = void 0;
|
|
4
|
+
const utils_js_1 = require("./utils.js");
|
|
5
|
+
const initialImportedFunctions = [
|
|
6
|
+
'extendCollection',
|
|
7
|
+
'defineCollection',
|
|
8
|
+
];
|
|
9
|
+
const generateJSCollections = (ast) => {
|
|
10
|
+
let javascriptCode = '';
|
|
11
|
+
const importsResult = (0, utils_js_1.makeASTImports)(ast, {
|
|
12
|
+
[utils_js_1.aeriaPackageName]: new Set(initialImportedFunctions),
|
|
13
|
+
});
|
|
14
|
+
javascriptCode += importsResult.code + '\n\n';
|
|
15
|
+
javascriptCode += makeJSCollections(ast, importsResult.modifiedSymbols) + '\n\n';
|
|
16
|
+
return javascriptCode;
|
|
17
|
+
};
|
|
18
|
+
exports.generateJSCollections = generateJSCollections;
|
|
19
|
+
const makeJSCollections = (ast, modifiedSymbols) => {
|
|
20
|
+
const collectionCodes = {};
|
|
21
|
+
for (const collectionNode of ast) {
|
|
22
|
+
const id = (0, utils_js_1.getCollectionId)(collectionNode.name); // CollectionName -> collectionName
|
|
23
|
+
const extendCollectionName = (0, utils_js_1.getExtendName)(collectionNode.name);
|
|
24
|
+
const collectionDefinition = `export const ${id} = ${collectionNode.extends
|
|
25
|
+
? `extendCollection(${id in modifiedSymbols
|
|
26
|
+
? modifiedSymbols[id]
|
|
27
|
+
: id}, ${makeJSCollectionSchema(collectionNode, id)})`
|
|
28
|
+
: `defineCollection(${makeJSCollectionSchema(collectionNode, id)})`}`;
|
|
29
|
+
const collectionDeclaration = `export const ${extendCollectionName} = (collection) => extendCollection(${id}, collection)`;
|
|
30
|
+
collectionCodes[collectionNode.name] = [
|
|
31
|
+
'//' + collectionNode.name,
|
|
32
|
+
collectionDefinition,
|
|
33
|
+
collectionDeclaration,
|
|
34
|
+
].join('\n');
|
|
35
|
+
}
|
|
36
|
+
return Object.values(collectionCodes).join('\n\n');
|
|
37
|
+
};
|
|
38
|
+
const makeJSCollectionSchema = (collectionNode, collectionId) => {
|
|
39
|
+
const collectionSchema = {
|
|
40
|
+
description: {
|
|
41
|
+
$id: collectionId,
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
for (const key of Object.keys(collectionNode)) {
|
|
45
|
+
if (collectionNode[key] === undefined) {
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
switch (key) {
|
|
49
|
+
case 'properties':
|
|
50
|
+
collectionSchema.description.properties = (0, utils_js_1.getProperties)(collectionNode[key]);
|
|
51
|
+
break;
|
|
52
|
+
case 'owned':
|
|
53
|
+
collectionSchema.description.owned = collectionNode[key];
|
|
54
|
+
break;
|
|
55
|
+
case 'functions':
|
|
56
|
+
collectionSchema.functions = {
|
|
57
|
+
[utils_js_1.UnquotedSymbol]: `{ ${makeJSFunctions(collectionNode[key])} }`,
|
|
58
|
+
};
|
|
59
|
+
collectionSchema.exposedFunctions = (0, utils_js_1.getExposedFunctions)(collectionNode[key]);
|
|
60
|
+
break;
|
|
61
|
+
case 'table':
|
|
62
|
+
case 'filters':
|
|
63
|
+
case 'indexes':
|
|
64
|
+
case 'form':
|
|
65
|
+
collectionSchema.description[key] = collectionNode[key];
|
|
66
|
+
break;
|
|
67
|
+
case 'actions':
|
|
68
|
+
case 'individualActions':
|
|
69
|
+
collectionSchema.description[key] = collectionNode[key];
|
|
70
|
+
break;
|
|
71
|
+
case 'icon':
|
|
72
|
+
collectionSchema.description[key] = collectionNode[key];
|
|
73
|
+
break;
|
|
74
|
+
case 'presets':
|
|
75
|
+
collectionSchema.description[key] = collectionNode[key];
|
|
76
|
+
break;
|
|
77
|
+
case 'search':
|
|
78
|
+
collectionSchema.description[key] = collectionNode[key];
|
|
79
|
+
break;
|
|
80
|
+
case 'required':
|
|
81
|
+
collectionSchema.description[key] = collectionNode[key];
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return (0, utils_js_1.stringify)(collectionSchema);
|
|
86
|
+
};
|
|
87
|
+
const makeJSFunctions = (functions) => {
|
|
88
|
+
return Object.entries(functions).map(([key, _value]) => utils_js_1.defaultFunctions.includes(key)
|
|
89
|
+
? key
|
|
90
|
+
: `${key}: () => { throw new Error('Function not implemented') }`).join(', ');
|
|
91
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
import { makeASTImports, getProperties, stringify, aeriaPackageName, getExtendName, getCollectionId, UnquotedSymbol, defaultFunctions, getExposedFunctions } from "./utils.mjs";
|
|
3
|
+
const initialImportedFunctions = [
|
|
4
|
+
"extendCollection",
|
|
5
|
+
"defineCollection"
|
|
6
|
+
];
|
|
7
|
+
export const generateJSCollections = (ast) => {
|
|
8
|
+
let javascriptCode = "";
|
|
9
|
+
const importsResult = makeASTImports(ast, {
|
|
10
|
+
[aeriaPackageName]: new Set(initialImportedFunctions)
|
|
11
|
+
});
|
|
12
|
+
javascriptCode += importsResult.code + "\n\n";
|
|
13
|
+
javascriptCode += makeJSCollections(ast, importsResult.modifiedSymbols) + "\n\n";
|
|
14
|
+
return javascriptCode;
|
|
15
|
+
};
|
|
16
|
+
const makeJSCollections = (ast, modifiedSymbols) => {
|
|
17
|
+
const collectionCodes = {};
|
|
18
|
+
for (const collectionNode of ast) {
|
|
19
|
+
const id = getCollectionId(collectionNode.name);
|
|
20
|
+
const extendCollectionName = getExtendName(collectionNode.name);
|
|
21
|
+
const collectionDefinition = `export const ${id} = ${collectionNode.extends ? `extendCollection(${id in modifiedSymbols ? modifiedSymbols[id] : id}, ${makeJSCollectionSchema(collectionNode, id)})` : `defineCollection(${makeJSCollectionSchema(collectionNode, id)})`}`;
|
|
22
|
+
const collectionDeclaration = `export const ${extendCollectionName} = (collection) => extendCollection(${id}, collection)`;
|
|
23
|
+
collectionCodes[collectionNode.name] = [
|
|
24
|
+
"//" + collectionNode.name,
|
|
25
|
+
collectionDefinition,
|
|
26
|
+
collectionDeclaration
|
|
27
|
+
].join("\n");
|
|
28
|
+
}
|
|
29
|
+
return Object.values(collectionCodes).join("\n\n");
|
|
30
|
+
};
|
|
31
|
+
const makeJSCollectionSchema = (collectionNode, collectionId) => {
|
|
32
|
+
const collectionSchema = {
|
|
33
|
+
description: {
|
|
34
|
+
$id: collectionId
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
for (const key of Object.keys(collectionNode)) {
|
|
38
|
+
if (collectionNode[key] === void 0) {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
switch (key) {
|
|
42
|
+
case "properties":
|
|
43
|
+
collectionSchema.description.properties = getProperties(collectionNode[key]);
|
|
44
|
+
break;
|
|
45
|
+
case "owned":
|
|
46
|
+
collectionSchema.description.owned = collectionNode[key];
|
|
47
|
+
break;
|
|
48
|
+
case "functions":
|
|
49
|
+
collectionSchema.functions = {
|
|
50
|
+
[UnquotedSymbol]: `{ ${makeJSFunctions(collectionNode[key])} }`
|
|
51
|
+
};
|
|
52
|
+
collectionSchema.exposedFunctions = getExposedFunctions(collectionNode[key]);
|
|
53
|
+
break;
|
|
54
|
+
case "table":
|
|
55
|
+
case "filters":
|
|
56
|
+
case "indexes":
|
|
57
|
+
case "form":
|
|
58
|
+
collectionSchema.description[key] = collectionNode[key];
|
|
59
|
+
break;
|
|
60
|
+
case "actions":
|
|
61
|
+
case "individualActions":
|
|
62
|
+
collectionSchema.description[key] = collectionNode[key];
|
|
63
|
+
break;
|
|
64
|
+
case "icon":
|
|
65
|
+
collectionSchema.description[key] = collectionNode[key];
|
|
66
|
+
break;
|
|
67
|
+
case "presets":
|
|
68
|
+
collectionSchema.description[key] = collectionNode[key];
|
|
69
|
+
break;
|
|
70
|
+
case "search":
|
|
71
|
+
collectionSchema.description[key] = collectionNode[key];
|
|
72
|
+
break;
|
|
73
|
+
case "required":
|
|
74
|
+
collectionSchema.description[key] = collectionNode[key];
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return stringify(collectionSchema);
|
|
79
|
+
};
|
|
80
|
+
const makeJSFunctions = (functions) => {
|
|
81
|
+
return Object.entries(functions).map(([key, _value]) => defaultFunctions.includes(key) ? key : `${key}: () => { throw new Error('Function not implemented') }`).join(", ");
|
|
82
|
+
};
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateTSCollections = void 0;
|
|
4
|
+
const utils_js_1 = require("./utils.js");
|
|
5
|
+
const initialImportedTypes = [
|
|
6
|
+
'Collection',
|
|
7
|
+
'SchemaWithId',
|
|
8
|
+
'ExtendCollection',
|
|
9
|
+
'Context',
|
|
10
|
+
];
|
|
11
|
+
const generateTSCollections = (ast) => {
|
|
12
|
+
let code = '';
|
|
13
|
+
code += `import type { ${initialImportedTypes.join(', ')} } from '${utils_js_1.aeriaPackageName}'\n`; //Used types
|
|
14
|
+
const importsResult = (0, utils_js_1.makeASTImports)(ast);
|
|
15
|
+
code += importsResult.code + '\n\n';
|
|
16
|
+
code += makeTSCollections(ast, importsResult.modifiedSymbols) + '\n';
|
|
17
|
+
return code;
|
|
18
|
+
};
|
|
19
|
+
exports.generateTSCollections = generateTSCollections;
|
|
20
|
+
/** Creates the code exporting the collection type, declaration, schema and extend for each collection and returns them in a string */
|
|
21
|
+
const makeTSCollections = (ast, modifiedSymbols) => {
|
|
22
|
+
const collectionCodes = {};
|
|
23
|
+
for (const collectionNode of ast) {
|
|
24
|
+
const id = (0, utils_js_1.getCollectionId)(collectionNode.name); // CollectionName -> collectionName
|
|
25
|
+
const schemaName = (0, utils_js_1.resizeFirstChar)(collectionNode.name, true); // collectionName -> CollectionName
|
|
26
|
+
const typeName = id + 'Collection'; // Pet -> petCollection
|
|
27
|
+
const collectionType = `export declare type ${typeName} = ${id in modifiedSymbols
|
|
28
|
+
? `ExtendCollection<typeof ${modifiedSymbols[id]}, ${makeTSCollectionSchema(collectionNode, id)}>`
|
|
29
|
+
: makeTSCollectionSchema(collectionNode, id)}`;
|
|
30
|
+
const collectionDeclaration = `export declare const ${id}: ${typeName} & { item: SchemaWithId<${typeName}["description"]> }`;
|
|
31
|
+
const collectionSchema = `export declare type ${schemaName} = SchemaWithId<typeof ${id}.description>`;
|
|
32
|
+
const collectionExtend = `export declare const extend${schemaName}Collection: <
|
|
33
|
+
const TCollection extends {
|
|
34
|
+
[P in Exclude<keyof Collection, "functions">]?: Partial<Collection[P]>
|
|
35
|
+
} & {
|
|
36
|
+
functions?: {
|
|
37
|
+
[F: string]: (payload: any, context: Context<typeof ${id}["description"]>) => unknown
|
|
38
|
+
}
|
|
39
|
+
}>(collection: TCollection) => ExtendCollection<typeof ${id}, TCollection>`;
|
|
40
|
+
collectionCodes[collectionNode.name] = [
|
|
41
|
+
'//' + collectionNode.name,
|
|
42
|
+
collectionType,
|
|
43
|
+
collectionDeclaration,
|
|
44
|
+
collectionSchema,
|
|
45
|
+
collectionExtend,
|
|
46
|
+
].join('\n');
|
|
47
|
+
}
|
|
48
|
+
return Object.values(collectionCodes).join('\n\n');
|
|
49
|
+
};
|
|
50
|
+
const makeTSCollectionSchema = (collectionNode, collectionId) => {
|
|
51
|
+
const collectionSchema = {
|
|
52
|
+
description: {
|
|
53
|
+
$id: collectionId,
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
for (const key of Object.keys(collectionNode)) {
|
|
57
|
+
if (collectionNode[key] === undefined) {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
switch (key) {
|
|
61
|
+
case 'properties':
|
|
62
|
+
collectionSchema.description.properties = (0, utils_js_1.getProperties)(collectionNode[key]);
|
|
63
|
+
break;
|
|
64
|
+
case 'owned':
|
|
65
|
+
collectionSchema.description.owned = collectionNode[key];
|
|
66
|
+
break;
|
|
67
|
+
case 'functions':
|
|
68
|
+
collectionSchema.functions = makeTSFunctions(collectionNode[key]);
|
|
69
|
+
collectionSchema.exposedFunctions = (0, utils_js_1.getExposedFunctions)(collectionNode[key]);
|
|
70
|
+
break;
|
|
71
|
+
case 'table':
|
|
72
|
+
case 'filters':
|
|
73
|
+
case 'indexes':
|
|
74
|
+
case 'form':
|
|
75
|
+
collectionSchema.description[key] = collectionNode[key];
|
|
76
|
+
break;
|
|
77
|
+
case 'actions':
|
|
78
|
+
case 'individualActions':
|
|
79
|
+
collectionSchema.description[key] = collectionNode[key];
|
|
80
|
+
break;
|
|
81
|
+
case 'icon':
|
|
82
|
+
collectionSchema.description[key] = collectionNode[key];
|
|
83
|
+
break;
|
|
84
|
+
case 'presets':
|
|
85
|
+
collectionSchema.description[key] = collectionNode[key];
|
|
86
|
+
break;
|
|
87
|
+
case 'search':
|
|
88
|
+
collectionSchema.description[key] = collectionNode[key];
|
|
89
|
+
break;
|
|
90
|
+
case 'required':
|
|
91
|
+
collectionSchema.description[key] = collectionNode[key];
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return (0, utils_js_1.stringify)(collectionSchema);
|
|
96
|
+
};
|
|
97
|
+
/** Turns each function to 'typeof functioName' if it's from aeria or not */
|
|
98
|
+
const makeTSFunctions = (functions) => {
|
|
99
|
+
return Object.keys(functions).reduce((acc, key) => {
|
|
100
|
+
acc[key] = {
|
|
101
|
+
[utils_js_1.UnquotedSymbol]: utils_js_1.defaultFunctions.includes(key)
|
|
102
|
+
? `typeof ${key}`
|
|
103
|
+
: '() => never',
|
|
104
|
+
};
|
|
105
|
+
return acc;
|
|
106
|
+
}, {});
|
|
107
|
+
};
|