@shaclmate/compiler 3.0.2 → 3.0.4
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.
- package/dist/_ShapesGraphToAstTransformer/flattenAstObjectCompositeTypeMemberTypes.d.ts +1 -1
- package/dist/ast/ObjectCompositeType.d.ts +1 -1
- package/dist/generators/ts/BooleanType.js +2 -2
- package/dist/generators/ts/DateTimeType.js +2 -2
- package/dist/generators/ts/Import.js +22 -26
- package/dist/generators/ts/ListType.js +3 -3
- package/dist/generators/ts/NumberType.js +2 -2
- package/dist/generators/ts/ObjectType.js +2 -2
- package/dist/generators/ts/ObjectUnionType.js +2 -2
- package/dist/generators/ts/OptionType.js +4 -3
- package/dist/generators/ts/SetType.js +1 -1
- package/dist/generators/ts/StringType.js +2 -2
- package/dist/generators/ts/TermType.js +2 -2
- package/dist/generators/ts/TsGenerator.js +4 -20
- package/dist/generators/ts/Type.d.ts +2 -2
- package/dist/generators/ts/UnionType.js +7 -3
- package/dist/generators/ts/_ObjectType/ShaclProperty.js +1 -1
- package/dist/generators/ts/_ObjectType/rdfFunctionDeclarations.js +7 -1
- package/dist/generators/ts/unsupportedObjectSetMethodDeclarations.js +4 -4
- package/dist/input/generated.d.ts +71 -32
- package/dist/input/generated.js +1018 -506
- package/package.json +19 -6
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { TsFeature } from "enums/TsFeature.js";
|
|
2
1
|
import { Either } from "purify-ts";
|
|
3
2
|
import type * as ast from "../ast/index.js";
|
|
3
|
+
import type { TsFeature } from "../enums/TsFeature.js";
|
|
4
4
|
import type * as input from "../input/index.js";
|
|
5
5
|
export declare function flattenAstObjectCompositeTypeMemberTypes({ objectCompositeTypeKind, memberTypes, shape, }: {
|
|
6
6
|
objectCompositeTypeKind: "ObjectIntersectionType" | "ObjectUnionType";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { TsFeature } from "enums/TsFeature.js";
|
|
2
1
|
import type { Maybe } from "purify-ts";
|
|
2
|
+
import type { TsFeature } from "../enums/TsFeature.js";
|
|
3
3
|
import type { CompositeType } from "./CompositeType.js";
|
|
4
4
|
import type { Name } from "./Name.js";
|
|
5
5
|
import type { ObjectType } from "./ObjectType.js";
|
|
@@ -61,10 +61,10 @@ export class BooleanType extends PrimitiveType {
|
|
|
61
61
|
.map((defaultValue) => {
|
|
62
62
|
if (defaultValue) {
|
|
63
63
|
// If the default is true, only serialize the value if it's false
|
|
64
|
-
return
|
|
64
|
+
return `(!${variables.value} ? [false] : [])`;
|
|
65
65
|
}
|
|
66
66
|
// If the default is false, only serialize the value if it's true
|
|
67
|
-
return
|
|
67
|
+
return `(${variables.value} ? [true] : [])`;
|
|
68
68
|
})
|
|
69
69
|
.orDefault(variables.value);
|
|
70
70
|
}
|
|
@@ -80,8 +80,8 @@ export class DateTimeType extends PrimitiveType {
|
|
|
80
80
|
toRdfExpression({ variables, }) {
|
|
81
81
|
const valueToRdf = `rdfLiteral.toRdf(${variables.value}, ${objectInitializer({ dataFactory: "dataFactory", datatype: rdfjsTermExpression(this.xsdDatatype) })})`;
|
|
82
82
|
return this.primitiveDefaultValue
|
|
83
|
-
.map((defaultValue) =>
|
|
84
|
-
.orDefault(valueToRdf);
|
|
83
|
+
.map((defaultValue) => `(${variables.value}.getTime() !== ${defaultValue.getTime()} ? [${valueToRdf}] : [])`)
|
|
84
|
+
.orDefault(`[${valueToRdf}]`);
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
87
|
__decorate([
|
|
@@ -6,64 +6,60 @@ export var Import;
|
|
|
6
6
|
(function (Import) {
|
|
7
7
|
Import.DATA_FACTORY = {
|
|
8
8
|
kind: StructureKind.ImportDeclaration,
|
|
9
|
-
moduleSpecifier: "
|
|
10
|
-
namedImports: ["dataFactory"],
|
|
11
|
-
};
|
|
12
|
-
Import.DATASET_FACTORY = {
|
|
13
|
-
kind: StructureKind.ImportDeclaration,
|
|
14
|
-
moduleSpecifier: "@shaclmate/runtime",
|
|
15
|
-
namedImports: ["datasetFactory"],
|
|
9
|
+
moduleSpecifier: "n3",
|
|
10
|
+
namedImports: [{ alias: "dataFactory", name: "DataFactory" }],
|
|
16
11
|
};
|
|
12
|
+
Import.DATASET_FACTORY = "import { StoreFactory as _DatasetFactory } from 'n3'; const datasetFactory = new _DatasetFactory();";
|
|
17
13
|
Import.GRAPHQL = {
|
|
18
14
|
kind: StructureKind.ImportDeclaration,
|
|
19
|
-
moduleSpecifier: "
|
|
20
|
-
|
|
15
|
+
moduleSpecifier: "graphql",
|
|
16
|
+
namespaceImport: "graphql",
|
|
21
17
|
};
|
|
22
18
|
Import.GRAPHQL_SCALARS = {
|
|
23
19
|
kind: StructureKind.ImportDeclaration,
|
|
24
|
-
moduleSpecifier: "
|
|
25
|
-
|
|
20
|
+
moduleSpecifier: "graphql-scalars",
|
|
21
|
+
namespaceImport: "graphqlScalars",
|
|
26
22
|
};
|
|
27
23
|
Import.PURIFY = {
|
|
28
24
|
kind: StructureKind.ImportDeclaration,
|
|
29
|
-
moduleSpecifier: "
|
|
30
|
-
|
|
25
|
+
moduleSpecifier: "purify-ts",
|
|
26
|
+
namespaceImport: "purify",
|
|
31
27
|
};
|
|
32
28
|
Import.RDF_LITERAL = {
|
|
33
29
|
kind: StructureKind.ImportDeclaration,
|
|
34
|
-
moduleSpecifier: "
|
|
35
|
-
|
|
30
|
+
moduleSpecifier: "rdf-literal",
|
|
31
|
+
namespaceImport: "rdfLiteral",
|
|
36
32
|
};
|
|
37
33
|
Import.RDFJS_RESOURCE = {
|
|
38
34
|
kind: StructureKind.ImportDeclaration,
|
|
39
|
-
moduleSpecifier: "
|
|
40
|
-
|
|
35
|
+
moduleSpecifier: "rdfjs-resource",
|
|
36
|
+
namespaceImport: "rdfjsResource",
|
|
41
37
|
};
|
|
42
38
|
Import.RDFJS_TYPES = {
|
|
43
39
|
isTypeOnly: true,
|
|
44
40
|
kind: StructureKind.ImportDeclaration,
|
|
45
|
-
moduleSpecifier: "@
|
|
46
|
-
|
|
41
|
+
moduleSpecifier: "@rdfjs/types",
|
|
42
|
+
namespaceImport: "rdfjs",
|
|
47
43
|
};
|
|
48
44
|
Import.SHA256 = {
|
|
49
45
|
kind: StructureKind.ImportDeclaration,
|
|
50
|
-
moduleSpecifier: "
|
|
46
|
+
moduleSpecifier: "js-sha256",
|
|
51
47
|
namedImports: ["sha256"],
|
|
52
48
|
};
|
|
53
49
|
Import.SPARQLJS = {
|
|
54
50
|
kind: StructureKind.ImportDeclaration,
|
|
55
|
-
moduleSpecifier: "
|
|
56
|
-
|
|
51
|
+
moduleSpecifier: "sparqljs",
|
|
52
|
+
namespaceImport: "sparqljs",
|
|
57
53
|
};
|
|
58
54
|
Import.UUID = {
|
|
59
55
|
kind: StructureKind.ImportDeclaration,
|
|
60
|
-
moduleSpecifier: "
|
|
61
|
-
|
|
56
|
+
moduleSpecifier: "uuid",
|
|
57
|
+
namespaceImport: "uuid",
|
|
62
58
|
};
|
|
63
59
|
Import.ZOD = {
|
|
64
60
|
kind: StructureKind.ImportDeclaration,
|
|
65
|
-
moduleSpecifier: "
|
|
66
|
-
namedImports: ["zod"],
|
|
61
|
+
moduleSpecifier: "zod",
|
|
62
|
+
namedImports: [{ alias: "zod", name: "z" }],
|
|
67
63
|
};
|
|
68
64
|
})(Import || (Import = {}));
|
|
69
65
|
//# sourceMappingURL=Import.js.map
|
|
@@ -289,7 +289,7 @@ export class ListType extends Type {
|
|
|
289
289
|
break;
|
|
290
290
|
}
|
|
291
291
|
}
|
|
292
|
-
return
|
|
292
|
+
return `[${variables.value}.length > 0 ? ${variables.value}.reduce(({ currentSubListResource, listResource }, item, itemIndex, list) => {
|
|
293
293
|
if (itemIndex === 0) {
|
|
294
294
|
currentSubListResource = listResource;
|
|
295
295
|
} else {
|
|
@@ -302,7 +302,7 @@ export class ListType extends Type {
|
|
|
302
302
|
|
|
303
303
|
${this.toRdfTypes.map((rdfType) => `currentSubListResource.add(${rdfjsTermExpression(rdf.type)}, dataFactory.namedNode("${rdfType.value}"))`).join("\n")}
|
|
304
304
|
|
|
305
|
-
currentSubListResource.add(${rdfjsTermExpression(rdf.first)},
|
|
305
|
+
currentSubListResource.add(${rdfjsTermExpression(rdf.first)}, ...${this.itemType.toRdfExpression({ variables: { mutateGraph: variables.mutateGraph, predicate: rdfjsTermExpression(rdf.first), resource: "currentSubListResource", resourceSet: variables.resourceSet, value: "item" } })});
|
|
306
306
|
|
|
307
307
|
if (itemIndex + 1 === list.length) {
|
|
308
308
|
currentSubListResource.add(${rdfjsTermExpression(rdf.rest)}, ${rdfjsTermExpression(rdf.nil)});
|
|
@@ -319,7 +319,7 @@ export class ListType extends Type {
|
|
|
319
319
|
currentSubListResource: ${mutableResourceTypeName} | null;
|
|
320
320
|
listResource: ${mutableResourceTypeName};
|
|
321
321
|
},
|
|
322
|
-
).listResource.identifier : ${rdfjsTermExpression(rdf.nil)}`;
|
|
322
|
+
).listResource.identifier : ${rdfjsTermExpression(rdf.nil)}]`;
|
|
323
323
|
}
|
|
324
324
|
useImports(parameters) {
|
|
325
325
|
const imports = this.itemType.useImports(parameters).concat();
|
|
@@ -58,8 +58,8 @@ export class NumberType extends PrimitiveType {
|
|
|
58
58
|
}
|
|
59
59
|
toRdfExpression({ variables, }) {
|
|
60
60
|
return this.primitiveDefaultValue
|
|
61
|
-
.map((defaultValue) =>
|
|
62
|
-
.orDefault(variables.value);
|
|
61
|
+
.map((defaultValue) => `(${variables.value} !== ${defaultValue} ? [${variables.value}] : [])`)
|
|
62
|
+
.orDefault(`[${variables.value}]`);
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
__decorate([
|
|
@@ -322,9 +322,9 @@ export class ObjectType extends DeclaredType {
|
|
|
322
322
|
toRdfExpression({ variables, }) {
|
|
323
323
|
switch (this.declarationType) {
|
|
324
324
|
case "class":
|
|
325
|
-
return
|
|
325
|
+
return `[${variables.value}.${syntheticNamePrefix}toRdf({ mutateGraph: ${variables.mutateGraph}, resourceSet: ${variables.resourceSet} }).identifier]`;
|
|
326
326
|
case "interface":
|
|
327
|
-
return
|
|
327
|
+
return `[${this.staticModuleName}.${syntheticNamePrefix}toRdf(${variables.value}, { mutateGraph: ${variables.mutateGraph}, resourceSet: ${variables.resourceSet} }).identifier]`;
|
|
328
328
|
}
|
|
329
329
|
}
|
|
330
330
|
useImports() {
|
|
@@ -621,9 +621,9 @@ return ${syntheticNamePrefix}strictEquals(left.${syntheticNamePrefix}type, right
|
|
|
621
621
|
const options = `{ mutateGraph: ${variables.mutateGraph}, resourceSet: ${variables.resourceSet} }`;
|
|
622
622
|
switch (this.memberTypes[0].declarationType) {
|
|
623
623
|
case "class":
|
|
624
|
-
return
|
|
624
|
+
return `[${variables.value}.${syntheticNamePrefix}toRdf(${options}).identifier]`;
|
|
625
625
|
case "interface":
|
|
626
|
-
return
|
|
626
|
+
return `[${this.staticModuleName}.${syntheticNamePrefix}toRdf(${variables.value}, ${options}).identifier]`;
|
|
627
627
|
}
|
|
628
628
|
}
|
|
629
629
|
useImports() {
|
|
@@ -137,10 +137,11 @@ export class OptionType extends Type {
|
|
|
137
137
|
const itemTypeToRdfExpression = this.itemType.toRdfExpression({
|
|
138
138
|
variables: { ...variables, value: "value" },
|
|
139
139
|
});
|
|
140
|
-
|
|
141
|
-
|
|
140
|
+
let toRdfExpression = `${variables.value}.toList()`;
|
|
141
|
+
if (itemTypeToRdfExpression !== "[value]") {
|
|
142
|
+
toRdfExpression = `${toRdfExpression}.flatMap((value) => ${itemTypeToRdfExpression})`;
|
|
142
143
|
}
|
|
143
|
-
return
|
|
144
|
+
return toRdfExpression;
|
|
144
145
|
}
|
|
145
146
|
useImports(parameters) {
|
|
146
147
|
return [...this.itemType.useImports(parameters), Import.PURIFY];
|
|
@@ -166,7 +166,7 @@ export class SetType extends Type {
|
|
|
166
166
|
return `${variables.value}.map(item => (${this.itemType.toJsonExpression({ variables: { value: "item" } })}))`;
|
|
167
167
|
}
|
|
168
168
|
toRdfExpression({ variables, }) {
|
|
169
|
-
return `${variables.value}.
|
|
169
|
+
return `${variables.value}.flatMap((item) => ${this.itemType.toRdfExpression({
|
|
170
170
|
variables: { ...variables, value: "item" },
|
|
171
171
|
})})`;
|
|
172
172
|
}
|
|
@@ -67,8 +67,8 @@ export class StringType extends PrimitiveType {
|
|
|
67
67
|
}
|
|
68
68
|
toRdfExpression({ variables, }) {
|
|
69
69
|
return this.primitiveDefaultValue
|
|
70
|
-
.map((defaultValue) =>
|
|
71
|
-
.orDefault(variables.value);
|
|
70
|
+
.map((defaultValue) => `(${variables.value} !== "${defaultValue}" ? [${variables.value}] : [])`)
|
|
71
|
+
.orDefault(`[${variables.value}]`);
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
74
|
__decorate([
|
|
@@ -252,8 +252,8 @@ export class TermType extends Type {
|
|
|
252
252
|
}
|
|
253
253
|
toRdfExpression({ variables, }) {
|
|
254
254
|
return this.defaultValue
|
|
255
|
-
.map((defaultValue) =>
|
|
256
|
-
.orDefault(variables.value);
|
|
255
|
+
.map((defaultValue) => `(!${variables.value}.equals(${rdfjsTermExpression(defaultValue)}) ? [${variables.value}] : [])`)
|
|
256
|
+
.orDefault(`[${variables.value}]`);
|
|
257
257
|
}
|
|
258
258
|
useImports() {
|
|
259
259
|
const imports = [Import.RDFJS_TYPES];
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { invariant } from "ts-invariant";
|
|
2
1
|
import { Project, } from "ts-morph";
|
|
3
2
|
import * as ast from "../../ast/index.js";
|
|
4
3
|
import { Import } from "./Import.js";
|
|
@@ -40,32 +39,17 @@ export class TsGenerator {
|
|
|
40
39
|
}
|
|
41
40
|
// Deduplicate and add imports
|
|
42
41
|
const stringImports = new Set();
|
|
43
|
-
const
|
|
42
|
+
const structureImportsByModuleSpecifier = {};
|
|
44
43
|
for (const import_ of imports) {
|
|
45
44
|
if (typeof import_ === "string") {
|
|
46
45
|
stringImports.add(import_);
|
|
47
|
-
continue;
|
|
48
46
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
structureImports.push(import_);
|
|
52
|
-
continue;
|
|
53
|
-
}
|
|
54
|
-
// Merge named imports
|
|
55
|
-
invariant(!import_.namespaceImport);
|
|
56
|
-
invariant(Array.isArray(import_.namedImports) &&
|
|
57
|
-
import_.namedImports.every((value) => typeof value === "string"));
|
|
58
|
-
invariant(!importWithSameModuleSpecifier.namespaceImport);
|
|
59
|
-
invariant(Array.isArray(importWithSameModuleSpecifier.namedImports) &&
|
|
60
|
-
importWithSameModuleSpecifier.namedImports.every((value) => typeof value === "string"));
|
|
61
|
-
for (const newNamedImport of import_.namedImports) {
|
|
62
|
-
if (!importWithSameModuleSpecifier.namedImports.includes(newNamedImport)) {
|
|
63
|
-
importWithSameModuleSpecifier.namedImports.push(newNamedImport);
|
|
64
|
-
}
|
|
47
|
+
else {
|
|
48
|
+
structureImportsByModuleSpecifier[import_.moduleSpecifier] = import_;
|
|
65
49
|
}
|
|
66
50
|
}
|
|
67
51
|
sourceFile.addStatements([...stringImports]);
|
|
68
|
-
sourceFile.addStatements(
|
|
52
|
+
sourceFile.addStatements(Object.values(structureImportsByModuleSpecifier));
|
|
69
53
|
// Deduplicate and add snippet declarations
|
|
70
54
|
const addedSnippetDeclarations = new Set();
|
|
71
55
|
for (const declaredType of declaredTypes) {
|
|
@@ -184,8 +184,8 @@ export declare abstract class Type {
|
|
|
184
184
|
};
|
|
185
185
|
}): string;
|
|
186
186
|
/**
|
|
187
|
-
* An expression that converts a property value of this type to
|
|
188
|
-
*
|
|
187
|
+
* An expression that converts a property value of this type to an array of values that can be .add'd to a rdfjsResource.MutableResource
|
|
188
|
+
* (BlankNode | Literal | NamedNode | boolean | number | string)[].
|
|
189
189
|
*/
|
|
190
190
|
abstract toRdfExpression(parameters: {
|
|
191
191
|
variables: {
|
|
@@ -374,12 +374,12 @@ ${this.memberTypes
|
|
|
374
374
|
}
|
|
375
375
|
toRdfExpression({ variables, }) {
|
|
376
376
|
return this.ternaryExpression({
|
|
377
|
-
memberTypeExpression: (memberType) => memberType.toRdfExpression({
|
|
377
|
+
memberTypeExpression: (memberType) => `(${memberType.toRdfExpression({
|
|
378
378
|
variables: {
|
|
379
379
|
...variables,
|
|
380
380
|
value: memberType.payload(variables.value),
|
|
381
381
|
},
|
|
382
|
-
})
|
|
382
|
+
})} as readonly Parameters<rdfjsResource.MutableResource["add"]>[1][])`,
|
|
383
383
|
variables,
|
|
384
384
|
});
|
|
385
385
|
}
|
|
@@ -400,9 +400,13 @@ ${this.memberTypes
|
|
|
400
400
|
if (expression.length === 0) {
|
|
401
401
|
return memberTypeExpression(memberType);
|
|
402
402
|
}
|
|
403
|
+
const memberTypeExpression_ = memberTypeExpression(memberType);
|
|
404
|
+
if (memberTypeExpression_ === expression) {
|
|
405
|
+
return expression;
|
|
406
|
+
}
|
|
403
407
|
return `(${memberType.discriminatorValues
|
|
404
408
|
.map((value) => `${this.discriminatorVariable(variables.value)} === "${value}"`)
|
|
405
|
-
.join(" || ")}) ? ${
|
|
409
|
+
.join(" || ")}) ? ${memberTypeExpression_} : ${expression}`;
|
|
406
410
|
}, "");
|
|
407
411
|
}
|
|
408
412
|
}
|
|
@@ -183,7 +183,7 @@ export class ShaclProperty extends Property {
|
|
|
183
183
|
}
|
|
184
184
|
toRdfStatements({ variables, }) {
|
|
185
185
|
return [
|
|
186
|
-
`${variables.resource}.add(${this.predicate},
|
|
186
|
+
`${variables.resource}.add(${this.predicate}, ...${this.type.toRdfExpression({
|
|
187
187
|
variables: { ...variables, predicate: this.predicate },
|
|
188
188
|
})});`,
|
|
189
189
|
];
|
|
@@ -71,6 +71,12 @@ function propertiesFromRdfFunctionDeclaration() {
|
|
|
71
71
|
this.fromRdfType.ifJust((fromRdfType) => {
|
|
72
72
|
const fromRdfTypeVariable = this.fromRdfTypeVariable.unsafeCoerce();
|
|
73
73
|
const predicate = rdfjsTermExpression(rdf.type);
|
|
74
|
+
// Check the expected type and its known subtypes
|
|
75
|
+
const cases = new Set();
|
|
76
|
+
cases.add(fromRdfType.value);
|
|
77
|
+
for (const descendantFromRdfType of this.descendantFromRdfTypes) {
|
|
78
|
+
cases.add(descendantFromRdfType.value);
|
|
79
|
+
}
|
|
74
80
|
statements.push(`\
|
|
75
81
|
if (!${variables.ignoreRdfType}) {
|
|
76
82
|
const ${syntheticNamePrefix}rdfTypeCheck: purify.Either<Error, true> = ${variables.resource}.value(${predicate})
|
|
@@ -78,7 +84,7 @@ if (!${variables.ignoreRdfType}) {
|
|
|
78
84
|
.chain((actualRdfType) => {
|
|
79
85
|
// Check the expected type and its known subtypes
|
|
80
86
|
switch (actualRdfType.value) {
|
|
81
|
-
${[
|
|
87
|
+
${[...cases].map((fromRdfType) => `case "${fromRdfType}":`).join("\n")}
|
|
82
88
|
return purify.Either.of(true);
|
|
83
89
|
}
|
|
84
90
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { StructureKind } from "ts-morph";
|
|
2
2
|
import { objectSetMethodSignatures } from "./objectSetMethodSignatures.js";
|
|
3
3
|
export function unsupportedObjectSetMethodDeclarations({ objectType, }) {
|
|
4
|
-
return Object.
|
|
4
|
+
return Object.values(objectSetMethodSignatures({ objectType })).map((methodSignature) => ({
|
|
5
5
|
...methodSignature,
|
|
6
6
|
kind: StructureKind.Method,
|
|
7
7
|
parameters: methodSignature.parameters
|
|
@@ -11,9 +11,9 @@ export function unsupportedObjectSetMethodDeclarations({ objectType, }) {
|
|
|
11
11
|
}))
|
|
12
12
|
: methodSignature.parameters,
|
|
13
13
|
isAsync: true,
|
|
14
|
-
statements:
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
statements: [
|
|
15
|
+
`return purify.Left(new Error("${methodSignature.name}: not supported")) satisfies Awaited<${methodSignature.returnType}>;`,
|
|
16
|
+
],
|
|
17
17
|
}));
|
|
18
18
|
}
|
|
19
19
|
//# sourceMappingURL=unsupportedObjectSetMethodDeclarations.js.map
|