@shaclmate/compiler 2.0.13 → 2.0.15

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 (210) 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 +178 -0
  17. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstCompositeType.d.ts +13 -0
  18. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstCompositeType.js +200 -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 +91 -19
  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 +19 -0
  74. package/generators/ts/DateTimeType.js +66 -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 +17 -0
  80. package/generators/ts/Import.js +54 -0
  81. package/generators/ts/ListType.d.ts +19 -8
  82. package/generators/ts/ListType.js +206 -44
  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 +48 -25
  88. package/generators/ts/ObjectType.js +234 -50
  89. package/generators/ts/ObjectUnionType.d.ts +58 -0
  90. package/generators/ts/ObjectUnionType.js +450 -0
  91. package/generators/ts/OptionType.d.ts +14 -4
  92. package/generators/ts/OptionType.js +70 -20
  93. package/generators/ts/PrimitiveType.d.ts +15 -5
  94. package/generators/ts/PrimitiveType.js +27 -31
  95. package/generators/ts/SetType.d.ts +14 -4
  96. package/generators/ts/SetType.js +108 -30
  97. package/generators/ts/SnippetDeclarations.d.ts +11 -0
  98. package/generators/ts/SnippetDeclarations.js +215 -0
  99. package/generators/ts/StringType.d.ts +8 -3
  100. package/generators/ts/StringType.js +57 -3
  101. package/generators/ts/TermType.d.ts +54 -0
  102. package/generators/ts/TermType.js +242 -0
  103. package/generators/ts/TsGenerator.d.ts +4 -12
  104. package/generators/ts/TsGenerator.js +57 -99
  105. package/generators/ts/Type.d.ts +117 -46
  106. package/generators/ts/Type.js +93 -59
  107. package/generators/ts/TypeFactory.d.ts +4 -5
  108. package/generators/ts/TypeFactory.js +224 -91
  109. package/generators/ts/UnionType.d.ts +20 -6
  110. package/generators/ts/UnionType.js +230 -120
  111. package/generators/ts/_ObjectType/IdentifierProperty.d.ts +27 -8
  112. package/generators/ts/_ObjectType/IdentifierProperty.js +153 -32
  113. package/generators/ts/_ObjectType/Property.d.ts +139 -9
  114. package/generators/ts/_ObjectType/Property.js +26 -3
  115. package/generators/ts/_ObjectType/ShaclProperty.d.ts +24 -5
  116. package/generators/ts/_ObjectType/ShaclProperty.js +103 -26
  117. package/generators/ts/_ObjectType/TypeDiscriminatorProperty.d.ts +23 -7
  118. package/generators/ts/_ObjectType/TypeDiscriminatorProperty.js +83 -14
  119. package/generators/ts/_ObjectType/classDeclaration.d.ts +2 -1
  120. package/generators/ts/_ObjectType/classDeclaration.js +79 -111
  121. package/generators/ts/_ObjectType/createFunctionDeclaration.d.ts +5 -0
  122. package/generators/ts/_ObjectType/createFunctionDeclaration.js +60 -0
  123. package/generators/ts/_ObjectType/equalsFunctionDeclaration.d.ts +2 -1
  124. package/generators/ts/_ObjectType/equalsFunctionDeclaration.js +12 -20
  125. package/generators/ts/_ObjectType/equalsFunctionOrMethodDeclaration.d.ts +11 -0
  126. package/generators/ts/_ObjectType/equalsFunctionOrMethodDeclaration.js +70 -0
  127. package/generators/ts/_ObjectType/fromJsonFunctionDeclarations.d.ts +4 -0
  128. package/generators/ts/_ObjectType/fromJsonFunctionDeclarations.js +78 -0
  129. package/generators/ts/_ObjectType/fromRdfFunctionDeclarations.d.ts +4 -0
  130. package/generators/ts/_ObjectType/fromRdfFunctionDeclarations.js +91 -0
  131. package/generators/ts/_ObjectType/hashFunctionDeclaration.d.ts +3 -3
  132. package/generators/ts/_ObjectType/hashFunctionDeclaration.js +11 -55
  133. package/generators/ts/_ObjectType/hashFunctionOrMethodDeclaration.d.ts +11 -0
  134. package/generators/ts/_ObjectType/hashFunctionOrMethodDeclaration.js +64 -0
  135. package/generators/ts/_ObjectType/index.d.ts +8 -2
  136. package/generators/ts/_ObjectType/index.js +8 -2
  137. package/generators/ts/_ObjectType/interfaceDeclaration.d.ts +3 -2
  138. package/generators/ts/_ObjectType/interfaceDeclaration.js +11 -2
  139. package/generators/ts/_ObjectType/jsonSchemaFunctionDeclaration.d.ts +5 -0
  140. package/generators/ts/_ObjectType/jsonSchemaFunctionDeclaration.js +19 -0
  141. package/generators/ts/_ObjectType/jsonUiSchemaFunctionDeclaration.d.ts +5 -0
  142. package/generators/ts/_ObjectType/jsonUiSchemaFunctionDeclaration.js +31 -0
  143. package/generators/ts/_ObjectType/jsonZodSchemaFunctionDeclaration.d.ts +5 -0
  144. package/generators/ts/_ObjectType/jsonZodSchemaFunctionDeclaration.js +37 -0
  145. package/generators/ts/_ObjectType/rdfjsTermExpression.d.ts +6 -0
  146. package/generators/ts/{rdfjsTermExpression.js → _ObjectType/rdfjsTermExpression.js} +3 -1
  147. package/generators/ts/_ObjectType/sparqlConstructQueryFunctionDeclaration.d.ts +5 -0
  148. package/generators/ts/_ObjectType/sparqlConstructQueryFunctionDeclaration.js +21 -0
  149. package/generators/ts/_ObjectType/sparqlConstructQueryStringFunctionDeclaration.d.ts +5 -0
  150. package/generators/ts/_ObjectType/sparqlConstructQueryStringFunctionDeclaration.js +20 -0
  151. package/generators/ts/_ObjectType/sparqlFunctionDeclarations.d.ts +4 -0
  152. package/generators/ts/_ObjectType/sparqlFunctionDeclarations.js +78 -0
  153. package/generators/ts/_ObjectType/toJsonFunctionDeclaration.d.ts +5 -0
  154. package/generators/ts/_ObjectType/toJsonFunctionDeclaration.js +19 -0
  155. package/generators/ts/_ObjectType/toJsonFunctionOrMethodDeclaration.d.ts +10 -0
  156. package/generators/ts/_ObjectType/toJsonFunctionOrMethodDeclaration.js +59 -0
  157. package/generators/ts/_ObjectType/toJsonReturnType.d.ts +3 -0
  158. package/generators/ts/_ObjectType/toJsonReturnType.js +19 -0
  159. package/generators/ts/_ObjectType/toRdfFunctionDeclaration.d.ts +3 -2
  160. package/generators/ts/_ObjectType/toRdfFunctionDeclaration.js +11 -45
  161. package/generators/ts/_ObjectType/toRdfFunctionOrMethodDeclaration.d.ts +10 -0
  162. package/generators/ts/_ObjectType/toRdfFunctionOrMethodDeclaration.js +67 -0
  163. package/generators/ts/objectInitializer.d.ts +2 -0
  164. package/generators/ts/objectInitializer.js +14 -0
  165. package/generators/ts/tsComment.d.ts +5 -0
  166. package/generators/ts/tsComment.js +11 -0
  167. package/generators/ts/tsName.d.ts +1 -1
  168. package/generators/ts/tsName.js +1 -0
  169. package/index.d.ts +1 -1
  170. package/index.js +1 -1
  171. package/input/NodeShape.d.ts +46 -0
  172. package/input/NodeShape.js +165 -0
  173. package/input/Ontology.d.ts +13 -0
  174. package/input/Ontology.js +30 -0
  175. package/input/PropertyShape.d.ts +15 -0
  176. package/input/PropertyShape.js +34 -0
  177. package/input/Shape.d.ts +4 -0
  178. package/input/Shape.js +2 -0
  179. package/input/ShapesGraph.d.ts +9 -0
  180. package/input/ShapesGraph.js +50 -0
  181. package/input/ancestorClassIris.d.ts +4 -0
  182. package/input/ancestorClassIris.js +21 -0
  183. package/input/descendantClassIris.d.ts +4 -0
  184. package/input/descendantClassIris.js +21 -0
  185. package/input/generated.d.ts +275 -0
  186. package/input/generated.js +1436 -0
  187. package/input/index.d.ts +7 -0
  188. package/input/index.js +7 -0
  189. package/input/tsFeatures.d.ts +10 -0
  190. package/input/tsFeatures.js +46 -0
  191. package/package.json +15 -9
  192. package/ast/MintingStrategy.d.ts +0 -8
  193. package/ast/MintingStrategy.js +0 -9
  194. package/generators/ts/Configuration.d.ts +0 -29
  195. package/generators/ts/Configuration.js +0 -40
  196. package/generators/ts/IntersectionType.d.ts +0 -14
  197. package/generators/ts/IntersectionType.js +0 -26
  198. package/generators/ts/RdfjsTermType.d.ts +0 -25
  199. package/generators/ts/RdfjsTermType.js +0 -46
  200. package/generators/ts/_ObjectType/fromRdfFunctionDeclaration.d.ts +0 -4
  201. package/generators/ts/_ObjectType/fromRdfFunctionDeclaration.js +0 -72
  202. package/generators/ts/_ObjectType/sparqlGraphPatternsClassDeclaration.d.ts +0 -4
  203. package/generators/ts/_ObjectType/sparqlGraphPatternsClassDeclaration.js +0 -46
  204. package/generators/ts/rdfjsTermExpression.d.ts +0 -5
  205. package/vocabularies/dashDataset.d.ts +0 -3
  206. package/vocabularies/dashDataset.js +0 -2370
  207. package/vocabularies/index.d.ts +0 -2
  208. package/vocabularies/index.js +0 -2
  209. package/vocabularies/shaclmate.d.ts +0 -15
  210. package/vocabularies/shaclmate.js +0 -6
