rads-db 3.0.78 → 3.0.80
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/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/integrations/lib.cjs +106 -106
- package/integrations/lib.mjs +109 -109
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -888,7 +888,7 @@ function getRadsDbMethods(key, computedContext, driverInstance) {
|
|
|
888
888
|
result2.id = uuid.v4();
|
|
889
889
|
for (const [fieldName, field] of Object.entries(type.fields)) {
|
|
890
890
|
if (field.defaultValue)
|
|
891
|
-
result2[fieldName] = field.defaultValue;
|
|
891
|
+
result2[fieldName] = ___default.cloneDeep(field.defaultValue);
|
|
892
892
|
if (field.defaultValueClass)
|
|
893
893
|
result2[fieldName] = construct(field.defaultValueClass);
|
|
894
894
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -881,7 +881,7 @@ function getRadsDbMethods(key, computedContext, driverInstance) {
|
|
|
881
881
|
result2.id = v4();
|
|
882
882
|
for (const [fieldName, field] of Object.entries(type.fields)) {
|
|
883
883
|
if (field.defaultValue)
|
|
884
|
-
result2[fieldName] = field.defaultValue;
|
|
884
|
+
result2[fieldName] = _.cloneDeep(field.defaultValue);
|
|
885
885
|
if (field.defaultValueClass)
|
|
886
886
|
result2[fieldName] = construct(field.defaultValueClass);
|
|
887
887
|
}
|
package/integrations/lib.cjs
CHANGED
|
@@ -152,13 +152,13 @@ function getOrder(f) {
|
|
|
152
152
|
};
|
|
153
153
|
return decorator.order ?? defaultOrders[decorator.preset] ?? 0;
|
|
154
154
|
}
|
|
155
|
-
function parseClassOrTypeDeclaration(typeDeclaration, typeName,
|
|
156
|
-
if (typeDeclaration.kind === _typescript.SyntaxKind.ClassDeclaration) return parseClassDeclaration(typeDeclaration, typeName,
|
|
157
|
-
if (typeDeclaration.kind === _typescript.SyntaxKind.TypeAliasDeclaration) return parseTypeAliasDeclaration(typeDeclaration, typeName,
|
|
155
|
+
function parseClassOrTypeDeclaration(typeDeclaration, typeName, ctx2) {
|
|
156
|
+
if (typeDeclaration.kind === _typescript.SyntaxKind.ClassDeclaration) return parseClassDeclaration(typeDeclaration, typeName, ctx2);
|
|
157
|
+
if (typeDeclaration.kind === _typescript.SyntaxKind.TypeAliasDeclaration) return parseTypeAliasDeclaration(typeDeclaration, typeName, ctx2);
|
|
158
158
|
throw new Error(`Unexpected type definition - ${typeName}`);
|
|
159
159
|
}
|
|
160
|
-
function parseClassDeclaration(typeDeclaration, typeName,
|
|
161
|
-
if (
|
|
160
|
+
function parseClassDeclaration(typeDeclaration, typeName, ctx2) {
|
|
161
|
+
if (ctx2.result[typeName]) return ctx2.result[typeName];
|
|
162
162
|
const {
|
|
163
163
|
modifiers
|
|
164
164
|
} = typeDeclaration;
|
|
@@ -166,7 +166,7 @@ function parseClassDeclaration(typeDeclaration, typeName, ctx) {
|
|
|
166
166
|
if (!nameNode || nameNode.kind !== _typescript.SyntaxKind.Identifier) throw new Error("Cannot detect class name");
|
|
167
167
|
const name = nameNode.text;
|
|
168
168
|
const comment = getCommentFromJsdocNode(typeDeclaration.jsDoc);
|
|
169
|
-
const decorators = parseDecorators(modifiers,
|
|
169
|
+
const decorators = parseDecorators(modifiers, ctx2);
|
|
170
170
|
const classDeclaration = typeDeclaration;
|
|
171
171
|
const {
|
|
172
172
|
members,
|
|
@@ -176,12 +176,12 @@ function parseClassDeclaration(typeDeclaration, typeName, ctx) {
|
|
|
176
176
|
const isExtending = isExtendingExpr?.text;
|
|
177
177
|
for (const m of members) {
|
|
178
178
|
if (m.kind !== _typescript.SyntaxKind.PropertyDeclaration) {
|
|
179
|
-
throw new Error(`Unexpected class member - only properties are allowed("${m.getText(
|
|
179
|
+
throw new Error(`Unexpected class member - only properties are allowed("${m.getText(ctx2.sourceFile)}")`);
|
|
180
180
|
}
|
|
181
181
|
}
|
|
182
182
|
const fields = {};
|
|
183
183
|
for (const m of members) {
|
|
184
|
-
const field = parseClassMember(m, fields, name,
|
|
184
|
+
const field = parseClassMember(m, fields, name, ctx2);
|
|
185
185
|
fields[field.name] = field;
|
|
186
186
|
}
|
|
187
187
|
const handle = _lodash.default.lowerFirst(name);
|
|
@@ -194,12 +194,12 @@ function parseClassDeclaration(typeDeclaration, typeName, ctx) {
|
|
|
194
194
|
fields,
|
|
195
195
|
isExtending,
|
|
196
196
|
comment,
|
|
197
|
-
sourceFile:
|
|
197
|
+
sourceFile: ctx2.sourceFile.fileName
|
|
198
198
|
};
|
|
199
199
|
return result;
|
|
200
200
|
}
|
|
201
|
-
function parseTypeAliasDeclaration(typeDeclaration, typeName,
|
|
202
|
-
if (
|
|
201
|
+
function parseTypeAliasDeclaration(typeDeclaration, typeName, ctx2) {
|
|
202
|
+
if (ctx2.result[typeName]) return ctx2.result[typeName];
|
|
203
203
|
const nameNode = typeDeclaration.name;
|
|
204
204
|
if (!nameNode || nameNode.kind !== _typescript.SyntaxKind.Identifier) throw new Error("Cannot detect class name");
|
|
205
205
|
const name = nameNode.text;
|
|
@@ -208,72 +208,72 @@ function parseTypeAliasDeclaration(typeDeclaration, typeName, ctx) {
|
|
|
208
208
|
const typeAliasType = typeAliasDeclaration.type;
|
|
209
209
|
if (typeAliasType.kind === _typescript.SyntaxKind.UnionType) {
|
|
210
210
|
const typeAliasValue = typeAliasDeclaration.type;
|
|
211
|
-
const enumValues = getEnumValues(typeAliasValue, typeDeclaration,
|
|
211
|
+
const enumValues = getEnumValues(typeAliasValue, typeDeclaration, ctx2);
|
|
212
212
|
return {
|
|
213
213
|
name,
|
|
214
214
|
enumValues,
|
|
215
215
|
comment,
|
|
216
216
|
decorators: {},
|
|
217
|
-
sourceFile:
|
|
217
|
+
sourceFile: ctx2.sourceFile.fileName
|
|
218
218
|
};
|
|
219
219
|
}
|
|
220
220
|
if (typeAliasType.kind === _typescript.SyntaxKind.TypeOperator && typeAliasType.operator === _typescript.SyntaxKind.KeyOfKeyword) {
|
|
221
|
-
const enumValues = getEnumValuesFromKeyOf(typeAliasType,
|
|
221
|
+
const enumValues = getEnumValuesFromKeyOf(typeAliasType, ctx2);
|
|
222
222
|
return {
|
|
223
223
|
name,
|
|
224
224
|
enumValues,
|
|
225
225
|
comment,
|
|
226
226
|
decorators: {},
|
|
227
|
-
sourceFile:
|
|
227
|
+
sourceFile: ctx2.sourceFile.fileName
|
|
228
228
|
};
|
|
229
229
|
}
|
|
230
|
-
throw new Error(`Unexpected type definition - ${typeDeclaration.getText(
|
|
230
|
+
throw new Error(`Unexpected type definition - ${typeDeclaration.getText(ctx2.sourceFile)}. Did you mean 'class'?`);
|
|
231
231
|
}
|
|
232
|
-
function parseDecorators(modifiers,
|
|
232
|
+
function parseDecorators(modifiers, ctx2) {
|
|
233
233
|
if (!modifiers) return {};
|
|
234
234
|
const decoratorNodes = modifiers?.filter(x => x.kind === _typescript.SyntaxKind.Decorator) || [];
|
|
235
|
-
const decoratorsArray = decoratorNodes?.map(x => parseDecorator(x,
|
|
235
|
+
const decoratorsArray = decoratorNodes?.map(x => parseDecorator(x, ctx2)) || [];
|
|
236
236
|
const decorators = {};
|
|
237
237
|
for (const d of decoratorsArray) {
|
|
238
238
|
decorators[d.name] = d.args;
|
|
239
239
|
}
|
|
240
240
|
return decorators;
|
|
241
241
|
}
|
|
242
|
-
function getEnumValues(node, parentNode,
|
|
242
|
+
function getEnumValues(node, parentNode, ctx2) {
|
|
243
243
|
const enumValuesArray = node.types.map(node2 => {
|
|
244
244
|
if (node2.kind !== _typescript.SyntaxKind.LiteralType) {
|
|
245
|
-
throw new Error(`Unexpected type definition - ${parentNode.getText(
|
|
245
|
+
throw new Error(`Unexpected type definition - ${parentNode.getText(ctx2.sourceFile)}`);
|
|
246
246
|
}
|
|
247
247
|
const {
|
|
248
248
|
literal
|
|
249
249
|
} = node2;
|
|
250
|
-
if (literal.kind !== _typescript.SyntaxKind.StringLiteral) throw new Error(`Unexpected type definition - ${parentNode.getText(
|
|
250
|
+
if (literal.kind !== _typescript.SyntaxKind.StringLiteral) throw new Error(`Unexpected type definition - ${parentNode.getText(ctx2.sourceFile)}`);
|
|
251
251
|
return {
|
|
252
252
|
name: literal.text
|
|
253
253
|
};
|
|
254
254
|
});
|
|
255
255
|
return _lodash.default.keyBy(enumValuesArray, "name");
|
|
256
256
|
}
|
|
257
|
-
function parseClassMember(node, parentFields, parentName,
|
|
258
|
-
const name = node.name.getText(
|
|
257
|
+
function parseClassMember(node, parentFields, parentName, ctx2) {
|
|
258
|
+
const name = node.name.getText(ctx2.sourceFile);
|
|
259
259
|
const {
|
|
260
|
-
defaultValue,
|
|
260
|
+
defaultValue: defaultValue2,
|
|
261
261
|
defaultValueCopyFrom,
|
|
262
262
|
defaultValueClass
|
|
263
|
-
} = parseDefaultValueExpression(node.initializer,
|
|
263
|
+
} = parseDefaultValueExpression(node.initializer, ctx2);
|
|
264
264
|
const isRequired = !node.questionToken;
|
|
265
265
|
const comment = getCommentFromJsdocNode(node.jsDoc);
|
|
266
|
-
const decorators = parseDecorators(node.modifiers,
|
|
267
|
-
let defaultValueType = defaultValueClass || getPrimitiveTypeFromDefaultValue(
|
|
266
|
+
const decorators = parseDecorators(node.modifiers, ctx2);
|
|
267
|
+
let defaultValueType = defaultValueClass || getPrimitiveTypeFromDefaultValue(defaultValue2);
|
|
268
268
|
if (defaultValueCopyFrom) {
|
|
269
269
|
const parentField = parentFields[defaultValueCopyFrom];
|
|
270
270
|
if (!parentField) throw new Error(`Cannot find field ${parentName}.${defaultValueCopyFrom}"`);
|
|
271
271
|
defaultValueType = parentField.type;
|
|
272
272
|
}
|
|
273
|
-
const parsedType = parseFieldType(
|
|
273
|
+
const parsedType = parseFieldType(ctx2, parentName, name, node, defaultValueType);
|
|
274
274
|
const result = {
|
|
275
275
|
...parsedType,
|
|
276
|
-
defaultValue,
|
|
276
|
+
defaultValue: defaultValue2,
|
|
277
277
|
defaultValueCopyFrom,
|
|
278
278
|
name,
|
|
279
279
|
isRequired,
|
|
@@ -297,7 +297,7 @@ function getPrimitiveTypeFromDefaultValue(value) {
|
|
|
297
297
|
if (_lodash.default.isObject(value)) return "object";
|
|
298
298
|
return void 0;
|
|
299
299
|
}
|
|
300
|
-
function parseFieldType(
|
|
300
|
+
function parseFieldType(ctx2, parentName, fieldName, node, defaultValueType) {
|
|
301
301
|
const parsedType = {
|
|
302
302
|
isArray: false,
|
|
303
303
|
isRelation: false,
|
|
@@ -308,19 +308,19 @@ function parseFieldType(ctx, parentName, fieldName, node, defaultValueType) {
|
|
|
308
308
|
node
|
|
309
309
|
};
|
|
310
310
|
parseFieldTypeArray(parsedType, parentName);
|
|
311
|
-
parseFieldTypeRelation(parsedType, parentName,
|
|
312
|
-
parseFieldTypeInverseRelation(parsedType, parentName,
|
|
313
|
-
parseFieldTypeInlineEnum(parsedType, parentName, fieldName,
|
|
314
|
-
parseFieldTypeKeyofEnum(parsedType, parentName, fieldName,
|
|
315
|
-
parseFieldTypeRecordEnum(parsedType, parentName, fieldName,
|
|
316
|
-
parsedType.type = parsedType.type ?? parsedType.nodeType?.getText(
|
|
317
|
-
if (!parsedType.type) throw new Error(`Cannot detect property type: '${node.getText(
|
|
311
|
+
parseFieldTypeRelation(parsedType, parentName, ctx2);
|
|
312
|
+
parseFieldTypeInverseRelation(parsedType, parentName, ctx2);
|
|
313
|
+
parseFieldTypeInlineEnum(parsedType, parentName, fieldName, ctx2);
|
|
314
|
+
parseFieldTypeKeyofEnum(parsedType, parentName, fieldName, ctx2);
|
|
315
|
+
parseFieldTypeRecordEnum(parsedType, parentName, fieldName, ctx2);
|
|
316
|
+
parsedType.type = parsedType.type ?? parsedType.nodeType?.getText(ctx2.sourceFile) ?? defaultValueType;
|
|
317
|
+
if (!parsedType.type) throw new Error(`Cannot detect property type: '${node.getText(ctx2.sourceFile)}'`);
|
|
318
318
|
if (parsedType.type.startsWith("Change<")) {
|
|
319
319
|
parsedType.type = parsedType.type.slice(7, -1);
|
|
320
|
-
if (!
|
|
320
|
+
if (!ctx2.typeNodesMap[parsedType.type]) throw new Error(`Unexpected property type: '${parsedType.type}'`);
|
|
321
321
|
parsedType.isChange = true;
|
|
322
322
|
} else {
|
|
323
|
-
if (!supportedPrimitiveTypes.includes(parsedType.type) && !
|
|
323
|
+
if (!supportedPrimitiveTypes.includes(parsedType.type) && !ctx2.typeNodesMap[parsedType.type] && !ctx2.result[parsedType.type]) {
|
|
324
324
|
throw new Error(`Unexpected property type: '${parsedType.type}'`);
|
|
325
325
|
}
|
|
326
326
|
}
|
|
@@ -334,17 +334,17 @@ function parseFieldType(ctx, parentName, fieldName, node, defaultValueType) {
|
|
|
334
334
|
type: parsedType.type
|
|
335
335
|
};
|
|
336
336
|
}
|
|
337
|
-
function parseFieldTypeRecordEnum(parsedType, parentName, fieldName,
|
|
337
|
+
function parseFieldTypeRecordEnum(parsedType, parentName, fieldName, ctx2) {
|
|
338
338
|
if (parsedType.nodeType?.kind !== _typescript.SyntaxKind.TypeReference) return;
|
|
339
339
|
const nt = parsedType.nodeType;
|
|
340
|
-
if (nt.typeName.getText(
|
|
340
|
+
if (nt.typeName.getText(ctx2.sourceFile) !== "Record") return;
|
|
341
341
|
if (nt.typeArguments?.length !== 2) return;
|
|
342
|
-
const keyTypeName = nt.typeArguments[0].getText(
|
|
343
|
-
const valueTypeName = nt.typeArguments[1].getText(
|
|
344
|
-
const keyType =
|
|
342
|
+
const keyTypeName = nt.typeArguments[0].getText(ctx2.sourceFile);
|
|
343
|
+
const valueTypeName = nt.typeArguments[1].getText(ctx2.sourceFile);
|
|
344
|
+
const keyType = ctx2.typeNodesMap[keyTypeName];
|
|
345
345
|
if (!keyType) return;
|
|
346
|
-
if (!
|
|
347
|
-
const enumValues =
|
|
346
|
+
if (!ctx2.result[keyTypeName]) ctx2.result[keyTypeName] = parseClassOrTypeDeclaration(keyType.node, keyTypeName, ctx2);
|
|
347
|
+
const enumValues = ctx2.result[keyTypeName].enumValues;
|
|
348
348
|
if (!enumValues) throw new Error(`Unexpected type - ${keyTypeName}`);
|
|
349
349
|
const newTypeName = `${parentName}_${_lodash.default.upperFirst(fieldName)}`;
|
|
350
350
|
const fieldsArray = _lodash.default.values(enumValues).map(v => ({
|
|
@@ -352,11 +352,11 @@ function parseFieldTypeRecordEnum(parsedType, parentName, fieldName, ctx) {
|
|
|
352
352
|
type: valueTypeName,
|
|
353
353
|
isRequired: true
|
|
354
354
|
}));
|
|
355
|
-
|
|
355
|
+
ctx2.result[newTypeName] = {
|
|
356
356
|
name: newTypeName,
|
|
357
357
|
decorators: {},
|
|
358
358
|
fields: _lodash.default.keyBy(fieldsArray, "name"),
|
|
359
|
-
sourceFile:
|
|
359
|
+
sourceFile: ctx2.sourceFile.fileName
|
|
360
360
|
};
|
|
361
361
|
parsedType.type = newTypeName;
|
|
362
362
|
}
|
|
@@ -371,126 +371,126 @@ function parseFieldTypeArray(parsedType, parentName) {
|
|
|
371
371
|
throw new Error(`Nested arrays are not supported (${parentName})`);
|
|
372
372
|
}
|
|
373
373
|
}
|
|
374
|
-
function parseFieldTypeRelation(parsedType, parentName,
|
|
374
|
+
function parseFieldTypeRelation(parsedType, parentName, ctx2) {
|
|
375
375
|
if (parsedType.nodeType?.kind !== _typescript.SyntaxKind.TypeReference) return;
|
|
376
376
|
const nt = parsedType.nodeType;
|
|
377
|
-
if (nt.typeName.getText(
|
|
377
|
+
if (nt.typeName.getText(ctx2.sourceFile) === "Relation") {
|
|
378
378
|
if (!nt.typeArguments?.length) throw new Error(`Missing type argument for Relation<>: '${parentName}'`);
|
|
379
379
|
parsedType.nodeType = nt.typeArguments[0];
|
|
380
380
|
parsedType.isRelation = true;
|
|
381
381
|
if (nt.typeArguments[1]) {
|
|
382
382
|
const ta = nt.typeArguments[1];
|
|
383
|
-
parsedType.relationDenormFields = getRelationDenormFields(
|
|
383
|
+
parsedType.relationDenormFields = getRelationDenormFields(ctx2, ta);
|
|
384
384
|
}
|
|
385
385
|
}
|
|
386
386
|
}
|
|
387
|
-
function parseFieldTypeInverseRelation(parsedType, parentName,
|
|
387
|
+
function parseFieldTypeInverseRelation(parsedType, parentName, ctx2) {
|
|
388
388
|
if (parsedType.nodeType?.kind !== _typescript.SyntaxKind.TypeReference) return;
|
|
389
389
|
const nt = parsedType.nodeType;
|
|
390
|
-
if (nt.typeName.getText(
|
|
390
|
+
if (nt.typeName.getText(ctx2.sourceFile) === "InverseRelation") {
|
|
391
391
|
if (!nt.typeArguments?.length) throw new Error(`Missing type argument for Relation<>: '${parentName}'`);
|
|
392
|
-
parsedType.type = parseStringField(
|
|
392
|
+
parsedType.type = parseStringField(ctx2, nt.typeArguments[0]);
|
|
393
393
|
parsedType.isInverseRelation = true;
|
|
394
394
|
if (nt.typeArguments[1]) {
|
|
395
395
|
const taNode = nt.typeArguments[1];
|
|
396
|
-
parsedType.inverseRelationField = parseStringField(
|
|
396
|
+
parsedType.inverseRelationField = parseStringField(ctx2, taNode);
|
|
397
397
|
}
|
|
398
398
|
}
|
|
399
399
|
}
|
|
400
|
-
function parseStringField(
|
|
400
|
+
function parseStringField(ctx2, node) {
|
|
401
401
|
if (node.kind === _typescript.SyntaxKind.LiteralType) {
|
|
402
402
|
const literal = node.literal;
|
|
403
403
|
if (literal.kind === _typescript.SyntaxKind.StringLiteral) {
|
|
404
404
|
return literal.text;
|
|
405
405
|
}
|
|
406
406
|
}
|
|
407
|
-
throw new Error(`Unexpected type - ${node.getText(
|
|
407
|
+
throw new Error(`Unexpected type - ${node.getText(ctx2.sourceFile)}`);
|
|
408
408
|
}
|
|
409
|
-
function parseFieldTypeInlineEnum(parsedType, parentName, fieldName,
|
|
409
|
+
function parseFieldTypeInlineEnum(parsedType, parentName, fieldName, ctx2) {
|
|
410
410
|
if (parsedType.nodeType?.kind !== _typescript.SyntaxKind.UnionType) return;
|
|
411
411
|
const nt = parsedType.nodeType;
|
|
412
|
-
const enumValues = getEnumValues(nt, parsedType.node,
|
|
412
|
+
const enumValues = getEnumValues(nt, parsedType.node, ctx2);
|
|
413
413
|
const newTypeName = `${parentName}_${_lodash.default.upperFirst(fieldName)}`;
|
|
414
|
-
|
|
414
|
+
ctx2.result[newTypeName] = {
|
|
415
415
|
name: newTypeName,
|
|
416
416
|
decorators: {},
|
|
417
417
|
enumValues,
|
|
418
|
-
sourceFile:
|
|
418
|
+
sourceFile: ctx2.sourceFile.fileName
|
|
419
419
|
};
|
|
420
420
|
parsedType.type = newTypeName;
|
|
421
421
|
}
|
|
422
|
-
function parseFieldTypeKeyofEnum(parsedType, parentName, fieldName,
|
|
422
|
+
function parseFieldTypeKeyofEnum(parsedType, parentName, fieldName, ctx2) {
|
|
423
423
|
if (parsedType.nodeType?.kind !== _typescript.SyntaxKind.TypeOperator) return;
|
|
424
424
|
const nt = parsedType.nodeType;
|
|
425
425
|
if (nt.operator !== _typescript.SyntaxKind.KeyOfKeyword) return;
|
|
426
|
-
const enumValues = getEnumValuesFromKeyOf(nt,
|
|
426
|
+
const enumValues = getEnumValuesFromKeyOf(nt, ctx2);
|
|
427
427
|
const newTypeName = `${parentName}_${_lodash.default.upperFirst(fieldName)}`;
|
|
428
|
-
|
|
428
|
+
ctx2.result[newTypeName] = {
|
|
429
429
|
name: newTypeName,
|
|
430
430
|
decorators: {},
|
|
431
431
|
enumValues,
|
|
432
|
-
sourceFile:
|
|
432
|
+
sourceFile: ctx2.sourceFile.fileName
|
|
433
433
|
};
|
|
434
434
|
parsedType.type = newTypeName;
|
|
435
435
|
}
|
|
436
|
-
function getEnumValuesFromKeyOf(nodeType,
|
|
436
|
+
function getEnumValuesFromKeyOf(nodeType, ctx2) {
|
|
437
437
|
if (nodeType.type.kind !== _typescript.SyntaxKind.TypeReference) {
|
|
438
|
-
throw new Error(`Unexpected type - ${nodeType.type.getText(
|
|
438
|
+
throw new Error(`Unexpected type - ${nodeType.type.getText(ctx2.sourceFile)}`);
|
|
439
439
|
}
|
|
440
440
|
const typeReferenceNode = nodeType.type;
|
|
441
441
|
if (typeReferenceNode.typeName.kind !== _typescript.SyntaxKind.Identifier) {
|
|
442
|
-
throw new Error(`Unexpected type - ${typeReferenceNode.getText(
|
|
442
|
+
throw new Error(`Unexpected type - ${typeReferenceNode.getText(ctx2.sourceFile)}`);
|
|
443
443
|
}
|
|
444
444
|
const typeName = typeReferenceNode.typeName.text;
|
|
445
|
-
const type =
|
|
445
|
+
const type = ctx2.typeNodesMap[typeName];
|
|
446
446
|
if (!type) {
|
|
447
447
|
throw new Error(`Unexpected type - ${typeName}`);
|
|
448
448
|
}
|
|
449
|
-
if (!
|
|
450
|
-
if (!
|
|
451
|
-
return _lodash.default.mapValues(
|
|
449
|
+
if (!ctx2.result[typeName]) ctx2.result[typeName] = parseClassOrTypeDeclaration(type.node, typeName, ctx2);
|
|
450
|
+
if (!ctx2.result[typeName].fields) throw new Error(`Unexpected type - ${typeName}`);
|
|
451
|
+
return _lodash.default.mapValues(ctx2.result[typeName].fields || {}, v => ({
|
|
452
452
|
name: v.name,
|
|
453
453
|
comment: v.comment,
|
|
454
454
|
decorators: v.decorators
|
|
455
455
|
}));
|
|
456
456
|
}
|
|
457
|
-
function getRelationDenormFields(
|
|
457
|
+
function getRelationDenormFields(ctx2, node) {
|
|
458
458
|
if (node.kind === _typescript.SyntaxKind.LiteralType) {
|
|
459
459
|
const literal = node.literal;
|
|
460
460
|
if (literal.kind === _typescript.SyntaxKind.StringLiteral) {
|
|
461
461
|
return [literal.text];
|
|
462
462
|
}
|
|
463
|
-
throw new Error(`Unexpected type - ${literal.getText(
|
|
463
|
+
throw new Error(`Unexpected type - ${literal.getText(ctx2.sourceFile)}`);
|
|
464
464
|
}
|
|
465
465
|
if (node.kind === _typescript.SyntaxKind.UnionType) {
|
|
466
466
|
const union = node;
|
|
467
|
-
return union.types.flatMap(t => getRelationDenormFields(
|
|
467
|
+
return union.types.flatMap(t => getRelationDenormFields(ctx2, t));
|
|
468
468
|
}
|
|
469
|
-
throw new Error(`Unexpected type - ${node.getText(
|
|
469
|
+
throw new Error(`Unexpected type - ${node.getText(ctx2.sourceFile)}`);
|
|
470
470
|
}
|
|
471
|
-
function verifyDefaultValueType(field,
|
|
471
|
+
function verifyDefaultValueType(field, ctx2) {
|
|
472
472
|
const {
|
|
473
473
|
isArray,
|
|
474
|
-
defaultValue,
|
|
474
|
+
defaultValue: defaultValue2,
|
|
475
475
|
type
|
|
476
476
|
} = field;
|
|
477
477
|
if (isArray) {
|
|
478
|
-
if (!_lodash.default.isArray(
|
|
478
|
+
if (!_lodash.default.isArray(defaultValue2)) {
|
|
479
479
|
throw new TypeError(`Default value type is different from field type: '${type}'`);
|
|
480
480
|
}
|
|
481
481
|
} else {
|
|
482
|
-
if (supportedPrimitiveTypes.includes(type) && getPrimitiveTypeFromDefaultValue(
|
|
482
|
+
if (supportedPrimitiveTypes.includes(type) && getPrimitiveTypeFromDefaultValue(defaultValue2) !== type) {
|
|
483
483
|
throw new Error(`Default value type is different from field type: '${type}'`);
|
|
484
484
|
}
|
|
485
|
-
if (!
|
|
486
|
-
const enumValues =
|
|
487
|
-
if (enumValues && !enumValues[
|
|
485
|
+
if (!ctx2.result[type] && ctx2.typeNodesMap[type]) ctx2.result[type] = parseClassOrTypeDeclaration(ctx2.typeNodesMap[type].node, type, ctx2);
|
|
486
|
+
const enumValues = ctx2.result[type]?.enumValues;
|
|
487
|
+
if (enumValues && !enumValues[defaultValue2]) {
|
|
488
488
|
const enumValuesStr = _lodash.default.keys(enumValues).map(x => `'x'`).join(", ");
|
|
489
489
|
throw new Error(`Default value must be one of: ${enumValuesStr}`);
|
|
490
490
|
}
|
|
491
491
|
}
|
|
492
492
|
}
|
|
493
|
-
function verifyDefaultValueTypeCopyFrom(field, sourceField,
|
|
493
|
+
function verifyDefaultValueTypeCopyFrom(field, sourceField, ctx2) {
|
|
494
494
|
const {
|
|
495
495
|
isArray,
|
|
496
496
|
type
|
|
@@ -505,27 +505,27 @@ function verifyDefaultValueTypeCopyFrom(field, sourceField, ctx) {
|
|
|
505
505
|
}
|
|
506
506
|
}
|
|
507
507
|
}
|
|
508
|
-
function parseDecorator(decoratorNode,
|
|
509
|
-
const
|
|
510
|
-
if (
|
|
511
|
-
const nameNode =
|
|
512
|
-
if (nameNode.kind !== _typescript.SyntaxKind.Identifier) throw new Error(`Unexpected decorator format: "${nameNode.getText(
|
|
508
|
+
function parseDecorator(decoratorNode, ctx2) {
|
|
509
|
+
const expr2 = decoratorNode.expression;
|
|
510
|
+
if (expr2.kind !== _typescript.SyntaxKind.CallExpression) throw new Error(`Unexpected decorator format: "${expr2.getText(ctx2.sourceFile)}"`);
|
|
511
|
+
const nameNode = expr2.expression;
|
|
512
|
+
if (nameNode.kind !== _typescript.SyntaxKind.Identifier) throw new Error(`Unexpected decorator format: "${nameNode.getText(ctx2.sourceFile)}"`);
|
|
513
513
|
return {
|
|
514
514
|
name: nameNode.text,
|
|
515
|
-
args: parseDecoratorArguments(
|
|
515
|
+
args: parseDecoratorArguments(expr2, ctx2)
|
|
516
516
|
};
|
|
517
517
|
}
|
|
518
|
-
function parseDecoratorArguments(
|
|
519
|
-
const args =
|
|
518
|
+
function parseDecoratorArguments(expr2, ctx2) {
|
|
519
|
+
const args = expr2.arguments;
|
|
520
520
|
if (args.length === 0) return {};
|
|
521
|
-
if (args.length > 1) throw new Error(`Too many arguments - one expected: "${
|
|
521
|
+
if (args.length > 1) throw new Error(`Too many arguments - one expected: "${expr2.getText(ctx2.sourceFile)}"`);
|
|
522
522
|
const arg = args[0];
|
|
523
|
-
return parseLiteralNode(arg,
|
|
523
|
+
return parseLiteralNode(arg, ctx2) ?? {};
|
|
524
524
|
}
|
|
525
|
-
function parseDefaultValueExpression(
|
|
526
|
-
if (!
|
|
527
|
-
if (
|
|
528
|
-
const identifier =
|
|
525
|
+
function parseDefaultValueExpression(expr2, ctx2) {
|
|
526
|
+
if (!expr2) return {};
|
|
527
|
+
if (expr2.kind === _typescript.SyntaxKind.NewExpression) {
|
|
528
|
+
const identifier = expr2.expression;
|
|
529
529
|
const type = identifier?.text;
|
|
530
530
|
if (type) {
|
|
531
531
|
return {
|
|
@@ -533,13 +533,13 @@ function parseDefaultValueExpression(expr, ctx) {
|
|
|
533
533
|
defaultValue: {}
|
|
534
534
|
};
|
|
535
535
|
}
|
|
536
|
-
} else if (
|
|
536
|
+
} else if (expr2.kind === _typescript.SyntaxKind.PropertyAccessExpression && expr2.expression.kind === _typescript.SyntaxKind.ThisKeyword) {
|
|
537
537
|
return {
|
|
538
|
-
defaultValueCopyFrom:
|
|
538
|
+
defaultValueCopyFrom: expr2.name?.text
|
|
539
539
|
};
|
|
540
540
|
}
|
|
541
541
|
return {
|
|
542
|
-
defaultValue: parseLiteralNode(
|
|
542
|
+
defaultValue: parseLiteralNode(expr2, ctx2)
|
|
543
543
|
};
|
|
544
544
|
}
|
|
545
545
|
function parseLiteralNode(expr, ctx) {
|
|
@@ -553,7 +553,7 @@ function parseLiteralNode(expr, ctx) {
|
|
|
553
553
|
const defaultValueStr = expr.getText(ctx.sourceFile);
|
|
554
554
|
let defaultValue;
|
|
555
555
|
try {
|
|
556
|
-
defaultValue =
|
|
556
|
+
defaultValue = eval(defaultValueStr);
|
|
557
557
|
} catch (e) {
|
|
558
558
|
throw new Error("Value must be valid array");
|
|
559
559
|
}
|
|
@@ -562,16 +562,16 @@ function parseLiteralNode(expr, ctx) {
|
|
|
562
562
|
}
|
|
563
563
|
throw new Error(`Unexpected property expression: "${expr.getText(ctx.sourceFile)}"`);
|
|
564
564
|
}
|
|
565
|
-
function parseObjectLiteral(arg,
|
|
565
|
+
function parseObjectLiteral(arg, ctx2) {
|
|
566
566
|
const result = {};
|
|
567
567
|
for (const p of arg.properties) {
|
|
568
|
-
if (!p.name || ![_typescript.SyntaxKind.Identifier, _typescript.SyntaxKind.StringLiteral].includes(p.name.kind)) throw new Error(`Unexpected property name: "${p.getText(
|
|
568
|
+
if (!p.name || ![_typescript.SyntaxKind.Identifier, _typescript.SyntaxKind.StringLiteral].includes(p.name.kind)) throw new Error(`Unexpected property name: "${p.getText(ctx2.sourceFile)}"`);
|
|
569
569
|
const nameNode = p.name;
|
|
570
570
|
const name = nameNode.text;
|
|
571
|
-
if (p.kind !== _typescript.SyntaxKind.PropertyAssignment) throw new Error(`Unexpected property value: "${p.getText(
|
|
571
|
+
if (p.kind !== _typescript.SyntaxKind.PropertyAssignment) throw new Error(`Unexpected property value: "${p.getText(ctx2.sourceFile)}"`);
|
|
572
572
|
const p2 = p;
|
|
573
573
|
const valueExpression = p2.initializer;
|
|
574
|
-
const value = parseLiteralNode(valueExpression,
|
|
574
|
+
const value = parseLiteralNode(valueExpression, ctx2);
|
|
575
575
|
result[name] = value;
|
|
576
576
|
}
|
|
577
577
|
return result;
|
package/integrations/lib.mjs
CHANGED
|
@@ -130,35 +130,35 @@ function getOrder(f) {
|
|
|
130
130
|
};
|
|
131
131
|
return decorator.order ?? defaultOrders[decorator.preset] ?? 0;
|
|
132
132
|
}
|
|
133
|
-
function parseClassOrTypeDeclaration(typeDeclaration, typeName,
|
|
133
|
+
function parseClassOrTypeDeclaration(typeDeclaration, typeName, ctx2) {
|
|
134
134
|
if (typeDeclaration.kind === SyntaxKind.ClassDeclaration)
|
|
135
|
-
return parseClassDeclaration(typeDeclaration, typeName,
|
|
135
|
+
return parseClassDeclaration(typeDeclaration, typeName, ctx2);
|
|
136
136
|
if (typeDeclaration.kind === SyntaxKind.TypeAliasDeclaration)
|
|
137
|
-
return parseTypeAliasDeclaration(typeDeclaration, typeName,
|
|
137
|
+
return parseTypeAliasDeclaration(typeDeclaration, typeName, ctx2);
|
|
138
138
|
throw new Error(`Unexpected type definition - ${typeName}`);
|
|
139
139
|
}
|
|
140
|
-
function parseClassDeclaration(typeDeclaration, typeName,
|
|
141
|
-
if (
|
|
142
|
-
return
|
|
140
|
+
function parseClassDeclaration(typeDeclaration, typeName, ctx2) {
|
|
141
|
+
if (ctx2.result[typeName])
|
|
142
|
+
return ctx2.result[typeName];
|
|
143
143
|
const { modifiers } = typeDeclaration;
|
|
144
144
|
const nameNode = typeDeclaration.name;
|
|
145
145
|
if (!nameNode || nameNode.kind !== SyntaxKind.Identifier)
|
|
146
146
|
throw new Error("Cannot detect class name");
|
|
147
147
|
const name = nameNode.text;
|
|
148
148
|
const comment = getCommentFromJsdocNode(typeDeclaration.jsDoc);
|
|
149
|
-
const decorators = parseDecorators(modifiers,
|
|
149
|
+
const decorators = parseDecorators(modifiers, ctx2);
|
|
150
150
|
const classDeclaration = typeDeclaration;
|
|
151
151
|
const { members, heritageClauses } = classDeclaration;
|
|
152
152
|
const isExtendingExpr = heritageClauses?.[0]?.types?.[0]?.expression;
|
|
153
153
|
const isExtending = isExtendingExpr?.text;
|
|
154
154
|
for (const m of members) {
|
|
155
155
|
if (m.kind !== SyntaxKind.PropertyDeclaration) {
|
|
156
|
-
throw new Error(`Unexpected class member - only properties are allowed("${m.getText(
|
|
156
|
+
throw new Error(`Unexpected class member - only properties are allowed("${m.getText(ctx2.sourceFile)}")`);
|
|
157
157
|
}
|
|
158
158
|
}
|
|
159
159
|
const fields = {};
|
|
160
160
|
for (const m of members) {
|
|
161
|
-
const field = parseClassMember(m, fields, name,
|
|
161
|
+
const field = parseClassMember(m, fields, name, ctx2);
|
|
162
162
|
fields[field.name] = field;
|
|
163
163
|
}
|
|
164
164
|
const handle = _.lowerFirst(name);
|
|
@@ -171,13 +171,13 @@ function parseClassDeclaration(typeDeclaration, typeName, ctx) {
|
|
|
171
171
|
fields,
|
|
172
172
|
isExtending,
|
|
173
173
|
comment,
|
|
174
|
-
sourceFile:
|
|
174
|
+
sourceFile: ctx2.sourceFile.fileName
|
|
175
175
|
};
|
|
176
176
|
return result;
|
|
177
177
|
}
|
|
178
|
-
function parseTypeAliasDeclaration(typeDeclaration, typeName,
|
|
179
|
-
if (
|
|
180
|
-
return
|
|
178
|
+
function parseTypeAliasDeclaration(typeDeclaration, typeName, ctx2) {
|
|
179
|
+
if (ctx2.result[typeName])
|
|
180
|
+
return ctx2.result[typeName];
|
|
181
181
|
const nameNode = typeDeclaration.name;
|
|
182
182
|
if (!nameNode || nameNode.kind !== SyntaxKind.Identifier)
|
|
183
183
|
throw new Error("Cannot detect class name");
|
|
@@ -187,55 +187,55 @@ function parseTypeAliasDeclaration(typeDeclaration, typeName, ctx) {
|
|
|
187
187
|
const typeAliasType = typeAliasDeclaration.type;
|
|
188
188
|
if (typeAliasType.kind === SyntaxKind.UnionType) {
|
|
189
189
|
const typeAliasValue = typeAliasDeclaration.type;
|
|
190
|
-
const enumValues = getEnumValues(typeAliasValue, typeDeclaration,
|
|
191
|
-
return { name, enumValues, comment, decorators: {}, sourceFile:
|
|
190
|
+
const enumValues = getEnumValues(typeAliasValue, typeDeclaration, ctx2);
|
|
191
|
+
return { name, enumValues, comment, decorators: {}, sourceFile: ctx2.sourceFile.fileName };
|
|
192
192
|
}
|
|
193
193
|
if (typeAliasType.kind === SyntaxKind.TypeOperator && typeAliasType.operator === SyntaxKind.KeyOfKeyword) {
|
|
194
|
-
const enumValues = getEnumValuesFromKeyOf(typeAliasType,
|
|
195
|
-
return { name, enumValues, comment, decorators: {}, sourceFile:
|
|
194
|
+
const enumValues = getEnumValuesFromKeyOf(typeAliasType, ctx2);
|
|
195
|
+
return { name, enumValues, comment, decorators: {}, sourceFile: ctx2.sourceFile.fileName };
|
|
196
196
|
}
|
|
197
|
-
throw new Error(`Unexpected type definition - ${typeDeclaration.getText(
|
|
197
|
+
throw new Error(`Unexpected type definition - ${typeDeclaration.getText(ctx2.sourceFile)}. Did you mean 'class'?`);
|
|
198
198
|
}
|
|
199
|
-
function parseDecorators(modifiers,
|
|
199
|
+
function parseDecorators(modifiers, ctx2) {
|
|
200
200
|
if (!modifiers)
|
|
201
201
|
return {};
|
|
202
202
|
const decoratorNodes = modifiers?.filter((x) => x.kind === SyntaxKind.Decorator) || [];
|
|
203
|
-
const decoratorsArray = decoratorNodes?.map((x) => parseDecorator(x,
|
|
203
|
+
const decoratorsArray = decoratorNodes?.map((x) => parseDecorator(x, ctx2)) || [];
|
|
204
204
|
const decorators = {};
|
|
205
205
|
for (const d of decoratorsArray) {
|
|
206
206
|
decorators[d.name] = d.args;
|
|
207
207
|
}
|
|
208
208
|
return decorators;
|
|
209
209
|
}
|
|
210
|
-
function getEnumValues(node, parentNode,
|
|
210
|
+
function getEnumValues(node, parentNode, ctx2) {
|
|
211
211
|
const enumValuesArray = node.types.map((node2) => {
|
|
212
212
|
if (node2.kind !== SyntaxKind.LiteralType) {
|
|
213
|
-
throw new Error(`Unexpected type definition - ${parentNode.getText(
|
|
213
|
+
throw new Error(`Unexpected type definition - ${parentNode.getText(ctx2.sourceFile)}`);
|
|
214
214
|
}
|
|
215
215
|
const { literal } = node2;
|
|
216
216
|
if (literal.kind !== SyntaxKind.StringLiteral)
|
|
217
|
-
throw new Error(`Unexpected type definition - ${parentNode.getText(
|
|
217
|
+
throw new Error(`Unexpected type definition - ${parentNode.getText(ctx2.sourceFile)}`);
|
|
218
218
|
return { name: literal.text };
|
|
219
219
|
});
|
|
220
220
|
return _.keyBy(enumValuesArray, "name");
|
|
221
221
|
}
|
|
222
|
-
function parseClassMember(node, parentFields, parentName,
|
|
223
|
-
const name = node.name.getText(
|
|
224
|
-
const { defaultValue, defaultValueCopyFrom, defaultValueClass } = parseDefaultValueExpression(node.initializer,
|
|
222
|
+
function parseClassMember(node, parentFields, parentName, ctx2) {
|
|
223
|
+
const name = node.name.getText(ctx2.sourceFile);
|
|
224
|
+
const { defaultValue: defaultValue2, defaultValueCopyFrom, defaultValueClass } = parseDefaultValueExpression(node.initializer, ctx2);
|
|
225
225
|
const isRequired = !node.questionToken;
|
|
226
226
|
const comment = getCommentFromJsdocNode(node.jsDoc);
|
|
227
|
-
const decorators = parseDecorators(node.modifiers,
|
|
228
|
-
let defaultValueType = defaultValueClass || getPrimitiveTypeFromDefaultValue(
|
|
227
|
+
const decorators = parseDecorators(node.modifiers, ctx2);
|
|
228
|
+
let defaultValueType = defaultValueClass || getPrimitiveTypeFromDefaultValue(defaultValue2);
|
|
229
229
|
if (defaultValueCopyFrom) {
|
|
230
230
|
const parentField = parentFields[defaultValueCopyFrom];
|
|
231
231
|
if (!parentField)
|
|
232
232
|
throw new Error(`Cannot find field ${parentName}.${defaultValueCopyFrom}"`);
|
|
233
233
|
defaultValueType = parentField.type;
|
|
234
234
|
}
|
|
235
|
-
const parsedType = parseFieldType(
|
|
235
|
+
const parsedType = parseFieldType(ctx2, parentName, name, node, defaultValueType);
|
|
236
236
|
const result = {
|
|
237
237
|
...parsedType,
|
|
238
|
-
defaultValue,
|
|
238
|
+
defaultValue: defaultValue2,
|
|
239
239
|
defaultValueCopyFrom,
|
|
240
240
|
name,
|
|
241
241
|
isRequired,
|
|
@@ -266,7 +266,7 @@ function getPrimitiveTypeFromDefaultValue(value) {
|
|
|
266
266
|
return "object";
|
|
267
267
|
return void 0;
|
|
268
268
|
}
|
|
269
|
-
function parseFieldType(
|
|
269
|
+
function parseFieldType(ctx2, parentName, fieldName, node, defaultValueType) {
|
|
270
270
|
const parsedType = {
|
|
271
271
|
isArray: false,
|
|
272
272
|
isRelation: false,
|
|
@@ -277,21 +277,21 @@ function parseFieldType(ctx, parentName, fieldName, node, defaultValueType) {
|
|
|
277
277
|
node
|
|
278
278
|
};
|
|
279
279
|
parseFieldTypeArray(parsedType, parentName);
|
|
280
|
-
parseFieldTypeRelation(parsedType, parentName,
|
|
281
|
-
parseFieldTypeInverseRelation(parsedType, parentName,
|
|
282
|
-
parseFieldTypeInlineEnum(parsedType, parentName, fieldName,
|
|
283
|
-
parseFieldTypeKeyofEnum(parsedType, parentName, fieldName,
|
|
284
|
-
parseFieldTypeRecordEnum(parsedType, parentName, fieldName,
|
|
285
|
-
parsedType.type = parsedType.type ?? parsedType.nodeType?.getText(
|
|
280
|
+
parseFieldTypeRelation(parsedType, parentName, ctx2);
|
|
281
|
+
parseFieldTypeInverseRelation(parsedType, parentName, ctx2);
|
|
282
|
+
parseFieldTypeInlineEnum(parsedType, parentName, fieldName, ctx2);
|
|
283
|
+
parseFieldTypeKeyofEnum(parsedType, parentName, fieldName, ctx2);
|
|
284
|
+
parseFieldTypeRecordEnum(parsedType, parentName, fieldName, ctx2);
|
|
285
|
+
parsedType.type = parsedType.type ?? parsedType.nodeType?.getText(ctx2.sourceFile) ?? defaultValueType;
|
|
286
286
|
if (!parsedType.type)
|
|
287
|
-
throw new Error(`Cannot detect property type: '${node.getText(
|
|
287
|
+
throw new Error(`Cannot detect property type: '${node.getText(ctx2.sourceFile)}'`);
|
|
288
288
|
if (parsedType.type.startsWith("Change<")) {
|
|
289
289
|
parsedType.type = parsedType.type.slice(7, -1);
|
|
290
|
-
if (!
|
|
290
|
+
if (!ctx2.typeNodesMap[parsedType.type])
|
|
291
291
|
throw new Error(`Unexpected property type: '${parsedType.type}'`);
|
|
292
292
|
parsedType.isChange = true;
|
|
293
293
|
} else {
|
|
294
|
-
if (!supportedPrimitiveTypes.includes(parsedType.type) && !
|
|
294
|
+
if (!supportedPrimitiveTypes.includes(parsedType.type) && !ctx2.typeNodesMap[parsedType.type] && !ctx2.result[parsedType.type]) {
|
|
295
295
|
throw new Error(`Unexpected property type: '${parsedType.type}'`);
|
|
296
296
|
}
|
|
297
297
|
}
|
|
@@ -305,31 +305,31 @@ function parseFieldType(ctx, parentName, fieldName, node, defaultValueType) {
|
|
|
305
305
|
type: parsedType.type
|
|
306
306
|
};
|
|
307
307
|
}
|
|
308
|
-
function parseFieldTypeRecordEnum(parsedType, parentName, fieldName,
|
|
308
|
+
function parseFieldTypeRecordEnum(parsedType, parentName, fieldName, ctx2) {
|
|
309
309
|
if (parsedType.nodeType?.kind !== SyntaxKind.TypeReference)
|
|
310
310
|
return;
|
|
311
311
|
const nt = parsedType.nodeType;
|
|
312
|
-
if (nt.typeName.getText(
|
|
312
|
+
if (nt.typeName.getText(ctx2.sourceFile) !== "Record")
|
|
313
313
|
return;
|
|
314
314
|
if (nt.typeArguments?.length !== 2)
|
|
315
315
|
return;
|
|
316
|
-
const keyTypeName = nt.typeArguments[0].getText(
|
|
317
|
-
const valueTypeName = nt.typeArguments[1].getText(
|
|
318
|
-
const keyType =
|
|
316
|
+
const keyTypeName = nt.typeArguments[0].getText(ctx2.sourceFile);
|
|
317
|
+
const valueTypeName = nt.typeArguments[1].getText(ctx2.sourceFile);
|
|
318
|
+
const keyType = ctx2.typeNodesMap[keyTypeName];
|
|
319
319
|
if (!keyType)
|
|
320
320
|
return;
|
|
321
|
-
if (!
|
|
322
|
-
|
|
323
|
-
const enumValues =
|
|
321
|
+
if (!ctx2.result[keyTypeName])
|
|
322
|
+
ctx2.result[keyTypeName] = parseClassOrTypeDeclaration(keyType.node, keyTypeName, ctx2);
|
|
323
|
+
const enumValues = ctx2.result[keyTypeName].enumValues;
|
|
324
324
|
if (!enumValues)
|
|
325
325
|
throw new Error(`Unexpected type - ${keyTypeName}`);
|
|
326
326
|
const newTypeName = `${parentName}_${_.upperFirst(fieldName)}`;
|
|
327
327
|
const fieldsArray = _.values(enumValues).map((v) => ({ name: v.name, type: valueTypeName, isRequired: true }));
|
|
328
|
-
|
|
328
|
+
ctx2.result[newTypeName] = {
|
|
329
329
|
name: newTypeName,
|
|
330
330
|
decorators: {},
|
|
331
331
|
fields: _.keyBy(fieldsArray, "name"),
|
|
332
|
-
sourceFile:
|
|
332
|
+
sourceFile: ctx2.sourceFile.fileName
|
|
333
333
|
};
|
|
334
334
|
parsedType.type = newTypeName;
|
|
335
335
|
}
|
|
@@ -345,122 +345,122 @@ function parseFieldTypeArray(parsedType, parentName) {
|
|
|
345
345
|
throw new Error(`Nested arrays are not supported (${parentName})`);
|
|
346
346
|
}
|
|
347
347
|
}
|
|
348
|
-
function parseFieldTypeRelation(parsedType, parentName,
|
|
348
|
+
function parseFieldTypeRelation(parsedType, parentName, ctx2) {
|
|
349
349
|
if (parsedType.nodeType?.kind !== SyntaxKind.TypeReference)
|
|
350
350
|
return;
|
|
351
351
|
const nt = parsedType.nodeType;
|
|
352
|
-
if (nt.typeName.getText(
|
|
352
|
+
if (nt.typeName.getText(ctx2.sourceFile) === "Relation") {
|
|
353
353
|
if (!nt.typeArguments?.length)
|
|
354
354
|
throw new Error(`Missing type argument for Relation<>: '${parentName}'`);
|
|
355
355
|
parsedType.nodeType = nt.typeArguments[0];
|
|
356
356
|
parsedType.isRelation = true;
|
|
357
357
|
if (nt.typeArguments[1]) {
|
|
358
358
|
const ta = nt.typeArguments[1];
|
|
359
|
-
parsedType.relationDenormFields = getRelationDenormFields(
|
|
359
|
+
parsedType.relationDenormFields = getRelationDenormFields(ctx2, ta);
|
|
360
360
|
}
|
|
361
361
|
}
|
|
362
362
|
}
|
|
363
|
-
function parseFieldTypeInverseRelation(parsedType, parentName,
|
|
363
|
+
function parseFieldTypeInverseRelation(parsedType, parentName, ctx2) {
|
|
364
364
|
if (parsedType.nodeType?.kind !== SyntaxKind.TypeReference)
|
|
365
365
|
return;
|
|
366
366
|
const nt = parsedType.nodeType;
|
|
367
|
-
if (nt.typeName.getText(
|
|
367
|
+
if (nt.typeName.getText(ctx2.sourceFile) === "InverseRelation") {
|
|
368
368
|
if (!nt.typeArguments?.length)
|
|
369
369
|
throw new Error(`Missing type argument for Relation<>: '${parentName}'`);
|
|
370
|
-
parsedType.type = parseStringField(
|
|
370
|
+
parsedType.type = parseStringField(ctx2, nt.typeArguments[0]);
|
|
371
371
|
parsedType.isInverseRelation = true;
|
|
372
372
|
if (nt.typeArguments[1]) {
|
|
373
373
|
const taNode = nt.typeArguments[1];
|
|
374
|
-
parsedType.inverseRelationField = parseStringField(
|
|
374
|
+
parsedType.inverseRelationField = parseStringField(ctx2, taNode);
|
|
375
375
|
}
|
|
376
376
|
}
|
|
377
377
|
}
|
|
378
|
-
function parseStringField(
|
|
378
|
+
function parseStringField(ctx2, node) {
|
|
379
379
|
if (node.kind === SyntaxKind.LiteralType) {
|
|
380
380
|
const literal = node.literal;
|
|
381
381
|
if (literal.kind === SyntaxKind.StringLiteral) {
|
|
382
382
|
return literal.text;
|
|
383
383
|
}
|
|
384
384
|
}
|
|
385
|
-
throw new Error(`Unexpected type - ${node.getText(
|
|
385
|
+
throw new Error(`Unexpected type - ${node.getText(ctx2.sourceFile)}`);
|
|
386
386
|
}
|
|
387
|
-
function parseFieldTypeInlineEnum(parsedType, parentName, fieldName,
|
|
387
|
+
function parseFieldTypeInlineEnum(parsedType, parentName, fieldName, ctx2) {
|
|
388
388
|
if (parsedType.nodeType?.kind !== SyntaxKind.UnionType)
|
|
389
389
|
return;
|
|
390
390
|
const nt = parsedType.nodeType;
|
|
391
|
-
const enumValues = getEnumValues(nt, parsedType.node,
|
|
391
|
+
const enumValues = getEnumValues(nt, parsedType.node, ctx2);
|
|
392
392
|
const newTypeName = `${parentName}_${_.upperFirst(fieldName)}`;
|
|
393
|
-
|
|
393
|
+
ctx2.result[newTypeName] = { name: newTypeName, decorators: {}, enumValues, sourceFile: ctx2.sourceFile.fileName };
|
|
394
394
|
parsedType.type = newTypeName;
|
|
395
395
|
}
|
|
396
|
-
function parseFieldTypeKeyofEnum(parsedType, parentName, fieldName,
|
|
396
|
+
function parseFieldTypeKeyofEnum(parsedType, parentName, fieldName, ctx2) {
|
|
397
397
|
if (parsedType.nodeType?.kind !== SyntaxKind.TypeOperator)
|
|
398
398
|
return;
|
|
399
399
|
const nt = parsedType.nodeType;
|
|
400
400
|
if (nt.operator !== SyntaxKind.KeyOfKeyword)
|
|
401
401
|
return;
|
|
402
|
-
const enumValues = getEnumValuesFromKeyOf(nt,
|
|
402
|
+
const enumValues = getEnumValuesFromKeyOf(nt, ctx2);
|
|
403
403
|
const newTypeName = `${parentName}_${_.upperFirst(fieldName)}`;
|
|
404
|
-
|
|
404
|
+
ctx2.result[newTypeName] = { name: newTypeName, decorators: {}, enumValues, sourceFile: ctx2.sourceFile.fileName };
|
|
405
405
|
parsedType.type = newTypeName;
|
|
406
406
|
}
|
|
407
|
-
function getEnumValuesFromKeyOf(nodeType,
|
|
407
|
+
function getEnumValuesFromKeyOf(nodeType, ctx2) {
|
|
408
408
|
if (nodeType.type.kind !== SyntaxKind.TypeReference) {
|
|
409
|
-
throw new Error(`Unexpected type - ${nodeType.type.getText(
|
|
409
|
+
throw new Error(`Unexpected type - ${nodeType.type.getText(ctx2.sourceFile)}`);
|
|
410
410
|
}
|
|
411
411
|
const typeReferenceNode = nodeType.type;
|
|
412
412
|
if (typeReferenceNode.typeName.kind !== SyntaxKind.Identifier) {
|
|
413
|
-
throw new Error(`Unexpected type - ${typeReferenceNode.getText(
|
|
413
|
+
throw new Error(`Unexpected type - ${typeReferenceNode.getText(ctx2.sourceFile)}`);
|
|
414
414
|
}
|
|
415
415
|
const typeName = typeReferenceNode.typeName.text;
|
|
416
|
-
const type =
|
|
416
|
+
const type = ctx2.typeNodesMap[typeName];
|
|
417
417
|
if (!type) {
|
|
418
418
|
throw new Error(`Unexpected type - ${typeName}`);
|
|
419
419
|
}
|
|
420
|
-
if (!
|
|
421
|
-
|
|
422
|
-
if (!
|
|
420
|
+
if (!ctx2.result[typeName])
|
|
421
|
+
ctx2.result[typeName] = parseClassOrTypeDeclaration(type.node, typeName, ctx2);
|
|
422
|
+
if (!ctx2.result[typeName].fields)
|
|
423
423
|
throw new Error(`Unexpected type - ${typeName}`);
|
|
424
|
-
return _.mapValues(
|
|
424
|
+
return _.mapValues(ctx2.result[typeName].fields || {}, (v) => ({
|
|
425
425
|
name: v.name,
|
|
426
426
|
comment: v.comment,
|
|
427
427
|
decorators: v.decorators
|
|
428
428
|
}));
|
|
429
429
|
}
|
|
430
|
-
function getRelationDenormFields(
|
|
430
|
+
function getRelationDenormFields(ctx2, node) {
|
|
431
431
|
if (node.kind === SyntaxKind.LiteralType) {
|
|
432
432
|
const literal = node.literal;
|
|
433
433
|
if (literal.kind === SyntaxKind.StringLiteral) {
|
|
434
434
|
return [literal.text];
|
|
435
435
|
}
|
|
436
|
-
throw new Error(`Unexpected type - ${literal.getText(
|
|
436
|
+
throw new Error(`Unexpected type - ${literal.getText(ctx2.sourceFile)}`);
|
|
437
437
|
}
|
|
438
438
|
if (node.kind === SyntaxKind.UnionType) {
|
|
439
439
|
const union = node;
|
|
440
|
-
return union.types.flatMap((t) => getRelationDenormFields(
|
|
440
|
+
return union.types.flatMap((t) => getRelationDenormFields(ctx2, t));
|
|
441
441
|
}
|
|
442
|
-
throw new Error(`Unexpected type - ${node.getText(
|
|
442
|
+
throw new Error(`Unexpected type - ${node.getText(ctx2.sourceFile)}`);
|
|
443
443
|
}
|
|
444
|
-
function verifyDefaultValueType(field,
|
|
445
|
-
const { isArray, defaultValue, type } = field;
|
|
444
|
+
function verifyDefaultValueType(field, ctx2) {
|
|
445
|
+
const { isArray, defaultValue: defaultValue2, type } = field;
|
|
446
446
|
if (isArray) {
|
|
447
|
-
if (!_.isArray(
|
|
447
|
+
if (!_.isArray(defaultValue2)) {
|
|
448
448
|
throw new TypeError(`Default value type is different from field type: '${type}'`);
|
|
449
449
|
}
|
|
450
450
|
} else {
|
|
451
|
-
if (supportedPrimitiveTypes.includes(type) && getPrimitiveTypeFromDefaultValue(
|
|
451
|
+
if (supportedPrimitiveTypes.includes(type) && getPrimitiveTypeFromDefaultValue(defaultValue2) !== type) {
|
|
452
452
|
throw new Error(`Default value type is different from field type: '${type}'`);
|
|
453
453
|
}
|
|
454
|
-
if (!
|
|
455
|
-
|
|
456
|
-
const enumValues =
|
|
457
|
-
if (enumValues && !enumValues[
|
|
454
|
+
if (!ctx2.result[type] && ctx2.typeNodesMap[type])
|
|
455
|
+
ctx2.result[type] = parseClassOrTypeDeclaration(ctx2.typeNodesMap[type].node, type, ctx2);
|
|
456
|
+
const enumValues = ctx2.result[type]?.enumValues;
|
|
457
|
+
if (enumValues && !enumValues[defaultValue2]) {
|
|
458
458
|
const enumValuesStr = _.keys(enumValues).map((x) => `'x'`).join(", ");
|
|
459
459
|
throw new Error(`Default value must be one of: ${enumValuesStr}`);
|
|
460
460
|
}
|
|
461
461
|
}
|
|
462
462
|
}
|
|
463
|
-
function verifyDefaultValueTypeCopyFrom(field, sourceField,
|
|
463
|
+
function verifyDefaultValueTypeCopyFrom(field, sourceField, ctx2) {
|
|
464
464
|
const { isArray, type } = field;
|
|
465
465
|
if (isArray) {
|
|
466
466
|
if (!sourceField.isArray) {
|
|
@@ -472,37 +472,37 @@ function verifyDefaultValueTypeCopyFrom(field, sourceField, ctx) {
|
|
|
472
472
|
}
|
|
473
473
|
}
|
|
474
474
|
}
|
|
475
|
-
function parseDecorator(decoratorNode,
|
|
476
|
-
const
|
|
477
|
-
if (
|
|
478
|
-
throw new Error(`Unexpected decorator format: "${
|
|
479
|
-
const nameNode =
|
|
475
|
+
function parseDecorator(decoratorNode, ctx2) {
|
|
476
|
+
const expr2 = decoratorNode.expression;
|
|
477
|
+
if (expr2.kind !== SyntaxKind.CallExpression)
|
|
478
|
+
throw new Error(`Unexpected decorator format: "${expr2.getText(ctx2.sourceFile)}"`);
|
|
479
|
+
const nameNode = expr2.expression;
|
|
480
480
|
if (nameNode.kind !== SyntaxKind.Identifier)
|
|
481
|
-
throw new Error(`Unexpected decorator format: "${nameNode.getText(
|
|
482
|
-
return { name: nameNode.text, args: parseDecoratorArguments(
|
|
481
|
+
throw new Error(`Unexpected decorator format: "${nameNode.getText(ctx2.sourceFile)}"`);
|
|
482
|
+
return { name: nameNode.text, args: parseDecoratorArguments(expr2, ctx2) };
|
|
483
483
|
}
|
|
484
|
-
function parseDecoratorArguments(
|
|
485
|
-
const args =
|
|
484
|
+
function parseDecoratorArguments(expr2, ctx2) {
|
|
485
|
+
const args = expr2.arguments;
|
|
486
486
|
if (args.length === 0)
|
|
487
487
|
return {};
|
|
488
488
|
if (args.length > 1)
|
|
489
|
-
throw new Error(`Too many arguments - one expected: "${
|
|
489
|
+
throw new Error(`Too many arguments - one expected: "${expr2.getText(ctx2.sourceFile)}"`);
|
|
490
490
|
const arg = args[0];
|
|
491
|
-
return parseLiteralNode(arg,
|
|
491
|
+
return parseLiteralNode(arg, ctx2) ?? {};
|
|
492
492
|
}
|
|
493
|
-
function parseDefaultValueExpression(
|
|
494
|
-
if (!
|
|
493
|
+
function parseDefaultValueExpression(expr2, ctx2) {
|
|
494
|
+
if (!expr2)
|
|
495
495
|
return {};
|
|
496
|
-
if (
|
|
497
|
-
const identifier =
|
|
496
|
+
if (expr2.kind === SyntaxKind.NewExpression) {
|
|
497
|
+
const identifier = expr2.expression;
|
|
498
498
|
const type = identifier?.text;
|
|
499
499
|
if (type) {
|
|
500
500
|
return { defaultValueClass: type, defaultValue: {} };
|
|
501
501
|
}
|
|
502
|
-
} else if (
|
|
503
|
-
return { defaultValueCopyFrom:
|
|
502
|
+
} else if (expr2.kind === SyntaxKind.PropertyAccessExpression && expr2.expression.kind === SyntaxKind.ThisKeyword) {
|
|
503
|
+
return { defaultValueCopyFrom: expr2.name?.text };
|
|
504
504
|
}
|
|
505
|
-
return { defaultValue: parseLiteralNode(
|
|
505
|
+
return { defaultValue: parseLiteralNode(expr2, ctx2) };
|
|
506
506
|
}
|
|
507
507
|
function parseLiteralNode(expr, ctx) {
|
|
508
508
|
if (!expr)
|
|
@@ -521,7 +521,7 @@ function parseLiteralNode(expr, ctx) {
|
|
|
521
521
|
const defaultValueStr = expr.getText(ctx.sourceFile);
|
|
522
522
|
let defaultValue;
|
|
523
523
|
try {
|
|
524
|
-
defaultValue =
|
|
524
|
+
defaultValue = eval(defaultValueStr);
|
|
525
525
|
} catch (e) {
|
|
526
526
|
throw new Error("Value must be valid array");
|
|
527
527
|
}
|
|
@@ -531,18 +531,18 @@ function parseLiteralNode(expr, ctx) {
|
|
|
531
531
|
}
|
|
532
532
|
throw new Error(`Unexpected property expression: "${expr.getText(ctx.sourceFile)}"`);
|
|
533
533
|
}
|
|
534
|
-
function parseObjectLiteral(arg,
|
|
534
|
+
function parseObjectLiteral(arg, ctx2) {
|
|
535
535
|
const result = {};
|
|
536
536
|
for (const p of arg.properties) {
|
|
537
537
|
if (!p.name || ![SyntaxKind.Identifier, SyntaxKind.StringLiteral].includes(p.name.kind))
|
|
538
|
-
throw new Error(`Unexpected property name: "${p.getText(
|
|
538
|
+
throw new Error(`Unexpected property name: "${p.getText(ctx2.sourceFile)}"`);
|
|
539
539
|
const nameNode = p.name;
|
|
540
540
|
const name = nameNode.text;
|
|
541
541
|
if (p.kind !== SyntaxKind.PropertyAssignment)
|
|
542
|
-
throw new Error(`Unexpected property value: "${p.getText(
|
|
542
|
+
throw new Error(`Unexpected property value: "${p.getText(ctx2.sourceFile)}"`);
|
|
543
543
|
const p2 = p;
|
|
544
544
|
const valueExpression = p2.initializer;
|
|
545
|
-
const value = parseLiteralNode(valueExpression,
|
|
545
|
+
const value = parseLiteralNode(valueExpression, ctx2);
|
|
546
546
|
result[name] = value;
|
|
547
547
|
}
|
|
548
548
|
return result;
|