@shaclmate/compiler 4.0.3 → 4.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.
@@ -1,5 +1,5 @@
1
1
  import { Either } from "purify-ts";
2
- import type { TsFeature } from "../enums/TsFeature.js";
2
+ import { type TsFeature } from "../enums/TsFeature.js";
3
3
  import type * as input from "../input/index.js";
4
4
  import type { ShapesGraphToAstTransformer } from "../ShapesGraphToAstTransformer.js";
5
5
  export declare function nodeShapeTsFeatures(this: ShapesGraphToAstTransformer, nodeShape: input.NodeShape): Either<Error, ReadonlySet<TsFeature>>;
@@ -1,19 +1,11 @@
1
1
  import { Either } from "purify-ts";
2
- const tsFeaturesAll = [
3
- "create",
4
- "equals",
5
- "graphql",
6
- "hash",
7
- "json",
8
- "rdf",
9
- "sparql",
10
- ];
2
+ import { TS_FEATURES } from "../enums/TsFeature.js";
11
3
  export function nodeShapeTsFeatures(nodeShape) {
12
4
  const tsFeaturesDefault = this.tsFeaturesDefault;
13
5
  function iriToTsFeatures(iri) {
14
6
  switch (iri.value) {
15
7
  case "http://purl.org/shaclmate/ontology#_TsFeatures_All":
16
- return tsFeaturesAll;
8
+ return TS_FEATURES;
17
9
  case "http://purl.org/shaclmate/ontology#_TsFeature_Create":
18
10
  return ["create"];
19
11
  case "http://purl.org/shaclmate/ontology#_TsFeatures_Default":
@@ -1,2 +1,3 @@
1
- export type TsFeature = "create" | "equals" | "graphql" | "hash" | "json" | "rdf" | "sparql";
1
+ export type TsFeature = (typeof TS_FEATURES)[number];
2
+ export declare const TS_FEATURES: readonly ["create", "equals", "graphql", "hash", "json", "rdf", "sparql"];
2
3
  //# sourceMappingURL=TsFeature.d.ts.map
@@ -1,2 +1,10 @@
1
- export {};
1
+ export const TS_FEATURES = [
2
+ "create",
3
+ "equals",
4
+ "graphql",
5
+ "hash",
6
+ "json",
7
+ "rdf",
8
+ "sparql",
9
+ ];
2
10
  //# sourceMappingURL=TsFeature.js.map
@@ -0,0 +1,6 @@
1
+ import * as ast from "../ast/index.js";
2
+ /**
3
+ * Code-safe name from an AST construct such as an ast.ObjectType.
4
+ */
5
+ export declare function codeName(sanitize: (unsanitized: string) => string, syntheticNamePrefix: string): (astConstruct: ast.ObjectType | ast.ObjectUnionType | ast.ObjectType.Property) => string;
6
+ //# sourceMappingURL=codeName.d.ts.map
@@ -0,0 +1,66 @@
1
+ import { Resource } from "rdfjs-resource";
2
+ import * as ast from "../ast/index.js";
3
+ /**
4
+ * Code-safe name from an AST construct such as an ast.ObjectType.
5
+ */
6
+ export function codeName(sanitize, syntheticNamePrefix) {
7
+ return (astConstruct) => {
8
+ // The order of checks determines the order of preference.
9
+ // Explicit shaclmate:name or sh:name
10
+ const name = astConstruct.name.extract();
11
+ if (name) {
12
+ if (astConstruct instanceof ast.ObjectType && astConstruct.synthetic) {
13
+ return `${syntheticNamePrefix}${name}`;
14
+ }
15
+ return sanitize(name);
16
+ }
17
+ // Explicit rdfs:label
18
+ const label = astConstruct.label.extract();
19
+ if (label) {
20
+ return sanitize(label.replace(" ", "_"));
21
+ }
22
+ let propertyPath;
23
+ if (astConstruct instanceof ast.ObjectType.Property) {
24
+ // Pick up the common pattern of a property shape identifier being the node shape's identifier -localName,
25
+ // like ex:NodeShape-property
26
+ if (astConstruct.objectType.shapeIdentifier.termType === "NamedNode" &&
27
+ astConstruct.shapeIdentifier.termType === "NamedNode") {
28
+ const propertyShapeIdentifierPrefix = `${astConstruct.objectType.shapeIdentifier.value}-`;
29
+ if (astConstruct.shapeIdentifier.value.startsWith(propertyShapeIdentifierPrefix) &&
30
+ astConstruct.shapeIdentifier.value.length >
31
+ propertyShapeIdentifierPrefix.length) {
32
+ return sanitize(astConstruct.shapeIdentifier.value.substring(propertyShapeIdentifierPrefix.length));
33
+ }
34
+ }
35
+ propertyPath = astConstruct.path;
36
+ }
37
+ // Unique reference part on a CURIE sh:path
38
+ if (propertyPath instanceof ast.Curie && propertyPath.hasUniqueReference) {
39
+ return sanitize(propertyPath.reference);
40
+ }
41
+ const shapeIdentifier = astConstruct.shapeIdentifier;
42
+ // Unique reference part on a CURIE shape identifier
43
+ if (shapeIdentifier instanceof ast.Curie &&
44
+ shapeIdentifier.hasUniqueReference) {
45
+ return sanitize(shapeIdentifier.reference);
46
+ }
47
+ // CURIE sh:path
48
+ if (propertyPath instanceof ast.Curie) {
49
+ return sanitize(`${propertyPath.prefix}_${propertyPath.reference}`);
50
+ }
51
+ // CURIE shape identifier
52
+ if (shapeIdentifier instanceof ast.Curie) {
53
+ return sanitize(`${shapeIdentifier.prefix}_${shapeIdentifier.reference}`);
54
+ }
55
+ // IRI shape identifier
56
+ if (shapeIdentifier.termType === "NamedNode") {
57
+ return sanitize(shapeIdentifier.value);
58
+ }
59
+ // IRI sh:path
60
+ if (propertyPath?.termType === "NamedNode") {
61
+ return sanitize(propertyPath.value);
62
+ }
63
+ throw new Error(`should never reach this point (shapeIdentifier=${Resource.Identifier.toString(shapeIdentifier)})`);
64
+ };
65
+ }
66
+ //# sourceMappingURL=codeName.js.map
@@ -1,9 +1,12 @@
1
1
  import TermMap from "@rdfjs/term-map";
2
2
  import TermSet from "@rdfjs/term-set";
3
+ import base62 from "@sindresorhus/base62";
3
4
  import { rdf, xsd } from "@tpluscode/rdf-ns-builders";
4
5
  import { LiteralDecoder, literalDatatypeDefinitions } from "rdfjs-resource";
6
+ import reservedTsIdentifiers_ from "reserved-identifiers";
5
7
  import { invariant } from "ts-invariant";
6
8
  import { logger } from "../../logger.js";
9
+ import { codeName } from "../codeName.js";
7
10
  import { BigDecimalType } from "./BigDecimalType.js";
8
11
  import { BigIntType } from "./BigIntType.js";
9
12
  import { BlankNodeType } from "./BlankNodeType.js";
@@ -28,8 +31,18 @@ import { StringType } from "./StringType.js";
28
31
  import { syntheticNamePrefix } from "./syntheticNamePrefix.js";
29
32
  import { TermType } from "./TermType.js";
30
33
  import { code } from "./ts-poet-wrapper.js";
31
- import { tsName } from "./tsName.js";
32
34
  import { UnionType } from "./UnionType.js";
35
+ const reservedTsIdentifiers = reservedTsIdentifiers_({
36
+ includeGlobalProperties: true,
37
+ });
38
+ const tsName = codeName((value) => {
39
+ // Adapted from https://github.com/sindresorhus/to-valid-identifier , MIT license
40
+ if (reservedTsIdentifiers.has(value)) {
41
+ // We prefix with underscore to avoid any potential conflicts with the Base62 encoded string.
42
+ return `$_${value}$`;
43
+ }
44
+ return value.replaceAll(/\P{ID_Continue}/gu, (x) => `$${base62.encodeInteger(x.codePointAt(0))}$`);
45
+ }, syntheticNamePrefix);
33
46
  export class TypeFactory {
34
47
  cachedObjectTypePropertiesByShapeIdentifier = new TermMap();
35
48
  cachedObjectTypesByShapeIdentifier = new TermMap();
@@ -13,7 +13,7 @@ export declare class MemberType {
13
13
  get declarationType(): import("../../../enums/TsObjectDeclarationType.js").TsObjectDeclarationType;
14
14
  get descendantFromRdfTypeVariables(): readonly import("ts-poet").Code[];
15
15
  get discriminantPropertyValues(): readonly string[];
16
- get features(): ReadonlySet<import("../../../enums/TsFeature.js").TsFeature>;
16
+ get features(): ReadonlySet<"create" | "equals" | "graphql" | "hash" | "json" | "rdf" | "sparql">;
17
17
  get filterFunction(): import("ts-poet").Code;
18
18
  get filterType(): import("ts-poet").Code;
19
19
  get fromRdfType(): import("purify-ts").Maybe<import("@rdfjs/types").NamedNode<string>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "dependencies": {
3
- "@shaclmate/shacl-ast": "4.0.3",
3
+ "@shaclmate/shacl-ast": "4.0.4",
4
4
  "@rdfjs/data-model": "~2.1.1",
5
5
  "@rdfjs/dataset": "~2.0.2",
6
6
  "@rdfjs/prefix-map": "~0.1.2",
@@ -74,10 +74,11 @@
74
74
  "depcheck": "depcheck .",
75
75
  "dev": "tsc -w --preserveWatchOutput",
76
76
  "dev:tests": "tsc -p __tests__ -w --preserveWatchOutput",
77
+ "test": "cd ../.. && vitest run --project @shaclmate/compiler",
77
78
  "typecheck": "tsc --noEmit",
78
79
  "typecheck:watch": "tsc --noEmit -w --preserveWatchOutput"
79
80
  },
80
81
  "type": "module",
81
82
  "types": "./dist/index.d.ts",
82
- "version": "4.0.3"
83
+ "version": "4.0.4"
83
84
  }
@@ -1,2 +0,0 @@
1
- export declare function stringToValidTsIdentifier(value: string): string;
2
- //# sourceMappingURL=stringToValidTsIdentifier.d.ts.map
@@ -1,14 +0,0 @@
1
- import base62 from "@sindresorhus/base62";
2
- import reservedTsIdentifiers_ from "reserved-identifiers";
3
- const reservedTsIdentifiers = reservedTsIdentifiers_({
4
- includeGlobalProperties: true,
5
- });
6
- // Adapted from https://github.com/sindresorhus/to-valid-identifier , MIT license
7
- export function stringToValidTsIdentifier(value) {
8
- if (reservedTsIdentifiers.has(value)) {
9
- // We prefix with underscore to avoid any potential conflicts with the Base62 encoded string.
10
- return `$_${value}$`;
11
- }
12
- return value.replaceAll(/\P{ID_Continue}/gu, (x) => `$${base62.encodeInteger(x.codePointAt(0))}$`);
13
- }
14
- //# sourceMappingURL=stringToValidTsIdentifier.js.map
@@ -1,12 +0,0 @@
1
- import type { BlankNode, NamedNode } from "@rdfjs/types";
2
- import type { Maybe } from "purify-ts";
3
- import { type PropertyPath } from "rdfjs-resource";
4
- import * as ast from "../../ast/index.js";
5
- export declare function tsName(astConstruct: {
6
- label: Maybe<string>;
7
- name: Maybe<string>;
8
- path?: ast.Curie | PropertyPath;
9
- synthetic?: boolean;
10
- shapeIdentifier: BlankNode | NamedNode;
11
- }): string;
12
- //# sourceMappingURL=tsName.d.ts.map
@@ -1,34 +0,0 @@
1
- import { Resource } from "rdfjs-resource";
2
- import * as ast from "../../ast/index.js";
3
- import { stringToValidTsIdentifier } from "./stringToValidTsIdentifier.js";
4
- import { syntheticNamePrefix } from "./syntheticNamePrefix.js";
5
- export function tsName(astConstruct) {
6
- const name = astConstruct.name.extract();
7
- if (name) {
8
- if (astConstruct.synthetic) {
9
- return `${syntheticNamePrefix}${name}`;
10
- }
11
- return stringToValidTsIdentifier(name);
12
- }
13
- const label = astConstruct.label.extract();
14
- if (label) {
15
- return stringToValidTsIdentifier(label.replace(" ", "_"));
16
- }
17
- const path = astConstruct.path;
18
- if (path instanceof ast.Curie) {
19
- if (path.hasUniqueReference) {
20
- return stringToValidTsIdentifier(path.reference);
21
- }
22
- return stringToValidTsIdentifier(`${path.prefix}_${path.reference}`);
23
- }
24
- const shapeIdentifier = astConstruct.shapeIdentifier;
25
- if (shapeIdentifier.termType === "NamedNode" &&
26
- shapeIdentifier instanceof ast.Curie) {
27
- if (shapeIdentifier.hasUniqueReference) {
28
- return stringToValidTsIdentifier(shapeIdentifier.reference);
29
- }
30
- return stringToValidTsIdentifier(`${shapeIdentifier.prefix}_${shapeIdentifier.reference}`);
31
- }
32
- throw new Error(`should never reach this point (shapeIdentifier=${Resource.Identifier.toString(shapeIdentifier)})`);
33
- }
34
- //# sourceMappingURL=tsName.js.map