rads-db 3.0.79 → 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.
@@ -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, ctx) {
156
- if (typeDeclaration.kind === _typescript.SyntaxKind.ClassDeclaration) return parseClassDeclaration(typeDeclaration, typeName, ctx);
157
- if (typeDeclaration.kind === _typescript.SyntaxKind.TypeAliasDeclaration) return parseTypeAliasDeclaration(typeDeclaration, typeName, ctx);
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, ctx) {
161
- if (ctx.result[typeName]) return ctx.result[typeName];
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, ctx);
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(ctx.sourceFile)}")`);
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, ctx);
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: ctx.sourceFile.fileName
197
+ sourceFile: ctx2.sourceFile.fileName
198
198
  };
199
199
  return result;
200
200
  }
201
- function parseTypeAliasDeclaration(typeDeclaration, typeName, ctx) {
202
- if (ctx.result[typeName]) return ctx.result[typeName];
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, ctx);
211
+ const enumValues = getEnumValues(typeAliasValue, typeDeclaration, ctx2);
212
212
  return {
213
213
  name,
214
214
  enumValues,
215
215
  comment,
216
216
  decorators: {},
217
- sourceFile: ctx.sourceFile.fileName
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, ctx);
221
+ const enumValues = getEnumValuesFromKeyOf(typeAliasType, ctx2);
222
222
  return {
223
223
  name,
224
224
  enumValues,
225
225
  comment,
226
226
  decorators: {},
227
- sourceFile: ctx.sourceFile.fileName
227
+ sourceFile: ctx2.sourceFile.fileName
228
228
  };
229
229
  }
230
- throw new Error(`Unexpected type definition - ${typeDeclaration.getText(ctx.sourceFile)}. Did you mean 'class'?`);
230
+ throw new Error(`Unexpected type definition - ${typeDeclaration.getText(ctx2.sourceFile)}. Did you mean 'class'?`);
231
231
  }
232
- function parseDecorators(modifiers, ctx) {
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, ctx)) || [];
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, ctx) {
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(ctx.sourceFile)}`);
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(ctx.sourceFile)}`);
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, ctx) {
258
- const name = node.name.getText(ctx.sourceFile);
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, ctx);
263
+ } = parseDefaultValueExpression(node.initializer, ctx2);
264
264
  const isRequired = !node.questionToken;
265
265
  const comment = getCommentFromJsdocNode(node.jsDoc);
266
- const decorators = parseDecorators(node.modifiers, ctx);
267
- let defaultValueType = defaultValueClass || getPrimitiveTypeFromDefaultValue(defaultValue);
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(ctx, parentName, name, node, defaultValueType);
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(ctx, parentName, fieldName, node, defaultValueType) {
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, ctx);
312
- parseFieldTypeInverseRelation(parsedType, parentName, ctx);
313
- parseFieldTypeInlineEnum(parsedType, parentName, fieldName, ctx);
314
- parseFieldTypeKeyofEnum(parsedType, parentName, fieldName, ctx);
315
- parseFieldTypeRecordEnum(parsedType, parentName, fieldName, ctx);
316
- parsedType.type = parsedType.type ?? parsedType.nodeType?.getText(ctx.sourceFile) ?? defaultValueType;
317
- if (!parsedType.type) throw new Error(`Cannot detect property type: '${node.getText(ctx.sourceFile)}'`);
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 (!ctx.typeNodesMap[parsedType.type]) throw new Error(`Unexpected property type: '${parsedType.type}'`);
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) && !ctx.typeNodesMap[parsedType.type] && !ctx.result[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, ctx) {
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(ctx.sourceFile) !== "Record") return;
340
+ if (nt.typeName.getText(ctx2.sourceFile) !== "Record") return;
341
341
  if (nt.typeArguments?.length !== 2) return;
