@shaclmate/compiler 2.0.12 → 2.0.14

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 (204) hide show
  1. package/Compiler.d.ts +15 -0
  2. package/Compiler.js +20 -0
  3. package/ShapesGraphToAstTransformer.d.ts +18 -23
  4. package/ShapesGraphToAstTransformer.js +21 -447
  5. package/_ShapesGraphToAstTransformer/NodeShapeAstType.d.ts +3 -0
  6. package/_ShapesGraphToAstTransformer/NodeShapeAstType.js +2 -0
  7. package/_ShapesGraphToAstTransformer/index.d.ts +10 -0
  8. package/_ShapesGraphToAstTransformer/index.js +10 -0
  9. package/_ShapesGraphToAstTransformer/pickLiteral.d.ts +4 -0
  10. package/_ShapesGraphToAstTransformer/pickLiteral.js +18 -0
  11. package/_ShapesGraphToAstTransformer/propertyShapeNodeKinds.d.ts +4 -0
  12. package/_ShapesGraphToAstTransformer/propertyShapeNodeKinds.js +26 -0
  13. package/_ShapesGraphToAstTransformer/shapeAstName.d.ts +5 -0
  14. package/_ShapesGraphToAstTransformer/shapeAstName.js +28 -0
  15. package/_ShapesGraphToAstTransformer/transformNodeShapeToAstType.d.ts +6 -0
  16. package/_ShapesGraphToAstTransformer/transformNodeShapeToAstType.js +165 -0
  17. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstCompositeType.d.ts +13 -0
  18. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstCompositeType.js +198 -0
  19. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstIdentifierType.d.ts +12 -0
  20. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstIdentifierType.js +29 -0
  21. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstLiteralType.d.ts +12 -0
  22. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstLiteralType.js +43 -0
  23. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstObjectTypeProperty.d.ts +6 -0
  24. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstObjectTypeProperty.js +31 -0
  25. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstTermType.d.ts +14 -0
  26. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstTermType.js +21 -0
  27. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstType.d.ts +16 -0
  28. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstType.js +58 -0
  29. package/ast/Ast.d.ts +5 -0
  30. package/ast/CompositeType.d.ts +13 -0
  31. package/ast/CompositeType.js +2 -0
  32. package/ast/IdentifierType.d.ts +3 -6
  33. package/ast/IntersectionType.d.ts +2 -2
  34. package/ast/ListType.d.ts +53 -0
  35. package/ast/ListType.js +2 -0
  36. package/ast/LiteralType.d.ts +4 -3
  37. package/ast/Name.d.ts +1 -0
  38. package/ast/ObjectCompositeType.d.ts +22 -0
  39. package/ast/ObjectCompositeType.js +2 -0
  40. package/ast/ObjectIntersectionType.d.ts +8 -0
  41. package/ast/ObjectIntersectionType.js +2 -0
  42. package/ast/ObjectType.d.ts +92 -20
  43. package/ast/ObjectType.js +26 -1
  44. package/ast/ObjectUnionType.d.ts +19 -0
  45. package/ast/ObjectUnionType.js +2 -0
  46. package/ast/PlaceholderType.d.ts +7 -0
  47. package/ast/PlaceholderType.js +2 -0
  48. package/ast/SetType.d.ts +1 -1
  49. package/ast/TermType.d.ts +11 -0
  50. package/ast/TermType.js +2 -0
  51. package/ast/Type.d.ts +11 -3
  52. package/ast/UnionType.d.ts +3 -3
  53. package/ast/index.d.ts +6 -3
  54. package/ast/index.js +6 -3
  55. package/enums/MintingStrategy.d.ts +5 -0
  56. package/enums/MintingStrategy.js +2 -0
  57. package/enums/PropertyVisibility.d.ts +2 -0
  58. package/enums/PropertyVisibility.js +2 -0
  59. package/enums/TsFeature.d.ts +5 -0
  60. package/enums/TsFeature.js +16 -0
  61. package/enums/TsObjectDeclarationType.d.ts +5 -0
  62. package/enums/TsObjectDeclarationType.js +2 -0
  63. package/enums/index.d.ts +5 -0
  64. package/enums/index.js +5 -0
  65. package/generators/Generator.d.ts +5 -0
  66. package/generators/Generator.js +2 -0
  67. package/generators/index.d.ts +2 -2
  68. package/generators/index.js +2 -2
  69. package/generators/json/AstJsonGenerator.d.ts +4 -5
  70. package/generators/json/AstJsonGenerator.js +31 -22
  71. package/generators/ts/BooleanType.d.ts +7 -3
  72. package/generators/ts/BooleanType.js +58 -4
  73. package/generators/ts/DateTimeType.d.ts +17 -0
  74. package/generators/ts/DateTimeType.js +58 -0
  75. package/generators/ts/DeclaredType.d.ts +17 -0
  76. package/generators/ts/DeclaredType.js +10 -0
  77. package/generators/ts/IdentifierType.d.ts +7 -17
  78. package/generators/ts/IdentifierType.js +69 -60
  79. package/generators/ts/Import.d.ts +18 -0
  80. package/generators/ts/Import.js +59 -0
  81. package/generators/ts/ListType.d.ts +18 -8
  82. package/generators/ts/ListType.js +197 -43
  83. package/generators/ts/LiteralType.d.ts +13 -11
  84. package/generators/ts/LiteralType.js +36 -51
  85. package/generators/ts/NumberType.d.ts +7 -3
  86. package/generators/ts/NumberType.js +55 -4
  87. package/generators/ts/ObjectType.d.ts +47 -25
  88. package/generators/ts/ObjectType.js +222 -49
  89. package/generators/ts/ObjectUnionType.d.ts +58 -0
  90. package/generators/ts/ObjectUnionType.js +481 -0
  91. package/generators/ts/OptionType.d.ts +12 -4
  92. package/generators/ts/OptionType.js +65 -16
  93. package/generators/ts/PrimitiveType.d.ts +15 -5
  94. package/generators/ts/PrimitiveType.js +20 -29
  95. package/generators/ts/SetType.d.ts +12 -4
  96. package/generators/ts/SetType.js +103 -29
  97. package/generators/ts/StringType.d.ts +8 -3
  98. package/generators/ts/StringType.js +57 -3
  99. package/generators/ts/TermType.d.ts +52 -0
  100. package/generators/ts/TermType.js +234 -0
  101. package/generators/ts/TsGenerator.d.ts +4 -12
  102. package/generators/ts/TsGenerator.js +44 -100
  103. package/generators/ts/Type.d.ts +107 -46
  104. package/generators/ts/Type.js +78 -61
  105. package/generators/ts/TypeFactory.d.ts +4 -5
  106. package/generators/ts/TypeFactory.js +212 -89
  107. package/generators/ts/UnionType.d.ts +19 -6
  108. package/generators/ts/UnionType.js +230 -120
  109. package/generators/ts/_ObjectType/IdentifierProperty.d.ts +27 -7
  110. package/generators/ts/_ObjectType/IdentifierProperty.js +143 -31
  111. package/generators/ts/_ObjectType/Property.d.ts +125 -9
  112. package/generators/ts/_ObjectType/Property.js +25 -3
  113. package/generators/ts/_ObjectType/ShaclProperty.d.ts +23 -5
  114. package/generators/ts/_ObjectType/ShaclProperty.js +100 -26
  115. package/generators/ts/_ObjectType/TypeDiscriminatorProperty.d.ts +24 -6
  116. package/generators/ts/_ObjectType/TypeDiscriminatorProperty.js +75 -13
  117. package/generators/ts/_ObjectType/classDeclaration.d.ts +2 -1
  118. package/generators/ts/_ObjectType/classDeclaration.js +79 -111
  119. package/generators/ts/_ObjectType/createFunctionDeclaration.d.ts +5 -0
  120. package/generators/ts/_ObjectType/createFunctionDeclaration.js +60 -0
  121. package/generators/ts/_ObjectType/equalsFunctionDeclaration.d.ts +2 -1
  122. package/generators/ts/_ObjectType/equalsFunctionDeclaration.js +12 -20
  123. package/generators/ts/_ObjectType/equalsFunctionOrMethodDeclaration.d.ts +11 -0
  124. package/generators/ts/_ObjectType/equalsFunctionOrMethodDeclaration.js +70 -0
  125. package/generators/ts/_ObjectType/fromJsonFunctionDeclarations.d.ts +4 -0
  126. package/generators/ts/_ObjectType/fromJsonFunctionDeclarations.js +78 -0
  127. package/generators/ts/_ObjectType/fromRdfFunctionDeclarations.d.ts +4 -0
  128. package/generators/ts/_ObjectType/fromRdfFunctionDeclarations.js +91 -0
  129. package/generators/ts/_ObjectType/hashFunctionDeclaration.d.ts +3 -3
  130. package/generators/ts/_ObjectType/hashFunctionDeclaration.js +11 -55
  131. package/generators/ts/_ObjectType/hashFunctionOrMethodDeclaration.d.ts +11 -0
  132. package/generators/ts/_ObjectType/hashFunctionOrMethodDeclaration.js +64 -0
  133. package/generators/ts/_ObjectType/index.d.ts +8 -2
  134. package/generators/ts/_ObjectType/index.js +8 -2
  135. package/generators/ts/_ObjectType/interfaceDeclaration.d.ts +3 -2
  136. package/generators/ts/_ObjectType/interfaceDeclaration.js +11 -2
  137. package/generators/ts/_ObjectType/jsonSchemaFunctionDeclaration.d.ts +5 -0
  138. package/generators/ts/_ObjectType/jsonSchemaFunctionDeclaration.js +19 -0
  139. package/generators/ts/_ObjectType/jsonUiSchemaFunctionDeclaration.d.ts +5 -0
  140. package/generators/ts/_ObjectType/jsonUiSchemaFunctionDeclaration.js +31 -0
  141. package/generators/ts/_ObjectType/jsonZodSchemaFunctionDeclaration.d.ts +5 -0
  142. package/generators/ts/_ObjectType/jsonZodSchemaFunctionDeclaration.js +37 -0
  143. package/generators/ts/_ObjectType/rdfjsTermExpression.d.ts +6 -0
  144. package/generators/ts/{rdfjsTermExpression.js → _ObjectType/rdfjsTermExpression.js} +3 -1
  145. package/generators/ts/_ObjectType/sparqlFunctionDeclarations.d.ts +4 -0
  146. package/generators/ts/_ObjectType/sparqlFunctionDeclarations.js +101 -0
  147. package/generators/ts/_ObjectType/toJsonFunctionDeclaration.d.ts +5 -0
  148. package/generators/ts/_ObjectType/toJsonFunctionDeclaration.js +19 -0
  149. package/generators/ts/_ObjectType/toJsonFunctionOrMethodDeclaration.d.ts +10 -0
  150. package/generators/ts/_ObjectType/toJsonFunctionOrMethodDeclaration.js +59 -0
  151. package/generators/ts/_ObjectType/toJsonReturnType.d.ts +3 -0
  152. package/generators/ts/_ObjectType/toJsonReturnType.js +19 -0
  153. package/generators/ts/_ObjectType/toRdfFunctionDeclaration.d.ts +3 -2
  154. package/generators/ts/_ObjectType/toRdfFunctionDeclaration.js +11 -45
  155. package/generators/ts/_ObjectType/toRdfFunctionOrMethodDeclaration.d.ts +10 -0
  156. package/generators/ts/_ObjectType/toRdfFunctionOrMethodDeclaration.js +67 -0
  157. package/generators/ts/objectInitializer.d.ts +2 -0
  158. package/generators/ts/objectInitializer.js +14 -0
  159. package/generators/ts/tsComment.d.ts +5 -0
  160. package/generators/ts/tsComment.js +11 -0
  161. package/generators/ts/tsName.d.ts +1 -1
  162. package/generators/ts/tsName.js +1 -0
  163. package/index.d.ts +1 -1
  164. package/index.js +1 -1
  165. package/input/NodeShape.d.ts +46 -0
  166. package/input/NodeShape.js +165 -0
  167. package/input/Ontology.d.ts +13 -0
  168. package/input/Ontology.js +30 -0
  169. package/input/PropertyShape.d.ts +15 -0
  170. package/input/PropertyShape.js +34 -0
  171. package/input/Shape.d.ts +4 -0
  172. package/input/Shape.js +2 -0
  173. package/input/ShapesGraph.d.ts +9 -0
  174. package/input/ShapesGraph.js +50 -0
  175. package/input/ancestorClassIris.d.ts +4 -0
  176. package/input/ancestorClassIris.js +21 -0
  177. package/input/descendantClassIris.d.ts +4 -0
  178. package/input/descendantClassIris.js +21 -0
  179. package/input/generated.d.ts +274 -0
  180. package/input/generated.js +1420 -0
  181. package/input/index.d.ts +7 -0
  182. package/input/index.js +7 -0
  183. package/input/tsFeatures.d.ts +10 -0
  184. package/input/tsFeatures.js +42 -0
  185. package/package.json +14 -8
  186. package/ast/MintingStrategy.d.ts +0 -8
  187. package/ast/MintingStrategy.js +0 -9
  188. package/generators/ts/Configuration.d.ts +0 -29
  189. package/generators/ts/Configuration.js +0 -40
  190. package/generators/ts/IntersectionType.d.ts +0 -14
  191. package/generators/ts/IntersectionType.js +0 -26
  192. package/generators/ts/RdfjsTermType.d.ts +0 -25
  193. package/generators/ts/RdfjsTermType.js +0 -46
  194. package/generators/ts/_ObjectType/fromRdfFunctionDeclaration.d.ts +0 -4
  195. package/generators/ts/_ObjectType/fromRdfFunctionDeclaration.js +0 -72
  196. package/generators/ts/_ObjectType/sparqlGraphPatternsClassDeclaration.d.ts +0 -4
  197. package/generators/ts/_ObjectType/sparqlGraphPatternsClassDeclaration.js +0 -46
  198. package/generators/ts/rdfjsTermExpression.d.ts +0 -5
  199. package/vocabularies/dashDataset.d.ts +0 -3
  200. package/vocabularies/dashDataset.js +0 -2370
  201. package/vocabularies/index.d.ts +0 -2
  202. package/vocabularies/index.js +0 -2
  203. package/vocabularies/shaclmate.d.ts +0 -15
  204. package/vocabularies/shaclmate.js +0 -6
