@shaclmate/compiler 2.0.23 → 3.0.0

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 (103) hide show
  1. package/dist/ShapesGraphToAstTransformer.d.ts +5 -5
  2. package/dist/ShapesGraphToAstTransformer.js +5 -5
  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/index.d.ts +5 -5
  8. package/dist/_ShapesGraphToAstTransformer/index.js +5 -5
  9. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstObjectTypeProperty.js +77 -29
  10. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstCompositeType.d.ts +10 -0
  11. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstCompositeType.js +185 -0
  12. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstIdentifierType.d.ts +10 -0
  13. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstIdentifierType.js +32 -0
  14. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstLiteralType.d.ts +10 -0
  15. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstLiteralType.js +46 -0
  16. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstTermType.d.ts +12 -0
  17. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstTermType.js +24 -0
  18. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstType.d.ts +13 -0
  19. package/dist/_ShapesGraphToAstTransformer/transformShapeToAstType.js +14 -0
  20. package/dist/ast/OptionType.d.ts +0 -2
  21. package/dist/ast/SetType.d.ts +1 -6
  22. package/dist/ast/index.d.ts +1 -0
  23. package/dist/ast/index.js +1 -0
  24. package/dist/generators/json/AstJsonGenerator.js +7 -1
  25. package/dist/generators/ts/BooleanType.d.ts +2 -1
  26. package/dist/generators/ts/BooleanType.js +9 -4
  27. package/dist/generators/ts/DateTimeType.d.ts +2 -1
  28. package/dist/generators/ts/DateTimeType.js +9 -4
  29. package/dist/generators/ts/IdentifierType.d.ts +1 -1
  30. package/dist/generators/ts/IdentifierType.js +20 -14
  31. package/dist/generators/ts/Import.d.ts +2 -1
  32. package/dist/generators/ts/Import.js +29 -24
  33. package/dist/generators/ts/ListType.js +16 -5
  34. package/dist/generators/ts/LiteralType.d.ts +5 -2
  35. package/dist/generators/ts/LiteralType.js +81 -15
  36. package/dist/generators/ts/NumberType.d.ts +2 -1
  37. package/dist/generators/ts/NumberType.js +9 -4
  38. package/dist/generators/ts/ObjectType.d.ts +3 -2
  39. package/dist/generators/ts/ObjectType.js +16 -6
  40. package/dist/generators/ts/ObjectUnionType.d.ts +2 -0
  41. package/dist/generators/ts/ObjectUnionType.js +21 -9
  42. package/dist/generators/ts/OptionType.d.ts +1 -1
  43. package/dist/generators/ts/OptionType.js +6 -4
  44. package/dist/generators/ts/PrimitiveType.d.ts +1 -1
  45. package/dist/generators/ts/PrimitiveType.js +6 -3
  46. package/dist/generators/ts/SetType.d.ts +4 -4
  47. package/dist/generators/ts/SetType.js +15 -12
  48. package/dist/generators/ts/SnippetDeclarations.d.ts +3 -2
  49. package/dist/generators/ts/SnippetDeclarations.js +41 -3
  50. package/dist/generators/ts/StringType.d.ts +2 -1
  51. package/dist/generators/ts/StringType.js +14 -7
  52. package/dist/generators/ts/TermType.d.ts +18 -19
  53. package/dist/generators/ts/TermType.js +55 -51
  54. package/dist/generators/ts/TsGenerator.js +24 -12
  55. package/dist/generators/ts/Type.d.ts +12 -3
  56. package/dist/generators/ts/Type.js +1 -0
  57. package/dist/generators/ts/TypeFactory.d.ts +8 -0
  58. package/dist/generators/ts/TypeFactory.js +114 -23
  59. package/dist/generators/ts/UnionType.js +1 -1
  60. package/dist/generators/ts/_ObjectType/EagerShaclProperty.js +3 -2
  61. package/dist/generators/ts/_ObjectType/IdentifierPrefixProperty.d.ts +1 -1
  62. package/dist/generators/ts/_ObjectType/IdentifierPrefixProperty.js +15 -1
  63. package/dist/generators/ts/_ObjectType/IdentifierProperty.d.ts +6 -6
  64. package/dist/generators/ts/_ObjectType/IdentifierProperty.js +59 -29
  65. package/dist/generators/ts/_ObjectType/LazyShaclProperty.d.ts +14 -1
  66. package/dist/generators/ts/_ObjectType/LazyShaclProperty.js +89 -11
  67. package/dist/generators/ts/_ObjectType/Property.d.ts +10 -4
  68. package/dist/generators/ts/_ObjectType/ShaclProperty.d.ts +1 -1
  69. package/dist/generators/ts/_ObjectType/ShaclProperty.js +14 -2
  70. package/dist/generators/ts/_ObjectType/TypeDiscriminatorProperty.d.ts +1 -1
  71. package/dist/generators/ts/_ObjectType/TypeDiscriminatorProperty.js +1 -1
  72. package/dist/generators/ts/_ObjectType/classDeclaration.js +16 -12
  73. package/dist/generators/ts/_ObjectType/createFunctionDeclaration.js +22 -10
  74. package/dist/generators/ts/_ObjectType/equalsFunctionOrMethodDeclaration.js +3 -1
  75. package/dist/generators/ts/_ObjectType/graphqlTypeVariableStatement.js +14 -2
  76. package/dist/generators/ts/_ObjectType/jsonFunctionDeclarations.js +1 -1
  77. package/dist/generators/ts/_ObjectType/rdfFunctionDeclarations.js +95 -84
  78. package/dist/generators/ts/_ObjectType/sparqlConstructQueryFunctionDeclaration.js +3 -3
  79. package/dist/generators/ts/_ObjectType/sparqlConstructQueryStringFunctionDeclaration.js +1 -1
  80. package/dist/generators/ts/_ObjectType/sparqlFunctionDeclarations.js +35 -15
  81. package/dist/generators/ts/_ObjectType/toRdfFunctionOrMethodDeclaration.js +19 -12
  82. package/dist/generators/ts/forwardingObjectSetClassDeclaration.d.ts +8 -0
  83. package/dist/generators/ts/forwardingObjectSetClassDeclaration.js +29 -0
  84. package/dist/generators/ts/objectSetDeclarations.js +2 -0
  85. package/dist/generators/ts/rdfjsDatasetObjectSetClassDeclaration.js +26 -19
  86. package/dist/generators/ts/rdfjsTermExpression.js +3 -3
  87. package/dist/generators/ts/sparqlObjectSetClassDeclaration.js +6 -6
  88. package/dist/input/PropertyPath.d.ts +2 -3
  89. package/dist/input/PropertyPath.js +6 -8
  90. package/dist/input/ShapesGraph.js +4 -8
  91. package/dist/input/generated.d.ts +96 -54
  92. package/dist/input/generated.js +1350 -744
  93. package/package.json +5 -8
  94. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstCompositeType.d.ts +0 -12
  95. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstCompositeType.js +0 -193
  96. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstIdentifierType.d.ts +0 -12
  97. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstIdentifierType.js +0 -29
  98. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstLiteralType.d.ts +0 -12
  99. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstLiteralType.js +0 -43
  100. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstTermType.d.ts +0 -14
  101. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstTermType.js +0 -21
  102. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstType.d.ts +0 -15
  103. package/dist/_ShapesGraphToAstTransformer/transformPropertyShapeToAstType.js +0 -60