342
- const keyTypeName = nt.typeArguments[0].getText(ctx.sourceFile);
343
- const valueTypeName = nt.typeArguments[1].getText(ctx.sourceFile);
344
- const keyType = ctx.typeNodesMap[keyTypeName];
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 (!ctx.result[keyTypeName]) ctx.result[keyTypeName] = parseClassOrTypeDeclaration(keyType.node, keyTypeName, ctx);
347
- const enumValues = ctx.result[keyTypeName].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
- ctx.result[newTypeName] = {
355
+ ctx2.result[newTypeName] = {
356
356
  name: newTypeName,
357
357
  decorators: {},
358
358
  fields: _lodash.default.keyBy(fieldsArray, "name"),
359
- sourceFile: ctx.sourceFile.fileName
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, ctx) {
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(ctx.sourceFile) === "Relation") {
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(ctx, ta);
383
+ parsedType.relationDenormFields = getRelationDenormFields(ctx2, ta);
384
384
  }
385
385
  }
386
386
  }
387
- function parseFieldTypeInverseRelation(parsedType, parentName, ctx) {
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(ctx.sourceFile) === "InverseRelation") {
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(ctx, nt.typeArguments[0]);
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(ctx, taNode);
396
+ parsedType.inverseRelationField = parseStringField(ctx2, taNode);
397
397
  }
398
398
  }
399
399
  }
400
- function parseStringField(ctx, node) {
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(ctx.sourceFile)}`);
407
+ throw new Error(`Unexpected type - ${node.getText(ctx2.sourceFile)}`);
408
408
  }
409
- function parseFieldTypeInlineEnum(parsedType, parentName, fieldName, ctx) {
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, ctx);
412
+ const enumValues = getEnumValues(nt, parsedType.node, ctx2);
413
413
  const newTypeName = `${parentName}_${_lodash.default.upperFirst(fieldName)}`;
414
- ctx.result[newTypeName] = {
414
+ ctx2.result[newTypeName] = {
415
415
  name: newTypeName,
416
416
  decorators: {},
417
417
  enumValues,
418
- sourceFile: ctx.sourceFile.fileName
418
+ sourceFile: ctx2.sourceFile.fileName
419
419
  };
420
420
  parsedType.type = newTypeName;
421
421
  }
422
- function parseFieldTypeKeyofEnum(parsedType, parentName, fieldName, ctx) {
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, ctx);
426
+ const enumValues = getEnumValuesFromKeyOf(nt, ctx2);
427
427
  const newTypeName = `${parentName}_${_lodash.default.upperFirst(fieldName)}`;
428
- ctx.result[newTypeName] = {
428
+ ctx2.result[newTypeName] = {
429
429
  name: newTypeName,
430
430
  decorators: {},
431
431
  enumValues,
432
- sourceFile: ctx.sourceFile.fileName
432
+ sourceFile: ctx2.sourceFile.fileName
433
433
  };
434
434
  parsedType.type = newTypeName;
435
435
  }
436
- function getEnumValuesFromKeyOf(nodeType, ctx) {
436
+ function getEnumValuesFromKeyOf(nodeType, ctx2) {
437
437
  if (nodeType.type.kind !== _typescript.SyntaxKind.TypeReference) {
438
- throw new Error(`Unexpected type - ${nodeType.type.getText(ctx.sourceFile)}`);
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(ctx.sourceFile)}`);
442
+ throw new Error(`Unexpected type - ${typeReferenceNode.getText(ctx2.sourceFile)}`);
443
443
  }
444
444
  const typeName = typeReferenceNode.typeName.text;
445
- const type = ctx.typeNodesMap[typeName];
445
+ const type = ctx2.typeNodesMap[typeName];
446
446
  if (!type) {
447
447
  throw new Error(`Unexpected type - ${typeName}`);
448
448
  }