@@ -0,0 +1,101 @@
1
+ import { rdf } from "@tpluscode/rdf-ns-builders";
2
+ import { camelCase } from "change-case";
3
+ import { StructureKind } from "ts-morph";
4
+ export function sparqlFunctionDeclarations() {
5
+ if (!this.features.has("sparql")) {
6
+ return [];
7
+ }
8
+ if (this.extern) {
9
+ return [];
10
+ }
11
+ const subjectDefault = camelCase(this.name);
12
+ const variables = { subject: "subject", variablePrefix: "variablePrefix" };
13
+ const rdfTypeVariable = `${this.dataFactoryVariable}.variable!(\`\${${variables.variablePrefix}}RdfType\`)`;
14
+ return [
15
+ {
16
+ isExported: true,
17
+ kind: StructureKind.Function,
18
+ name: "sparqlConstructQuery",
19
+ parameters: [
20
+ {
21
+ hasQuestionToken: true,
22
+ name: "parameters",
23
+ type: '{ ignoreRdfType?: boolean; prefixes?: { [prefix: string]: string }; subject?: sparqljs.Triple["subject"]; variablePrefix?: string; } & Omit<sparqljs.ConstructQuery, "prefixes" | "queryType" | "type">',
24
+ },
25
+ ],
26
+ returnType: "sparqljs.ConstructQuery",
27
+ statements: [
28
+ "const { ignoreRdfType, subject, variablePrefix, ...queryParameters } = parameters ?? {}",
29
+ `return { ...queryParameters, prefixes: parameters?.prefixes ?? {}, queryType: "CONSTRUCT", template: (queryParameters.template ?? []).concat(${this.name}.sparqlConstructTemplateTriples({ ignoreRdfType, subject })), type: "query", where: (queryParameters.where ?? []).concat(${this.name}.sparqlWherePatterns({ ignoreRdfType, subject, variablePrefix })) };`,
30
+ ],
31
+ },
32
+ {
33
+ isExported: true,
34
+ kind: StructureKind.Function,
35
+ name: "sparqlConstructQueryString",
36
+ parameters: [
37
+ {
38
+ hasQuestionToken: true,
39
+ name: "parameters",
40
+ type: '{ ignoreRdfType?: boolean; subject?: sparqljs.Triple["subject"]; variablePrefix?: string; } & Omit<sparqljs.ConstructQuery, "prefixes" | "queryType" | "type"> & sparqljs.GeneratorOptions',
41
+ },
42
+ ],
43
+ returnType: "string",
44
+ statements: [
45
+ `return new sparqljs.Generator(parameters).stringify(${this.name}.sparqlConstructQuery(parameters));`,
46
+ ],
47
+ },
48
+ {
49
+ isExported: true,
50
+ kind: StructureKind.Function,
51
+ name: "sparqlConstructTemplateTriples",
52
+ parameters: [
53
+ {
54
+ hasQuestionToken: true,
55
+ name: "parameters",
56
+ type: '{ ignoreRdfType?: boolean; subject?: sparqljs.Triple["subject"], variablePrefix?: string }',
57
+ },
58
+ ],
59
+ returnType: "readonly sparqljs.Triple[]",
60
+ statements: [
61
+ `const subject = parameters?.subject ?? ${this.dataFactoryVariable}.variable!("${subjectDefault}");`,
62
+ `const variablePrefix = parameters?.variablePrefix ?? (subject.termType === "Variable" ? subject.value : "${subjectDefault}");`,
63
+ `return [${[
64
+ ...this.parentObjectTypes.map((parentObjectType) => `...${parentObjectType.name}.sparqlConstructTemplateTriples({ ignoreRdfType: true, subject, variablePrefix })`),
65
+ ...(this.fromRdfType.isJust()
66
+ ? [
67
+ `...(parameters?.ignoreRdfType ? [] : [{ subject, predicate: ${this.rdfjsTermExpression(rdf.type)}, object: ${rdfTypeVariable} }])`,
68
+ ]
69
+ : []),
70
+ this.ownProperties.flatMap((property) => property.sparqlConstructTemplateTriples({ variables })),
71
+ ].join(", ")}];`,
72
+ ],
73
+ },
74
+ {
75
+ isExported: true,
76
+ kind: StructureKind.Function,
77
+ name: "sparqlWherePatterns",
78
+ parameters: [
79
+ {
80
+ name: "parameters",
81
+ type: '{ ignoreRdfType?: boolean; subject?: sparqljs.Triple["subject"], variablePrefix?: string }',
82
+ },
83
+ ],
84
+ returnType: "readonly sparqljs.Pattern[]",
85
+ statements: [
86
+ `const subject = parameters?.subject ?? ${this.dataFactoryVariable}.variable!("${subjectDefault}");`,
87
+ `const variablePrefix = parameters?.variablePrefix ?? (subject.termType === "Variable" ? subject.value : "${subjectDefault}");`,
88
+ `return [${[
89
+ ...this.parentObjectTypes.map((parentObjectType) => `...${parentObjectType.name}.sparqlWherePatterns({ ignoreRdfType: true, subject, variablePrefix })`),
90
+ ...(this.fromRdfType.isJust()
91
+ ? [
92
+ `...(parameters?.ignoreRdfType ? [] : [{ triples: [{ subject, predicate: ${this.rdfjsTermExpression(rdf.type)}, object: ${this.rdfjsTermExpression(this.fromRdfType.unsafeCoerce())} }], type: "bgp" as const }, { triples: [{ subject, predicate: ${this.rdfjsTermExpression(rdf.type)}, object: ${rdfTypeVariable} }], type: "bgp" as const }])`,
93
+ ]
94
+ : []),
95
+ this.ownProperties.flatMap((property) => property.sparqlWherePatterns({ variables })),
96
+ ].join(", ")}];`,
97
+ ],
98
+ },
99
+ ];
100
+ }
101
+ //# sourceMappingURL=sparqlFunctionDeclarations.js.map
@@ -0,0 +1,5 @@
1
+ import { Maybe } from "purify-ts";
2
+ import { type FunctionDeclarationStructure } from "ts-morph";
3
+ import type { ObjectType } from "../ObjectType.js";
4
+ export declare function toJsonFunctionDeclaration(this: ObjectType): Maybe<FunctionDeclarationStructure>;
5
+ //# sourceMappingURL=toJsonFunctionDeclaration.d.ts.map
@@ -0,0 +1,19 @@
1
+ import { Maybe } from "purify-ts";
2
+ import { StructureKind } from "ts-morph";
3
+ import { toJsonFunctionOrMethodDeclaration } from "./toJsonFunctionOrMethodDeclaration.js";
4
+ export function toJsonFunctionDeclaration() {
5
+ if (this.declarationType !== "interface") {
6
+ return Maybe.empty();
7
+ }
8
+ if (this.extern) {
9
+ return Maybe.empty();
10
+ }
11
+ return toJsonFunctionOrMethodDeclaration
12
+ .bind(this)()
13
+ .map((toJsonFunctionOrMethodDeclaration) => ({
14
+ ...toJsonFunctionOrMethodDeclaration,
15
+ isExported: true,
16
+ kind: StructureKind.Function,
17
+ }));
18
+ }
19
+ //# sourceMappingURL=toJsonFunctionDeclaration.js.map
@@ -0,0 +1,10 @@
1
+ import { Maybe } from "purify-ts";
2
+ import type { OptionalKind, ParameterDeclarationStructure } from "ts-morph";
3
+ import type { ObjectType } from "../ObjectType.js";
4
+ export declare function toJsonFunctionOrMethodDeclaration(this: ObjectType): Maybe<{
5
+ name: string;
6
+ parameters: OptionalKind<ParameterDeclarationStructure>[];
7
+ returnType: string;
8
+ statements: string[];
9
+ }>;
10
+ //# sourceMappingURL=toJsonFunctionOrMethodDeclaration.d.ts.map
@@ -0,0 +1,59 @@
1
+ import { Maybe } from "purify-ts";
2
+ import { toJsonReturnType } from "./toJsonReturnType.js";
3
+ export function toJsonFunctionOrMethodDeclaration() {
4
+ if (!this.features.has("toJson")) {
5
+ return Maybe.empty();
6
+ }
7
+ if (this.declarationType === "class" &&
8
+ this.ownProperties.length === 0 &&
9
+ this.parentObjectTypes.length > 0) {
10
+ return Maybe.empty();
11
+ }
12
+ const jsonObjectMembers = [];
13
+ const parameters = [];
14
+ switch (this.declarationType) {
15
+ case "class":
16
+ if (this.parentObjectTypes.length > 0) {
17
+ jsonObjectMembers.push("...super.toJson()");
18
+ }
19
+ break;
20
+ case "interface":
21
+ for (const parentObjectType of this.parentObjectTypes) {
22
+ jsonObjectMembers.push(`...${parentObjectType.name}.toJson(${this.thisVariable});`);
23
+ }
24
+ parameters.push({
25
+ name: this.thisVariable,
26
+ type: this.name,
27
+ });
28
+ break;
29
+ }
30
+ if (this.ownProperties.length > 0) {
31
+ for (const property of this.ownProperties) {
32
+ jsonObjectMembers.push(property.toJsonObjectMember({
33
+ variables: { value: `${this.thisVariable}.${property.name}` },
34
+ }));
35
+ }
36
+ }
37
+ // 20241220: don't add @type until we're doing JSON-LD
38
+ // switch (this.toRdfTypes.length) {
39
+ // case 0:
40
+ // break;
41
+ // case 1:
42
+ // jsonObjectMembers.push(`"@type": "${this.toRdfTypes[0].value}"`);
43
+ // break;
44
+ // default:
45
+ // jsonObjectMembers.push(
46
+ // `"@type": ${JSON.stringify(this.toRdfTypes.map((rdfType) => rdfType.value))}`,
47
+ // );
48
+ // break;
49
+ // }
50
+ return Maybe.of({
51
+ name: "toJson",
52
+ parameters,
53
+ returnType: toJsonReturnType.bind(this)(),
54
+ statements: [
55
+ `return JSON.parse(JSON.stringify({ ${jsonObjectMembers.join(",")} } satisfies ${this.jsonName}));`,
56
+ ],
57
+ });
58
+ }
59
+ //# sourceMappingURL=toJsonFunctionOrMethodDeclaration.js.map
@@ -0,0 +1,3 @@
1
+ import type { ObjectType } from "../ObjectType.js";
2
+ export declare function toJsonReturnType(this: ObjectType): string;
3
+ //# sourceMappingURL=toJsonReturnType.d.ts.map
@@ -0,0 +1,19 @@
1
+ export function toJsonReturnType() {
2
+ const typeMembers = [];
3
+ if (this.ownProperties.length > 0) {
4
+ typeMembers.push(`{ ${this.ownProperties
5
+ .map((property) => {
6
+ const propertySignature = property.jsonPropertySignature;
7
+ return `readonly "${propertySignature.name}": ${propertySignature.type}`;
8
+ })
9
+ .join("; ")} }`);
10
+ }
11
+ for (const parentObjectType of this.parentObjectTypes) {
12
+ typeMembers.push(parentObjectType.jsonName);
13
+ }
14
+ if (typeMembers.length === 0) {
15
+ return "object";
16
+ }
17
+ return typeMembers.join(" & ");
18
+ }
19
+ //# sourceMappingURL=toJsonReturnType.js.map
@@ -1,4 +1,5 @@
1
+ import { Maybe } from "purify-ts";
1
2
  import { type FunctionDeclarationStructure } from "ts-morph";
