@shaclmate/compiler 4.0.40 → 4.0.41

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.
Files changed (114) hide show
  1. package/dist/ShapesGraphToAstTransformer.d.ts +0 -8
  2. package/dist/ShapesGraphToAstTransformer.js +5 -85
  3. package/dist/_ShapesGraphToAstTransformer/shapeNodeKinds.js +128 -160
  4. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstObjectType.d.ts +1 -1
  5. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstObjectType.js +0 -16
  6. package/dist/ast/AbstractCompoundType.d.ts +2 -1
  7. package/dist/ast/AbstractCompoundType.js +1 -1
  8. package/dist/ast/AbstractLazyObjectType.d.ts +0 -2
  9. package/dist/ast/Ast.d.ts +1 -3
  10. package/dist/ast/ListType.d.ts +1 -1
  11. package/dist/ast/ListType.js +1 -1
  12. package/dist/ast/ObjectType.d.ts +0 -12
  13. package/dist/ast/ObjectType.js +0 -78
  14. package/dist/generators/AstJsonGenerator.js +7 -3
  15. package/dist/generators/transformAstToLabeledPropertyGraph.js +59 -56
  16. package/dist/generators/ts/AbstractCollectionType.d.ts +4 -11
  17. package/dist/generators/ts/AbstractCollectionType.js +3 -62
  18. package/dist/generators/ts/AbstractContainerType.d.ts +0 -1
  19. package/dist/generators/ts/AbstractContainerType.js +1 -4
  20. package/dist/generators/ts/AbstractDateType.d.ts +4 -1
  21. package/dist/generators/ts/AbstractDateType.js +6 -1
  22. package/dist/generators/ts/AbstractIdentifierType.d.ts +4 -0
  23. package/dist/generators/ts/AbstractIdentifierType.js +3 -0
  24. package/dist/generators/ts/AbstractLazyObjectType.d.ts +1 -1
  25. package/dist/generators/ts/AbstractLazyObjectType.js +12 -1
  26. package/dist/generators/ts/AbstractLiteralType.d.ts +1 -0
  27. package/dist/generators/ts/AbstractNumericType.js +7 -7
  28. package/dist/generators/ts/AbstractPrimitiveType.d.ts +1 -0
  29. package/dist/generators/ts/AbstractTermType.d.ts +1 -2
  30. package/dist/generators/ts/AbstractTermType.js +1 -3
  31. package/dist/generators/ts/AbstractType.d.ts +46 -16
  32. package/dist/generators/ts/AbstractType.js +31 -16
  33. package/dist/generators/ts/BigDecimalType.d.ts +4 -0
  34. package/dist/generators/ts/BigDecimalType.js +4 -1
  35. package/dist/generators/ts/BigIntType.d.ts +3 -1
  36. package/dist/generators/ts/BigIntType.js +1 -1
  37. package/dist/generators/ts/BlankNodeType.d.ts +1 -1
  38. package/dist/generators/ts/BlankNodeType.js +3 -3
  39. package/dist/generators/ts/BooleanType.d.ts +3 -1
  40. package/dist/generators/ts/BooleanType.js +1 -1
  41. package/dist/generators/ts/DefaultValueType.d.ts +39 -1
  42. package/dist/generators/ts/DefaultValueType.js +6 -3
  43. package/dist/generators/ts/FloatType.d.ts +3 -1
  44. package/dist/generators/ts/FloatType.js +1 -1
  45. package/dist/generators/ts/GraphqlSchema.js +2 -2
  46. package/dist/generators/ts/IdentifierType.d.ts +1 -1
  47. package/dist/generators/ts/IdentifierType.js +7 -5
  48. package/dist/generators/ts/IntType.d.ts +3 -1
  49. package/dist/generators/ts/IntType.js +1 -1
  50. package/dist/generators/ts/IriType.js +4 -2
  51. package/dist/generators/ts/LazyObjectOptionType.d.ts +1 -0
  52. package/dist/generators/ts/LazyObjectOptionType.js +8 -4
  53. package/dist/generators/ts/LazyObjectSetType.js +9 -3
  54. package/dist/generators/ts/LazyObjectType.js +5 -2
  55. package/dist/generators/ts/ListType.d.ts +10 -1
  56. package/dist/generators/ts/ListType.js +45 -8
  57. package/dist/generators/ts/LiteralType.d.ts +4 -0
  58. package/dist/generators/ts/LiteralType.js +6 -3
  59. package/dist/generators/ts/ObjectType.d.ts +8 -22
  60. package/dist/generators/ts/ObjectType.js +6 -74
  61. package/dist/generators/ts/ObjectUnionType.js +2 -2
  62. package/dist/generators/ts/OptionType.d.ts +5 -1
  63. package/dist/generators/ts/OptionType.js +10 -5
  64. package/dist/generators/ts/RdfjsDatasetObjectSetType.js +1 -4
  65. package/dist/generators/ts/SetType.d.ts +16 -1
  66. package/dist/generators/ts/SetType.js +80 -2
  67. package/dist/generators/ts/Snippets.d.ts +3 -1
  68. package/dist/generators/ts/Snippets.js +18 -4
  69. package/dist/generators/ts/StringType.d.ts +3 -1
  70. package/dist/generators/ts/StringType.js +5 -1
  71. package/dist/generators/ts/TermType.d.ts +4 -0
  72. package/dist/generators/ts/TermType.js +3 -0
  73. package/dist/generators/ts/TsGenerator.d.ts +1 -1
  74. package/dist/generators/ts/TsGenerator.js +53 -28
  75. package/dist/generators/ts/TypeFactory.js +12 -25
  76. package/dist/generators/ts/UnionType.d.ts +3 -6
  77. package/dist/generators/ts/UnionType.js +42 -55
  78. package/dist/generators/ts/ZodGenerator.d.ts +1 -1
  79. package/dist/generators/ts/ZodGenerator.js +19 -12
  80. package/dist/generators/ts/_ObjectType/AbstractProperty.d.ts +6 -3
  81. package/dist/generators/ts/_ObjectType/DiscriminantProperty.d.ts +8 -12
  82. package/dist/generators/ts/_ObjectType/DiscriminantProperty.js +18 -35
  83. package/dist/generators/ts/_ObjectType/IdentifierProperty.js +5 -5
  84. package/dist/generators/ts/_ObjectType/ObjectType_createFunctionDeclaration.js +9 -30
  85. package/dist/generators/ts/_ObjectType/ObjectType_equalsFunctionDeclaration.js +0 -4
  86. package/dist/generators/ts/_ObjectType/ObjectType_filterFunctionDeclaration.js +0 -3
  87. package/dist/generators/ts/_ObjectType/ObjectType_filterTypeDeclaration.js +0 -3
  88. package/dist/generators/ts/_ObjectType/ObjectType_focusSparqlConstructTriplesFunctionDeclaration.js +0 -4
  89. package/dist/generators/ts/_ObjectType/ObjectType_focusSparqlWherePatternsFunctionDeclaration.js +3 -22
  90. package/dist/generators/ts/_ObjectType/ObjectType_fromJsonFunctionDeclaration.js +4 -29
  91. package/dist/generators/ts/_ObjectType/ObjectType_fromRdfResourceFunctionDeclaration.js +10 -39
  92. package/dist/generators/ts/_ObjectType/ObjectType_hashFunctionDeclarations.js +25 -29
  93. package/dist/generators/ts/_ObjectType/ObjectType_interfaceDeclaration.js +1 -5
  94. package/dist/generators/ts/_ObjectType/ObjectType_isTypeFunctionDeclaration.js +11 -8
  95. package/dist/generators/ts/_ObjectType/ObjectType_jsonSchemaFunctionDeclaration.js +3 -9
  96. package/dist/generators/ts/_ObjectType/ObjectType_jsonTypeAliasDeclaration.js +1 -8
  97. package/dist/generators/ts/_ObjectType/ObjectType_jsonUiSchemaFunctionDeclaration.js +1 -3
  98. package/dist/generators/ts/_ObjectType/ObjectType_schemaVariableStatement.js +2 -4
  99. package/dist/generators/ts/_ObjectType/ObjectType_toJsonFunctionDeclaration.js +10 -31
  100. package/dist/generators/ts/_ObjectType/ObjectType_toRdfResourceFunctionDeclaration.js +1 -1
  101. package/dist/generators/ts/_ObjectType/ObjectType_toStringFunctionDeclarations.js +2 -9
  102. package/dist/generators/ts/_ObjectType/ShaclProperty.js +6 -6
  103. package/dist/generators/ts/_ObjectType/identifierTypeDeclarations.js +0 -11
  104. package/dist/generators/ts/_snippets/snippets_convertToArraySet.d.ts +3 -0
  105. package/dist/generators/ts/_snippets/snippets_convertToArraySet.js +8 -0
  106. package/dist/generators/ts/_snippets/snippets_convertToList.d.ts +3 -0
  107. package/dist/generators/ts/_snippets/snippets_convertToList.js +7 -0
  108. package/dist/generators/ts/_snippets/snippets_convertToScalarSet.d.ts +3 -0
  109. package/dist/generators/ts/_snippets/snippets_convertToScalarSet.js +15 -0
  110. package/dist/input/generated.d.ts +756 -115
  111. package/dist/input/generated.js +634 -126
  112. package/package.json +2 -4
  113. package/dist/generators/ts/_snippets/snippets_convertToArray.d.ts +0 -3
  114. package/dist/generators/ts/_snippets/snippets_convertToArray.js +0 -8