449
- if (!ctx.result[typeName]) ctx.result[typeName] = parseClassOrTypeDeclaration(type.node, typeName, ctx);
450
- if (!ctx.result[typeName].fields) throw new Error(`Unexpected type - ${typeName}`);
451
- return _lodash.default.mapValues(ctx.result[typeName].fields || {}, v => ({
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(ctx, node) {
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(ctx.sourceFile)}`);
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(ctx, t));
467
+ return union.types.flatMap(t => getRelationDenormFields(ctx2, t));
468
468
  }
469
- throw new Error(`Unexpected type - ${node.getText(ctx.sourceFile)}`);
469
+ throw new Error(`Unexpected type - ${node.getText(ctx2.sourceFile)}`);
470
470
  }
471
- function verifyDefaultValueType(field, ctx) {
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(defaultValue)) {
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(defaultValue) !== type) {
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 (!ctx.result[type] && ctx.typeNodesMap[type]) ctx.result[type] = parseClassOrTypeDeclaration(ctx.typeNodesMap[type].node, type, ctx);
486
- const enumValues = ctx.result[type]?.enumValues;
487
- if (enumValues && !enumValues[defaultValue]) {
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, ctx) {
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, ctx) {
509
- const expr = decoratorNode.expression;
510
- if (expr.kind !== _typescript.SyntaxKind.CallExpression) throw new Error(`Unexpected decorator format: "${expr.getText(ctx.sourceFile)}"`);
511
- const nameNode = expr.expression;
512
- if (nameNode.kind !== _typescript.SyntaxKind.Identifier) throw new Error(`Unexpected decorator format: "${nameNode.getText(ctx.sourceFile)}"`);
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(expr, ctx)
515
+ args: parseDecoratorArguments(expr2, ctx2)
516
516
  };
517
517
  }
518
- function parseDecoratorArguments(expr, ctx) {
519
- const args = expr.arguments;
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: "${expr.getText(ctx.sourceFile)}"`);
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, ctx) ?? {};
523
+ return parseLiteralNode(arg, ctx2) ?? {};
524
524
  }
525
- function parseDefaultValueExpression(expr, ctx) {
526
- if (!expr) return {};
527
- if (expr.kind === _typescript.SyntaxKind.NewExpression) {
528
- const identifier = expr.expression;
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 (expr.kind === _typescript.SyntaxKind.PropertyAccessExpression && expr.expression.kind === _typescript.SyntaxKind.ThisKeyword) {
536
+ } else if (expr2.kind === _typescript.SyntaxKind.PropertyAccessExpression && expr2.expression.kind === _typescript.SyntaxKind.ThisKeyword) {
537
537
  return {
538
- defaultValueCopyFrom: expr.name?.text
538
+ defaultValueCopyFrom: expr2.name?.text
539
539
  };
540
540
  }
541
541
  return {
542
- defaultValue: parseLiteralNode(expr, ctx)
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 = JSON.parse(defaultValueStr.replace(/[\'\`]/gi, '"'));
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, ctx) {
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(ctx.sourceFile)}"`);
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(ctx.sourceFile)}"`);
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, ctx);
574
+ const value = parseLiteralNode(valueExpression, ctx2);
575
575
  result[name] = value;
576
576
  }
577
577
  return result;
@@ -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, ctx) {
133
+ function parseClassOrTypeDeclaration(typeDeclaration, typeName, ctx2) {
134
134
  if (typeDeclaration.kind === SyntaxKind.ClassDeclaration)
135
- return parseClassDeclaration(typeDeclaration, typeName, ctx);
135
+ return parseClassDeclaration(typeDeclaration, typeName, ctx2);
136
136
  if (typeDeclaration.kind === SyntaxKind.TypeAliasDeclaration)
137
- return parseTypeAliasDeclaration(typeDeclaration, typeName, ctx);
137
+ return parseTypeAliasDeclaration(typeDeclaration, typeName, ctx2);
138
138
  throw new Error(`Unexpected type definition - ${typeName}`);
139
139
  }
140
- function parseClassDeclaration(typeDeclaration, typeName, ctx) {
141
- if (ctx.result[typeName])
142
- return ctx.result[typeName];
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, ctx);
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(ctx.sourceFile)}")`);
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, ctx);
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: ctx.sourceFile.fileName
174
+ sourceFile: ctx2.sourceFile.fileName
175
175
  };
176
176
  return result;
177
177
  }
