@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
@@ -1,29 +1,47 @@
1
1
  import { Maybe } from "purify-ts";
2
2
  import type { GetAccessorDeclarationStructure, OptionalKind, PropertyDeclarationStructure, PropertySignatureStructure } from "ts-morph";
3
+ import type { TsObjectDeclarationType } from "../../../enums/index.js";
3
4
  import { Property } from "./Property.js";
4
5
  export declare class TypeDiscriminatorProperty extends Property<TypeDiscriminatorProperty.Type> {
5
6
  readonly equalsFunction = "purifyHelpers.Equatable.strictEquals";
7
+ readonly mutable = false;
6
8
  readonly value: string;
7
9
  private readonly abstract;
10
+ private readonly objectTypeDeclarationType;
8
11
  private readonly override;
9
- constructor({ abstract, override, value, ...superParameters }: {
12
+ constructor({ abstract, objectTypeDeclarationType, override, type, value, ...superParameters }: {
10
13
  abstract: boolean;
14
+ objectTypeDeclarationType: TsObjectDeclarationType;
11
15
  override: boolean;
16
+ type: TypeDiscriminatorProperty.Type;
12
17
  value: string;
13
18
  } & ConstructorParameters<typeof Property>[0]);
14
- get classConstructorParametersPropertySignature(): Maybe<OptionalKind<PropertySignatureStructure>>;
15
19
  get classGetAccessorDeclaration(): Maybe<OptionalKind<GetAccessorDeclarationStructure>>;
16
- get classPropertyDeclaration(): OptionalKind<PropertyDeclarationStructure>;
20
+ get classPropertyDeclaration(): Maybe<OptionalKind<PropertyDeclarationStructure>>;
21
+ get constructorParametersPropertySignature(): Maybe<OptionalKind<PropertySignatureStructure>>;
17
22
  get interfacePropertySignature(): OptionalKind<PropertySignatureStructure>;
23
+ get jsonPropertySignature(): OptionalKind<PropertySignatureStructure>;
18
24
  classConstructorStatements(): readonly string[];
25
+ fromJsonStatements(): readonly string[];
19
26
  fromRdfStatements(): readonly string[];
20
27
  hashStatements(): readonly string[];
21
- sparqlGraphPatternExpression(): Maybe<string>;
28
+ interfaceConstructorStatements(): readonly string[];
29
+ jsonUiSchemaElement({ variables, }: Parameters<Property<TypeDiscriminatorProperty.Type>["jsonUiSchemaElement"]>[0]): Maybe<string>;
30
+ jsonZodSchema({ variables, }: Parameters<Property<TypeDiscriminatorProperty.Type>["jsonZodSchema"]>[0]): ReturnType<Property<TypeDiscriminatorProperty.Type>["jsonZodSchema"]>;
31
+ sparqlConstructTemplateTriples(): readonly string[];
32
+ sparqlWherePatterns(): readonly string[];
33
+ toJsonObjectMember({ variables, }: Parameters<Property<TypeDiscriminatorProperty.Type>["toJsonObjectMember"]>[0]): string;
22
34
  toRdfStatements(): readonly string[];
23
35
  }
24
36
  export declare namespace TypeDiscriminatorProperty {
25
- interface Type {
26
- readonly name: string;
37
+ class Type {
38
+ readonly mutable: boolean;
39
+ readonly values: readonly string[];
40
+ constructor({ mutable, values, }: {
41
+ mutable: boolean;
42
+ values: readonly string[];
43
+ });
44
+ get name(): string;
27
45
  }
28
46
  }
29
47
  //# sourceMappingURL=TypeDiscriminatorProperty.d.ts.map
@@ -1,30 +1,52 @@
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";
2
10
  import { Property } from "./Property.js";
3
11
  export class TypeDiscriminatorProperty extends Property {
4
- constructor({ abstract, override, value, ...superParameters }) {
5
- super(superParameters);
12
+ constructor({ abstract, objectTypeDeclarationType, override, type, value, ...superParameters }) {
13
+ super({ ...superParameters, type });
6
14
  this.equalsFunction = "purifyHelpers.Equatable.strictEquals";
15
+ this.mutable = false;
16
+ invariant(this.visibility === "public");
7
17
  this.abstract = abstract;
18
+ this.objectTypeDeclarationType = objectTypeDeclarationType;
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,
@@ -34,20 +56,60 @@ export class TypeDiscriminatorProperty extends Property {
34
56
  classConstructorStatements() {
35
57
  return [];
36
58
  }
59
+ fromJsonStatements() {
60
+ return this.fromRdfStatements();
61
+ }
37
62
  fromRdfStatements() {
38
- return !this.abstract &&
39
- this.configuration.objectTypeDeclarationType === "interface"
63
+ return !this.abstract && this.objectTypeDeclarationType === "interface"
40
64
  ? [`const ${this.name} = "${this.value}" as const`]
41
65
  : [];
42
66
  }
43
67
  hashStatements() {
44
68
  return [];
45
69
  }
46
- sparqlGraphPatternExpression() {
47
- return Maybe.empty();
70
+ interfaceConstructorStatements() {
71
+ return !this.abstract
72
+ ? [`const ${this.name} = "${this.value}" as const`]
73
+ : [];
74
+ }
75
+ jsonUiSchemaElement({ variables, }) {
76
+ const scope = `\`\${${variables.scopePrefix}}/properties/${this.name}\``;
77
+ return Maybe.of(`{ rule: { condition: { schema: { const: "${this.value}" }, scope: ${scope} }, effect: "HIDE" }, scope: ${scope}, type: "Control" }`);
78
+ }
79
+ jsonZodSchema({ variables, }) {
80
+ return {
81
+ key: this.name,
82
+ schema: this.type.values.length > 1
83
+ ? `${variables.zod}.enum(${JSON.stringify(this.type.values)})`
84
+ : `${variables.zod}.literal("${this.type.values[0]}")`,
85
+ };
86
+ }
87
+ sparqlConstructTemplateTriples() {
88
+ return [];
89
+ }
90
+ sparqlWherePatterns() {
91
+ return [];
92
+ }
93
+ toJsonObjectMember({ variables, }) {
94
+ return `${this.name}: ${variables.value}`;
48
95
  }
49
96
  toRdfStatements() {
50
97
  return [];
51
98
  }
52
99
  }
100
+ (function (TypeDiscriminatorProperty) {
101
+ class Type {
102
+ constructor({ mutable, values, }) {
103
+ this.mutable = mutable;
104
+ this.values = values;
105
+ }
106
+ get name() {
107
+ return this.values.map((name) => `"${name}"`).join(" | ");
108
+ }
109
+ }
110
+ __decorate([
111
+ Memoize()
112
+ ], Type.prototype, "name", null);
113
+ TypeDiscriminatorProperty.Type = Type;
114
+ })(TypeDiscriminatorProperty || (TypeDiscriminatorProperty = {}));
53
115
  //# 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
@@ -0,0 +1,60 @@
1
+ import { Maybe } from "purify-ts";
2
+ import { invariant } from "ts-invariant";
3
+ import { StructureKind } from "ts-morph";
4
+ export function createFunctionDeclaration() {
5
+ if (!this.features.has("create")) {
6
+ return Maybe.empty();
7
+ }
8
+ if (this.declarationType !== "interface") {
9
+ return Maybe.empty();
10
+ }
11
+ if (this.extern) {
12
+ return Maybe.empty();
13
+ }
14
+ const parametersPropertySignatures = this.properties.flatMap((property) => property.constructorParametersPropertySignature
15
+ .map((propertySignature) => `readonly ${propertySignature.name}${propertySignature.hasQuestionToken ? "?" : ""}: ${propertySignature.type}`)
16
+ .toList());
17
+ // Should always have at least an identifier
18
+ invariant(parametersPropertySignatures.length > 0);
19
+ const parametersType = [`{ ${parametersPropertySignatures.join(", ")} }`]
20
+ .concat(this.parentObjectTypes.map((parentObjectType) => `Parameters<typeof ${parentObjectType.name}.create>[0]`))
21
+ .join(" & ");
22
+ const propertyInitializers = [];
23
+ const omitPropertyNames = [];
24
+ const propertyStatements = [];
25
+ for (const parentObjectType of this.parentObjectTypes) {
26
+ propertyInitializers.push(`...${parentObjectType.name}.create(parameters)`);
27
+ }
28
+ for (const property of this.properties) {
29
+ const thisPropertyStatements = property.interfaceConstructorStatements({
30
+ variables: { parameter: `parameters.${property.name}` },
31
+ });
32
+ if (thisPropertyStatements.length > 0) {
33
+ propertyInitializers.push(property.name);
34
+ propertyStatements.push(...thisPropertyStatements);
35
+ }
36
+ else {
37
+ omitPropertyNames.push(property.name);
38
+ }
39
+ }
40
+ invariant(propertyInitializers.length > 0);
41
+ invariant(propertyStatements.length > 0);
42
+ return Maybe.of({
43
+ isExported: true,
44
+ kind: StructureKind.Function,
45
+ name: "create",
46
+ parameters: [
47
+ {
48
+ name: "parameters",
49
+ type: parametersType,
50
+ },
51
+ ],
52
+ returnType: omitPropertyNames.length === 0
53
+ ? this.name
54
+ : `Omit<${this.name}, ${omitPropertyNames.map((omitPropertyName) => `"${omitPropertyName}"`).join(" | ")}>`,
55
+ statements: propertyStatements.concat([
56
+ `return { ${propertyInitializers.join(", ")} }`,
57
+ ]),
58
+ });
59
+ }
60
+ //# sourceMappingURL=createFunctionDeclaration.js.map
@@ -1,4 +1,5 @@
1
+ import { Maybe } from "purify-ts";
1
2
  import { type FunctionDeclarationStructure } from "ts-morph";
2
3
  import type { ObjectType } from "../ObjectType.js";
3
- export declare function equalsFunctionDeclaration(this: ObjectType): FunctionDeclarationStructure;
4
+ export declare function equalsFunctionDeclaration(this: ObjectType): Maybe<FunctionDeclarationStructure>;
4
5
  //# sourceMappingURL=equalsFunctionDeclaration.d.ts.map
@@ -1,27 +1,19 @@
1
+ import { Maybe } from "purify-ts";
1
2
  import { StructureKind } from "ts-morph";
3
+ import { equalsFunctionOrMethodDeclaration } from "./equalsFunctionOrMethodDeclaration.js";
2
4
  export function equalsFunctionDeclaration() {
3
- let expression = `purifyHelpers.Equatable.objectEquals(left, right, { ${this.properties
4
- .map((property) => `${property.name}: ${property.equalsFunction}`)
5
- .join()} })`;
6
- for (const parentObjectType of this.parentObjectTypes) {
7
- expression = `${parentObjectType.name}.equals(left, right).chain(() => ${expression})`;
5
+ if (this.declarationType !== "interface") {
6
+ return Maybe.empty();
8
7
  }
9
- return {
8
+ if (this.extern) {
9
+ return Maybe.empty();
10
+ }
11
+ return equalsFunctionOrMethodDeclaration
12
+ .bind(this)()
13
+ .map((equalsFunctionOrMethodDeclaration) => ({
14
+ ...equalsFunctionOrMethodDeclaration,
10
15
  isExported: true,
11
16
  kind: StructureKind.Function,
12
- name: "equals",
13
- parameters: [
14
- {
15
- name: "left",
16
- type: this.name,
17
- },
18
- {
19
- name: "right",
20
- type: this.name,
21
- },
22
- ],
23
- statements: [`return ${expression};`],
24
- returnType: "purifyHelpers.Equatable.EqualsResult",
25
- };
17
+ }));
26
18
  }
27
19
  //# sourceMappingURL=equalsFunctionDeclaration.js.map
@@ -0,0 +1,11 @@
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 equalsFunctionOrMethodDeclaration(this: ObjectType): Maybe<{
5
+ hasOverrideKeyword: boolean;
6
+ name: string;
7
+ parameters: OptionalKind<ParameterDeclarationStructure>[];
8
+ returnType: string;
9
+ statements: string[];
10
+ }>;
11
+ //# sourceMappingURL=equalsFunctionOrMethodDeclaration.d.ts.map
@@ -0,0 +1,70 @@
1
+ import { Maybe } from "purify-ts";
2
+ export function equalsFunctionOrMethodDeclaration() {
3
+ if (!this.features.has("equals")) {
4
+ return Maybe.empty();
5
+ }
6
+ if (this.declarationType === "class" && this.ownProperties.length === 0) {
7
+ // If there's a parent class and no properties in this class, can skip overriding equals
8
+ return Maybe.empty();
9
+ }
10
+ let leftVariable;
11
+ let rightVariable;
12
+ switch (this.declarationType) {
13
+ case "class":
14
+ leftVariable = "this";
15
+ rightVariable = "other";
16
+ break;
17
+ case "interface":
18
+ leftVariable = "left";
19
+ rightVariable = "right";
20
+ }
21
+ const chain = [];
22
+ let hasOverrideKeyword = false;
23
+ if (this.parentObjectTypes.length > 0) {
24
+ switch (this.declarationType) {
25
+ case "class": {
26
+ chain.push("super.equals(other)");
27
+ hasOverrideKeyword = true;
28
+ break;
29
+ }
30
+ case "interface": {
31
+ // For every parent, find the nearest equals implementation
32
+ for (const parentObjectType of this.parentObjectTypes) {
33
+ chain.push(`${parentObjectType.name}.equals(left, right)`);
34
+ }
35
+ break;
36
+ }
37
+ }
38
+ }
39
+ for (const property of this.ownProperties) {
40
+ chain.push(`(${property.equalsFunction})(${leftVariable}.${property.name}, ${rightVariable}.${property.name}).mapLeft(propertyValuesUnequal => ({ left: ${leftVariable}, right: ${rightVariable}, propertyName: "${property.name}", propertyValuesUnequal, type: "Property" as const }))`);
41
+ }
42
+ return Maybe.of({
43
+ hasOverrideKeyword,
44
+ name: "equals",
45
+ parameters: this.declarationType === "interface"
46
+ ? [
47
+ {
48
+ name: "left",
49
+ type: this.name,
50
+ },
51
+ {
52
+ name: "right",
53
+ type: this.name,
54
+ },
55
+ ]
56
+ : [
57
+ {
58
+ name: "other",
59
+ type: this.name,
60
+ },
61
+ ],
62
+ returnType: "purifyHelpers.Equatable.EqualsResult",
63
+ statements: [
64
+ `return ${chain
65
+ .map((chainPart, chainPartI) => chainPartI === 0 ? chainPart : `chain(() => ${chainPart})`)
66
+ .join(".")};`,
67
+ ],
68
+ });
69
+ }
70
+ //# sourceMappingURL=equalsFunctionOrMethodDeclaration.js.map
@@ -0,0 +1,4 @@
1
+ import { type FunctionDeclarationStructure } from "ts-morph";
2
+ import type { ObjectType } from "../ObjectType.js";
3
+ export declare function fromJsonFunctionDeclarations(this: ObjectType): readonly FunctionDeclarationStructure[];
4
+ //# sourceMappingURL=fromJsonFunctionDeclarations.d.ts.map