@@ -5,18 +5,80 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
5
5
  return c > 3 && r && Object.defineProperty(target, key, r), r;
6
6
  };
7
7
  import { Maybe } from "purify-ts";
8
+ import { invariant } from "ts-invariant";
8
9
  import { Memoize } from "typescript-memoize";
9
10
  import { AbstractCollectionType } from "./AbstractCollectionType.js";
10
- import { code, joinCode } from "./ts-poet-wrapper.js";
11
+ import { code, joinCode, literalOf } from "./ts-poet-wrapper.js";
11
12
  export class SetType extends AbstractCollectionType {
12
13
  graphqlArgs = Maybe.empty();
13
14
  kind = "Set";
15
+ minCount;
16
+ jsTypes = [
17
+ { instanceof: "Array", typeof: "object" },
18
+ ];
19
+ constructor({ minCount, ...superParameters }) {
20
+ super(superParameters);
21
+ this.minCount = minCount;
22
+ invariant(this.minCount >= 0n);
23
+ if (this._mutable) {
24
+ invariant(this.minCount === 0n);
25
+ }
26
+ }
27
+ get conversionFunction() {
28
+ const itemConversionFunction = this.itemType.conversionFunction.orDefault(this.itemConversionFunctionDefault);
29
+ let conversionFunction;
30
+ const sourceTypes = [];
31
+ if (itemConversionFunction.sourceTypes.some((sourceType) => sourceType.jsType.typeof === "object" &&
32
+ sourceType.jsType.instanceof === "Array")) {
33
+ conversionFunction = this.reusables.snippets.convertToArraySet;
34
+ }
35
+ else {
36
+ conversionFunction = this.reusables.snippets.convertToScalarSet;
37
+ // Convert from a single item
38
+ sourceTypes.push(...itemConversionFunction.sourceTypes);
39
+ }
40
+ // Convert from an array of items
41
+ sourceTypes.push({
42
+ expression: code `readonly (${joinCode(itemConversionFunction.sourceTypes.map((itemSourceType) => code `${itemSourceType.expression}`), { on: " | " })})[]`,
43
+ jsType: { instanceof: "Array", typeof: "object" },
44
+ });
45
+ // Convert from undefined to an empty array
46
+ if (this.minCount === 0n) {
47
+ sourceTypes.push({
48
+ expression: code `undefined`,
49
+ jsType: { typeof: "undefined" },
50
+ });
51
+ }
52
+ return Maybe.of({
53
+ code: code `${conversionFunction}(${itemConversionFunction.code}, ${literalOf(!this._mutable)})`,
54
+ sourceTypes,
55
+ });
56
+ }
57
+ get toRdfResourceValueTypes() {
58
+ return this.itemType.toRdfResourceValueTypes;
59
+ }
14
60
  get valueSparqlConstructTriplesFunction() {
15
61
  return code `${this.reusables.snippets.setSparqlConstructTriples}<${this.itemType.filterType}, ${this.itemType.schemaType}>(${this.itemType.valueSparqlConstructTriplesFunction})`;
16
62
  }
17
63
  get valueSparqlWherePatternsFunction() {
18
64
  return code `${this.reusables.snippets.setSparqlWherePatterns}<${this.itemType.filterType}, ${this.itemType.schemaType}>(${this.itemType.valueSparqlWherePatternsFunction})`;
19
65
  }
66
+ get schemaInitializers() {
67
+ let schemaInitializers = super.schemaInitializers;
68
+ if (this.minCount > 0n) {
69
+ schemaInitializers = schemaInitializers.concat(code `minCount: ${Number(this.minCount)}`);
70
+ }
71
+ return schemaInitializers;
72
+ }
73
+ fromJsonExpression({ variables, }) {
74
+ let expression = variables.value;
75
+ if (this.minCount === 0n) {
76
+ expression = code `(${expression} ?? [])`;
77
+ }
78
+ return code `${this.reusables.imports.Either}.sequence<Error, ${this.itemType.expression}>(${expression}.map(item => (${this.itemType.fromJsonExpression({
79
+ variables: { value: code `item` },
80
+ })})))`;
81
+ }
20
82
  fromRdfResourceValuesExpression(parameters) {
21
83
  const { variables } = parameters;
22
84
  return joinCode([
@@ -25,8 +87,21 @@ export class SetType extends AbstractCollectionType {
25
87
  code `map(valuesArray => ${this.reusables.imports.Resource}.Values.fromValue({ focusResource: ${variables.resource}, propertyPath: ${variables.propertyPath}, value: valuesArray }))`,
26
88
  ], { on: "." });
27
89
  }
90
+ jsonSchema(parameters) {
91
+ let schema = code `${this.itemType.jsonSchema(parameters)}.array()`;
92
+ if (this.minCount > 0n) {
93
+ schema = code `${schema}.nonempty().min(${this.minCount})`;
94
+ }
95
+ else {
96
+ schema = code `${schema}.optional()`;
97
+ }
98
+ if (!this._mutable) {
99
+ schema = code `${schema}.readonly()`;
100
+ }
101
+ return schema;
102
+ }
28
103
  jsonType() {
29
- const name = code `${!this.mutable ? "readonly " : ""}(${this.itemType.jsonType().name})[]`;
104
+ const name = code `${!this.mutable ? "readonly " : ""}(${this.itemType.jsonType().expression})[]`;
30
105
  if (this.minCount === 0n) {
31
106
  return new AbstractCollectionType.JsonType(name, { optional: true });
32
107
  }
@@ -41,6 +116,9 @@ export class SetType extends AbstractCollectionType {
41
116
  return code `(${variables.value}.length > 0 ? \`[\${${variables.value}.map(item => (${this.itemType.toStringExpression({ variables: { value: code `item` } })}))}]\` : undefined)`;
42
117
  }
43
118
  }
119
+ __decorate([
120
+ Memoize()
121
+ ], SetType.prototype, "conversionFunction", null);
44
122
  __decorate([
45
123
  Memoize()
46
124
  ], SetType.prototype, "valueSparqlConstructTriplesFunction", null);
@@ -69,7 +69,7 @@ export declare class Snippets {
69
69
  get booleanEquals(): Snippet;
70
70
  get booleanSparqlWherePatterns(): Snippet;
71
71
  get compactRecord(): Snippet;
72
- get convertToArray(): Snippet;
72
+ get convertToArraySet(): Snippet;
73
73
  get convertToBlankNode(): Snippet;
74
74
  get convertToBlankNodeIdentifierProperty(): Snippet;
75
75
  get convertToIdentifier(): Snippet;
@@ -79,8 +79,10 @@ export declare class Snippets {
79
79
  get convertToLazyObject(): Snippet;
80
80
  get convertToLazyObjectOption(): Snippet;
81
81
  get convertToLazyObjectSet(): Snippet;
82
+ get convertToList(): Snippet;
82
83
  get convertToLiteral(): Snippet;
83
84
  get convertToMaybe(): Snippet;
85
+ get convertToScalarSet(): Snippet;
84
86
  get convertWithDefaultValue(): Snippet;
85
87
  get dateEquals(): Snippet;
86
88
  get dateSparqlWherePatterns(): Snippet;
@@ -23,7 +23,7 @@ import { snippets_CollectionFilter } from "./_snippets/snippets_CollectionFilter
23
23
  import { snippets_CollectionSchema } from "./_snippets/snippets_CollectionSchema.js";
24
24
  import { snippets_ConversionFunction } from "./_snippets/snippets_ConversionFunction.js";
25
25
  import { snippets_compactRecord } from "./_snippets/snippets_compactRecord.js";
26
- import { snippets_convertToArray } from "./_snippets/snippets_convertToArray.js";
26
+ import { snippets_convertToArraySet } from "./_snippets/snippets_convertToArraySet.js";
27
27
  import { snippets_convertToBlankNode } from "./_snippets/snippets_convertToBlankNode.js";
28
28
  import { snippets_convertToBlankNodeIdentifierProperty } from "./_snippets/snippets_convertToBlankNodeIdentifierProperty.js";
29
29
  import { snippets_convertToIdentifier } from "./_snippets/snippets_convertToIdentifier.js";
@@ -33,8 +33,10 @@ import { snippets_convertToIriIdentifierProperty } from "./_snippets/snippets_co
33
33
  import { snippets_convertToLazyObject } from "./_snippets/snippets_convertToLazyObject.js";
34
34
  import { snippets_convertToLazyObjectOption } from "./_snippets/snippets_convertToLazyObjectOption.js";
35
35
  import { snippets_convertToLazyObjectSet } from "./_snippets/snippets_convertToLazyObjectSet.js";
36
+ import { snippets_convertToList } from "./_snippets/snippets_convertToList.js";
36
37
  import { snippets_convertToLiteral } from "./_snippets/snippets_convertToLiteral.js";
37
38
  import { snippets_convertToMaybe } from "./_snippets/snippets_convertToMaybe.js";
39
+ import { snippets_convertToScalarSet } from "./_snippets/snippets_convertToScalarSet.js";
38
40
  import { snippets_convertWithDefaultValue } from "./_snippets/snippets_convertWithDefaultValue.js";
39
41
  import { snippets_DateFilter } from "./_snippets/snippets_DateFilter.js";
40
42
  import { snippets_DateSchema } from "./_snippets/snippets_DateSchema.js";
@@ -326,8 +328,8 @@ export class Snippets {
326
328
  get compactRecord() {
327
329
  return this.snippet(snippets_compactRecord);
328
330
  }
329
- get convertToArray() {
330
- return this.snippet(snippets_convertToArray);
331
+ get convertToArraySet() {
332
+ return this.snippet(snippets_convertToArraySet);
331
333
  }
332
334
  get convertToBlankNode() {
333
335
  return this.snippet(snippets_convertToBlankNode);
@@ -356,12 +358,18 @@ export class Snippets {
356
358
  get convertToLazyObjectSet() {
357
359
  return this.snippet(snippets_convertToLazyObjectSet);
358
360
  }
361
+ get convertToList() {
362
+ return this.snippet(snippets_convertToList);
363
+ }
359
364
  get convertToLiteral() {
360
365
  return this.snippet(snippets_convertToLiteral);
361
366
  }
362
367
  get convertToMaybe() {
363
368
  return this.snippet(snippets_convertToMaybe);
364
369
  }
370
+ get convertToScalarSet() {
371
+ return this.snippet(snippets_convertToScalarSet);
372
+ }
365
373
  get convertWithDefaultValue() {
366
374
  return this.snippet(snippets_convertWithDefaultValue);
367
375
  }
@@ -779,7 +787,7 @@ __decorate([
779
787
  ], Snippets.prototype, "compactRecord", null);
780
788
  __decorate([
781
789
  Memoize()
782
- ], Snippets.prototype, "convertToArray", null);
790
+ ], Snippets.prototype, "convertToArraySet", null);
783
791
  __decorate([
784
792
  Memoize()
785
793
  ], Snippets.prototype, "convertToBlankNode", null);
@@ -807,12 +815,18 @@ __decorate([
807
815
  __decorate([
808
816
  Memoize()
809
817
  ], Snippets.prototype, "convertToLazyObjectSet", null);
818
+ __decorate([
819
+ Memoize()
820
+ ], Snippets.prototype, "convertToList", null);
810
821
  __decorate([
811
822
  Memoize()
812
823
  ], Snippets.prototype, "convertToLiteral", null);
813
824
  __decorate([
814
825
  Memoize()
815
826
  ], Snippets.prototype, "convertToMaybe", null);
827
+ __decorate([
828
+ Memoize()
829
+ ], Snippets.prototype, "convertToScalarSet", null);
816
830
  __decorate([
817
831
  Memoize()
818
832
  ], Snippets.prototype, "convertWithDefaultValue", null);
@@ -6,8 +6,10 @@ export declare class StringType extends AbstractPrimitiveType<string> {
6
6
  readonly filterType: Code;
7
7
  readonly graphqlType: import("./AbstractType.js").AbstractType.GraphqlType;
8
8
  readonly hashFunction: Code;
9
+ readonly jsTypes: readonly [{
10
+ readonly typeof: "string";
11
+ }];
9
12
  readonly kind = "String";
10
- readonly typeofs: "string"[];
11
13
  readonly valueSparqlWherePatternsFunction: Code;
12
14
  get expression(): Code;
13
15
  get schemaType(): Code;
@@ -13,8 +13,12 @@ export class StringType extends AbstractPrimitiveType {
13
13
  filterType = code `${this.reusables.snippets.StringFilter}`;
14
14
  graphqlType = new AbstractPrimitiveType.GraphqlType(code `${this.reusables.imports.GraphQLString}`, this.reusables);
15
15
  hashFunction = code `${this.reusables.snippets.hashString}`;
16
+ jsTypes = [
17
+ {
18
+ typeof: "string",
19
+ },
20
+ ];
16
21
  kind = "String";
17
- typeofs = ["string"];
18
22
  valueSparqlWherePatternsFunction = code `${this.reusables.snippets.stringSparqlWherePatterns}`;
19
23
  get expression() {
20
24
  if (this.primitiveIn.length > 0) {
@@ -10,6 +10,10 @@ export declare class TermType<ConstantTermT extends Literal | NamedNode = Litera
10
10
  readonly kind = "Term";
11
11
  readonly nodeKinds: ReadonlySet<NodeKind>;
12
12
  readonly schemaType: Code;
13
+ readonly jsTypes: readonly [{
14
+ readonly instanceof: "Object";
15
+ readonly typeof: "object";
16
+ }];
13
17
  readonly valueSparqlWherePatternsFunction: Code;
14
18
  constructor({ nodeKinds, ...superParameters }: ConstructorParameters<typeof AbstractTermType<ConstantTermT, RuntimeTermT>>[0] & {
15
19
  nodeKinds: ReadonlySet<NodeKind>;
@@ -18,6 +18,9 @@ export class TermType extends AbstractTermType {
18
18
  kind = "Term";
19
19
  nodeKinds;
20
20
  schemaType = code `${this.reusables.snippets.TermSchema}`;
21
+ jsTypes = [
22
+ { instanceof: "Object", typeof: "object" },
23
+ ];
21
24
  valueSparqlWherePatternsFunction = code `${this.reusables.snippets.termSparqlWherePatterns}`;
22
25
  constructor({ nodeKinds, ...superParameters }) {
23
26
  super(superParameters);
@@ -1,5 +1,5 @@
1
1
  import type { Logger } from "ts-log";
2
- import * as ast from "../../ast/index.js";
2
+ import type * as ast from "../../ast/index.js";
3
3
  import type { Generator } from "../Generator.js";
4
4
  import type { TsFeature } from "./TsFeature.js";
5
5
  export declare class TsGenerator implements Generator {
@@ -1,6 +1,5 @@
1
1
  import { Maybe } from "purify-ts";
2
2
  import { invariant } from "ts-invariant";
3
- import * as ast from "../../ast/index.js";
4
3
  import { BlankNodeType } from "./BlankNodeType.js";
5
4
  import { GraphqlSchema } from "./GraphqlSchema.js";
6
5
  import { IdentifierType } from "./IdentifierType.js";
@@ -12,6 +11,9 @@ import { Reusables } from "./Reusables.js";
12
11
  import { SparqlObjectSetType } from "./SparqlObjectSetType.js";
13
12
  import { TypeFactory } from "./TypeFactory.js";
14
13
  import { code, joinCode } from "./ts-poet-wrapper.js";
14
+ function compareTsNamedType(left, right) {
15
+ return left.alias.unsafeCoerce().localeCompare(right.alias.unsafeCoerce());
16
+ }
15
17
  export class TsGenerator {
16
18
  configuration;
17
19
  logger;
@@ -31,54 +33,77 @@ export class TsGenerator {
31
33
  reusables,
32
34
  });
33
35
  let declarations = [];
34
- for (const namedObjectType of ast_.namedObjectTypes) {
35
- for (const tsImport of namedObjectType.tsImports) {
36
- declarations.push(code `${tsImport}`);
36
+ const tsNamedTypes = [];
37
+ const tsNamedObjectTypes = [];
38
+ const tsNamedObjectUnionTypes = [];
39
+ for (const astNamedType of ast_.namedTypes) {
40
+ const tsNamedType = typeFactory.createType(astNamedType);
41
+ tsNamedTypes.push(tsNamedType);
42
+ if (astNamedType.kind === "Object") {
43
+ for (const tsImport of astNamedType.tsImports) {
44
+ declarations.push(code `${tsImport}`);
45
+ }
37
46
  }
38
- }
39
- for (const astNamedUnionType of ast_.namedUnionTypes) {
40
- if (astNamedUnionType.isObjectUnionType()) {
41
- continue;
47
+ switch (tsNamedType.kind) {
48
+ case "Object":
49
+ tsNamedObjectTypes.push(tsNamedType);
50
+ break;
51
+ case "ObjectUnion":
52
+ tsNamedObjectUnionTypes.push(tsNamedType);
53
+ break;
42
54
  }
43
- declarations = declarations.concat(typeFactory.createType(astNamedUnionType).declaration.toList());
44
55
  }
45
- const namedObjectTypesToposorted = ast.ObjectType.toposort(ast_.namedObjectTypes).map((astObjectType) => typeFactory.createObjectType(astObjectType));
46
- const namedObjectUnionTypesToposorted = ast_.namedUnionTypes
47
- .filter((_) => _.isObjectUnionType())
48
- .map((astObjectUnionType) => typeFactory.createObjectUnionType(astObjectUnionType));
49
- for (const namedObjectType of namedObjectTypesToposorted) {
50
- declarations = declarations.concat(namedObjectType.declaration.toList());
56
+ tsNamedTypes.sort(compareTsNamedType);
57
+ tsNamedObjectTypes.sort(compareTsNamedType);
58
+ tsNamedObjectUnionTypes.sort(compareTsNamedType);
59
+ for (const tsNamedType of tsNamedTypes) {
60
+ switch (tsNamedType.kind) {
61
+ case "ObjectUnion":
62
+ case "Union":
63
+ continue; // Declare compound types last.
64
+ }
65
+ tsNamedType.declaration.ifJust((declaration) => {
66
+ declarations.push(declaration);
67
+ });
51
68
  }
52
- for (const namedObjectUnionType of namedObjectUnionTypesToposorted) {
53
- declarations = declarations.concat(namedObjectUnionType.declaration.toList());
69
+ // Declare compound types last.
70
+ for (const tsNamedType of tsNamedTypes) {
71
+ switch (tsNamedType.kind) {
72
+ case "ObjectUnion":
73
+ case "Union":
74
+ break;
75
+ default:
76
+ continue;
77
+ }
78
+ tsNamedType.declaration.ifJust((declaration) => {
79
+ declarations.push(declaration);
80
+ });
54
81
  }
55
- const namedObjectTypesNameSorted = namedObjectTypesToposorted.toSorted((left, right) => left.alias.unsafeCoerce().localeCompare(right.alias.unsafeCoerce()));
56
- const namedObjectUnionTypesNameSorted = namedObjectUnionTypesToposorted.toSorted((left, right) => left.alias.unsafeCoerce().localeCompare(right.alias.unsafeCoerce()));
57
- switch (namedObjectTypesNameSorted.length) {
82
+ switch (tsNamedObjectTypes.length) {
58
83
  case 0:
59
84
  break;
60
85
  case 1:
61
- declarations.push(code `type ${configuration.syntheticNamePrefix}Object = ${namedObjectTypesNameSorted[0].expression};`);
86
+ declarations.push(code `type ${configuration.syntheticNamePrefix}Object = ${tsNamedObjectTypes[0].expression};`);
62
87
  break;
63
88
  default: {
64
89
  const uberObjectUnionType = this.synthesizeUberObjectUnionType({
65
90
  configuration,
66
- namedObjectTypes: namedObjectTypesToposorted.toReversed(), // Reverse topological order so children ane before parents
91
+ namedObjectTypes: tsNamedObjectTypes,
67
92
  reusables,
68
93
  });
69
94
  declarations = declarations.concat(uberObjectUnionType.declaration.toList());
70
- namedObjectUnionTypesNameSorted.push(uberObjectUnionType);
95
+ tsNamedObjectUnionTypes.push(uberObjectUnionType);
71
96
  }
72
97
  }
73
98
  declarations.push(...this.objectSetTypeDeclarations({
74
99
  configuration,
75
- namedObjectTypes: namedObjectTypesNameSorted,
76
- namedObjectUnionTypes: namedObjectUnionTypesNameSorted,
100
+ namedObjectTypes: tsNamedObjectTypes,
101
+ namedObjectUnionTypes: tsNamedObjectUnionTypes,
77
102
  reusables,
78
103
  }));
79
104
  if (configuration.features.has("GraphQL")) {
80
- const graphqlNamedObjectTypes = namedObjectTypesNameSorted.filter((namedObjectType) => !namedObjectType.synthetic);
81
- const graphqlNamedObjectUnionTypes = namedObjectUnionTypesNameSorted.filter((namedObjectUnionType) => !namedObjectUnionType.synthetic);
105
+ const graphqlNamedObjectTypes = tsNamedObjectTypes.filter((tsNamedObjectType) => !tsNamedObjectType.synthetic);
106
+ const graphqlNamedObjectUnionTypes = tsNamedObjectUnionTypes.filter((tsNamedObjectUnionType) => !tsNamedObjectUnionType.synthetic);
82
107
  if (graphqlNamedObjectTypes.length > 0) {
83
108
  declarations.push(new GraphqlSchema({
84
109
  configuration,
@@ -116,7 +141,7 @@ export class TsGenerator {
116
141
  * Synthesize the $Object union.
117
142
  */
118
143
  synthesizeUberObjectUnionType({ configuration, namedObjectTypes, reusables, }) {
119
- const filteredNamedObjectTypes = namedObjectTypes.filter((namedObjectType) => !namedObjectType.extern);
144
+ const filteredNamedObjectTypes = namedObjectTypes.filter((namedObjectType) => !namedObjectType.extern && !namedObjectType.synthetic);
120
145
  invariant(filteredNamedObjectTypes.length > 0);
121
146
  const nodeKinds = filteredNamedObjectTypes.reduce((nodeKinds, namedObjectType) => {
122
147
  for (const nodeKind of namedObjectType.identifierType.nodeKinds) {
@@ -49,38 +49,24 @@ export class TypeFactory {
49
49
  return cachedObjectType;
50
50
  }
51
51
  }
52
+ const discriminantProperty = astType.name.map((name) => new ObjectType.DiscriminantProperty({
53
+ configuration: this.configuration,
54
+ logger: this.logger,
55
+ name: `${this.configuration.syntheticNamePrefix}type`,
56
+ objectType: { alias: astType.name },
57
+ reusables: this.reusables,
58
+ value: name,
59
+ }));
52
60
  const identifierType = this.createIdentifierType(astType.identifierType);
53
61
  const objectType = new ObjectType({
54
62
  alias: astType.name.map((name) => this.tsName(name, { synthetic: astType.synthetic })),
63
+ discriminantProperty,
55
64
  comment: astType.comment,
56
65
  configuration: this.configuration,
57
66
  extern: astType.extern,
58
67
  fromRdfType: astType.fromRdfType,
59
68
  identifierType,
60
69
  label: astType.label,
61
- lazyAncestorObjectTypes: () => astType.ancestorObjectTypes.map((astType) => this.createObjectType(astType)),
62
- lazyChildObjectTypes: () => astType.childObjectTypes.map((astType) => this.createObjectType(astType)),
63
- lazyDescendantObjectTypes: () => astType.descendantObjectTypes.map((astType) => this.createObjectType(astType)),
64
- lazyDiscriminantProperty: (objectType) => {
65
- // Discriminant property
66
- const discriminantDescendantValues = new Set();
67
- for (const descendantObjectType of objectType.descendantObjectTypes) {
68
- discriminantDescendantValues.add(descendantObjectType.discriminantValue);
69
- }
70
- return new ObjectType.DiscriminantProperty({
71
- configuration: this.configuration,
72
- logger: this.logger,
73
- name: `${this.configuration.syntheticNamePrefix}type`,
74
- objectType,
75
- reusables: this.reusables,
76
- type: new ObjectType.DiscriminantProperty.Type({
77
- descendantValues: [...discriminantDescendantValues].sort(),
78
- mutable: false,
79
- ownValues: [objectType.discriminantValue],
80
- }),
81
- });
82
- },
83
- lazyParentObjectTypes: () => astType.parentObjectTypes.map((astType) => this.createObjectType(astType)),
84
70
  lazyProperties: (objectType) => {
85
71
  const properties = astType.properties
86
72
  .toSorted((left, right) => {
@@ -96,7 +82,9 @@ export class TypeFactory {
96
82
  astObjectTypeProperty: astProperty,
97
83
  objectType,
98
84
  }));
99
- properties.splice(0, 0, objectType._discriminantProperty);
85
+ discriminantProperty.ifJust((discriminantProperty) => {
86
+ properties.splice(0, 0, discriminantProperty);
87
+ });
100
88
  properties.splice(0, 0, new ObjectType.IdentifierProperty({
101
89
  configuration: this.configuration,
102
90
  logger: this.logger,
@@ -297,7 +285,6 @@ export class TypeFactory {
297
285
  itemType,
298
286
  label: astType.label,
299
287
  logger: this.logger,
300
- minCount: 0n,
301
288
  mutable: astType.mutable,
302
289
  reusables: this.reusables,
303
290
  toRdfTypes: astType.toRdfTypes,
@@ -38,7 +38,7 @@ export declare class UnionType<MemberTypeT extends Type> extends AbstractType {
38
38
  get referencesObjectType(): boolean;
39
39
  get schemaType(): Code;
40
40
  get toRdfResourceValueTypes(): AbstractType["toRdfResourceValueTypes"];
41
- get typeofs(): AbstractType["typeofs"];
41
+ get jsTypes(): AbstractType["jsTypes"];
42
42
  get valueSparqlConstructTriplesFunction(): Code;
43
43
  get valueSparqlWherePatternsFunction(): Code;
44
44
  protected get inlineEqualsFunction(): Code;
@@ -84,17 +84,14 @@ type HybridDiscriminant = {
84
84
  readonly kind: "Hybrid";
85
85
  readonly memberValues: readonly {
86
86
  readonly kind: "Extrinsic" | "Intrinsic";
87
- readonly ownValues: readonly AbstractType.DiscriminantProperty.Value[];
87
+ readonly values: readonly AbstractType.DiscriminantProperty.Value[];
88
88
  }[];
89
89
  readonly name: string;
90
90
  };
91
91
  type IntrinsicDiscriminant = {
92
92
  readonly jsonName: string;
93
93
  readonly kind: "Intrinsic";
94
- readonly memberValues: readonly {
95
- readonly descendantValues: readonly AbstractType.DiscriminantProperty.Value[];
96
- readonly ownValues: readonly AbstractType.DiscriminantProperty.Value[];
97
- }[];
94
+ readonly memberValues: readonly AbstractType.DiscriminantProperty.Value[];
98
95
  readonly name: string;
99
96
  };
100
97
  type TypeofDiscriminant = {