178
- function parseTypeAliasDeclaration(typeDeclaration, typeName, ctx) {
179
- if (ctx.result[typeName])
180
- return ctx.result[typeName];
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, ctx);
191
- return { name, enumValues, comment, decorators: {}, sourceFile: ctx.sourceFile.fileName };
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, ctx);
195
- return { name, enumValues, comment, decorators: {}, sourceFile: ctx.sourceFile.fileName };
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(ctx.sourceFile)}. Did you mean 'class'?`);
197
+ throw new Error(`Unexpected type definition - ${typeDeclaration.getText(ctx2.sourceFile)}. Did you mean 'class'?`);
198
198
  }
199
- function parseDecorators(modifiers, ctx) {
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, ctx)) || [];
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, ctx) {
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(ctx.sourceFile)}`);
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(ctx.sourceFile)}`);
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, ctx) {
223
- const name = node.name.getText(ctx.sourceFile);
224
- const { defaultValue, defaultValueCopyFrom, defaultValueClass } = parseDefaultValueExpression(node.initializer, ctx);
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, ctx);
228
- let defaultValueType = defaultValueClass || getPrimitiveTypeFromDefaultValue(defaultValue);
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(ctx, parentName, name, node, defaultValueType);
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(ctx, parentName, fieldName, node, defaultValueType) {
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, ctx);
281
- parseFieldTypeInverseRelation(parsedType, parentName, ctx);
282
- parseFieldTypeInlineEnum(parsedType, parentName, fieldName, ctx);
283
- parseFieldTypeKeyofEnum(parsedType, parentName, fieldName, ctx);
284
- parseFieldTypeRecordEnum(parsedType, parentName, fieldName, ctx);
285
- parsedType.type = parsedType.type ?? parsedType.nodeType?.getText(ctx.sourceFile) ?? defaultValueType;
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(ctx.sourceFile)}'`);
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 (!ctx.typeNodesMap[parsedType.type])
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) && !ctx.typeNodesMap[parsedType.type] && !ctx.result[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, ctx) {
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(ctx.sourceFile) !== "Record")
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(ctx.sourceFile);
317
- const valueTypeName = nt.typeArguments[1].getText(ctx.sourceFile);
318
- const keyType = ctx.typeNodesMap[keyTypeName];
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 (!ctx.result[keyTypeName])
322
- ctx.result[keyTypeName] = parseClassOrTypeDeclaration(keyType.node, keyTypeName, ctx);
323
- const enumValues = ctx.result[keyTypeName].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
- ctx.result[newTypeName] = {
328
+ ctx2.result[newTypeName] = {
329
329
  name: newTypeName,
330
330
  decorators: {},
331
331
  fields: _.keyBy(fieldsArray, "name"),
332
- sourceFile: ctx.sourceFile.fileName
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, ctx) {
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(ctx.sourceFile) === "Relation") {
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(ctx, ta);
359
+ parsedType.relationDenormFields = getRelationDenormFields(ctx2, ta);
360
360
  }
361
361
  }
362
362
  }
363
- function parseFieldTypeInverseRelation(parsedType, parentName, ctx) {
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(ctx.sourceFile) === "InverseRelation") {
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(ctx, nt.typeArguments[0]);
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(ctx, taNode);
374
+ parsedType.inverseRelationField = parseStringField(ctx2, taNode);
375
375
  }
376
376
  }
377
377
  }
378
- function parseStringField(ctx, node) {
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(ctx.sourceFile)}`);
385
+ throw new Error(`Unexpected type - ${node.getText(ctx2.sourceFile)}`);
386
386
  }
387
- function parseFieldTypeInlineEnum(parsedType, parentName, fieldName, ctx) {
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, ctx);
391
+ const enumValues = getEnumValues(nt, parsedType.node, ctx2);
392
392
  const newTypeName = `${parentName}_${_.upperFirst(fieldName)}`;
393
- ctx.result[newTypeName] = { name: newTypeName, decorators: {}, enumValues, sourceFile: ctx.sourceFile.fileName };
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, ctx) {
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, ctx);
402
+ const enumValues = getEnumValuesFromKeyOf(nt, ctx2);
403
403
  const newTypeName = `${parentName}_${_.upperFirst(fieldName)}`;
404
- ctx.result[newTypeName] = { name: newTypeName, decorators: {}, enumValues, sourceFile: ctx.sourceFile.fileName };
404
+ ctx2.result[newTypeName] = { name: newTypeName, decorators: {}, enumValues, sourceFile: ctx2.sourceFile.fileName };
405
405
  parsedType.type = newTypeName;
406
406
  }
407
- function getEnumValuesFromKeyOf(nodeType, ctx) {
407
+ function getEnumValuesFromKeyOf(nodeType, ctx2) {
408
408
  if (nodeType.type.kind !== SyntaxKind.TypeReference) {
409
- throw new Error(`Unexpected type - ${nodeType.type.getText(ctx.sourceFile)}`);
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(ctx.sourceFile)}`);
413
+ throw new Error(`Unexpected type - ${typeReferenceNode.getText(ctx2.sourceFile)}`);
414
414
  }
