@shaclmate/compiler 2.0.22 → 2.0.24

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 (194) hide show
  1. package/dist/ShapesGraphToAstTransformer.d.ts +6 -5
  2. package/dist/ShapesGraphToAstTransformer.js +61 -17
  3. package/dist/_ShapesGraphToAstTransformer/InheritableShapeConstraints.d.ts +2 -0
  4. package/dist/_ShapesGraphToAstTransformer/InheritableShapeConstraints.js +2 -0
  5. package/dist/_ShapesGraphToAstTransformer/ShapeStack.d.ts +15 -0
  6. package/dist/_ShapesGraphToAstTransformer/ShapeStack.js +51 -0
  7. package/dist/_ShapesGraphToAstTransformer/flattenAstObjectCompositeTypeMemberTypes.d.ts +13 -0
  8. package/dist/_ShapesGraphToAstTransformer/flattenAstObjectCompositeTypeMemberTypes.js +51 -0
  9. package/dist/_ShapesGraphToAstTransformer/index.d.ts +5 -5
  10. package/dist/_ShapesGraphToAstTransformer/index.js +5 -5
  11. package/dist/_ShapesGraphToAstTransformer/shapeAstName.js +53 -12
  12. package/dist/_ShapesGraphToAstTransformer/transformNodeShapeToAstType.d.ts +5 -0
  13. package/dist/_ShapesGraphToAstTransformer/transformNodeShapeToAstType.js +130 -54
  14. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstObjectTypeProperty.js +175 -5
  15. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstCompositeType.d.ts +10 -0
  16. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstCompositeType.js +185 -0
  17. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstIdentifierType.d.ts +10 -0
  18. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstIdentifierType.js +32 -0
  19. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstLiteralType.d.ts +10 -0
  20. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstLiteralType.js +46 -0
  21. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstTermType.d.ts +12 -0
  22. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstTermType.js +24 -0
  23. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstType.d.ts +13 -0
  24. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstType.js +14 -0
  25. package/dist/ast/Ast.d.ts +0 -1
  26. package/dist/ast/Curie.d.ts +14 -0
  27. package/dist/ast/Curie.js +21 -0
  28. package/dist/ast/IdentifierType.d.ts +3 -2
  29. package/dist/ast/ListType.d.ts +2 -1
  30. package/dist/ast/LiteralType.d.ts +1 -1
  31. package/dist/ast/Name.d.ts +40 -5
  32. package/dist/ast/ObjectCompositeType.d.ts +11 -0
  33. package/dist/ast/ObjectType.d.ts +18 -13
  34. package/dist/ast/ObjectUnionType.d.ts +0 -11
  35. package/dist/ast/OptionType.d.ts +2 -4
  36. package/dist/ast/SetType.d.ts +2 -7
  37. package/dist/ast/TermType.d.ts +12 -4
  38. package/dist/ast/Type.d.ts +1 -1
  39. package/dist/ast/index.d.ts +1 -0
  40. package/dist/ast/index.js +1 -0
  41. package/dist/enums/TsFeature.d.ts +1 -4
  42. package/dist/enums/TsFeature.js +1 -15
  43. package/dist/generators/json/AstJsonGenerator.js +26 -4
  44. package/dist/generators/ts/BooleanType.d.ts +4 -2
  45. package/dist/generators/ts/BooleanType.js +12 -3
  46. package/dist/generators/ts/DateTimeType.d.ts +9 -8
  47. package/dist/generators/ts/DateTimeType.js +32 -8
  48. package/dist/generators/ts/DateType.d.ts +5 -2
  49. package/dist/generators/ts/DateType.js +15 -1
  50. package/dist/generators/ts/DeclaredType.d.ts +2 -2
  51. package/dist/generators/ts/DeclaredType.js +2 -2
  52. package/dist/generators/ts/FloatType.d.ts +6 -0
  53. package/dist/generators/ts/FloatType.js +18 -0
  54. package/dist/generators/ts/IdentifierType.d.ts +17 -7
  55. package/dist/generators/ts/IdentifierType.js +112 -18
  56. package/dist/generators/ts/Import.d.ts +2 -0
  57. package/dist/generators/ts/Import.js +10 -0
  58. package/dist/generators/ts/IntType.d.ts +6 -0
  59. package/dist/generators/ts/IntType.js +18 -0
  60. package/dist/generators/ts/ListType.d.ts +15 -12
  61. package/dist/generators/ts/ListType.js +113 -61
  62. package/dist/generators/ts/LiteralType.d.ts +17 -9
  63. package/dist/generators/ts/LiteralType.js +92 -17
  64. package/dist/generators/ts/NumberType.d.ts +3 -2
  65. package/dist/generators/ts/NumberType.js +8 -3
  66. package/dist/generators/ts/ObjectType.d.ts +65 -43
  67. package/dist/generators/ts/ObjectType.js +143 -123
  68. package/dist/generators/ts/ObjectUnionType.d.ts +50 -8
  69. package/dist/generators/ts/ObjectUnionType.js +303 -101
  70. package/dist/generators/ts/OptionType.d.ts +14 -10
  71. package/dist/generators/ts/OptionType.js +62 -34
  72. package/dist/generators/ts/PrimitiveType.d.ts +23 -5
  73. package/dist/generators/ts/PrimitiveType.js +34 -6
  74. package/dist/generators/ts/SetType.d.ts +17 -13
  75. package/dist/generators/ts/SetType.js +76 -44
  76. package/dist/generators/ts/SnippetDeclarations.d.ts +13 -8
  77. package/dist/generators/ts/SnippetDeclarations.js +226 -101
  78. package/dist/generators/ts/StringType.d.ts +4 -2
  79. package/dist/generators/ts/StringType.js +20 -8
  80. package/dist/generators/ts/TermType.d.ts +29 -33
  81. package/dist/generators/ts/TermType.js +89 -68
  82. package/dist/generators/ts/TsGenerator.d.ts +2 -1
  83. package/dist/generators/ts/TsGenerator.js +26 -12
  84. package/dist/generators/ts/Type.d.ts +100 -32
  85. package/dist/generators/ts/Type.js +58 -54
  86. package/dist/generators/ts/TypeFactory.d.ts +12 -5
  87. package/dist/generators/ts/TypeFactory.js +380 -226
  88. package/dist/generators/ts/UnionType.d.ts +14 -10
  89. package/dist/generators/ts/UnionType.js +319 -142
  90. package/dist/generators/ts/_ObjectType/EagerShaclProperty.d.ts +12 -0
  91. package/dist/generators/ts/_ObjectType/EagerShaclProperty.js +31 -0
  92. package/dist/generators/ts/_ObjectType/IdentifierPrefixProperty.d.ts +13 -12
  93. package/dist/generators/ts/_ObjectType/IdentifierPrefixProperty.js +59 -46
  94. package/dist/generators/ts/_ObjectType/IdentifierProperty.d.ts +20 -13
  95. package/dist/generators/ts/_ObjectType/IdentifierProperty.js +198 -140
  96. package/dist/generators/ts/_ObjectType/LazyShaclProperty.d.ts +100 -0
  97. package/dist/generators/ts/_ObjectType/LazyShaclProperty.js +315 -0
  98. package/dist/generators/ts/_ObjectType/Property.d.ts +47 -46
  99. package/dist/generators/ts/_ObjectType/Property.js +1 -10
  100. package/dist/generators/ts/_ObjectType/ShaclProperty.d.ts +25 -30
  101. package/dist/generators/ts/_ObjectType/ShaclProperty.js +120 -85
  102. package/dist/generators/ts/_ObjectType/TypeDiscriminatorProperty.d.ts +21 -17
  103. package/dist/generators/ts/_ObjectType/TypeDiscriminatorProperty.js +61 -53
  104. package/dist/generators/ts/_ObjectType/classDeclaration.js +22 -17
  105. package/dist/generators/ts/_ObjectType/createFunctionDeclaration.js +26 -13
  106. package/dist/generators/ts/_ObjectType/equalsFunctionOrMethodDeclaration.js +8 -5
  107. package/dist/generators/ts/_ObjectType/fromRdfTypeVariableStatement.d.ts +1 -1
  108. package/dist/generators/ts/_ObjectType/fromRdfTypeVariableStatement.js +8 -5
  109. package/dist/generators/ts/_ObjectType/graphqlTypeVariableStatement.d.ts +5 -0
  110. package/dist/generators/ts/_ObjectType/graphqlTypeVariableStatement.js +49 -0
  111. package/dist/generators/ts/_ObjectType/hashFunctionOrMethodDeclarations.js +7 -8
  112. package/dist/generators/ts/_ObjectType/identifierTypeDeclarations.d.ts +7 -0
  113. package/dist/generators/ts/_ObjectType/identifierTypeDeclarations.js +54 -0
  114. package/dist/generators/ts/_ObjectType/index.d.ts +9 -7
  115. package/dist/generators/ts/_ObjectType/index.js +9 -7
  116. package/dist/generators/ts/_ObjectType/interfaceDeclaration.js +1 -1
  117. package/dist/generators/ts/_ObjectType/jsonFunctionDeclarations.d.ts +4 -0
  118. package/dist/generators/ts/_ObjectType/jsonFunctionDeclarations.js +189 -0
  119. package/dist/generators/ts/_ObjectType/jsonTypeAliasDeclaration.d.ts +5 -0
  120. package/dist/generators/ts/_ObjectType/jsonTypeAliasDeclaration.js +28 -0
  121. package/dist/generators/ts/_ObjectType/objectSetMethodNames.d.ts +9 -0
  122. package/dist/generators/ts/_ObjectType/objectSetMethodNames.js +18 -0
  123. package/dist/generators/ts/_ObjectType/propertiesVariableStatement.d.ts +5 -0
  124. package/dist/generators/ts/_ObjectType/propertiesVariableStatement.js +37 -0
  125. package/dist/generators/ts/_ObjectType/rdfFunctionDeclarations.d.ts +4 -0
  126. package/dist/generators/ts/_ObjectType/rdfFunctionDeclarations.js +144 -0
  127. package/dist/generators/ts/_ObjectType/sparqlConstructQueryFunctionDeclaration.d.ts +1 -1
  128. package/dist/generators/ts/_ObjectType/sparqlConstructQueryFunctionDeclaration.js +6 -5
  129. package/dist/generators/ts/_ObjectType/sparqlConstructQueryStringFunctionDeclaration.d.ts +1 -1
  130. package/dist/generators/ts/_ObjectType/sparqlConstructQueryStringFunctionDeclaration.js +4 -3
  131. package/dist/generators/ts/_ObjectType/sparqlFunctionDeclarations.js +109 -37
  132. package/dist/generators/ts/_ObjectType/toJsonFunctionOrMethodDeclaration.js +6 -6
  133. package/dist/generators/ts/_ObjectType/toRdfFunctionOrMethodDeclaration.js +33 -24
  134. package/dist/generators/ts/graphqlSchemaVariableStatement.d.ts +9 -0
  135. package/dist/generators/ts/graphqlSchemaVariableStatement.js +86 -0
  136. package/dist/generators/ts/objectSetDeclarations.d.ts +8 -0
  137. package/dist/generators/ts/objectSetDeclarations.js +59 -0
  138. package/dist/generators/ts/objectSetInterfaceDeclaration.d.ts +8 -0
  139. package/dist/generators/ts/objectSetInterfaceDeclaration.js +46 -0
  140. package/dist/generators/ts/objectSetMethodSignatures.d.ts +11 -0
  141. package/dist/generators/ts/objectSetMethodSignatures.js +52 -0
  142. package/dist/generators/ts/rdfjsDatasetObjectSetClassDeclaration.d.ts +8 -0
  143. package/dist/generators/ts/rdfjsDatasetObjectSetClassDeclaration.js +393 -0
  144. package/dist/generators/ts/rdfjsTermExpression.d.ts +3 -0
  145. package/dist/generators/ts/rdfjsTermExpression.js +57 -0
  146. package/dist/generators/ts/sparqlObjectSetClassDeclaration.d.ts +8 -0
  147. package/dist/generators/ts/sparqlObjectSetClassDeclaration.js +415 -0
  148. package/dist/generators/ts/syntheticNamePrefix.d.ts +2 -0
  149. package/dist/generators/ts/syntheticNamePrefix.js +2 -0
  150. package/dist/generators/ts/tsName.js +27 -13
  151. package/dist/generators/ts/unsupportedObjectSetMethodDeclarations.d.ts +10 -0
  152. package/dist/generators/ts/unsupportedObjectSetMethodDeclarations.js +19 -0
  153. package/dist/input/NodeShape.d.ts +6 -8
  154. package/dist/input/NodeShape.js +20 -44
  155. package/dist/input/Ontology.d.ts +0 -3
  156. package/dist/input/Ontology.js +0 -9
  157. package/dist/input/PropertyPath.d.ts +5 -5
  158. package/dist/input/PropertyPath.js +14 -24
  159. package/dist/input/PropertyShape.d.ts +3 -1
  160. package/dist/input/PropertyShape.js +8 -2
  161. package/dist/input/ShapesGraph.js +4 -8
  162. package/dist/input/generated.d.ts +967 -107
  163. package/dist/input/generated.js +2548 -1046
  164. package/dist/input/tsFeatures.d.ts +3 -2
  165. package/dist/input/tsFeatures.js +44 -27
  166. package/package.json +18 -16
  167. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstCompositeType.d.ts +0 -13
  168. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstCompositeType.js +0 -200
  169. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstIdentifierType.d.ts +0 -12
  170. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstIdentifierType.js +0 -29
  171. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstLiteralType.d.ts +0 -12
  172. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstLiteralType.js +0 -43
  173. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstTermType.d.ts +0 -14
  174. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstTermType.js +0 -21
  175. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstType.d.ts +0 -16
  176. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstType.js +0 -60
  177. package/dist/generators/ts/_ObjectType/fromJsonFunctionDeclarations.d.ts +0 -4
  178. package/dist/generators/ts/_ObjectType/fromJsonFunctionDeclarations.js +0 -78
  179. package/dist/generators/ts/_ObjectType/fromRdfFunctionDeclarations.d.ts +0 -4
  180. package/dist/generators/ts/_ObjectType/fromRdfFunctionDeclarations.js +0 -91
  181. package/dist/generators/ts/_ObjectType/jsonSchemaFunctionDeclaration.d.ts +0 -5
  182. package/dist/generators/ts/_ObjectType/jsonSchemaFunctionDeclaration.js +0 -19
  183. package/dist/generators/ts/_ObjectType/jsonUiSchemaFunctionDeclaration.d.ts +0 -5
  184. package/dist/generators/ts/_ObjectType/jsonUiSchemaFunctionDeclaration.js +0 -31
  185. package/dist/generators/ts/_ObjectType/jsonZodSchemaFunctionDeclaration.d.ts +0 -5
  186. package/dist/generators/ts/_ObjectType/jsonZodSchemaFunctionDeclaration.js +0 -37
  187. package/dist/generators/ts/_ObjectType/rdfjsTermExpression.d.ts +0 -6
  188. package/dist/generators/ts/_ObjectType/rdfjsTermExpression.js +0 -17
  189. package/dist/generators/ts/_ObjectType/toJsonFunctionDeclaration.d.ts +0 -5
  190. package/dist/generators/ts/_ObjectType/toJsonFunctionDeclaration.js +0 -19
  191. package/dist/generators/ts/_ObjectType/toJsonReturnType.d.ts +0 -3
  192. package/dist/generators/ts/_ObjectType/toJsonReturnType.js +0 -17
  193. package/dist/generators/ts/_ObjectType/toRdfFunctionDeclaration.d.ts +0 -5
  194. package/dist/generators/ts/_ObjectType/toRdfFunctionDeclaration.js +0 -19
