@traqula/generator-sparql-1-2 0.0.20 → 0.0.22

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.
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var lib_exports = {};
20
+ __export(lib_exports, {
21
+ Generator: () => Generator,
22
+ sparql12GeneratorBuilder: () => sparql12GeneratorBuilder
23
+ });
24
+ module.exports = __toCommonJS(lib_exports);
25
+ var import_core = require("@traqula/core");
26
+ var import_generator_sparql_1_1 = require("@traqula/generator-sparql-1-1");
27
+ var import_rules_sparql_1_2 = require("@traqula/rules-sparql-1-2");
28
+ const sparql12GeneratorBuilder = import_core.GeneratorBuilder.create(import_generator_sparql_1_1.sparql11GeneratorBuilder).widenContext().typePatch().addRule(import_rules_sparql_1_2.gram.tripleTerm).addRule(import_rules_sparql_1_2.gram.reifiedTriple).patchRule(import_rules_sparql_1_2.gram.graphNodePath).addRule(import_rules_sparql_1_2.gram.annotationBlockPath).addRule(import_rules_sparql_1_2.gram.annotationPath).addRule(import_rules_sparql_1_2.gram.versionDecl).patchRule(import_rules_sparql_1_2.gram.prologue).patchRule(import_rules_sparql_1_2.gram.generateTriplesBlock).patchRule(import_rules_sparql_1_2.gram.generateGraphTerm);
29
+ class Generator {
30
+ defaultContext;
31
+ constructor(defaultContext = {}) {
32
+ this.defaultContext = defaultContext;
33
+ }
34
+ generator = sparql12GeneratorBuilder.build();
35
+ F = new import_rules_sparql_1_2.AstFactory();
36
+ generate(ast, context = {}) {
37
+ return this.generator.queryOrUpdate(ast, (0, import_rules_sparql_1_2.completeParseContext)({ ...this.defaultContext, ...context })).trim();
38
+ }
39
+ generatePath(ast, context = {}) {
40
+ return this.generator.path(ast, (0, import_rules_sparql_1_2.completeParseContext)({ ...this.defaultContext, ...context }), void 0).trim();
41
+ }
42
+ }
43
+ // Annotate the CommonJS export names for ESM import in node:
44
+ 0 && (module.exports = {
45
+ Generator,
46
+ sparql12GeneratorBuilder
47
+ });
@@ -0,0 +1,150 @@
1
+ "use strict";
2
+ var import_parser_sparql_1_2 = require("@traqula/parser-sparql-1-2");
3
+ var import_rules_sparql_1_1 = require("@traqula/rules-sparql-1-1");
4
+ var import_rules_sparql_1_2 = require("@traqula/rules-sparql-1-2");
5
+ var import_vitest = require("vitest");
6
+ var import_lib = require("../lib/index.js");
7
+ (0, import_vitest.describe)("a SPARQL 1.2 generator", () => {
8
+ const generator = new import_lib.Generator();
9
+ const parser = new import_parser_sparql_1_2.Parser();
10
+ const F = new import_rules_sparql_1_2.AstFactory();
11
+ const transformer = new import_rules_sparql_1_1.AstTransformer();
12
+ (0, import_vitest.it)("generates simple round tripped", ({ expect }) => {
13
+ const query = "SELECT * WHERE { ?s ?p ?o }";
14
+ const ast = parser.parse(query);
15
+ const result = generator.generate(ast, { origSource: query });
16
+ expect(result).toBe(query);
17
+ });
18
+ (0, import_vitest.describe)("on altered nodes", () => {
19
+ (0, import_vitest.it)("translates ?s -> ?subject", ({ expect }) => {
20
+ const query = "SELECT * WHERE { ?s ?p ?o }";
21
+ const ast = parser.parse(query);
22
+ const altered = transformer.transformNodeSpecific(ast, {}, { term: { variable: {
23
+ transform: (variable) => variable.value === "s" ? F.termVariable("subject", F.sourceLocationNodeReplaceUnsafe(variable.loc)) : variable
24
+ } } });
25
+ const result = generator.generate(altered, { origSource: query });
26
+ expect(result).toBe(`SELECT * WHERE { ?subject ?p ?o }`);
27
+ });
28
+ (0, import_vitest.it)("translates blanknodes -> variables", ({ expect }) => {
29
+ const query = `BASE <ex:>
30
+ CONSTRUCT {
31
+ ?s0 ?p0 _:g_0 .
32
+ _:g_0 <a0> _:g_1 .
33
+ _:g_1 <b0><c0> .
34
+ [
35
+ <a0> [
36
+ <b0> <c0> ;
37
+ ] ;
38
+ ] ?p0 ?o0 .
39
+
40
+ }
41
+ WHERE {
42
+ ?s1 ?p1 [], <a1>; ?q1 <b1>, <c1>.
43
+ [] ?p2 ?o2.
44
+ ?s3 ?p3 [ <a3> <b3> ].
45
+ ?s4 ?p4 [ <a4> <b4>; <c4> <d4>, <e4> ].
46
+ ?s5 ?p5 [ <a5> [ <b5> <c5> ] ].
47
+ [ <a6> [ <b6> <c6> ] ] ?p6 ?o6.
48
+ [ <a7> <b7>; <c7> <d7>, <e7> ].
49
+ }`;
50
+ const ast = parser.parse(query);
51
+ function extractCollection(collection) {
52
+ const result2 = [];
53
+ for (const entry of collection.triples) {
54
+ const subject = entry.subject;
55
+ const pred = entry.predicate;
56
+ if (F.isTripleCollection(entry.object)) {
57
+ const identifier = { ...F.graphNodeIdentifier(entry.object) };
58
+ result2.push(F.annotatedTriple(subject, pred, identifier, void 0, F.gen()));
59
+ result2.push(...extractCollection(entry.object));
60
+ } else {
61
+ result2.push(F.annotatedTriple(subject, pred, entry.object, void 0, F.gen()));
62
+ }
63
+ }
64
+ return result2;
65
+ }
66
+ const flattenCollections = transformer.transformNodeSpecific(ast, {}, { pattern: { bgp: {
67
+ transform: (current) => {
68
+ const bgpCopy = F.forcedAutoGenTree(current);
69
+ const newTriples = [];
70
+ for (const entry of bgpCopy.triples) {
71
+ if (F.isTriple(entry)) {
72
+ const subject = entry.subject;
73
+ const pred = entry.predicate;
74
+ if (F.isTripleCollection(entry.object)) {
75
+ const object = entry.object;
76
+ const identifier = { ...F.graphNodeIdentifier(object) };
77
+ newTriples.push(F.annotatedTriple(subject, pred, identifier));
78
+ newTriples.push(...extractCollection(object));
79
+ } else {
80
+ newTriples.push(F.annotatedTriple(subject, pred, entry.object, void 0, F.gen()));
81
+ }
82
+ } else {
83
+ const genTriples = extractCollection(entry);
84
+ newTriples.push(...genTriples);
85
+ }
86
+ }
87
+ return F.patternBgp(newTriples, F.sourceLocationNodeReplaceUnsafe(current.loc));
88
+ }
89
+ } } });
90
+ const result = generator.generate(flattenCollections, { origSource: query });
91
+ expect(result).toBe(`BASE <ex:>
92
+ CONSTRUCT {
93
+ ?s0 ?p0 _:g_0 .
94
+ _:g_0 <a0> _:g_1 .
95
+ _:g_1 <b0> <c0> .
96
+ [
97
+ <a0> [
98
+ <b0> <c0> ;
99
+ ] ;
100
+ ] ?p0 ?o0 .
101
+
102
+
103
+ }
104
+ WHERE {
105
+ ?s1 ?p1 _:g_2 .
106
+ ?s1 ?p1 <a1> .
107
+ ?s1 ?q1 <b1> .
108
+ ?s1 ?q1 <c1> .
109
+ _:g_3 ?p2 ?o2 .
110
+ ?s3 ?p3 _:g_4 .
111
+ _:g_4 <a3> <b3> .
112
+ ?s4 ?p4 _:g_5 .
113
+ _:g_5 <a4> <b4> .
114
+ _:g_5 <c4> <d4> .
115
+ _:g_5 <c4> <e4> .
116
+ ?s5 ?p5 _:g_6 .
117
+ _:g_6 <a5> _:g_7 .
118
+ _:g_7 <b5> <c5> .
119
+ [
120
+ <a6> [
121
+ <b6> <c6> ;
122
+ ] ;
123
+ ] ?p6 ?o6 .
124
+ _:g_10 <a7> <b7> .
125
+ _:g_10 <c7> <d7> .
126
+ _:g_10 <c7> <e7> .
127
+
128
+ }`);
129
+ });
130
+ });
131
+ (0, import_vitest.it)("generates hand constructed query", ({ expect }) => {
132
+ const query = `
133
+ SELECT * WHERE {
134
+ ?s ?p ?o .
135
+ }`;
136
+ const ast = F.querySelect({
137
+ variables: [F.wildcard(F.gen())],
138
+ datasets: F.datasetClauses([], F.sourceLocation()),
139
+ context: [],
140
+ where: F.patternGroup([
141
+ F.patternBgp([
142
+ F.triple(F.termVariable("s", F.gen()), F.termVariable("p", F.gen()), F.termVariable("o", F.gen()))
143
+ ], F.gen())
144
+ ], F.gen()),
145
+ solutionModifiers: {}
146
+ }, F.gen());
147
+ const result = generator.generate(ast);
148
+ expect(result.trim()).toBe(query.trim());
149
+ });
150
+ });
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (let key of __getOwnPropNames(from))
11
+ if (!__hasOwnProp.call(to, key) && key !== except)
12
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
+ }
14
+ return to;
15
+ };
16
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
+ // If the importer is in node compatibility mode or this is not an ESM
18
+ // file that has been converted to a CommonJS file using a Babel-
19
+ // compatible transform (i.e. "__esModule" has not been set), then set
20
+ // "default" to the CommonJS "module.exports" for node compatibility.
21
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
+ mod
23
+ ));
24
+ var import_node_fs = __toESM(require("node:fs"), 1);
25
+ var import_node_path = __toESM(require("node:path"), 1);
26
+ var import_rules_sparql_1_2 = require("@traqula/rules-sparql-1-2");
27
+ var import_test_utils = require("@traqula/test-utils");
28
+ var import_vitest = require("vitest");
29
+ var import_lib = require("../lib/index.js");
30
+ (0, import_vitest.describe)("a SPARQL 1.2 generator", () => {
31
+ const generator = new import_lib.Generator();
32
+ const F = new import_rules_sparql_1_2.AstFactory();
33
+ function _sinkGenerated(suite, test, response) {
34
+ const dir = (0, import_test_utils.getStaticFilePath)();
35
+ const fileLoc = import_node_path.default.join(dir, suite, `${test}-generated.sparql`);
36
+ import_node_fs.default.writeFileSync(fileLoc, response);
37
+ }
38
+ (0, import_vitest.describe)("positive paths", () => {
39
+ for (const { name, statics } of (0, import_test_utils.positiveTest)("paths")) {
40
+ (0, import_vitest.it)(`can parse ${name}`, async ({ expect }) => {
41
+ const { query, ast, autoGen } = await statics();
42
+ const path2 = ast;
43
+ const generated = generator.generatePath(path2, { origSource: query });
44
+ expect(generated, "round tripped generation").toEqual(query.trim());
45
+ const replaceLoc = F.sourceLocationNodeReplaceUnsafe(path2.loc);
46
+ const autoGenAst = F.forcedAutoGenTree(path2);
47
+ autoGenAst.loc = replaceLoc;
48
+ const selfGenerated = generator.generatePath(autoGenAst);
49
+ expect(selfGenerated, "auto generated").toEqual(autoGen.trim());
50
+ });
51
+ }
52
+ });
53
+ (0, import_vitest.describe)("positive sparql 1.1", () => {
54
+ for (const { name, statics } of (0, import_test_utils.positiveTest)("sparql-1-1")) {
55
+ (0, import_vitest.it)(`can parse ${name}`, async ({ expect }) => {
56
+ const { query, ast, autoGen } = await statics();
57
+ const queryUpdate = ast;
58
+ const roundTripped = generator.generate(queryUpdate, { origSource: query });
59
+ expect(roundTripped, "round-tripped generation").toEqual(query.trim());
60
+ const replaceLoc = F.sourceLocationNodeReplaceUnsafe(queryUpdate.loc);
61
+ const autoGenAst = F.forcedAutoGenTree(queryUpdate);
62
+ autoGenAst.loc = replaceLoc;
63
+ const selfGenerated = generator.generate(autoGenAst);
64
+ expect(selfGenerated, "auto generated").toEqual(autoGen.trim());
65
+ });
66
+ }
67
+ });
68
+ (0, import_vitest.describe)("positive sparql 1.2", () => {
69
+ for (const { name, statics } of (0, import_test_utils.positiveTest)("sparql-1-2")) {
70
+ (0, import_vitest.it)(`can parse ${name}`, async ({ expect }) => {
71
+ const { query, ast, autoGen } = await statics();
72
+ const queryUpdate = ast;
73
+ const roundTripped = generator.generate(queryUpdate, { origSource: query });
74
+ expect(roundTripped, "round-tripped generation").toEqual(query.trim());
75
+ const replaceLoc = F.sourceLocationNodeReplaceUnsafe(queryUpdate.loc);
76
+ const autoGenAst = F.forcedAutoGenTree(queryUpdate);
77
+ autoGenAst.loc = replaceLoc;
78
+ const selfGenerated = generator.generate(autoGenAst);
79
+ expect(selfGenerated, "auto generated").toEqual(autoGen.trim());
80
+ });
81
+ }
82
+ });
83
+ });
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEjD,OAAO,EAAE,wBAAwB,EAAE,MAAM,+BAA+B,CAAC;AAIzE,OAAO,EAAE,oBAAoB,EAAE,UAAU,EAAE,IAAI,IAAI,GAAG,EAAE,MAAM,2BAA2B,CAAC;AAG1F,MAAM,CAAC,MAAM,wBAAwB,GACnC,gBAAgB,CAAC,MAAM,CAAC,wBAAwB,CAAC;KAC9C,YAAY,EAA8B;KAC1C,SAAS,EA6EN;KACH,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC;KACvB,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC;KAC1B,SAAS,CAAC,GAAG,CAAC,aAAa,CAAC;KAC5B,OAAO,CAAC,GAAG,CAAC,mBAAmB,CAAC;KAChC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC;KAC3B,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC;KACxB,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC;KACvB,SAAS,CAAC,GAAG,CAAC,oBAAoB,CAAC;KACnC,SAAS,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;AAItC,MAAM,OAAO,SAAS;IACgB;IAApC,YAAoC,iBAAsD,EAAE;QAAxD,mBAAc,GAAd,cAAc,CAA0C;IAAG,CAAC;IAE/E,SAAS,GAAoB,wBAAwB,CAAC,KAAK,EAAE,CAAC;IAC9D,CAAC,GAAG,IAAI,UAAU,EAAE,CAAC;IAE/B,QAAQ,CAAC,GAA2B,EAAE,UAA+C,EAAE;QAC5F,OAAO,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,GAAG,EAAE,oBAAoB,CAAC,EAAE,GAAG,IAAI,CAAC,cAAc,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;IAChH,CAAC;IAEM,YAAY,CAAC,GAAa,EAAE,UAA+C,EAAE;QAClF,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,EAAE,oBAAoB,CAAC,EAAE,GAAG,IAAI,CAAC,cAAc,EAAE,GAAG,OAAO,EAAE,CAAC,EAAE,SAAS,CAAC,CAAC,IAAI,EAAE,CAAC;IAClH,CAAC;CACF","sourcesContent":["import { GeneratorBuilder } from '@traqula/core';\nimport type { Wrap, Patch } from '@traqula/core';\nimport { sparql11GeneratorBuilder } from '@traqula/generator-sparql-1-1';\nimport type {\n gram as g11,\n} from '@traqula/rules-sparql-1-1';\nimport { completeParseContext, AstFactory, gram as g12 } from '@traqula/rules-sparql-1-2';\nimport type * as T12 from '@traqula/rules-sparql-1-2';\n\nexport const sparql12GeneratorBuilder =\n GeneratorBuilder.create(sparql11GeneratorBuilder)\n .widenContext<T12.SparqlGeneratorContext>()\n .typePatch<{\n [g11.queryOrUpdate.name]: [T12.SparqlQuery];\n [g11.query.name]: [T12.Query];\n [g11.selectQuery.name]: [Omit<T12.QuerySelect, g11.HandledByBase>];\n [g11.constructQuery.name]: [ Omit<T12.QueryConstruct, g11.HandledByBase>];\n [g11.describeQuery.name]: [Omit<T12.QueryDescribe, g11.HandledByBase>];\n [g11.askQuery.name]: [Omit<T12.QueryAsk, g11.HandledByBase>];\n [g11.selectClause.name]: [ Wrap<Pick<T12.QuerySelect, 'variables' | 'distinct' | 'reduced'>> ];\n\n [g11.update.name]: [T12.Update];\n [g11.update1.name]: [T12.UpdateOperation];\n [g11.load.name]: [T12.UpdateOperationLoad];\n [g11.clear.name]: [T12.UpdateOperationClear];\n [g11.drop.name]: [T12.UpdateOperationDrop];\n [g11.create.name]: [T12.UpdateOperationCreate];\n [g11.copy.name]: [T12.UpdateOperationCopy];\n [g11.move.name]: [T12.UpdateOperationMove];\n [g11.add.name]: [T12.UpdateOperationAdd];\n [g11.insertData.name]: [T12.UpdateOperationInsertData];\n [g11.deleteData.name]: [T12.UpdateOperationDeleteData];\n [g11.deleteWhere.name]: [T12.UpdateOperationDeleteWhere];\n [g11.modify.name]: [T12.UpdateOperationModify];\n [g11.graphRef.name]: [T12.TermIri];\n [g11.graphRefAll.name]: [T12.GraphRef];\n [g11.quads.name]: [Wrap<T12.Quads[]>];\n [g11.quadsNotTriples.name]: [T12.GraphQuads];\n\n [g11.aggregate.name]: [ T12.ExpressionAggregate ];\n\n [g11.datasetClauseStar.name]: [ T12.DatasetClauses ];\n [g11.usingClauseStar.name]: [ T12.DatasetClauses ];\n\n // [g11.datasetClause.name]: unchanged;\n [g11.argList.name]: [ Patch<g11.IArgList, { args: T12.Expression[] }> ];\n [g11.expression.name]: [ T12.Expression ];\n [g11.iriOrFunction.name]: [T12.TermIri | T12.ExpressionFunctionCall];\n\n [g11.prologue.name]: [ T12.ContextDefinition[] ];\n [g11.prefixDecl.name]: [ T12.ContextDefinitionPrefix ];\n [g11.baseDecl.name]: [ T12.ContextDefinitionBase ];\n // [g11.var_.name]: unchanged;\n [g11.varOrTerm.name]: [ T12.Term ];\n [g11.graphTerm.name]: [ T12.GraphTerm ];\n\n [g11.rdfLiteral.name]: [T12.TermLiteral];\n // [g11.string.name]: unchanged;\n // [g11.iri.name]: unchanged;\n // [g11.iriFull.name]: unchanged;\n // [g11.prefixedName.name]: unchanged;\n // [g11.blankNode.name]: unchanged;\n\n // [g11.path.name]: unchanged;\n //\n [g11.solutionModifier.name]: [ T12.SolutionModifiers ];\n [g11.groupClause.name]: [ T12.SolutionModifierGroup ];\n [g11.havingClause.name]: [ T12.SolutionModifierHaving ];\n [g11.orderClause.name]: [ T12.SolutionModifierOrder ];\n [g11.limitOffsetClauses.name]: [ T12.SolutionModifierLimitOffset ];\n\n [g11.triplesBlock.name]: [ T12.PatternBgp ];\n [g11.collectionPath.name]: [ T12.TripleCollectionList ];\n [g11.blankNodePropertyListPath.name]: [ T12.TripleCollectionBlankNodeProperties ];\n [g11.triplesNodePath.name]: [ T12.TripleCollection ];\n [g11.graphNodePath.name]: [ T12.Term | T12.TripleCollection ];\n\n [g11.whereClause.name]: [ Wrap<T12.PatternGroup> ];\n [g11.generatePattern.name]: [ T12.Pattern ];\n [g11.groupGraphPattern.name]: [ T12.PatternGroup ];\n [g11.graphPatternNotTriples.name]: [ Exclude<T12.Pattern, T12.SubSelect | T12.PatternBgp> ];\n [g11.optionalGraphPattern.name]: [ T12.PatternOptional ];\n [g11.graphGraphPattern.name]: [ T12.PatternGroup ];\n [g11.serviceGraphPattern.name]: [ T12.PatternService ];\n [g11.bind.name]: [ T12.PatternBind ];\n [g11.inlineData.name]: [ T12.PatternValues ];\n [g11.minusGraphPattern.name]: [ T12.PatternMinus ];\n [g11.groupOrUnionGraphPattern.name]: [ T12.PatternGroup | T12.PatternUnion ];\n [g11.filter.name]: [ T12.PatternFilter ];\n }>()\n .addRule(g12.tripleTerm)\n .addRule(g12.reifiedTriple)\n .patchRule(g12.graphNodePath)\n .addRule(g12.annotationBlockPath)\n .addRule(g12.annotationPath)\n .addRule(g12.versionDecl)\n .patchRule(g12.prologue)\n .patchRule(g12.generateTriplesBlock)\n .patchRule(g12.generateGraphTerm);\n\nexport type SparqlGenerator = ReturnType<typeof sparql12GeneratorBuilder.build>;\n\nexport class Generator {\n public constructor(private readonly defaultContext: Partial<T12.SparqlGeneratorContext> = {}) {}\n\n private readonly generator: SparqlGenerator = sparql12GeneratorBuilder.build();\n private readonly F = new AstFactory();\n\n public generate(ast: T12.Query | T12.Update, context: Partial<T12.SparqlGeneratorContext> = {}): string {\n return this.generator.queryOrUpdate(ast, completeParseContext({ ...this.defaultContext, ...context })).trim();\n }\n\n public generatePath(ast: T12.Path, context: Partial<T12.SparqlGeneratorContext> = {}): string {\n return this.generator.path(ast, completeParseContext({ ...this.defaultContext, ...context }), undefined).trim();\n }\n}\n"]}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,155 @@
1
+ import { Parser } from '@traqula/parser-sparql-1-2';
2
+ import { AstTransformer } from '@traqula/rules-sparql-1-1';
3
+ import { AstFactory } from '@traqula/rules-sparql-1-2';
4
+ import { describe, it } from 'vitest';
5
+ import { Generator } from '../lib/index.js';
6
+ describe('a SPARQL 1.2 generator', () => {
7
+ const generator = new Generator();
8
+ const parser = new Parser();
9
+ const F = new AstFactory();
10
+ const transformer = new AstTransformer();
11
+ it('generates simple round tripped', ({ expect }) => {
12
+ const query = 'SELECT * WHERE { ?s ?p ?o }';
13
+ const ast = parser.parse(query);
14
+ const result = generator.generate(ast, { origSource: query });
15
+ expect(result).toBe(query);
16
+ });
17
+ describe('on altered nodes', () => {
18
+ it('translates ?s -> ?subject', ({ expect }) => {
19
+ const query = 'SELECT * WHERE { ?s ?p ?o }';
20
+ const ast = parser.parse(query);
21
+ const altered = transformer.transformNodeSpecific(ast, {}, { term: { variable: {
22
+ transform: variable => variable.value === 's' ?
23
+ F.termVariable('subject', F.sourceLocationNodeReplaceUnsafe(variable.loc)) :
24
+ variable,
25
+ } } });
26
+ const result = generator.generate(altered, { origSource: query });
27
+ expect(result).toBe(`SELECT * WHERE { ?subject ?p ?o }`);
28
+ });
29
+ it('translates blanknodes -> variables', ({ expect }) => {
30
+ const query = `BASE <ex:>
31
+ CONSTRUCT {
32
+ ?s0 ?p0 _:g_0 .
33
+ _:g_0 <a0> _:g_1 .
34
+ _:g_1 <b0><c0> .
35
+ [
36
+ <a0> [
37
+ <b0> <c0> ;
38
+ ] ;
39
+ ] ?p0 ?o0 .
40
+
41
+ }
42
+ WHERE {
43
+ ?s1 ?p1 [], <a1>; ?q1 <b1>, <c1>.
44
+ [] ?p2 ?o2.
45
+ ?s3 ?p3 [ <a3> <b3> ].
46
+ ?s4 ?p4 [ <a4> <b4>; <c4> <d4>, <e4> ].
47
+ ?s5 ?p5 [ <a5> [ <b5> <c5> ] ].
48
+ [ <a6> [ <b6> <c6> ] ] ?p6 ?o6.
49
+ [ <a7> <b7>; <c7> <d7>, <e7> ].
50
+ }`;
51
+ const ast = parser.parse(query);
52
+ function extractCollection(collection) {
53
+ const result = [];
54
+ for (const entry of collection.triples) {
55
+ const subject = entry.subject;
56
+ const pred = entry.predicate;
57
+ if (F.isTripleCollection(entry.object)) {
58
+ const identifier = { ...F.graphNodeIdentifier(entry.object) };
59
+ result.push(F.annotatedTriple(subject, pred, identifier, undefined, F.gen()));
60
+ result.push(...extractCollection(entry.object));
61
+ }
62
+ else {
63
+ result.push(F.annotatedTriple(subject, pred, entry.object, undefined, F.gen()));
64
+ }
65
+ }
66
+ return result;
67
+ }
68
+ const flattenCollections = transformer.transformNodeSpecific(ast, {}, { pattern: { bgp: {
69
+ transform: (current) => {
70
+ const bgpCopy = F.forcedAutoGenTree(current);
71
+ const newTriples = [];
72
+ for (const entry of bgpCopy.triples) {
73
+ if (F.isTriple(entry)) {
74
+ const subject = entry.subject;
75
+ const pred = entry.predicate;
76
+ if (F.isTripleCollection(entry.object)) {
77
+ const object = entry.object;
78
+ const identifier = { ...F.graphNodeIdentifier(object) };
79
+ newTriples.push(F.annotatedTriple(subject, pred, identifier));
80
+ newTriples.push(...extractCollection(object));
81
+ }
82
+ else {
83
+ newTriples.push(F.annotatedTriple(subject, pred, entry.object, undefined, F.gen()));
84
+ }
85
+ }
86
+ else {
87
+ const genTriples = extractCollection(entry);
88
+ newTriples.push(...genTriples);
89
+ }
90
+ }
91
+ return F.patternBgp(newTriples, F.sourceLocationNodeReplaceUnsafe(current.loc));
92
+ },
93
+ } } });
94
+ const result = generator.generate(flattenCollections, { origSource: query });
95
+ expect(result).toBe(`BASE <ex:>
96
+ CONSTRUCT {
97
+ ?s0 ?p0 _:g_0 .
98
+ _:g_0 <a0> _:g_1 .
99
+ _:g_1 <b0> <c0> .
100
+ [
101
+ <a0> [
102
+ <b0> <c0> ;
103
+ ] ;
104
+ ] ?p0 ?o0 .
105
+
106
+
107
+ }
108
+ WHERE {
109
+ ?s1 ?p1 _:g_2 .
110
+ ?s1 ?p1 <a1> .
111
+ ?s1 ?q1 <b1> .
112
+ ?s1 ?q1 <c1> .
113
+ _:g_3 ?p2 ?o2 .
114
+ ?s3 ?p3 _:g_4 .
115
+ _:g_4 <a3> <b3> .
116
+ ?s4 ?p4 _:g_5 .
117
+ _:g_5 <a4> <b4> .
118
+ _:g_5 <c4> <d4> .
119
+ _:g_5 <c4> <e4> .
120
+ ?s5 ?p5 _:g_6 .
121
+ _:g_6 <a5> _:g_7 .
122
+ _:g_7 <b5> <c5> .
123
+ [
124
+ <a6> [
125
+ <b6> <c6> ;
126
+ ] ;
127
+ ] ?p6 ?o6 .
128
+ _:g_10 <a7> <b7> .
129
+ _:g_10 <c7> <d7> .
130
+ _:g_10 <c7> <e7> .
131
+
132
+ }`);
133
+ });
134
+ });
135
+ it('generates hand constructed query', ({ expect }) => {
136
+ const query = `
137
+ SELECT * WHERE {
138
+ ?s ?p ?o .
139
+ }`;
140
+ const ast = F.querySelect({
141
+ variables: [F.wildcard(F.gen())],
142
+ datasets: F.datasetClauses([], F.sourceLocation()),
143
+ context: [],
144
+ where: F.patternGroup([
145
+ F.patternBgp([
146
+ F.triple(F.termVariable('s', F.gen()), F.termVariable('p', F.gen()), F.termVariable('o', F.gen())),
147
+ ], F.gen()),
148
+ ], F.gen()),
149
+ solutionModifiers: {},
150
+ }, F.gen());
151
+ const result = generator.generate(ast);
152
+ expect(result.trim()).toBe(query.trim());
153
+ });
154
+ });
155
+ //# sourceMappingURL=integration.test.js.map
@@ -0,0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AAM,CAAC,CAAC,CAAC,YAAY,CAAC,GAAG,EAAE,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC,YAAY,CAAC,GAAG,EAAE,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC,YAAY,CAAC,GAAG,EAAE,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC;iBACnG,EAAE,CAAC,CAAC,GAAG,EAAE,CAAC;aACZ,EAAE,CAAC,CAAC,GAAG,EAAE,CAAC;YACX,iBAAiB,EAAE,EAAE;SACtB,EAAE,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC;QACZ,MAAM,MAAM,GAAG,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QACvC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;IAC3C,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { Parser } from '@traqula/parser-sparql-1-2';\nimport type * as T11 from '@traqula/rules-sparql-1-1';\nimport { AstTransformer } from '@traqula/rules-sparql-1-1';\nimport type * as T12 from '@traqula/rules-sparql-1-2';\nimport { AstFactory } from '@traqula/rules-sparql-1-2';\nimport { describe, it } from 'vitest';\nimport { Generator } from '../lib/index.js';\n\ndescribe('a SPARQL 1.2 generator', () => {\n const generator = new Generator();\n const parser = new Parser();\n const F = new AstFactory();\n const transformer = new AstTransformer();\n\n it ('generates simple round tripped', ({ expect }) => {\n const query = 'SELECT * WHERE { ?s ?p ?o }';\n const ast = <T11.Query> parser.parse(query);\n const result = generator.generate(ast, { origSource: query });\n expect(result).toBe(query);\n });\n\n describe('on altered nodes', () => {\n it('translates ?s -> ?subject', ({ expect }) => {\n const query = 'SELECT * WHERE { ?s ?p ?o }';\n const ast = <T11.Query> parser.parse(query);\n\n const altered = transformer.transformNodeSpecific<'unsafe', T11.Query>(\n ast,\n {},\n { term: { variable: {\n transform: variable => variable.value === 's' ?\n F.termVariable('subject', F.sourceLocationNodeReplaceUnsafe(variable.loc)) :\n variable,\n }}},\n );\n\n const result = generator.generate(altered, { origSource: query });\n expect(result).toBe(`SELECT * WHERE { ?subject ?p ?o }`);\n });\n\n it('translates blanknodes -> variables', ({ expect }) => {\n const query = `BASE <ex:>\nCONSTRUCT { \n ?s0 ?p0 _:g_0 .\n_:g_0 <a0> _:g_1 .\n_:g_1 <b0><c0> .\n[\n <a0> [\n <b0> <c0> ;\n ] ;\n] ?p0 ?o0 .\n\n}\nWHERE {\n ?s1 ?p1 [], <a1>; ?q1 <b1>, <c1>.\n [] ?p2 ?o2.\n ?s3 ?p3 [ <a3> <b3> ].\n ?s4 ?p4 [ <a4> <b4>; <c4> <d4>, <e4> ].\n ?s5 ?p5 [ <a5> [ <b5> <c5> ] ].\n [ <a6> [ <b6> <c6> ] ] ?p6 ?o6.\n [ <a7> <b7>; <c7> <d7>, <e7> ].\n}`;\n const ast = <T12.Query> parser.parse(query);\n\n function extractCollection(collection: T12.TripleCollection): T12.TripleNesting[] {\n const result: T12.TripleNesting[] = [];\n for (const entry of collection.triples) {\n const subject = entry.subject;\n const pred = entry.predicate;\n if (F.isTripleCollection(entry.object)) {\n const identifier = { ...F.graphNodeIdentifier(<T11.GraphNode>entry.object) };\n result.push(F.annotatedTriple(subject, pred, identifier, undefined, F.gen()));\n result.push(...extractCollection(entry.object));\n } else {\n result.push(F.annotatedTriple(\n subject,\n pred,\n entry.object,\n undefined,\n F.gen(),\n ));\n }\n }\n return result;\n }\n const flattenCollections = transformer.transformNodeSpecific<'unsafe', T12.Query>(\n ast,\n {},\n { pattern: { bgp: {\n transform: (current) => {\n const bgpCopy = F.forcedAutoGenTree(current);\n const newTriples: T12.TripleNesting[] = [];\n for (const entry of bgpCopy.triples) {\n if (F.isTriple(entry)) {\n const subject = entry.subject;\n const pred = entry.predicate;\n if (F.isTripleCollection(entry.object)) {\n const object = entry.object;\n const identifier = { ...F.graphNodeIdentifier(<T11.GraphNode>object) };\n newTriples.push(F.annotatedTriple(subject, pred, identifier));\n newTriples.push(...extractCollection(object));\n } else {\n newTriples.push(F.annotatedTriple(\n subject,\n pred,\n entry.object,\n undefined,\n F.gen(),\n ));\n }\n } else {\n const genTriples = extractCollection(entry);\n newTriples.push(...genTriples);\n }\n }\n return F.patternBgp(<T11.BasicGraphPattern> newTriples, F.sourceLocationNodeReplaceUnsafe(current.loc));\n },\n }}},\n );\n\n const result = generator.generate(flattenCollections, { origSource: query });\n expect(result).toBe(`BASE <ex:>\nCONSTRUCT { \n ?s0 ?p0 _:g_0 .\n_:g_0 <a0> _:g_1 .\n_:g_1 <b0> <c0> .\n[\n <a0> [\n <b0> <c0> ;\n ] ;\n] ?p0 ?o0 .\n\n\n}\nWHERE {\n ?s1 ?p1 _:g_2 .\n?s1 ?p1 <a1> .\n?s1 ?q1 <b1> .\n?s1 ?q1 <c1> .\n_:g_3 ?p2 ?o2 .\n?s3 ?p3 _:g_4 .\n_:g_4 <a3> <b3> .\n?s4 ?p4 _:g_5 .\n_:g_5 <a4> <b4> .\n_:g_5 <c4> <d4> .\n_:g_5 <c4> <e4> .\n?s5 ?p5 _:g_6 .\n_:g_6 <a5> _:g_7 .\n_:g_7 <b5> <c5> .\n[\n <a6> [\n <b6> <c6> ;\n ] ;\n] ?p6 ?o6 .\n_:g_10 <a7> <b7> .\n_:g_10 <c7> <d7> .\n_:g_10 <c7> <e7> .\n\n}`);\n });\n });\n\n it ('generates hand constructed query', ({ expect }) => {\n const query = `\nSELECT * WHERE {\n ?s ?p ?o .\n}`;\n const ast = F.querySelect({\n variables: [ F.wildcard(F.gen()) ],\n datasets: F.datasetClauses([], F.sourceLocation()),\n context: [],\n where: F.patternGroup([\n F.patternBgp([\n F.triple(F.termVariable('s', F.gen()), F.termVariable('p', F.gen()), F.termVariable('o', F.gen())),\n ], F.gen()),\n ], F.gen()),\n solutionModifiers: {},\n }, F.gen());\n const result = generator.generate(ast);\n expect(result.trim()).toBe(query.trim());\n });\n});\n"]}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,64 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { AstFactory } from '@traqula/rules-sparql-1-2';
4
+ import { getStaticFilePath, positiveTest } from '@traqula/test-utils';
5
+ import { describe, it } from 'vitest';
6
+ import { Generator } from '../lib/index.js';
7
+ describe('a SPARQL 1.2 generator', () => {
8
+ const generator = new Generator();
9
+ const F = new AstFactory();
10
+ function _sinkGenerated(suite, test, response) {
11
+ const dir = getStaticFilePath();
12
+ const fileLoc = path.join(dir, suite, `${test}-generated.sparql`);
13
+ // eslint-disable-next-line no-sync
14
+ fs.writeFileSync(fileLoc, response);
15
+ }
16
+ describe('positive paths', () => {
17
+ for (const { name, statics } of positiveTest('paths')) {
18
+ it(`can parse ${name}`, async ({ expect }) => {
19
+ const { query, ast, autoGen } = await statics();
20
+ const path = ast;
21
+ const generated = generator.generatePath(path, { origSource: query });
22
+ expect(generated, 'round tripped generation').toEqual(query.trim());
23
+ const replaceLoc = F.sourceLocationNodeReplaceUnsafe(path.loc);
24
+ const autoGenAst = F.forcedAutoGenTree(path);
25
+ autoGenAst.loc = replaceLoc;
26
+ const selfGenerated = generator.generatePath(autoGenAst);
27
+ expect(selfGenerated, 'auto generated').toEqual(autoGen.trim());
28
+ });
29
+ }
30
+ });
31
+ describe('positive sparql 1.1', () => {
32
+ for (const { name, statics } of positiveTest('sparql-1-1')) {
33
+ it(`can parse ${name}`, async ({ expect }) => {
34
+ const { query, ast, autoGen } = await statics();
35
+ const queryUpdate = ast;
36
+ const roundTripped = generator.generate(queryUpdate, { origSource: query });
37
+ expect(roundTripped, 'round-tripped generation').toEqual(query.trim());
38
+ const replaceLoc = F.sourceLocationNodeReplaceUnsafe(queryUpdate.loc);
39
+ const autoGenAst = F.forcedAutoGenTree(queryUpdate);
40
+ autoGenAst.loc = replaceLoc;
41
+ const selfGenerated = generator.generate(autoGenAst);
42
+ // _sinkGenerated('sparql-1-1', name, selfGenerated);
43
+ expect(selfGenerated, 'auto generated').toEqual(autoGen.trim());
44
+ });
45
+ }
46
+ });
47
+ describe('positive sparql 1.2', () => {
48
+ for (const { name, statics } of positiveTest('sparql-1-2')) {
49
+ it(`can parse ${name}`, async ({ expect }) => {
50
+ const { query, ast, autoGen } = await statics();
51
+ const queryUpdate = ast;
52
+ const roundTripped = generator.generate(queryUpdate, { origSource: query });
53
+ expect(roundTripped, 'round-tripped generation').toEqual(query.trim());
54
+ const replaceLoc = F.sourceLocationNodeReplaceUnsafe(queryUpdate.loc);
55
+ const autoGenAst = F.forcedAutoGenTree(queryUpdate);
56
+ autoGenAst.loc = replaceLoc;
57
+ const selfGenerated = generator.generate(autoGenAst);
58
+ // _sinkGenerated('sparql-1-2', name, selfGenerated);
59
+ expect(selfGenerated, 'auto generated').toEqual(autoGen.trim());
60
+ });
61
+ }
62
+ });
63
+ });
64
+ //# sourceMappingURL=statics.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"statics.test.js","sourceRoot":"","sources":["../../../test/statics.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AACvD,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACtE,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,QAAQ,CAAC,wBAAwB,EAAE,GAAG,EAAE;IACtC,MAAM,SAAS,GAAG,IAAI,SAAS,EAAE,CAAC;IAClC,MAAM,CAAC,GAAG,IAAI,UAAU,EAAE,CAAC;IAE3B,SAAS,cAAc,CAAC,KAAa,EAAE,IAAY,EAAE,QAAgB;QACnE,MAAM,GAAG,GAAG,iBAAiB,EAAE,CAAC;QAChC,MAAM,OA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,WAAW,GAA2B,GAAG,CAAC;gBAEhD,MAAM,YAAY,GAAG,SAAS,CAAC,QAAQ,CAAC,WAAW,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,CAAC;gBAC5E,MAAM,CAAC,YAAY,EAAE,0BAA0B,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;gBAEvE,MAAM,UAAU,GAAG,CAAC,CAAC,+BAA+B,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;gBACtE,MAAM,UAAU,GAAG,CAAC,CAAC,iBAAiB,CAAC,WAAW,CAAC,CAAC;gBACpD,UAAU,CAAC,GAAG,GAAG,UAAU,CAAC;gBAC5B,MAAM,aAAa,GAAG,SAAS,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;gBACrD,qDAAqD;gBACrD,MAAM,CAAC,aAAa,EAAE,gBAAgB,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC;YAClE,CAAC,CAAC,CAAC;QACL,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import fs from 'node:fs';\nimport path from 'node:path';\nimport type * as T12 from '@traqula/rules-sparql-1-1';\nimport { AstFactory } from '@traqula/rules-sparql-1-2';\nimport { getStaticFilePath, positiveTest } from '@traqula/test-utils';\nimport { describe, it } from 'vitest';\nimport { Generator } from '../lib/index.js';\n\ndescribe('a SPARQL 1.2 generator', () => {\n const generator = new Generator();\n const F = new AstFactory();\n\n function _sinkGenerated(suite: string, test: string, response: string): void {\n const dir = getStaticFilePath();\n const fileLoc = path.join(dir, suite, `${test}-generated.sparql`);\n // eslint-disable-next-line no-sync\n fs.writeFileSync(fileLoc, response);\n }\n\n describe('positive paths', () => {\n for (const { name, statics } of positiveTest('paths')) {\n it(`can parse ${name}`, async({ expect }) => {\n const { query, ast, autoGen } = await statics();\n const path = <T12.Path>ast;\n\n const generated = generator.generatePath(path, { origSource: query });\n expect(generated, 'round tripped generation').toEqual(query.trim());\n\n const replaceLoc = F.sourceLocationNodeReplaceUnsafe(path.loc);\n const autoGenAst = F.forcedAutoGenTree(path);\n autoGenAst.loc = replaceLoc;\n const selfGenerated = generator.generatePath(autoGenAst);\n expect(selfGenerated, 'auto generated').toEqual(autoGen.trim());\n });\n }\n });\n\n describe('positive sparql 1.1', () => {\n for (const { name, statics } of positiveTest('sparql-1-1')) {\n it(`can parse ${name}`, async({ expect }) => {\n const { query, ast, autoGen } = await statics();\n const queryUpdate = <T12.Query | T12.Update>ast;\n\n const roundTripped = generator.generate(queryUpdate, { origSource: query });\n expect(roundTripped, 'round-tripped generation').toEqual(query.trim());\n\n const replaceLoc = F.sourceLocationNodeReplaceUnsafe(queryUpdate.loc);\n const autoGenAst = F.forcedAutoGenTree(queryUpdate);\n autoGenAst.loc = replaceLoc;\n const selfGenerated = generator.generate(autoGenAst);\n // _sinkGenerated('sparql-1-1', name, selfGenerated);\n expect(selfGenerated, 'auto generated').toEqual(autoGen.trim());\n });\n }\n });\n\n describe('positive sparql 1.2', () => {\n for (const { name, statics } of positiveTest('sparql-1-2')) {\n it(`can parse ${name}`, async({ expect }) => {\n const { query, ast, autoGen } = await statics();\n const queryUpdate = <T12.Query | T12.Update>ast;\n\n const roundTripped = generator.generate(queryUpdate, { origSource: query });\n expect(roundTripped, 'round-tripped generation').toEqual(query.trim());\n\n const replaceLoc = F.sourceLocationNodeReplaceUnsafe(queryUpdate.loc);\n const autoGenAst = F.forcedAutoGenTree(queryUpdate);\n autoGenAst.loc = replaceLoc;\n const selfGenerated = generator.generate(autoGenAst);\n // _sinkGenerated('sparql-1-2', name, selfGenerated);\n expect(selfGenerated, 'auto generated').toEqual(autoGen.trim());\n });\n }\n });\n});\n"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@traqula/generator-sparql-1-2",
3
3
  "type": "module",
4
- "version": "0.0.20",
4
+ "version": "0.0.22",
5
5
  "description": "SPARQL 1.2 generator",
6
6
  "lsd:module": true,
7
7
  "license": "MIT",
@@ -17,39 +17,37 @@
17
17
  "exports": {
18
18
  "./package.json": "./package.json",
19
19
  ".": {
20
- "types": "./lib/index.d.ts",
21
- "import": "./lib/index.js",
22
- "require": "./lib/index.cjs"
20
+ "types": "./dist/esm/lib/index.d.ts",
21
+ "import": "./dist/esm/lib/index.js",
22
+ "require": "./dist/cjs/lib/index.js"
23
23
  }
24
24
  },
25
- "main": "lib/index.js",
25
+ "main": "dist/esm/lib/index.js",
26
26
  "publishConfig": {
27
27
  "access": "public"
28
28
  },
29
29
  "files": [
30
- "lib/**/*.cjs",
31
- "lib/**/*.d.ts",
32
- "lib/**/*.js",
33
- "lib/**/*.js.map"
30
+ "dist/**/*.d.ts",
31
+ "dist/**/*.js",
32
+ "dist/**/*.js.map"
34
33
  ],
35
34
  "engines": {
36
35
  "node": "^20.19.0 || >=22.12.0"
37
36
  },
38
- "typings": "lib/index",
39
37
  "scripts": {
40
38
  "build": "yarn build:ts && yarn build:transpile",
41
- "build:ts": "node \"../../node_modules/typescript/bin/tsc\"",
42
- "build:transpile": " node \"../../node_modules/esbuild/bin/esbuild\" --format=cjs --bundle --log-level=error --outfile=lib/index.cjs lib/index.ts"
39
+ "build:ts": "node \"../../node_modules/typescript/bin/tsc\" -b",
40
+ "build:transpile": "esbuild \"dist/esm/**/*.js\" --outdir=dist/cjs --platform=node --format=cjs"
43
41
  },
44
42
  "dependencies": {
45
- "@traqula/core": "^0.0.20",
46
- "@traqula/generator-sparql-1-1": "^0.0.20",
47
- "@traqula/rules-sparql-1-1": "^0.0.20",
48
- "@traqula/rules-sparql-1-2": "^0.0.20"
43
+ "@traqula/core": "^0.0.22",
44
+ "@traqula/generator-sparql-1-1": "^0.0.22",
45
+ "@traqula/rules-sparql-1-1": "^0.0.22",
46
+ "@traqula/rules-sparql-1-2": "^0.0.22"
49
47
  },
50
48
  "devDependencies": {
51
- "@traqula/parser-sparql-1-2": "^0.0.20",
52
- "@traqula/test-utils": "^0.0.20"
49
+ "@traqula/parser-sparql-1-2": "^0.0.22",
50
+ "@traqula/test-utils": "^0.0.22"
53
51
  },
54
- "gitHead": "caf35c28d4ddbfec5947566b697d34f38c1e6e31"
52
+ "gitHead": "e0cf04a50b3558229ebcc5992cf9562b14f9a077"
55
53
  }