@@ -13,12 +13,12 @@ export declare class ShapesGraphToAstTransformer {
13
13
  protected shapeAstName: typeof _ShapesGraphToAstTransformer.shapeAstName;
14
14
  protected readonly shapesGraph: input.ShapesGraph;
15
15
  protected transformNodeShapeToAstType: typeof _ShapesGraphToAstTransformer.transformNodeShapeToAstType;
16
- protected transformPropertyShapeToAstCompositeType: typeof _ShapesGraphToAstTransformer.transformPropertyShapeToAstCompositeType;
17
- protected transformPropertyShapeToAstIdentifierType: typeof _ShapesGraphToAstTransformer.transformPropertyShapeToAstIdentifierType;
18
- protected transformPropertyShapeToAstLiteralType: typeof _ShapesGraphToAstTransformer.transformPropertyShapeToAstLiteralType;
19
16
  protected transformPropertyShapeToAstObjectTypeProperty: typeof _ShapesGraphToAstTransformer.transformPropertyShapeToAstObjectTypeProperty;
20
- protected transformPropertyShapeToAstTermType: typeof _ShapesGraphToAstTransformer.transformPropertyShapeToAstTermType;
21
- protected transformPropertyShapeToAstType: typeof _ShapesGraphToAstTransformer.transformPropertyShapeToAstType;
17
+ protected transformShapeToAstCompositeType: typeof _ShapesGraphToAstTransformer.transformShapeToAstCompositeType;
18
+ protected transformShapeToAstIdentifierType: typeof _ShapesGraphToAstTransformer.transformShapeToAstIdentifierType;
19
+ protected transformShapeToAstLiteralType: typeof _ShapesGraphToAstTransformer.transformShapeToAstLiteralType;
20
+ protected transformShapeToAstTermType: typeof _ShapesGraphToAstTransformer.transformShapeToAstTermType;
21
+ protected transformShapeToAstType: typeof _ShapesGraphToAstTransformer.transformShapeToAstType;
22
22
  constructor({ iriPrefixMap, shapesGraph, }: {
23
23
  iriPrefixMap: PrefixMap;
24
24
  shapesGraph: input.ShapesGraph;
@@ -11,12 +11,12 @@ export class ShapesGraphToAstTransformer {
11
11
  shapeAstName = _ShapesGraphToAstTransformer.shapeAstName;
12
12
  shapesGraph;
13
13
  transformNodeShapeToAstType = _ShapesGraphToAstTransformer.transformNodeShapeToAstType;
14
- transformPropertyShapeToAstCompositeType = _ShapesGraphToAstTransformer.transformPropertyShapeToAstCompositeType;
15
- transformPropertyShapeToAstIdentifierType = _ShapesGraphToAstTransformer.transformPropertyShapeToAstIdentifierType;
16
- transformPropertyShapeToAstLiteralType = _ShapesGraphToAstTransformer.transformPropertyShapeToAstLiteralType;
17
14
  transformPropertyShapeToAstObjectTypeProperty = _ShapesGraphToAstTransformer.transformPropertyShapeToAstObjectTypeProperty;
18
- transformPropertyShapeToAstTermType = _ShapesGraphToAstTransformer.transformPropertyShapeToAstTermType;
19
- transformPropertyShapeToAstType = _ShapesGraphToAstTransformer.transformPropertyShapeToAstType;
15
+ transformShapeToAstCompositeType = _ShapesGraphToAstTransformer.transformShapeToAstCompositeType;
16
+ transformShapeToAstIdentifierType = _ShapesGraphToAstTransformer.transformShapeToAstIdentifierType;
17
+ transformShapeToAstLiteralType = _ShapesGraphToAstTransformer.transformShapeToAstLiteralType;
18
+ transformShapeToAstTermType = _ShapesGraphToAstTransformer.transformShapeToAstTermType;
19
+ transformShapeToAstType = _ShapesGraphToAstTransformer.transformShapeToAstType;
20
20
  constructor({ iriPrefixMap, shapesGraph, }) {
21
21
  this.iriPrefixMap = iriPrefixMap;
22
22
  this.shapesGraph = shapesGraph;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=InheritableShapeConstraints.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=InheritableShapeConstraints.js.map
@@ -0,0 +1,15 @@
1
+ import type { Literal, NamedNode } from "@rdfjs/types";
2
+ import { Maybe } from "purify-ts";
3
+ import * as input from "../input/index.js";
4
+ export declare class ShapeStack {
5
+ private readonly stack;
6
+ constructor();
7
+ readonly constraints: {
8
+ readonly hasValues: readonly (Literal | NamedNode)[];
9
+ readonly in_: readonly (Literal | NamedNode)[];
10
+ };
11
+ get defaultValue(): Maybe<Literal | NamedNode>;
12
+ pop(shape: input.Shape): this;
13
+ push(shape: input.Shape): this;
14
+ }
15
+ //# sourceMappingURL=ShapeStack.d.ts.map
@@ -0,0 +1,51 @@
1
+ import { Maybe } from "purify-ts";
2
+ import { invariant } from "ts-invariant";
3
+ import * as input from "../input/index.js";
4
+ export class ShapeStack {
5
+ stack = [];
6
+ constructor() {
7
+ const stack = this.stack;
8
+ this.constraints = {
9
+ get hasValues() {
10
+ for (const shape of stack.toReversed()) {
11
+ if (shape.constraints.hasValues.length > 0) {
12
+ return shape.constraints.hasValues;
13
+ }
14
+ }
15
+ return [];
16
+ },
17
+ get in_() {
18
+ for (const shape of stack.toReversed()) {
19
+ if (shape.constraints.in_.length > 0) {
20
+ return shape.constraints.in_;
21
+ }
22
+ }
23
+ return [];
24
+ },
25
+ };
26
+ }
27
+ constraints;
28
+ get defaultValue() {
29
+ for (const shape of this.stack.toReversed()) {
30
+ if (!(shape instanceof input.PropertyShape)) {
31
+ continue;
32
+ }
33
+ if (shape.defaultValue.isJust()) {
34
+ return shape.defaultValue;
35
+ }
36
+ }
37
+ return Maybe.empty();
38
+ }
39
+ pop(shape) {
40
+ const poppedShape = this.stack.pop();
41
+ invariant(poppedShape, "stack is empty");
42
+ invariant(Object.is(poppedShape, shape), "tried to pop wrong shape from stack");
43
+ return this;
44
+ }
45
+ push(shape) {
46
+ invariant(!this.stack.some((stackShape) => Object.is(stackShape, shape)), "shape already on stack");
47
+ this.stack.push(shape);
48
+ return this;
49
+ }
50
+ }
51
+ //# sourceMappingURL=ShapeStack.js.map
@@ -2,9 +2,9 @@ export * from "./NodeShapeAstType.js";
2
2
  export * from "./shapeAstName.js";
3
3
  export * from "./transformNodeShapeToAstType.js";
4
4
  export * from "./transformPropertyShapeToAstObjectTypeProperty.js";
5
- export * from "./transformPropertyShapeToAstCompositeType.js";
6
- export * from "./transformPropertyShapeToAstIdentifierType.js";
7
- export * from "./transformPropertyShapeToAstLiteralType.js";
8
- export * from "./transformPropertyShapeToAstType.js";
9
- export * from "./transformPropertyShapeToAstTermType.js";
5
+ export * from "./transformShapeToAstCompositeType.js";
6
+ export * from "./transformShapeToAstIdentifierType.js";
7
+ export * from "./transformShapeToAstLiteralType.js";
8
+ export * from "./transformShapeToAstType.js";
9
+ export * from "./transformShapeToAstTermType.js";
10
10
  //# sourceMappingURL=index.d.ts.map
@@ -2,9 +2,9 @@ export * from "./NodeShapeAstType.js";
2
2
  export * from "./shapeAstName.js";
3
3
  export * from "./transformNodeShapeToAstType.js";
4
4
  export * from "./transformPropertyShapeToAstObjectTypeProperty.js";
5
- export * from "./transformPropertyShapeToAstCompositeType.js";
6
- export * from "./transformPropertyShapeToAstIdentifierType.js";
7
- export * from "./transformPropertyShapeToAstLiteralType.js";
8
- export * from "./transformPropertyShapeToAstType.js";
9
- export * from "./transformPropertyShapeToAstTermType.js";
5
+ export * from "./transformShapeToAstCompositeType.js";
6
+ export * from "./transformShapeToAstIdentifierType.js";
7
+ export * from "./transformShapeToAstLiteralType.js";
8
+ export * from "./transformShapeToAstType.js";
9
+ export * from "./transformShapeToAstTermType.js";
10
10
  //# sourceMappingURL=index.js.map
@@ -1,6 +1,7 @@
1
1
  import N3 from "n3";
2
2
  import { Either, Left, Maybe } from "purify-ts";
3
3
  import { invariant } from "ts-invariant";
4
+ import { ShapeStack } from "./ShapeStack.js";
4
5
  import { pickLiteral } from "./pickLiteral.js";
5
6
  function identifierNodeKinds(type) {
6
7
  switch (type.kind) {
@@ -56,6 +57,53 @@ function synthesizeStubAstObjectType({ identifierNodeKinds, tsFeatures, }) {
56
57
  tsObjectDeclarationType: "class",
57
58
  };
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
+ }
59
107
  export function transformPropertyShapeToAstObjectTypeProperty(propertyShape) {
60
108
  {
61
109
  const property = this.astObjectTypePropertiesByIdentifier.get(propertyShape.identifier);
@@ -63,13 +111,13 @@ export function transformPropertyShapeToAstObjectTypeProperty(propertyShape) {
63
111
  return Either.of(property);
64
112
  }
65
113
  }
66
- const typeEither = this.transformPropertyShapeToAstType(propertyShape, null);
114
+ const typeEither = transformPropertyShapeToAstType.bind(this)(propertyShape);
67
115
  if (typeEither.isLeft()) {
68
116
  return typeEither;
69
117
  }
70
118
  const type = typeEither.unsafeCoerce();
71
119
  let stubType = Maybe.empty();
72
- let propertyShapeStubType;
120
+ let propertyShapeStubItemType;
73
121
  if (propertyShape.stub.isJust()) {
74
122
  const propertyShapeStubTypeEither = this.transformNodeShapeToAstType(propertyShape.stub.unsafeCoerce()).chain((propertyShapeStubType) => {
75
123
  switch (propertyShapeStubType.kind) {
@@ -84,48 +132,48 @@ export function transformPropertyShapeToAstObjectTypeProperty(propertyShape) {
84
132
  if (propertyShapeStubTypeEither.isLeft()) {
85
133
  return propertyShapeStubTypeEither;
86
134
  }
87
- propertyShapeStubType = propertyShapeStubTypeEither.unsafeCoerce();
135
+ propertyShapeStubItemType = propertyShapeStubTypeEither.unsafeCoerce();
88
136
  }
89
- if (propertyShapeStubType || propertyShape.lazy.orDefault(false)) {
137
+ if (propertyShapeStubItemType || propertyShape.lazy.orDefault(false)) {
90
138
  switch (type.kind) {
91
139
  case "ObjectType":
92
- case "ObjectUnionType": {
93
- stubType = Maybe.of(propertyShapeStubType ??
140
+ case "ObjectUnionType":
141
+ stubType = Maybe.of(propertyShapeStubItemType ??
94
142
  synthesizeStubAstObjectType({
95
143
  identifierNodeKinds: identifierNodeKinds(type),
96
144
  tsFeatures: type.tsFeatures,
97
145
  }));
98
146
  break;
99
- }
100
147
  case "OptionType":
101
148
  case "SetType": {
102
149
  switch (type.itemType.kind) {
103
150
  case "ObjectType":
104
- case "ObjectUnionType": {
105
- const stubItemType = propertyShapeStubType ??
106
- synthesizeStubAstObjectType({
107
- identifierNodeKinds: identifierNodeKinds(type.itemType),
108
- tsFeatures: type.itemType.tsFeatures,
109
- });
110
- if (type.kind === "OptionType") {
111
- stubType = Maybe.of({
112
- kind: "OptionType",
113
- itemType: stubItemType,
114
- });
115
- }
116
- else {
117
- stubType = Maybe.of({
118
- kind: "SetType",
119
- itemType: stubItemType,
120
- minCount: 0,
121
- mutable: Maybe.empty(),
122
- });
123
- }
151
+ case "ObjectUnionType":
124
152
  break;
125
- }
126
153
  default:
127
154
  return Left(new Error(`${propertyShape} marked lazy but has ${type.kind} of ${type.itemType.kind}`));
128
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
+ }
129
177
  break;
130
178
  }
131
179
  default:
@@ -145,7 +193,7 @@ export function transformPropertyShapeToAstObjectTypeProperty(propertyShape) {
145
193
  order: propertyShape.order.orDefault(0),
146
194
  path,
147
195
  stubType,
148
- type,
196
+ type: type,
149
197
  visibility: propertyShape.visibility,
150
198
  };
151
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: [...new Set(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