@@ -7,13 +7,30 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
7
7
  import { pascalCase } from "change-case";
8
8
  import { Maybe } from "purify-ts";
9
9
  import { Memoize } from "typescript-memoize";
10
+ import { tsComment } from "../tsComment.js";
10
11
  import { Property } from "./Property.js";
11
12
  export class ShaclProperty extends Property {
12
- constructor({ path, ...superParameters }) {
13
+ constructor({ comment, description, label, mutable, path, ...superParameters }) {
13
14
  super(superParameters);
15
+ this.comment = comment;
16
+ this.description = description;
17
+ this.label = label;
18
+ this.mutable = mutable;
14
19
  this.path = path;
15
20
  }
16
- get classConstructorParametersPropertySignature() {
21
+ get classGetAccessorDeclaration() {
22
+ return Maybe.empty();
23
+ }
24
+ get classPropertyDeclaration() {
25
+ return Maybe.of({
26
+ isReadonly: !this.mutable,
27
+ leadingTrivia: this.declarationComment,
28
+ name: this.name,
29
+ scope: Property.visibilityToScope(this.visibility),
30
+ type: this.type.name,
31
+ });
32
+ }
33
+ get constructorParametersPropertySignature() {
17
34
  let hasQuestionToken = false;
18
35
  const typeNames = new Set(); // Remove duplicates with a set
19
36
  for (const conversion of this.type.conversions) {
@@ -27,35 +44,44 @@ export class ShaclProperty extends Property {
27
44
  return Maybe.of({
28
45
  hasQuestionToken,
29
46
  isReadonly: true,
47
+ leadingTrivia: this.declarationComment,
30
48
  name: this.name,
31
49
  type: [...typeNames].sort().join(" | "),
32
50
  });
33
51
  }
34
- get classGetAccessorDeclaration() {
35
- return Maybe.empty();
52
+ get declarationImports() {
53
+ return this.type.useImports(this.objectType.features);
36
54
  }
37
- get classPropertyDeclaration() {
55
+ get equalsFunction() {
56
+ return this.type.equalsFunction;
57
+ }
58
+ get interfacePropertySignature() {
38
59
  return {
39
- isReadonly: true,
60
+ isReadonly: !this.mutable,
61
+ leadingTrivia: this.declarationComment,
40
62
  name: this.name,
41
63
  type: this.type.name,
42
64
  };
43
65
  }
44
- get equalsFunction() {
45
- return this.type.equalsFunction();
46
- }
47
- get importStatements() {
48
- return this.type.importStatements;
49
- }
50
- get interfacePropertySignature() {
66
+ get jsonPropertySignature() {
51
67
  return {
52
68
  isReadonly: true,
53
69
  name: this.name,
54
- type: this.type.name,
70
+ type: this.type.jsonName,
55
71
  };
56
72
  }
73
+ get snippetDeclarations() {
74
+ return this.type.snippetDeclarations(this.objectType.features);
75
+ }
76
+ get declarationComment() {
77
+ return this.comment
78
+ .alt(this.description)
79
+ .alt(this.label)
80
+ .map(tsComment)
81
+ .extract();
82
+ }
57
83
  get pathExpression() {
58
- return `${this.configuration.dataFactoryVariable}.namedNode("${this.path.value}")`;
84
+ return `${this.dataFactoryVariable}.namedNode("${this.path.value}")`;
59
85
  }
60
86
  classConstructorStatements({ variables, }) {
61
87
  const typeConversions = this.type.conversions;
@@ -64,12 +90,17 @@ export class ShaclProperty extends Property {
64
90
  }
65
91
  const statements = [];
66
92
  for (const conversion of this.type.conversions) {
67
- statements.push(`if (${conversion.sourceTypeCheckExpression ? conversion.sourceTypeCheckExpression(variables.parameter) : `typeof ${variables.parameter} === "${conversion.sourceTypeName}"`}) { this.${this.name} = ${conversion.conversionExpression(variables.parameter)}; }`);
93
+ statements.push(`if (${conversion.sourceTypeCheckExpression(variables.parameter)}) { this.${this.name} = ${conversion.conversionExpression(variables.parameter)}; }`);
68
94
  }
69
95
  // We shouldn't need this else, since the parameter now has the never type, but have to add it to appease the TypeScript compiler
70
- statements.push(`{ this.${this.name} = ${variables.parameter}; // never\n }`);
96
+ statements.push(`{ this.${this.name} =( ${variables.parameter}) as never;\n }`);
71
97
  return [statements.join(" else ")];
72
98
  }
99
+ fromJsonStatements({ variables, }) {
100
+ return [
101
+ `const ${this.name} = ${this.type.fromJsonExpression({ variables: { value: `${variables.jsonObject}["${this.name}"]` } })};`,
102
+ ];
103
+ }
73
104
  fromRdfStatements({ variables, }) {
74
105
  return [
75
106
  `const _${this.name}Either: purify.Either<rdfjsResource.Resource.ValueError, ${this.type.name}> = ${this.type.fromRdfExpression({ variables: { ...variables, predicate: this.pathExpression, resourceValues: `${variables.resource}.values(${this.pathExpression}, { unique: true })` } })};`,
@@ -80,17 +111,63 @@ export class ShaclProperty extends Property {
80
111
  hashStatements(parameters) {
81
112
  return this.type.hashStatements(parameters);
82
113
  }
83
- sparqlGraphPatternExpression() {
84
- return Maybe.of(this.type
85
- .propertySparqlGraphPatternExpression({
114
+ interfaceConstructorStatements({ variables, }) {
115
+ const typeConversions = this.type.conversions;
116
+ if (typeConversions.length === 1) {
117
+ return [`const ${this.name} = ${variables.parameter};`];
118
+ }
119
+ const statements = [`let ${this.name}: ${this.type.name};`];
120
+ const conversionBranches = [];
121
+ for (const conversion of this.type.conversions) {
122
+ conversionBranches.push(`if (${conversion.sourceTypeCheckExpression(variables.parameter)}) { ${this.name} = ${conversion.conversionExpression(variables.parameter)}; }`);
123
+ }
124
+ // We shouldn't need this else, since the parameter now has the never type, but have to add it to appease the TypeScript compiler
125
+ conversionBranches.push(`{ ${this.name} =( ${variables.parameter}) as never;\n }`);
126
+ statements.push(conversionBranches.join(" else "));
127
+ return statements;
128
+ }
129
+ jsonUiSchemaElement({ variables, }) {
130
+ const scope = `\`\${${variables.scopePrefix}}/properties/${this.name}\``;
131
+ return this.type
132
+ .jsonUiSchemaElement({ variables: { scopePrefix: scope } })
133
+ .altLazy(() => Maybe.of(`{ ${this.label.isJust() ? `label: "${this.label.unsafeCoerce()}", ` : ""}scope: ${scope}, type: "Control" }`));
134
+ }
135
+ jsonZodSchema(parameters) {
136
+ let schema = this.type.jsonZodSchema(parameters);
137
+ this.comment.alt(this.description).ifJust((description) => {
138
+ schema = `${schema}.describe(${JSON.stringify(description)})`;
139
+ });
140
+ return {
141
+ key: this.name,
142
+ schema,
143
+ };
144
+ }
145
+ sparqlConstructTemplateTriples({ variables, }) {
146
+ const objectString = `\`\${${variables.variablePrefix}}${pascalCase(this.name)}\``;
147
+ return this.type.sparqlConstructTemplateTriples({
148
+ context: "property",
86
149
  variables: {
87
- object: `this.variable("${pascalCase(this.name)}")`,
88
- predicate: this.pathExpression,
89
- subject: "this.subject",
150
+ object: `${this.dataFactoryVariable}.variable!(${objectString})`,
151
+ predicate: this.rdfjsTermExpression(this.path),
152
+ subject: variables.subject,
153
+ variablePrefix: objectString,
90
154
  },
91
- })
92
- .toSparqlGraphPatternExpression()
93
- .toString());
155
+ });
156
+ }
157
+ sparqlWherePatterns({ variables, }) {
158
+ const objectString = `\`\${${variables.variablePrefix}}${pascalCase(this.name)}\``;
159
+ return this.type.sparqlWherePatterns({
160
+ context: "property",
161
+ variables: {
162
+ object: `${this.dataFactoryVariable}.variable!(${objectString})`,
163
+ predicate: this.rdfjsTermExpression(this.path),
164
+ subject: variables.subject,
165
+ variablePrefix: objectString,
166
+ },
167
+ });
168
+ }
169
+ toJsonObjectMember(parameters) {
170
+ return `${this.name}: ${this.type.toJsonExpression(parameters)}`;
94
171
  }
95
172
  toRdfStatements({ variables, }) {
96
173
  return [
@@ -2,28 +2,44 @@ import { Maybe } from "purify-ts";
2
2
  import type { GetAccessorDeclarationStructure, OptionalKind, PropertyDeclarationStructure, PropertySignatureStructure } from "ts-morph";
3
3
  import { Property } from "./Property.js";
4
4
  export declare class TypeDiscriminatorProperty extends Property<TypeDiscriminatorProperty.Type> {
5
- readonly equalsFunction = "purifyHelpers.Equatable.strictEquals";
5
+ readonly equalsFunction = "strictEquals";
6
+ readonly mutable = false;
6
7
  readonly value: string;
7
8
  private readonly abstract;
8
9
  private readonly override;
9
- constructor({ abstract, override, value, ...superParameters }: {
10
+ constructor({ abstract, override, type, value, ...superParameters }: {
10
11
  abstract: boolean;
11
12
  override: boolean;
13
+ type: TypeDiscriminatorProperty.Type;
12
14
  value: string;
13
15
  } & ConstructorParameters<typeof Property>[0]);
14
- get classConstructorParametersPropertySignature(): Maybe<OptionalKind<PropertySignatureStructure>>;
15
16
  get classGetAccessorDeclaration(): Maybe<OptionalKind<GetAccessorDeclarationStructure>>;
16
- get classPropertyDeclaration(): OptionalKind<PropertyDeclarationStructure>;
17
+ get classPropertyDeclaration(): Maybe<OptionalKind<PropertyDeclarationStructure>>;
18
+ get constructorParametersPropertySignature(): Maybe<OptionalKind<PropertySignatureStructure>>;
17
19
  get interfacePropertySignature(): OptionalKind<PropertySignatureStructure>;
20
+ get jsonPropertySignature(): OptionalKind<PropertySignatureStructure>;
21
+ get snippetDeclarations(): readonly string[];
18
22
  classConstructorStatements(): readonly string[];
23
+ fromJsonStatements(): readonly string[];
19
24
  fromRdfStatements(): readonly string[];
20
25
  hashStatements(): readonly string[];
21
- sparqlGraphPatternExpression(): Maybe<string>;
26
+ interfaceConstructorStatements(): readonly string[];
27
+ jsonUiSchemaElement({ variables, }: Parameters<Property<TypeDiscriminatorProperty.Type>["jsonUiSchemaElement"]>[0]): Maybe<string>;
28
+ jsonZodSchema({ variables, }: Parameters<Property<TypeDiscriminatorProperty.Type>["jsonZodSchema"]>[0]): ReturnType<Property<TypeDiscriminatorProperty.Type>["jsonZodSchema"]>;
29
+ sparqlConstructTemplateTriples(): readonly string[];
30
+ sparqlWherePatterns(): readonly string[];
31
+ toJsonObjectMember({ variables, }: Parameters<Property<TypeDiscriminatorProperty.Type>["toJsonObjectMember"]>[0]): string;
22
32
  toRdfStatements(): readonly string[];
23
33
  }
24
34
  export declare namespace TypeDiscriminatorProperty {
25
- interface Type {
26
- readonly name: string;
35
+ class Type {
36
+ readonly mutable: boolean;
37
+ readonly values: readonly string[];
38
+ constructor({ mutable, values, }: {
39
+ mutable: boolean;
40
+ values: readonly string[];
41
+ });
42
+ get name(): string;
27
43
  }
28
44
  }
29
45
  //# sourceMappingURL=TypeDiscriminatorProperty.d.ts.map
@@ -1,53 +1,122 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
1
7
  import { Maybe } from "purify-ts";
8
+ import { invariant } from "ts-invariant";
9
+ import { Memoize } from "typescript-memoize";
10
+ import { SnippetDeclarations } from "../SnippetDeclarations.js";
2
11
  import { Property } from "./Property.js";
3
12
  export class TypeDiscriminatorProperty extends Property {
4
- constructor({ abstract, override, value, ...superParameters }) {
5
- super(superParameters);
6
- this.equalsFunction = "purifyHelpers.Equatable.strictEquals";
13
+ constructor({ abstract, override, type, value, ...superParameters }) {
14
+ super({ ...superParameters, type });
15
+ this.equalsFunction = "strictEquals";
16
+ this.mutable = false;
17
+ invariant(this.visibility === "public");
7
18
  this.abstract = abstract;
8
19
  this.override = override;
9
20
  this.value = value;
10
21
  }
11
- get classConstructorParametersPropertySignature() {
12
- return Maybe.empty();
13
- }
14
22
  get classGetAccessorDeclaration() {
15
23
  return Maybe.empty();
16
24
  }
17
25
  get classPropertyDeclaration() {
18
- return {
19
- isAbstract: this.abstract,
20
- hasOverrideKeyword: this.override,
26
+ return Maybe.of({
27
+ // Work around a ts-morph bug that puts the override keyword before the abstract keyword
28
+ isAbstract: this.abstract && this.override ? undefined : this.abstract,
29
+ hasOverrideKeyword: this.abstract && this.override ? undefined : this.override,
21
30
  initializer: !this.abstract ? `"${this.value}"` : undefined,
31
+ isReadonly: true,
32
+ leadingTrivia: this.abstract && this.override ? "abstract override " : undefined,
33
+ name: this.name,
34
+ type: !this.abstract && this.type.name === `"${this.value}"`
35
+ ? undefined
36
+ : this.type.name,
37
+ });
38
+ }
39
+ get constructorParametersPropertySignature() {
40
+ return Maybe.empty();
41
+ }
42
+ get interfacePropertySignature() {
43
+ return {
22
44
  isReadonly: true,
23
45
  name: this.name,
24
46
  type: this.type.name,
25
47
  };
26
48
  }
27
- get interfacePropertySignature() {
49
+ get jsonPropertySignature() {
28
50
  return {
29
51
  isReadonly: true,
30
52
  name: this.name,
31
53
  type: this.type.name,
32
54
  };
33
55
  }
56
+ get snippetDeclarations() {
57
+ const snippetDeclarations = [];
58
+ if (this.objectType.features.has("equals")) {
59
+ snippetDeclarations.push(SnippetDeclarations.strictEquals);
60
+ }
61
+ return snippetDeclarations;
62
+ }
34
63
  classConstructorStatements() {
35
64
  return [];
36
65
  }
66
+ fromJsonStatements() {
67
+ return this.fromRdfStatements();
68
+ }
37
69
  fromRdfStatements() {
38
- return !this.abstract &&
39
- this.configuration.objectTypeDeclarationType === "interface"
70
+ return !this.abstract && this.objectType.declarationType === "interface"
40
71
  ? [`const ${this.name} = "${this.value}" as const`]
41
72
  : [];
42
73
  }
43
74
  hashStatements() {
44
75
  return [];
45
76
  }
46
- sparqlGraphPatternExpression() {
47
- return Maybe.empty();
77
+ interfaceConstructorStatements() {
78
+ return !this.abstract
79
+ ? [`const ${this.name} = "${this.value}" as const`]
80
+ : [];
81
+ }
82
+ jsonUiSchemaElement({ variables, }) {
83
+ const scope = `\`\${${variables.scopePrefix}}/properties/${this.name}\``;
84
+ return Maybe.of(`{ rule: { condition: { schema: { const: "${this.value}" }, scope: ${scope} }, effect: "HIDE" }, scope: ${scope}, type: "Control" }`);
85
+ }
86
+ jsonZodSchema({ variables, }) {
87
+ return {
88
+ key: this.name,
89
+ schema: this.type.values.length > 1
90
+ ? `${variables.zod}.enum(${JSON.stringify(this.type.values)})`
91
+ : `${variables.zod}.literal("${this.type.values[0]}")`,
92
+ };
93
+ }
94
+ sparqlConstructTemplateTriples() {
95
+ return [];
96
+ }
97
+ sparqlWherePatterns() {
98
+ return [];
99
+ }
100
+ toJsonObjectMember({ variables, }) {
101
+ return `${this.name}: ${variables.value}`;
48
102
  }
49
103
  toRdfStatements() {
50
104
  return [];
51
105
  }
52
106
  }
107
+ (function (TypeDiscriminatorProperty) {
108
+ class Type {
109
+ constructor({ mutable, values, }) {
110
+ this.mutable = mutable;
111
+ this.values = values;
112
+ }
113
+ get name() {
114
+ return this.values.map((name) => `"${name}"`).join(" | ");
115
+ }
116
+ }
117
+ __decorate([
118
+ Memoize()
119
+ ], Type.prototype, "name", null);
120
+ TypeDiscriminatorProperty.Type = Type;
121
+ })(TypeDiscriminatorProperty || (TypeDiscriminatorProperty = {}));
53
122
  //# sourceMappingURL=TypeDiscriminatorProperty.js.map
@@ -1,4 +1,5 @@
1
+ import { Maybe } from "purify-ts";
1
2
  import { type ClassDeclarationStructure } from "ts-morph";
2
3
  import type { ObjectType } from "../ObjectType.js";
3
- export declare function classDeclaration(this: ObjectType): ClassDeclarationStructure;
4
+ export declare function classDeclaration(this: ObjectType): Maybe<ClassDeclarationStructure>;
4
5
  //# sourceMappingURL=classDeclaration.d.ts.map
@@ -1,154 +1,122 @@
1
- import { rdf } from "@tpluscode/rdf-ns-builders";
1
+ import { Maybe } from "purify-ts";
2
2
  import { StructureKind, } from "ts-morph";
3
- import { hasherTypeConstraint } from "./hashFunctionDeclaration.js";
3
+ import { tsComment } from "../tsComment.js";
4
+ import { equalsFunctionOrMethodDeclaration } from "./equalsFunctionOrMethodDeclaration.js";
5
+ import { hashFunctionOrMethodDeclaration } from "./hashFunctionOrMethodDeclaration.js";
6
+ import { toJsonFunctionOrMethodDeclaration } from "./toJsonFunctionOrMethodDeclaration.js";
7
+ import { toRdfFunctionOrMethodDeclaration } from "./toRdfFunctionOrMethodDeclaration.js";
4
8
  function constructorDeclaration() {
5
- if (this.properties.length === 0) {
6
- return null;
9
+ const parametersPropertySignatures = this.properties.flatMap((property) => property.constructorParametersPropertySignature
10
+ .map((propertySignature) => `readonly ${propertySignature.name}${propertySignature.hasQuestionToken ? "?" : ""}: ${propertySignature.type}`)
11
+ .toList());
12
+ let parametersType;
13
+ if (parametersPropertySignatures.length > 0) {
14
+ parametersType = `{ ${parametersPropertySignatures.join(", ")} }`;
15
+ }
16
+ else {
17
+ parametersType = "";
7
18
  }
8
- const statements = [];
9
19
  if (this.parentObjectTypes.length > 0) {
10
- statements.push("super(parameters);");
20
+ // Pass up parameters
21
+ parametersType = `${parametersType}${parametersType.length > 0 ? " & " : ""}ConstructorParameters<typeof ${this.parentObjectTypes[0].name}>[0]`;
11
22
  }
12
- for (const property of this.properties) {
13
- for (const statement of property.classConstructorStatements({
14
- variables: { parameter: `parameters.${property.name}` },
15
- })) {
16
- statements.push(statement);
17
- }
23
+ if (parametersType.length === 0) {
24
+ parametersType = "object";
18
25
  }
19
- const constructorParameterPropertySignatures = this.properties.flatMap((property) => property.classConstructorParametersPropertySignature
20
- .map((propertySignature) => `readonly ${propertySignature.name}${propertySignature.hasQuestionToken ? "?" : ""}: ${propertySignature.type}`)
21
- .toList());
22
- if (constructorParameterPropertySignatures.length === 0) {
23
- return null;
24
- }
25
- let constructorParametersType = `{ ${constructorParameterPropertySignatures.join(", ")} }`;
26
+ const statements = [];
26
27
  if (this.parentObjectTypes.length > 0) {
27
- constructorParametersType = `${constructorParametersType} & ConstructorParameters<typeof ${this.parentObjectTypes[0].name}>[0]`;
28
+ // An ancestor object type may be extern so we always have a constructor and always pass up parameters instead
29
+ // of trying to sense whether we need to or not.
30
+ statements.push("super(parameters);");
28
31
  }
32
+ const propertyStatements = this.properties.flatMap((property) => property.classConstructorStatements({
33
+ variables: { parameter: `parameters.${property.name}` },
34
+ }));
35
+ statements.push(...propertyStatements);
29
36
  return {
37
+ leadingTrivia: propertyStatements.length === 0
38
+ ? "// biome-ignore lint/complexity/noUselessConstructor: Always have a constructor\n"
39
+ : undefined,
30
40
  parameters: [
31
41
  {
32
- name: "parameters",
33
- type: constructorParametersType,
42
+ name: statements.length > 0 ? "parameters" : "_parameters",
43
+ type: parametersType,
34
44
  },
35
45
  ],
36
46
  statements,
37
47
  };
38
48
  }
39
49
  export function classDeclaration() {
40
- this.ensureAtMostOneSuperObjectType();
41
- const constructorDeclaration_ = constructorDeclaration.bind(this)();
42
- const methods = [];
43
- if (this.configuration.features.has("equals")) {
44
- methods.push(equalsMethodDeclaration.bind(this)());
45
- }
46
- if (this.configuration.features.has("hash")) {
47
- methods.push(hashMethodDeclaration.bind(this)());
50
+ if (this.declarationType !== "class") {
51
+ return Maybe.empty();
48
52
  }
49
- if (this.configuration.features.has("toRdf")) {
50
- methods.push(toRdfMethodDeclaration.bind(this)());
53
+ if (this.extern) {
54
+ return Maybe.empty();
51
55
  }
56
+ this.ensureAtMostOneSuperObjectType();
52
57
  const getAccessors = [];
53
58
  const properties = [];
54
59
  for (const property of this.properties) {
55
- properties.push(property.classPropertyDeclaration);
60
+ property.classPropertyDeclaration.ifJust((propertyDeclaration) => properties.push(propertyDeclaration));
56
61
  property.classGetAccessorDeclaration.ifJust((getAccessor) => getAccessors.push(getAccessor));
57
62
  }
58
- return {
59
- ctors: constructorDeclaration_ !== null ? [constructorDeclaration_] : undefined,
63
+ return Maybe.of({
64
+ ctors: [constructorDeclaration.bind(this)()],
60
65
  extends: this.parentObjectTypes.length > 0
61
66
  ? this.parentObjectTypes[0].name
62
67
  : undefined,
63
68
  getAccessors,
64
69
  isAbstract: this.abstract,
70
+ isExported: this.export,
65
71
  kind: StructureKind.Class,
66
- isExported: this.export_,
67
- methods,
72
+ leadingTrivia: this.comment.alt(this.label).map(tsComment).extract(),
73
+ methods: [
74
+ ...equalsMethodDeclaration.bind(this)().toList(),
75
+ ...hashMethodDeclaration.bind(this)().toList(),
76
+ ...toJsonMethodDeclaration.bind(this)().toList(),
77
+ ...toRdfMethodDeclaration.bind(this)().toList(),
78
+ ...toStringMethodDeclaration.bind(this)().toList(),
79
+ ],
68
80
  name: this.name,
69
81
  properties,
70
- };
82
+ });
71
83
  }
72
84
  function equalsMethodDeclaration() {
73
- let expression = `purifyHelpers.Equatable.objectEquals(this, other, { ${this.properties
74
- .map((property) => `${property.name}: ${property.equalsFunction}`)
75
- .join()} })`;
76
- if (this.parentObjectTypes.length > 0) {
77
- expression = `super.equals(other).chain(() => ${expression})`;
78
- }
79
- return {
80
- hasOverrideKeyword: this.parentObjectTypes.length > 0,
81
- name: "equals",
82
- parameters: [
83
- {
84
- name: "other",
85
- type: this.name,
86
- },
87
- ],
88
- statements: [`return ${expression};`],
89
- returnType: "purifyHelpers.Equatable.EqualsResult",
90
- };
85
+ return equalsFunctionOrMethodDeclaration.bind(this)();
91
86
  }
92
87
  function hashMethodDeclaration() {
93
- return {
94
- hasOverrideKeyword: this.parentObjectTypes.length > 0,
88
+ return hashFunctionOrMethodDeclaration
89
+ .bind(this)()
90
+ .map((hashFunctionOrMethodDeclaration) => ({
91
+ ...hashFunctionOrMethodDeclaration,
95
92
  name: "hash",
96
- parameters: [
97
- {
98
- name: "hasher",
99
- type: "HasherT",
100
- },
101
- ],
102
- returnType: "HasherT",
103
- statements: [`return ${this.name}.${this.hashFunctionName}(this, hasher);`],
104
- typeParameters: [
105
- {
106
- name: "HasherT",
107
- constraint: hasherTypeConstraint,
108
- },
109
- ],
110
- };
93
+ }));
94
+ }
95
+ function toJsonMethodDeclaration() {
96
+ return toJsonFunctionOrMethodDeclaration
97
+ .bind(this)()
98
+ .map((toJsonFunctionOrMethodDeclaration) => ({
99
+ ...toJsonFunctionOrMethodDeclaration,
100
+ hasOverrideKeyword: this.parentObjectTypes.length > 0,
101
+ }));
111
102
  }
112
103
  function toRdfMethodDeclaration() {
113
- const variables = {
114
- ignoreRdfType: "ignoreRdfType",
115
- mutateGraph: "mutateGraph",
116
- resource: "resource",
117
- resourceSet: "resourceSet",
118
- };
119
- let usedIgnoreRdfTypeVariable = false;
120
- const statements = [];
121
- if (this.parentObjectTypes.length > 0) {
122
- statements.push(`const ${variables.resource} = super.toRdf({ ${variables.mutateGraph}, ${variables.ignoreRdfType}: true, ${variables.resourceSet} });`);
123
- usedIgnoreRdfTypeVariable = true;
124
- }
125
- else if (this.identifierType.isNamedNodeKind) {
126
- statements.push(`const ${variables.resource} = ${variables.resourceSet}.mutableNamedResource({ identifier: this.${this.configuration.objectTypeIdentifierPropertyName}, ${variables.mutateGraph} });`);
127
- }
128
- else {
129
- statements.push(`const ${variables.resource} = ${variables.resourceSet}.mutableResource({ identifier: this.${this.configuration.objectTypeIdentifierPropertyName}, ${variables.mutateGraph} });`);
130
- }
131
- this.rdfType.ifJust((rdfType) => {
132
- statements.push(`if (!${variables.ignoreRdfType}) { ${variables.resource}.add(${variables.resource}.dataFactory.namedNode("${rdf.type.value}"), ${variables.resource}.dataFactory.namedNode("${rdfType.value}")); }`);
133
- usedIgnoreRdfTypeVariable = true;
134
- });
135
- for (const property of this.properties) {
136
- statements.push(...property.toRdfStatements({
137
- variables: { ...variables, value: `this.${property.name}` },
138
- }));
104
+ return toRdfFunctionOrMethodDeclaration
105
+ .bind(this)()
106
+ .map((toRdfFunctionOrMethodDeclaration) => ({
107
+ ...toRdfFunctionOrMethodDeclaration,
108
+ hasOverrideKeyword: this.parentObjectTypes.length > 0,
109
+ }));
110
+ }
111
+ function toStringMethodDeclaration() {
112
+ if (!this.features.has("toJson")) {
113
+ return Maybe.empty();
139
114
  }
140
- statements.push(`return ${variables.resource};`);
141
- return {
115
+ return Maybe.of({
142
116
  hasOverrideKeyword: this.parentObjectTypes.length > 0,
143
- name: "toRdf",
144
- parameters: [
145
- {
146
- name: `{ ${usedIgnoreRdfTypeVariable ? `${variables.ignoreRdfType},` : ""} ${variables.mutateGraph}, ${variables.resourceSet} }`,
147
- type: `{ ${variables.ignoreRdfType}?: boolean; ${variables.mutateGraph}: rdfjsResource.MutableResource.MutateGraph, ${variables.resourceSet}: rdfjsResource.MutableResourceSet }`,
148
- },
149
- ],
150
- returnType: this.rdfjsResourceType({ mutable: true }).name,
151
- statements,
152
- };
117
+ name: "toString",
118
+ returnType: "string",
119
+ statements: ["return JSON.stringify(this.toJson());"],
120
+ });
153
121
  }
154
122
  //# sourceMappingURL=classDeclaration.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 createFunctionDeclaration(this: ObjectType): Maybe<FunctionDeclarationStructure>;
5
+ //# sourceMappingURL=createFunctionDeclaration.d.ts.map