2
- import type { ObjectType } from "../ObjectType";
3
- export declare function toRdfFunctionDeclaration(this: ObjectType): FunctionDeclarationStructure;
3
+ import type { ObjectType } from "../ObjectType.js";
4
+ export declare function toRdfFunctionDeclaration(this: ObjectType): Maybe<FunctionDeclarationStructure>;
4
5
  //# sourceMappingURL=toRdfFunctionDeclaration.d.ts.map
@@ -1,53 +1,19 @@
1
- import { rdf } from "@tpluscode/rdf-ns-builders";
2
- import { camelCase } from "change-case";
1
+ import { Maybe } from "purify-ts";
3
2
  import { StructureKind } from "ts-morph";
4
- const variables = {
5
- ignoreRdfType: "ignoreRdfType",
6
- mutateGraph: "mutateGraph",
7
- resource: "resource",
8
- resourceSet: "resourceSet",
9
- };
3
+ import { toRdfFunctionOrMethodDeclaration } from "./toRdfFunctionOrMethodDeclaration.js";
10
4
  export function toRdfFunctionDeclaration() {
11
- this.ensureAtMostOneSuperObjectType();
12
- let usedIgnoreRdfTypeVariable = false;
13
- const thisVariable = camelCase(this.name);
14
- const statements = [];
15
- if (this.parentObjectTypes.length > 0) {
16
- statements.push(`const ${variables.resource} = ${this.parentObjectTypes[0].name}.toRdf(${thisVariable}, { ${variables.mutateGraph}, ${variables.ignoreRdfType}: true, ${variables.resourceSet} });`);
17
- usedIgnoreRdfTypeVariable = true;
5
+ if (this.declarationType !== "interface") {
6
+ return Maybe.empty();
18
7
  }
19
- else if (this.identifierType.isNamedNodeKind) {
20
- statements.push(`const ${variables.resource} = ${variables.resourceSet}.mutableNamedResource({ identifier: ${thisVariable}.${this.configuration.objectTypeIdentifierPropertyName}, ${variables.mutateGraph} });`);
8
+ if (this.extern) {
9
+ return Maybe.empty();
21
10
  }
22
- else {
23
- statements.push(`const ${variables.resource} = ${variables.resourceSet}.mutableResource({ identifier: ${thisVariable}.${this.configuration.objectTypeIdentifierPropertyName}, ${variables.mutateGraph} });`);
24
- }
25
- this.rdfType.ifJust((rdfType) => {
26
- statements.push(`if (!${variables.ignoreRdfType}) { ${variables.resource}.add(${variables.resource}.dataFactory.namedNode("${rdf.type.value}"), ${variables.resource}.dataFactory.namedNode("${rdfType.value}")); }`);
27
- usedIgnoreRdfTypeVariable = true;
28
- });
29
- for (const property of this.properties) {
30
- statements.push(...property.toRdfStatements({
31
- variables: { ...variables, value: `${thisVariable}.${property.name}` },
32
- }));
33
- }
34
- statements.push(`return ${variables.resource};`);
35
- return {
11
+ return toRdfFunctionOrMethodDeclaration
12
+ .bind(this)()
13
+ .map((toRdfFunctionOrMethodDeclaration) => ({
14
+ ...toRdfFunctionOrMethodDeclaration,
36
15
  isExported: true,
37
16
  kind: StructureKind.Function,
38
- name: "toRdf",
39
- parameters: [
40
- {
41
- name: thisVariable,
42
- type: this.name,
43
- },
44
- {
45
- name: `{ ${usedIgnoreRdfTypeVariable ? `${variables.ignoreRdfType},` : ""} ${variables.mutateGraph}, ${variables.resourceSet} }`,
46
- type: `{ ${variables.ignoreRdfType}?: boolean; ${variables.mutateGraph}: rdfjsResource.MutableResource.MutateGraph, ${variables.resourceSet}: rdfjsResource.MutableResourceSet }`,
47
- },
48
- ],
49
- returnType: this.rdfjsResourceType({ mutable: true }).name,
50
- statements,
51
- };
17
+ }));
52
18
  }
53
19
  //# sourceMappingURL=toRdfFunctionDeclaration.js.map
@@ -0,0 +1,10 @@
1
+ import { Maybe } from "purify-ts";
2
+ import type { OptionalKind, ParameterDeclarationStructure } from "ts-morph";
3
+ import type { ObjectType } from "../ObjectType.js";
4
+ export declare function toRdfFunctionOrMethodDeclaration(this: ObjectType): Maybe<{
5
+ name: string;
6
+ parameters: OptionalKind<ParameterDeclarationStructure>[];
7
+ returnType: string;
8
+ statements: string[];
9
+ }>;
10
+ //# sourceMappingURL=toRdfFunctionOrMethodDeclaration.d.ts.map
@@ -0,0 +1,67 @@
1
+ import { rdf } from "@tpluscode/rdf-ns-builders";
2
+ import { Maybe } from "purify-ts";
3
+ const variables = {
4
+ ignoreRdfType: "ignoreRdfType",
5
+ mutateGraph: "mutateGraph",
6
+ resource: "_resource",
7
+ resourceSet: "resourceSet",
8
+ };
9
+ export function toRdfFunctionOrMethodDeclaration() {
10
+ if (!this.features.has("toRdf")) {
11
+ return Maybe.empty();
12
+ }
13
+ this.ensureAtMostOneSuperObjectType();
14
+ let usedIgnoreRdfTypeVariable = false;
15
+ const statements = [];
16
+ if (this.parentObjectTypes.length > 0) {
17
+ const superToRdfOptions = `{ ${variables.ignoreRdfType}: true, ${variables.mutateGraph}, ${variables.resourceSet} }`;
18
+ let superToRdfCall;
19
+ switch (this.declarationType) {
20
+ case "class":
21
+ superToRdfCall = `super.toRdf(${superToRdfOptions})`;
22
+ break;
23
+ case "interface":
24
+ superToRdfCall = `${this.parentObjectTypes[0].name}.toRdf(${this.thisVariable}, ${superToRdfOptions})`;
25
+ break;
26
+ }
27
+ statements.push(`const ${variables.resource} = ${superToRdfCall};`);
28
+ usedIgnoreRdfTypeVariable = !this.parentObjectTypes[0].abstract;
29
+ }
30
+ else if (this.identifierType.isNamedNodeKind) {
31
+ statements.push(`const ${variables.resource} = ${variables.resourceSet}.mutableNamedResource({ identifier: ${this.thisVariable}.${this.identifierProperty.name}, ${variables.mutateGraph} });`);
32
+ }
33
+ else {
34
+ statements.push(`const ${variables.resource} = ${variables.resourceSet}.mutableResource({ identifier: ${this.thisVariable}.${this.identifierProperty.name}, ${variables.mutateGraph} });`);
35
+ }
36
+ if (this.toRdfTypes.length > 0) {
37
+ statements.push(`if (!${variables.ignoreRdfType}) { ${this.toRdfTypes.map((toRdfType) => `${variables.resource}.add(${variables.resource}.dataFactory.namedNode("${rdf.type.value}"), ${variables.resource}.dataFactory.namedNode("${toRdfType.value}"));`).join(" ")} }`);
38
+ usedIgnoreRdfTypeVariable = true;
39
+ }
40
+ for (const property of this.properties) {
41
+ statements.push(...property.toRdfStatements({
42
+ variables: {
43
+ ...variables,
44
+ value: `${this.thisVariable}.${property.name}`,
45
+ },
46
+ }));
47
+ }
48
+ statements.push(`return ${variables.resource};`);
49
+ const parameters = [];
50
+ if (this.declarationType === "interface") {
51
+ parameters.push({
52
+ name: this.thisVariable,
53
+ type: this.name,
54
+ });
55
+ }
56
+ parameters.push({
57
+ name: `{ ${usedIgnoreRdfTypeVariable ? `${variables.ignoreRdfType}, ` : ""}${variables.mutateGraph}, ${variables.resourceSet} }`,
58
+ type: `{ ${variables.ignoreRdfType}?: boolean; ${variables.mutateGraph}: rdfjsResource.MutableResource.MutateGraph, ${variables.resourceSet}: rdfjsResource.MutableResourceSet }`,
59
+ });
60
+ return Maybe.of({
61
+ name: "toRdf",
62
+ parameters,
63
+ returnType: this.rdfjsResourceType({ mutable: true }).name,
64
+ statements,
65
+ });
66
+ }
67
+ //# sourceMappingURL=toRdfFunctionOrMethodDeclaration.js.map
@@ -0,0 +1,2 @@
1
+ export declare function objectInitializer(properties: Record<string, boolean | number | string | undefined>): string;
2
+ //# sourceMappingURL=objectInitializer.d.ts.map
@@ -0,0 +1,14 @@
1
+ export function objectInitializer(properties) {
2
+ return `{ ${Object.entries(properties)
3
+ .flatMap(([propertyName, propertyValue]) => {
4
+ if (typeof propertyValue === "undefined") {
5
+ return [];
6
+ }
7
+ if (typeof propertyValue === "string" && propertyName === propertyValue) {
8
+ return [propertyName];
9
+ }
10
+ return [`${propertyName}: ${propertyValue}`];
11
+ })
12
+ .join(", ")} }`;
13
+ }
14
+ //# sourceMappingURL=objectInitializer.js.map
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Prefix a comment with multiline comment delimiters.
3
+ */
4
+ export declare function tsComment(comment: string): string;
5
+ //# sourceMappingURL=tsComment.d.ts.map
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Prefix a comment with multiline comment delimiters.
3
+ */
4
+ export function tsComment(comment) {
5
+ return `/**\n${comment
6
+ .trim()
7
+ .split(/\r?\n|\r|\n/g)
8
+ .map((line) => ` * ${line}`)
9
+ .join("\n")}\n */\n`;
10
+ }
11
+ //# sourceMappingURL=tsComment.js.map
@@ -1,3 +1,3 @@
1
- import type * as ast from "../../ast";
1
+ import type * as ast from "../../ast/index.js";
2
2
  export declare function tsName(astName: ast.Name): string;
3
3
  //# sourceMappingURL=tsName.d.ts.map
@@ -4,6 +4,7 @@ export function tsName(astName) {
4
4
  for (const tsNameAlternative of [
5
5
  astName.shaclmateName.extract(),
6
6
  astName.shName.extract()?.replace(" ", "_"),
7
+ astName.label.extract()?.replace(" ", "_"),
7
8
  astName.curie.map((curie) => curie.replace(":", "_")).extract(),
8
9
  astName.propertyPath
9
10
  .chain((propertyPath) => propertyPath.curie.map((curie) => curie.replace(":", "_")))
package/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export * from "./ShapesGraphToAstTransformer.js";
1
+ export * from "./Compiler.js";
2
2
  //# sourceMappingURL=index.d.ts.map
package/index.js CHANGED
@@ -1,2 +1,2 @@
1
- export * from "./ShapesGraphToAstTransformer.js";
1
+ export * from "./Compiler.js";
2
2
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,46 @@
1
+ import type { NamedNode } from "@rdfjs/types";
2
+ import { NodeShape as ShaclCoreNodeShape } from "@shaclmate/shacl-ast";
3
+ import { Maybe } from "purify-ts";
4
+ import type { MintingStrategy, TsFeature, TsObjectDeclarationType } from "../enums/index.js";
5
+ import type { Shape } from "./Shape.js";
6
+ import type * as generated from "./generated.js";
7
+ import type { Ontology, PropertyGroup, PropertyShape, ShapesGraph } from "./index.js";
8
+ export declare class NodeShape extends ShaclCoreNodeShape<any, Ontology, PropertyGroup, PropertyShape, Shape> {
9
+ readonly isClass: boolean;
10
+ readonly isList: boolean;
11
+ private readonly ancestorClassIris;
12
+ private readonly childClassIris;
13
+ private readonly descendantClassIris;
14
+ private readonly generatedShaclmateNodeShape;
15
+ private readonly parentClassIris;
16
+ constructor({ ancestorClassIris, childClassIris, descendantClassIris, generatedShaclmateNodeShape, isClass, isList, parentClassIris, shapesGraph, }: {
17
+ ancestorClassIris: readonly NamedNode[];
18
+ generatedShaclmateNodeShape: generated.ShaclmateNodeShape;
19
+ childClassIris: readonly NamedNode[];
20
+ descendantClassIris: readonly NamedNode[];
21
+ isClass: boolean;
22
+ isList: boolean;
23
+ parentClassIris: readonly NamedNode[];
24
+ shapesGraph: ShapesGraph;
25
+ });
26
+ get abstract(): Maybe<boolean>;
27
+ get ancestorNodeShapes(): readonly NodeShape[];
28
+ get childNodeShapes(): readonly NodeShape[];
29
+ get descendantNodeShapes(): readonly NodeShape[];
30
+ get export(): Maybe<boolean>;
31
+ get extern(): Maybe<boolean>;
32
+ get fromRdfType(): Maybe<NamedNode>;
33
+ get mintingStrategy(): Maybe<MintingStrategy>;
34
+ get mutable(): Maybe<boolean>;
35
+ get nodeKinds(): Set<"BlankNode" | "NamedNode">;
36
+ get parentNodeShapes(): readonly NodeShape[];
37
+ get shaclmateName(): Maybe<string>;
38
+ get toRdfTypes(): readonly NamedNode[];
39
+ get tsFeatures(): Maybe<Set<TsFeature>>;
40
+ get tsImports(): readonly string[];
41
+ get tsObjectDeclarationType(): Maybe<TsObjectDeclarationType>;
42
+ get tsObjectIdentifierPropertyName(): Maybe<string>;
43
+ get tsObjectTypeDiscriminatorPropertyName(): Maybe<string>;
44
+ private get _mintingStrategy();
45
+ }
46
+ //# sourceMappingURL=NodeShape.d.ts.map
@@ -0,0 +1,165 @@
1
+ import { NodeShape as ShaclCoreNodeShape } from "@shaclmate/shacl-ast";
2
+ import { Maybe } from "purify-ts";
3
+ import { tsFeatures } from "./tsFeatures.js";
4
+ export class NodeShape extends ShaclCoreNodeShape {
5
+ constructor({ ancestorClassIris, childClassIris, descendantClassIris, generatedShaclmateNodeShape, isClass, isList, parentClassIris, shapesGraph, }) {
6
+ super(generatedShaclmateNodeShape, shapesGraph);
7
+ this.ancestorClassIris = ancestorClassIris;
8
+ this.childClassIris = childClassIris;
9
+ this.descendantClassIris = descendantClassIris;
10
+ this.generatedShaclmateNodeShape = generatedShaclmateNodeShape;
11
+ this.isClass = isClass;
12
+ this.isList = isList;
13
+ this.parentClassIris = parentClassIris;
14
+ }
15
+ get abstract() {
16
+ return this.generatedShaclmateNodeShape.abstract;
17
+ }
18
+ get ancestorNodeShapes() {
19
+ return this.isClass
20
+ ? this.ancestorClassIris.flatMap((classIri) => this.shapesGraph.nodeShapeByIdentifier(classIri).toList())
21
+ : [];
22
+ }
23
+ get childNodeShapes() {
24
+ return this.isClass
25
+ ? this.childClassIris.flatMap((classIri) => this.shapesGraph.nodeShapeByIdentifier(classIri).toList())
26
+ : [];
27
+ }
28
+ get descendantNodeShapes() {
29
+ return this.isClass
30
+ ? this.descendantClassIris.flatMap((classIri) => this.shapesGraph.nodeShapeByIdentifier(classIri).toList())
31
+ : [];
32
+ }
33
+ get export() {
34
+ return this.generatedShaclmateNodeShape.export_;
35
+ }
36
+ get extern() {
37
+ return this.generatedShaclmateNodeShape.extern;
38
+ }
39
+ get fromRdfType() {
40
+ // Check for an explicit shaclmate:fromRdfType
41
+ const fromRdfType = this.generatedShaclmateNodeShape.fromRdfType;
42
+ if (fromRdfType.isJust()) {
43
+ return fromRdfType;
44
+ }
45
+ // No explicit shaclmate:fromRdfType
46
+ // If the shape is a class, not abstract, and identified by an IRI then use the shape IRI as the fromRdfType.
47
+ if (!this.abstract.orDefault(false) &&
48
+ this.isClass &&
49
+ this.identifier.termType === "NamedNode") {
50
+ return Maybe.of(this.identifier);
51
+ }
52
+ return Maybe.empty();
53
+ }
54
+ get mintingStrategy() {
55
+ const thisMintingStrategy = this._mintingStrategy;
56
+ if (thisMintingStrategy.isNothing()) {
57
+ for (const ancestorNodeShape of this.ancestorNodeShapes) {
58
+ const ancestorMintingStrategy = ancestorNodeShape._mintingStrategy;
59
+ if (ancestorMintingStrategy.isJust()) {
60
+ return ancestorMintingStrategy;
61
+ }
62
+ }
63
+ }
64
+ return thisMintingStrategy;
65
+ }
66
+ get mutable() {
67
+ return this.generatedShaclmateNodeShape.mutable;
68
+ }
69
+ get nodeKinds() {
70
+ const thisNodeKinds = new Set([...this.constraints.nodeKinds.orDefault(new Set())].filter((nodeKind) => nodeKind !== "Literal"));
71
+ const parentNodeKinds = new Set();
72
+ for (const parentNodeShape of this.parentNodeShapes) {
73
+ for (const parentNodeKind of parentNodeShape.nodeKinds) {
74
+ parentNodeKinds.add(parentNodeKind);
75
+ }
76
+ }
77
+ if (thisNodeKinds.size === 0 && parentNodeKinds.size > 0) {
78
+ // No node kinds on this shape, use the parent's
79
+ return parentNodeKinds;
80
+ }
81
+ if (thisNodeKinds.size > 0 && parentNodeKinds.size > 0) {
82
+ // Node kinds on this shape and the parent's shape
83
+ // This node kinds must be a subset of parent node kinds.
84
+ for (const thisNodeKind of thisNodeKinds) {
85
+ if (!parentNodeKinds.has(thisNodeKind)) {
86
+ throw new Error(`${this} has a nodeKind ${thisNodeKind} that is not in its parent's node kinds`);
87
+ }
88
+ }
89
+ }
90
+ if (thisNodeKinds.size === 0) {
91
+ // Default: both node kinds
92
+ thisNodeKinds.add("BlankNode");
93
+ thisNodeKinds.add("NamedNode");
94
+ }
95
+ return thisNodeKinds;
96
+ }
97
+ get parentNodeShapes() {
98
+ return this.isClass
99
+ ? this.parentClassIris.flatMap((classIri) => this.shapesGraph.nodeShapeByIdentifier(classIri).toList())
100
+ : [];
101
+ }
102
+ get shaclmateName() {
103
+ return this.generatedShaclmateNodeShape.name;
104
+ }
105
+ get toRdfTypes() {
106
+ // Look for one or more explicit shaclmate:toRdfType's
107
+ const toRdfTypes = this.generatedShaclmateNodeShape.toRdfTypes.concat();
108
+ // Ensure the toRdfTypes includes the fromRdfType if there is one
109
+ this.fromRdfType.ifJust((fromRdfType) => {
110
+ if (!toRdfTypes.some((toRdfType) => toRdfType.equals(fromRdfType))) {
111
+ toRdfTypes.push(fromRdfType);
112
+ }
113
+ });
114
+ if (toRdfTypes.length === 0) {
115
+ // No explicit shaclmate:toRdfType's
116
+ // If the shape is a class, not abstract, and identified by an IRI then use the shape IRI as the fromRdfType.
117
+ if (!this.abstract.orDefault(false) &&
118
+ this.isClass &&
119
+ this.identifier.termType === "NamedNode") {
120
+ toRdfTypes.push(this.identifier);
121
+ }
122
+ }
123
+ return toRdfTypes;
124
+ }
125
+ get tsFeatures() {
126
+ return tsFeatures(this.generatedShaclmateNodeShape).altLazy(() => this.isDefinedBy.chain((ontology) => ontology.tsFeatures));
127
+ }
128
+ get tsImports() {
129
+ return this.generatedShaclmateNodeShape.tsImports;
130
+ }
131
+ get tsObjectDeclarationType() {
132
+ return this.generatedShaclmateNodeShape.tsObjectDeclarationType
133
+ .map((iri) => {
134
+ switch (iri.value) {
135
+ case "http://minorg.github.io/shaclmate/ns#_TsObjectDeclarationType_Class":
136
+ return "class";
137
+ case "http://minorg.github.io/shaclmate/ns#_TsObjectDeclarationType_Interface":
138
+ return "interface";
139
+ default:
140
+ throw new RangeError(iri.value);
141
+ }
142
+ })
143
+ .altLazy(() => this.isDefinedBy.chain((ontology) => ontology.tsObjectDeclarationType));
144
+ }
145
+ get tsObjectIdentifierPropertyName() {
146
+ return this.generatedShaclmateNodeShape.tsObjectIdentifierPropertyName;
147
+ }
148
+ get tsObjectTypeDiscriminatorPropertyName() {
149
+ return this.generatedShaclmateNodeShape
150
+ .tsObjectTypeDiscriminatorPropertyName;
151
+ }
152
+ get _mintingStrategy() {
153
+ return this.generatedShaclmateNodeShape.mintingStrategy.map((iri) => {
154
+ switch (iri.value) {
155
+ case "http://minorg.github.io/shaclmate/ns#_MintingStrategy_SHA256":
156
+ return "sha256";
157
+ case "http://minorg.github.io/shaclmate/ns#_MintingStrategy_UUIDv4":
158
+ return "uuidv4";
159
+ default:
160
+ throw new RangeError(iri.value);
161
+ }
162
+ });
163
+ }
164
+ }
165
+ //# sourceMappingURL=NodeShape.js.map