@aeriajs/compiler 0.0.10 → 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.
@@ -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
- contractSchema.response = {
51
- [utils_js_1.UnquotedSymbol]: responseString,
52
- };
53
- return `export const ${name}Contract = defineContract(${(0, utils_js_1.stringify)(contractSchema)})`;
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}Contract: ${(0, utils_js_1.stringify)({
82
+ return `export declare const ${contractNode.name}: ${(0, utils_js_1.stringify)({
80
83
  ...contractProperties,
81
- response: responseSchema,
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
- contractSchema.response = {
45
- [UnquotedSymbol]: responseString
46
- };
47
- return `export const ${name}Contract = defineContract(${stringify(contractSchema)})`;
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}Contract: ${stringify({
75
+ return `export declare const ${contractNode.name}: ${stringify({
73
76
  ...contractProperties,
74
- response: responseSchema
77
+ ...responseSchema && {
78
+ response: responseSchema
79
+ }
75
80
  })}`;
76
81
  }).join("\n\n");
77
82
  };
@@ -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: (propertyNode: AST.PropertyNode) => Property;
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;
@@ -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 = (propertyNode) => {
60
- const propertySchema = propertyNode.property;
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 (propertyNode.nestedProperties && 'type' in propertySchema) {
66
+ if (nestedProperties && 'type' in propertySchema) {
68
67
  if (propertySchema.type === 'object' && 'properties' in propertySchema) {
69
- propertySchema.properties = (0, exports.getProperties)(propertyNode.nestedProperties);
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)(propertyNode.nestedProperties),
73
+ properties: (0, exports.getProperties)(nestedProperties),
75
74
  };
76
75
  }
77
76
  }
@@ -48,20 +48,20 @@ export const makeASTImports = (ast, initialImports) => {
48
48
  modifiedSymbols
49
49
  };
50
50
  };
51
- export const propertyToSchema = (propertyNode) => {
52
- const propertySchema = propertyNode.property;
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 (propertyNode.nestedProperties && "type" in propertySchema) {
58
+ if (nestedProperties && "type" in propertySchema) {
59
59
  if (propertySchema.type === "object" && "properties" in propertySchema) {
60
- propertySchema.properties = getProperties(propertyNode.nestedProperties);
60
+ propertySchema.properties = getProperties(nestedProperties);
61
61
  } else if (propertySchema.type === "array") {
62
62
  propertySchema.items = {
63
63
  type: "object",
64
- properties: getProperties(propertyNode.nestedProperties)
64
+ properties: getProperties(nestedProperties)
65
65
  };
66
66
  }
67
67
  }
package/dist/parser.js CHANGED
@@ -126,9 +126,19 @@ const parse = (tokens) => {
126
126
  }
127
127
  }
128
128
  };
129
- const parseArray = (type) => {
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(token_js_1.TokenType.QuotedString);
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 = (type) => {
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(TokenType.QuotedString);
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/compiler",
3
- "version": "0.0.10",
3
+ "version": "0.0.11",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",