415
415
  const typeName = typeReferenceNode.typeName.text;
416
- const type = ctx.typeNodesMap[typeName];
416
+ const type = ctx2.typeNodesMap[typeName];
417
417
  if (!type) {
418
418
  throw new Error(`Unexpected type - ${typeName}`);
419
419
  }
420
- if (!ctx.result[typeName])
421
- ctx.result[typeName] = parseClassOrTypeDeclaration(type.node, typeName, ctx);
422
- if (!ctx.result[typeName].fields)
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(ctx.result[typeName].fields || {}, (v) => ({
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(ctx, node) {
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(ctx.sourceFile)}`);
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(ctx, t));
440
+ return union.types.flatMap((t) => getRelationDenormFields(ctx2, t));
441
441
  }
442
- throw new Error(`Unexpected type - ${node.getText(ctx.sourceFile)}`);
442
+ throw new Error(`Unexpected type - ${node.getText(ctx2.sourceFile)}`);
443
443
  }
444
- function verifyDefaultValueType(field, ctx) {
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(defaultValue)) {
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(defaultValue) !== type) {
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 (!ctx.result[type] && ctx.typeNodesMap[type])
455
- ctx.result[type] = parseClassOrTypeDeclaration(ctx.typeNodesMap[type].node, type, ctx);
456
- const enumValues = ctx.result[type]?.enumValues;
457
- if (enumValues && !enumValues[defaultValue]) {
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, ctx) {
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, ctx) {
476
- const expr = decoratorNode.expression;
477
- if (expr.kind !== SyntaxKind.CallExpression)
478
- throw new Error(`Unexpected decorator format: "${expr.getText(ctx.sourceFile)}"`);
479
- const nameNode = expr.expression;
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(ctx.sourceFile)}"`);
482
- return { name: nameNode.text, args: parseDecoratorArguments(expr, ctx) };
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(expr, ctx) {
485
- const args = expr.arguments;
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: "${expr.getText(ctx.sourceFile)}"`);
489
+ throw new Error(`Too many arguments - one expected: "${expr2.getText(ctx2.sourceFile)}"`);
490
490
  const arg = args[0];
491
- return parseLiteralNode(arg, ctx) ?? {};
491
+ return parseLiteralNode(arg, ctx2) ?? {};
492
492
  }
493
- function parseDefaultValueExpression(expr, ctx) {
494
- if (!expr)
493
+ function parseDefaultValueExpression(expr2, ctx2) {
494
+ if (!expr2)
495
495
  return {};
496
- if (expr.kind === SyntaxKind.NewExpression) {
497
- const identifier = expr.expression;
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 (expr.kind === SyntaxKind.PropertyAccessExpression && expr.expression.kind === SyntaxKind.ThisKeyword) {
503
- return { defaultValueCopyFrom: expr.name?.text };
502
+ } else if (expr2.kind === SyntaxKind.PropertyAccessExpression && expr2.expression.kind === SyntaxKind.ThisKeyword) {
503
+ return { defaultValueCopyFrom: expr2.name?.text };
504
504
  }
505
- return { defaultValue: parseLiteralNode(expr, ctx) };
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 = JSON.parse(defaultValueStr.replace(/[\'\`]/gi, '"'));
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, ctx) {
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(ctx.sourceFile)}"`);
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(ctx.sourceFile)}"`);
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, ctx);
545
+ const value = parseLiteralNode(valueExpression, ctx2);
546
546
  result[name] = value;
547
547
  }
548
548
  return result;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rads-db",
3
- "version": "3.0.79",
3
+ "version": "3.0.80",
4
4
  "packageManager": "pnpm@8.6.1",
5
5
  "description": "Say goodbye to boilerplate code and hello to efficient and elegant syntax.",
6
6
  "author": "",