@@ -1,5 +1,109 @@
1
- import { Either, Left } from "purify-ts";
1
+ import N3 from "n3";
2
+ import { Either, Left, Maybe } from "purify-ts";
3
+ import { invariant } from "ts-invariant";
4
+ import { ShapeStack } from "./ShapeStack.js";
2
5
  import { pickLiteral } from "./pickLiteral.js";
6
+ function identifierNodeKinds(type) {
7
+ switch (type.kind) {
8
+ case "ObjectType":
9
+ return type.identifierNodeKinds;
10
+ case "ObjectUnionType":
11
+ return new Set(type.memberTypes.flatMap((memberType) => [
12
+ ...memberType.identifierNodeKinds,
13
+ ]));
14
+ }
15
+ }
16
+ function synthesizeStubAstObjectType({ identifierNodeKinds, tsFeatures, }) {
17
+ let syntheticName;
18
+ switch (identifierNodeKinds.size) {
19
+ case 1:
20
+ invariant(identifierNodeKinds.has("NamedNode"));
21
+ syntheticName = "NamedDefaultStub";
22
+ break;
23
+ case 2:
24
+ syntheticName = "DefaultStub";
25
+ break;
26
+ default:
27
+ throw new Error("should never happen");
28
+ }
29
+ return {
30
+ abstract: false,
31
+ ancestorObjectTypes: [],
32
+ childObjectTypes: [],
33
+ comment: Maybe.empty(),
34
+ descendantObjectTypes: [],
35
+ export: true,
36
+ extern: false,
37
+ fromRdfType: Maybe.empty(),
38
+ identifierIn: [],
39
+ identifierNodeKinds,
40
+ identifierMintingStrategy: Maybe.empty(),
41
+ kind: "ObjectType",
42
+ label: Maybe.empty(),
43
+ name: {
44
+ identifier: N3.DataFactory.blankNode(),
45
+ label: Maybe.empty(),
46
+ propertyPath: Maybe.empty(),
47
+ shName: Maybe.empty(),
48
+ shaclmateName: Maybe.empty(),
49
+ syntheticName: Maybe.of(syntheticName),
50
+ },
51
+ parentObjectTypes: [],
52
+ properties: [],
53
+ synthetic: true,
54
+ toRdfTypes: [],
55
+ tsFeatures,
56
+ tsImports: [],
57
+ tsObjectDeclarationType: "class",
58
+ };
59
+ }
60
+ function transformPropertyShapeToAstType(propertyShape) {
61
+ const itemTypeEither = this.transformShapeToAstType(propertyShape, new ShapeStack());
62
+ if (itemTypeEither.isLeft()) {
63
+ return itemTypeEither;
64
+ }
65
+ const itemType = itemTypeEither.unsafeCoerce();
66
+ if (propertyShape.defaultValue.isJust()) {
67
+ return Either.of(itemType);
68
+ }
69
+ if (propertyShape.constraints.maxCount.isNothing() &&
70
+ propertyShape.constraints.minCount.isNothing()) {
71
+ return Either.of({
72
+ itemType,
73
+ kind: "SetType",
74
+ mutable: propertyShape.mutable,
75
+ minCount: 0,
76
+ });
77
+ }
78
+ let maxCount = propertyShape.constraints.maxCount.orDefault(Number.MAX_SAFE_INTEGER);
79
+ let minCount = propertyShape.constraints.minCount.orDefault(0);
80
+ if (minCount < 0) {
81
+ minCount = 0;
82
+ }
83
+ if (propertyShape.constraints.hasValues.length > minCount) {
84
+ minCount = propertyShape.constraints.hasValues.length;
85
+ }
86
+ if (maxCount < minCount) {
87
+ maxCount = minCount;
88
+ }
89
+ if (minCount === 0 && maxCount === 1) {
90
+ return Either.of({
91
+ itemType,
92
+ kind: "OptionType",
93
+ });
94
+ }
95
+ if (minCount === 1 && maxCount === 1) {
96
+ return Either.of(itemType);
97
+ }
98
+ invariant(propertyShape.constraints.minCount.isJust() ||
99
+ propertyShape.constraints.maxCount.isJust());
100
+ return Either.of({
101
+ itemType,
102
+ kind: "SetType",
103
+ minCount,
104
+ mutable: propertyShape.mutable,
105
+ });
106
+ }
3
107
  export function transformPropertyShapeToAstObjectTypeProperty(propertyShape) {
4
108
  {
5
109
  const property = this.astObjectTypePropertiesByIdentifier.get(propertyShape.identifier);
@@ -7,9 +111,74 @@ export function transformPropertyShapeToAstObjectTypeProperty(propertyShape) {
7
111
  return Either.of(property);
8
112
  }
9
113
  }
10
- const type = this.transformPropertyShapeToAstType(propertyShape, null);
11
- if (type.isLeft()) {
12
- return type;
114
+ const typeEither = transformPropertyShapeToAstType.bind(this)(propertyShape);
115
+ if (typeEither.isLeft()) {
116
+ return typeEither;
117
+ }
118
+ const type = typeEither.unsafeCoerce();
119
+ let stubType = Maybe.empty();
120
+ let propertyShapeStubItemType;
121
+ if (propertyShape.stub.isJust()) {
122
+ const propertyShapeStubTypeEither = this.transformNodeShapeToAstType(propertyShape.stub.unsafeCoerce()).chain((propertyShapeStubType) => {
123
+ switch (propertyShapeStubType.kind) {
124
+ case "ListType":
125
+ case "ObjectIntersectionType":
126
+ return Left(new Error(`${propertyShape} stub cannot refer to a ${propertyShapeStubType.kind}`));
127
+ case "ObjectType":
128
+ case "ObjectUnionType":
129
+ return Either.of(propertyShapeStubType);
130
+ }
131
+ });
132
+ if (propertyShapeStubTypeEither.isLeft()) {
133
+ return propertyShapeStubTypeEither;
134
+ }
135
+ propertyShapeStubItemType = propertyShapeStubTypeEither.unsafeCoerce();
136
+ }
137
+ if (propertyShapeStubItemType || propertyShape.lazy.orDefault(false)) {
138
+ switch (type.kind) {
139
+ case "ObjectType":
140
+ case "ObjectUnionType":
141
+ stubType = Maybe.of(propertyShapeStubItemType ??
142
+ synthesizeStubAstObjectType({
143
+ identifierNodeKinds: identifierNodeKinds(type),
144
+ tsFeatures: type.tsFeatures,
145
+ }));
146
+ break;
147
+ case "OptionType":
148
+ case "SetType": {
149
+ switch (type.itemType.kind) {
150
+ case "ObjectType":
151
+ case "ObjectUnionType":
152
+ break;
153
+ default:
154
+ return Left(new Error(`${propertyShape} marked lazy but has ${type.kind} of ${type.itemType.kind}`));
155
+ }
156
+ const stubItemType = propertyShapeStubItemType ??
157
+ synthesizeStubAstObjectType({
158
+ identifierNodeKinds: identifierNodeKinds(type.itemType),
159
+ tsFeatures: type.itemType.tsFeatures,
160
+ });
161
+ switch (type.kind) {
162
+ case "OptionType":
163
+ stubType = Maybe.of({
164
+ kind: "OptionType",
165
+ itemType: stubItemType,
166
+ });
167
+ break;
168
+ case "SetType":
169
+ stubType = Maybe.of({
170
+ kind: "SetType",
171
+ itemType: stubItemType,
172
+ minCount: 0,
173
+ mutable: Maybe.empty(),
174
+ });
175
+ break;
176
+ }
177
+ break;
178
+ }
179
+ default:
180
+ return Left(new Error(`${propertyShape} marked lazy but has ${type.kind}`));
181
+ }
13
182
  }
14
183
  const path = propertyShape.path;
15
184
  if (path.kind !== "PredicatePath") {
@@ -23,7 +192,8 @@ export function transformPropertyShapeToAstObjectTypeProperty(propertyShape) {
23
192
  name: this.shapeAstName(propertyShape),
24
193
  order: propertyShape.order.orDefault(0),
25
194
  path,
26
- type: type.extract(),
195
+ stubType,
196
+ type: type,
27
197
  visibility: propertyShape.visibility,
28
198
  };
29
199
  this.astObjectTypePropertiesByIdentifier.set(propertyShape.identifier, property);
@@ -0,0 +1,10 @@
1
+ import { Either } from "purify-ts";
2
+ import type { ShapesGraphToAstTransformer } from "../ShapesGraphToAstTransformer.js";
3
+ import type * as ast from "../ast/index.js";
4
+ import * as input from "../input/index.js";
5
+ import type { ShapeStack } from "./ShapeStack.js";
6
+ /**
7
+ * Try to convert a shape to a composite type (intersection or union) using some heuristics.
8
+ */
9
+ export declare function transformShapeToAstCompositeType(this: ShapesGraphToAstTransformer, shape: input.Shape, shapeStack: ShapeStack): Either<Error, ast.Type>;
10
+ //# sourceMappingURL=transformShapeToAstCompositeType.d.ts.map
@@ -0,0 +1,185 @@
1
+ import { owl, rdfs } from "@tpluscode/rdf-ns-builders";
2
+ import { Either, Left, Maybe } from "purify-ts";
3
+ import { invariant } from "ts-invariant";
4
+ import * as input from "../input/index.js";
5
+ import { logger } from "../logger.js";
6
+ import { flattenAstObjectCompositeTypeMemberTypes } from "./flattenAstObjectCompositeTypeMemberTypes.js";
7
+ /**
8
+ * Try to convert a shape to a composite type (intersection or union) using some heuristics.
9
+ */
10
+ export function transformShapeToAstCompositeType(shape, shapeStack) {
11
+ shapeStack.push(shape);
12
+ try {
13
+ let memberTypeEithers;
14
+ let compositeTypeKind;
15
+ if (shape.constraints.and.length > 0) {
16
+ memberTypeEithers = shape.constraints.and.map((memberShape) => this.transformShapeToAstType(memberShape, shapeStack));
17
+ compositeTypeKind = "IntersectionType";
18
+ }
19
+ else if (shape.constraints.classes.length > 0) {
20
+ memberTypeEithers = shape.constraints.classes.map((classIri) => {
21
+ if (classIri.equals(owl.Class) ||
22
+ classIri.equals(owl.Thing) ||
23
+ classIri.equals(rdfs.Class)) {
24
+ return Left(new Error(`class ${classIri.value} is not transformable`));
25
+ }
26
+ const classNodeShape = this.shapesGraph
27
+ .nodeShapeByIdentifier(classIri)
28
+ .extractNullable();
29
+ if (classNodeShape === null) {
30
+ return Left(new Error(`class ${classIri.value} did not resolve to a node shape`));
31
+ }
32
+ return this.transformNodeShapeToAstType(classNodeShape);
33
+ });
34
+ compositeTypeKind = "IntersectionType";
35
+ if (Either.rights(memberTypeEithers).length === 0) {
36
+ // This frequently happens with e.g., sh:class skos:Concept
37
+ logger.debug("shape %s sh:class(es) did not map to any node shapes", shape);
38
+ return memberTypeEithers[0];
39
+ }
40
+ }
41
+ else if (shape.constraints.nodes.length > 0) {
42
+ memberTypeEithers = shape.constraints.nodes.map((nodeShape) => this.transformNodeShapeToAstType(nodeShape));
43
+ compositeTypeKind = "IntersectionType";
44
+ }
45
+ else if (shape.constraints.xone.length > 0) {
46
+ memberTypeEithers = shape.constraints.xone.map((memberShape) => this.transformShapeToAstType(memberShape, shapeStack));
47
+ compositeTypeKind = "UnionType";
48
+ }
49
+ else {
50
+ return Left(new Error(`unable to transform ${shape} into an AST type`));
51
+ }
52
+ invariant(memberTypeEithers.length > 0);
53
+ const memberObjectTypes = [];
54
+ let memberTypes = [];
55
+ for (const memberTypeEither of memberTypeEithers) {
56
+ if (memberTypeEither.isLeft()) {
57
+ return memberTypeEither;
58
+ }
59
+ const memberType = memberTypeEither.unsafeCoerce();
60
+ memberTypes.push(memberType);
61
+ switch (memberType.kind) {
62
+ case "ObjectType":
63
+ case "ObjectIntersectionType":
64
+ case "ObjectUnionType":
65
+ memberObjectTypes.push(memberType);
66
+ break;
67
+ }
68
+ }
69
+ if (memberTypes.length === 1) {
70
+ return Either.of(memberTypes[0]);
71
+ }
72
+ if (memberTypes.length === memberObjectTypes.length) {
73
+ // If all the member types are ast.ObjectType, flatten them.
74
+ const flattenedMemberObjectTypesEither = flattenAstObjectCompositeTypeMemberTypes({
75
+ objectCompositeTypeKind: compositeTypeKind === "IntersectionType"
76
+ ? "ObjectIntersectionType"
77
+ : "ObjectUnionType",
78
+ memberTypes: memberObjectTypes,
79
+ shape,
80
+ });
81
+ if (flattenedMemberObjectTypesEither.isLeft()) {
82
+ return flattenedMemberObjectTypesEither;
83
+ }
84
+ const { memberTypes: flattenedMemberTypes } = flattenedMemberObjectTypesEither.unsafeCoerce();
85
+ memberTypes = flattenedMemberTypes.concat();
86
+ }
87
+ return widenAstCompositeTypeToSingleType({
88
+ memberTypes,
89
+ shape,
90
+ shapeStack,
91
+ }).altLazy(() =>
92
+ // True composite type
93
+ Either.of({
94
+ kind: compositeTypeKind,
95
+ memberTypes,
96
+ }));
97
+ }
98
+ finally {
99
+ shapeStack.pop(shape);
100
+ }
101
+ }
102
+ function widenAstCompositeTypeToSingleType({ memberTypes, shape, shapeStack, }) {
103
+ const defaultValue = shapeStack.defaultValue;
104
+ const hasValues = shapeStack.constraints.hasValues;
105
+ if (hasValues.length > 0) {
106
+ return Left(new Error(`shape ${shape} hasValues, not attempting to widen composite type into a single type`));
107
+ }
108
+ if (shape instanceof input.PropertyShape && !shape.widen.orDefault(true)) {
109
+ return Left(new Error(`shape ${shape} has widening disabled`));
110
+ }
111
+ const canWiden = (memberType) => {
112
+ if (memberType.in_.length > 0) {
113
+ return false;
114
+ }
115
+ switch (memberType.kind) {
116
+ case "LiteralType": {
117
+ if (memberType.maxExclusive.isJust()) {
118
+ return false;
119
+ }
120
+ if (memberType.maxInclusive.isJust()) {
121
+ return false;
122
+ }
123
+ if (memberType.minExclusive.isJust()) {
124
+ return false;
125
+ }
126
+ if (memberType.minInclusive.isJust()) {
127
+ return false;
128
+ }
129
+ }
130
+ }
131
+ return true;
132
+ };
133
+ if (memberTypes.every((memberType) => memberType.kind === "IdentifierType" && canWiden(memberType))) {
134
+ // Special case: all member types are identifiers without further constraints
135
+ return Either.of({
136
+ defaultValue: defaultValue.filter((term) => term.termType === "NamedNode"),
137
+ hasValues: [],
138
+ in_: [],
139
+ kind: "IdentifierType",
140
+ nodeKinds: new Set(memberTypes
141
+ .filter((memberType) => memberType.kind === "IdentifierType")
142
+ .flatMap((memberType) => [
143
+ ...memberType.nodeKinds,
144
+ ])),
145
+ });
146
+ }
147
+ if (memberTypes.every((memberType) => memberType.kind === "LiteralType" && canWiden(memberType))) {
148
+ // Special case: all the member types are Literals without further constraints,
149
+ // like dash:StringOrLangString
150
+ // Don't try to widen range constraints.
151
+ return Either.of({
152
+ datatype: Maybe.empty(),
153
+ defaultValue: defaultValue.filter((term) => term.termType === "Literal"),
154
+ hasValues: [],
155
+ in_: [],
156
+ kind: "LiteralType",
157
+ languageIn: [],
158
+ maxExclusive: Maybe.empty(),
159
+ maxInclusive: Maybe.empty(),
160
+ minExclusive: Maybe.empty(),
161
+ minInclusive: Maybe.empty(),
162
+ nodeKinds: new Set(["Literal"]),
163
+ });
164
+ }
165
+ if (memberTypes.every((memberType) => (memberType.kind === "IdentifierType" ||
166
+ memberType.kind === "LiteralType" ||
167
+ memberType.kind === "TermType") &&
168
+ canWiden(memberType))) {
169
+ // Special case: all member types are terms without further constraints
170
+ const nodeKinds = new Set(memberTypes.flatMap((memberType) => [
171
+ ...memberType.nodeKinds,
172
+ ]));
173
+ invariant(nodeKinds.has("Literal") &&
174
+ (nodeKinds.has("BlankNode") || nodeKinds.has("NamedNode"))); // The identifier-identifier and literal-literal cases should have been caught above
175
+ return Either.of({
176
+ defaultValue,
177
+ hasValues: [],
178
+ in_: [],
179
+ kind: "TermType",
180
+ nodeKinds,
181
+ });
182
+ }
183
+ return Left(new Error(`shape ${shape} member types could not be widened into a single type`));
184
+ }
185
+ //# sourceMappingURL=transformShapeToAstCompositeType.js.map
@@ -0,0 +1,10 @@
1
+ import { Either } from "purify-ts";
2
+ import type { ShapesGraphToAstTransformer } from "../ShapesGraphToAstTransformer.js";
3
+ import type * as ast from "../ast/index.js";
4
+ import type * as input from "../input/index.js";
5
+ import type { ShapeStack } from "./ShapeStack.js";
6
+ /**
7
+ * Try to convert a property shape to an AST IdentifierType using some heuristics.
8
+ */
9
+ export declare function transformShapeToAstIdentifierType(this: ShapesGraphToAstTransformer, shape: input.Shape, shapeStack: ShapeStack): Either<Error, ast.IdentifierType>;
10
+ //# sourceMappingURL=transformShapeToAstIdentifierType.d.ts.map
@@ -0,0 +1,32 @@
1
+ import { Either, Left } from "purify-ts";
2
+ import { propertyShapeNodeKinds } from "./propertyShapeNodeKinds.js";
3
+ /**
4
+ * Try to convert a property shape to an AST IdentifierType using some heuristics.
5
+ */
6
+ export function transformShapeToAstIdentifierType(shape, shapeStack) {
7
+ shapeStack.push(shape);
8
+ try {
9
+ // defaultValue / hasValue / in only makes sense with IRIs
10
+ const identifierDefaultValue = shapeStack.defaultValue.filter((value) => value.termType === "NamedNode");
11
+ const identifierHasValues = shapeStack.constraints.hasValues.filter((term) => term.termType === "NamedNode");
12
+ const identifierIn = shapeStack.constraints.in_.filter((term) => term.termType === "NamedNode");
13
+ const nodeKinds = propertyShapeNodeKinds(shape);
14
+ if (identifierHasValues.length > 0 ||
15
+ identifierDefaultValue.isJust() ||
16
+ identifierIn.length > 0 ||
17
+ (nodeKinds.size > 0 && nodeKinds.size <= 2 && !nodeKinds.has("Literal"))) {
18
+ return Either.of({
19
+ defaultValue: identifierDefaultValue,
20
+ hasValues: identifierHasValues,
21
+ in_: identifierIn,
22
+ kind: "IdentifierType",
23
+ nodeKinds: nodeKinds,
24
+ });
25
+ }
26
+ return Left(new Error(`unable to transform ${shape} into an AST type`));
27
+ }
28
+ finally {
29
+ shapeStack.pop(shape);
30
+ }
31
+ }
32
+ //# sourceMappingURL=transformShapeToAstIdentifierType.js.map
@@ -0,0 +1,10 @@
1
+ import { Either } from "purify-ts";
2
+ import type { ShapesGraphToAstTransformer } from "../ShapesGraphToAstTransformer.js";
3
+ import type * as ast from "../ast/index.js";
4
+ import type * as input from "../input/index.js";
5
+ import type { ShapeStack } from "./ShapeStack.js";
6
+ /**
7
+ * Try to convert a property shape to an AST LiteralType using some heuristics.
8
+ */
9
+ export declare function transformShapeToAstLiteralType(this: ShapesGraphToAstTransformer, shape: input.Shape, shapeStack: ShapeStack): Either<Error, ast.LiteralType>;
10
+ //# sourceMappingURL=transformShapeToAstLiteralType.d.ts.map
@@ -0,0 +1,46 @@
1
+ import { Either, Left } from "purify-ts";
2
+ import { propertyShapeNodeKinds } from "./propertyShapeNodeKinds.js";
3
+ /**
4
+ * Try to convert a property shape to an AST LiteralType using some heuristics.
5
+ */
6
+ export function transformShapeToAstLiteralType(shape, shapeStack) {
7
+ shapeStack.push(shape);
8
+ try {
9
+ const literalDefaultValue = shapeStack.defaultValue.filter((term) => term.termType === "Literal");
10
+ const literalHasValues = shapeStack.constraints.hasValues.filter((term) => term.termType === "Literal");
11
+ const literalIn = shapeStack.constraints.in_.filter((term) => term.termType === "Literal");
12
+ const nodeKinds = propertyShapeNodeKinds(shape);
13
+ if ([
14
+ // Treat any shape with the constraints in the list as a literal type
15
+ shape.constraints.datatype,
16
+ shape.constraints.maxExclusive,
17
+ shape.constraints.maxInclusive,
18
+ shape.constraints.minExclusive,
19
+ shape.constraints.minInclusive,
20
+ ].some((constraint) => constraint.isJust()) ||
21
+ shape.constraints.languageIn.length > 0 ||
22
+ literalDefaultValue.isJust() ||
23
+ literalHasValues.length > 0 ||
24
+ literalIn.length > 0 ||
25
+ // Treat any shape with a single sh:nodeKind of sh:Literal as a literal type
26
+ (nodeKinds.size === 1 && nodeKinds.has("Literal")))
27
+ return Either.of({
28
+ datatype: shape.constraints.datatype,
29
+ defaultValue: literalDefaultValue,
30
+ hasValues: literalHasValues,
31
+ in_: literalIn,
32
+ kind: "LiteralType",
33
+ languageIn: shape.constraints.languageIn,
34
+ maxExclusive: shape.constraints.maxExclusive,
35
+ maxInclusive: shape.constraints.maxInclusive,
36
+ minExclusive: shape.constraints.minExclusive,
37
+ minInclusive: shape.constraints.minInclusive,
38
+ nodeKinds: new Set(["Literal"]),
39
+ });
40
+ return Left(new Error(`unable to transform ${shape} into an AST type`));
41
+ }
42
+ finally {
43
+ shapeStack.pop(shape);
44
+ }
45
+ }
46
+ //# sourceMappingURL=transformShapeToAstLiteralType.js.map
@@ -0,0 +1,12 @@
1
+ import { Either } from "purify-ts";
2
+ import type { ShapesGraphToAstTransformer } from "../ShapesGraphToAstTransformer.js";
3
+ import type * as ast from "../ast/index.js";
4
+ import type * as input from "../input/index.js";
5
+ import type { ShapeStack } from "./ShapeStack.js";
6
+ /**
7
+ * Try to convert a shape to an AST TermType using some heuristics.
8
+ */
9
+ export declare function transformShapeToAstTermType(this: ShapesGraphToAstTransformer, shape: input.Shape, shapeStack: ShapeStack): Either<Error, Omit<ast.TermType, "kind"> & {
10
+ readonly kind: "TermType";
11
+ }>;
12
+ //# sourceMappingURL=transformShapeToAstTermType.d.ts.map
@@ -0,0 +1,24 @@
1
+ import { Either } from "purify-ts";
2
+ import { propertyShapeNodeKinds } from "./propertyShapeNodeKinds.js";
3
+ /**
4
+ * Try to convert a shape to an AST TermType using some heuristics.
5
+ */
6
+ export function transformShapeToAstTermType(shape, shapeStack) {
7
+ shapeStack.push(shape);
8
+ try {
9
+ const nodeKinds = propertyShapeNodeKinds(shape);
10
+ return Either.of({
11
+ defaultValue: shapeStack.defaultValue,
12
+ hasValues: shapeStack.constraints.hasValues,
13
+ in_: shapeStack.constraints.in_,
14
+ kind: "TermType",
15
+ nodeKinds: nodeKinds.size > 0
16
+ ? nodeKinds
17
+ : new Set(["BlankNode", "NamedNode", "Literal"]),
18
+ });
19
+ }
20
+ finally {
21
+ shapeStack.pop(shape);
22
+ }
23
+ }
24
+ //# sourceMappingURL=transformShapeToAstTermType.js.map
@@ -0,0 +1,13 @@
1
+ import type { Either } from "purify-ts";
2
+ import type { ShapesGraphToAstTransformer } from "../ShapesGraphToAstTransformer.js";
3
+ import type * as ast from "../ast/index.js";
4
+ import type * as input from "../input/index.js";
5
+ import type { ShapeStack } from "./ShapeStack.js";
6
+ /**
7
+ * Try to convert a shape to a type using some heuristics.
8
+ *
9
+ * We don't try to handle exotic cases allowed by the SHACL spec, such as combinations of sh:in and sh:node. Instead we assume
10
+ * a shape has one type.
11
+ */
12
+ export declare function transformShapeToAstType(this: ShapesGraphToAstTransformer, shape: input.Shape, shapeStack: ShapeStack): Either<Error, ast.Type>;
13
+ //# sourceMappingURL=transformShapeToAstType.d.ts.map
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Try to convert a shape to a type using some heuristics.
3
+ *
4
+ * We don't try to handle exotic cases allowed by the SHACL spec, such as combinations of sh:in and sh:node. Instead we assume
5
+ * a shape has one type.
6
+ */
7
+ export function transformShapeToAstType(shape, shapeStack) {
8
+ // Try to transform the property shape into an AST type without cardinality constraints
9
+ return this.transformShapeToAstCompositeType(shape, shapeStack)
10
+ .altLazy(() => this.transformShapeToAstIdentifierType(shape, shapeStack))
11
+ .altLazy(() => this.transformShapeToAstLiteralType(shape, shapeStack))
12
+ .altLazy(() => this.transformShapeToAstTermType(shape, shapeStack));
13
+ }
14
+ //# sourceMappingURL=transformShapeToAstType.js.map
package/dist/ast/Ast.d.ts CHANGED
@@ -5,6 +5,5 @@ export interface Ast {
5
5
  readonly objectIntersectionTypes: readonly ObjectIntersectionType[];
6
6
  readonly objectTypes: readonly ObjectType[];
7
7
  readonly objectUnionTypes: readonly ObjectUnionType[];
8
- readonly tsDataFactoryVariable: string;
9
8
  }
10
9
  //# sourceMappingURL=Ast.d.ts.map
@@ -0,0 +1,14 @@
1
+ /**
2
+ * A Compact URI (https://www.w3.org/TR/curie)
3
+ */
4
+ export declare class Curie {
5
+ readonly prefix: string;
6
+ readonly reference: string;
7
+ constructor({ prefix, reference, }: {
8
+ prefix: string;
9
+ reference: string;
10
+ });
11
+ static parse(curie: string): Curie;
12
+ toString(): string;
13
+ }
14
+ //# sourceMappingURL=Curie.d.ts.map
@@ -0,0 +1,21 @@
1
+ import { invariant } from "ts-invariant";
2
+ /**
3
+ * A Compact URI (https://www.w3.org/TR/curie)
4
+ */
5
+ export class Curie {
6
+ prefix;
7
+ reference;
8
+ constructor({ prefix, reference, }) {
9
+ this.prefix = prefix;
10
+ this.reference = reference;
11
+ }
12
+ static parse(curie) {
13
+ const split = curie.split(":", 2);
14
+ invariant(split.length === 2);
15
+ return new Curie({ prefix: split[0], reference: split[1] });
16
+ }
17
+ toString() {
18
+ return `${this.prefix}:${this.reference}`;
19
+ }
20
+ }
21
+ //# sourceMappingURL=Curie.js.map
@@ -1,10 +1,11 @@
1
1
  import type { BlankNode, NamedNode } from "@rdfjs/types";
2
+ import type { IdentifierNodeKind } from "@shaclmate/shacl-ast";
2
3
  import type { TermType } from "./TermType.js";
3
4
  /**
4
5
  * A type corresponding to sh:nodeKind of a blank node or IRI, and not corresponding to a node shape.
5
6
  */
6
- export interface IdentifierType extends TermType<BlankNode | NamedNode> {
7
+ export interface IdentifierType extends TermType<NamedNode, BlankNode | NamedNode> {
7
8
  readonly kind: "IdentifierType";
8
- readonly nodeKinds: Set<"BlankNode" | "NamedNode">;
9
+ readonly nodeKinds: Set<IdentifierNodeKind>;
9
10
  }
10
11
  //# sourceMappingURL=IdentifierType.d.ts.map
@@ -1,4 +1,5 @@
1
1
  import type { NamedNode } from "@rdfjs/types";
2
+ import type { IdentifierNodeKind } from "@shaclmate/shacl-ast";
2
3
  import type { Maybe } from "purify-ts";
3
4
  import type { IdentifierMintingStrategy } from "../enums/IdentifierMintingStrategy.js";
4
5
  import type { Name } from "./Name.js";
@@ -18,7 +19,7 @@ export interface ListType {
18
19
  /**
19
20
  * Type of identifier (blank or named node) to use for lists and sub-lists.
20
21
  */
21
- readonly identifierNodeKind: "BlankNode" | "NamedNode";
22
+ readonly identifierNodeKind: IdentifierNodeKind;
22
23
  /**
23
24
  * List item type.
24
25
  *
@@ -1,7 +1,7 @@
1
1
  import type { Literal, NamedNode } from "@rdfjs/types";
2
2
  import type { Maybe } from "purify-ts";
3
3
  import type { TermType } from "./TermType.js";
4
- export interface LiteralType extends TermType<Literal> {
4
+ export interface LiteralType extends TermType<Literal, Literal> {
5
5
  readonly datatype: Maybe<NamedNode>;
6
6
  readonly kind: "LiteralType";
7
7
  readonly languageIn: readonly string[];