@ts-graphviz/ast 0.0.0-pr956-20240225073457 → 0.0.0-pr956-20240225160253
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/CHANGELOG.md +291 -3
- package/lib/ast.cjs +25 -60
- package/lib/ast.js +25 -60
- package/package.json +43 -24
- package/src/ast.ts +0 -8
- package/src/builder/__snapshots__/create-element.test.ts.snap +0 -166
- package/src/builder/builder.test.ts +0 -36
- package/src/builder/builder.ts +0 -44
- package/src/builder/create-element.test.ts +0 -110
- package/src/builder/create-element.ts +0 -13
- package/src/builder/index.ts +0 -3
- package/src/builder/types.ts +0 -226
- package/src/dot-shim/index.ts +0 -2
- package/src/dot-shim/parser/__snapshots__/parse.test.ts.snap +0 -2613
- package/src/dot-shim/parser/dot.peggy +0 -396
- package/src/dot-shim/parser/index.ts +0 -1
- package/src/dot-shim/parser/parse.test.ts +0 -263
- package/src/dot-shim/parser/parse.ts +0 -121
- package/src/dot-shim/parser/peggy.options.json +0 -15
- package/src/dot-shim/printer/index.ts +0 -6
- package/src/dot-shim/printer/plugins/AttributeListPrintPlugin.ts +0 -22
- package/src/dot-shim/printer/plugins/AttributePrintPlugin.ts +0 -11
- package/src/dot-shim/printer/plugins/CommentPrintPlugin.ts +0 -40
- package/src/dot-shim/printer/plugins/DotPrintPlugin.ts +0 -12
- package/src/dot-shim/printer/plugins/EdgePrintPlugin.ts +0 -27
- package/src/dot-shim/printer/plugins/GraphPrintPlugin.ts +0 -32
- package/src/dot-shim/printer/plugins/LiteralPrintPlugin.ts +0 -24
- package/src/dot-shim/printer/plugins/NodePrintPlugin.ts +0 -24
- package/src/dot-shim/printer/plugins/NodeRefGroupPrintPlugin.ts +0 -17
- package/src/dot-shim/printer/plugins/NodeRefPrintPlugin.ts +0 -18
- package/src/dot-shim/printer/plugins/SubgraphPrintPlugin.ts +0 -27
- package/src/dot-shim/printer/plugins/index.ts +0 -26
- package/src/dot-shim/printer/plugins/utils/index.ts +0 -1
- package/src/dot-shim/printer/plugins/utils/tokens.ts +0 -51
- package/src/dot-shim/printer/printer.ts +0 -46
- package/src/dot-shim/printer/stringify.test.ts +0 -823
- package/src/dot-shim/printer/stringify.ts +0 -19
- package/src/dot-shim/printer/types.ts +0 -74
- package/src/model-shim/from-model/converter.ts +0 -43
- package/src/model-shim/from-model/from-model.ts +0 -19
- package/src/model-shim/from-model/index.ts +0 -3
- package/src/model-shim/from-model/plugins/AttributeListPlugin.ts +0 -19
- package/src/model-shim/from-model/plugins/EdgePlugin.ts +0 -140
- package/src/model-shim/from-model/plugins/GraphPlugin.ts +0 -36
- package/src/model-shim/from-model/plugins/NodePlugin.ts +0 -33
- package/src/model-shim/from-model/plugins/SubraphPlugin.ts +0 -28
- package/src/model-shim/from-model/plugins/index.ts +0 -13
- package/src/model-shim/from-model/plugins/utils/convert-attribute.ts +0 -47
- package/src/model-shim/from-model/plugins/utils/convert-cluster-children.ts +0 -44
- package/src/model-shim/from-model/plugins/utils/convert-comment.ts +0 -16
- package/src/model-shim/from-model/plugins/utils/index.ts +0 -3
- package/src/model-shim/from-model/types.ts +0 -62
- package/src/model-shim/index.ts +0 -2
- package/src/model-shim/to-model/converter.ts +0 -42
- package/src/model-shim/to-model/index.ts +0 -3
- package/src/model-shim/to-model/plugins/DotPlugin.ts +0 -25
- package/src/model-shim/to-model/plugins/EdgePlugin.ts +0 -26
- package/src/model-shim/to-model/plugins/GraphPlugin.ts +0 -15
- package/src/model-shim/to-model/plugins/NodePlugin.ts +0 -25
- package/src/model-shim/to-model/plugins/SubgraphPlugin.ts +0 -14
- package/src/model-shim/to-model/plugins/index.ts +0 -13
- package/src/model-shim/to-model/plugins/utils/apply-statments.ts +0 -96
- package/src/model-shim/to-model/plugins/utils/comment-holder.ts +0 -31
- package/src/model-shim/to-model/plugins/utils/convert-to-edge-target-tuple.ts +0 -21
- package/src/model-shim/to-model/to-model.test.ts +0 -34
- package/src/model-shim/to-model/to-model.ts +0 -16
- package/src/model-shim/to-model/types.ts +0 -71
- package/src/types.ts +0 -370
- package/tsconfig.json +0 -8
- package/typedoc.json +0 -4
- package/vite.config.ts +0 -22
package/lib/ast.js
CHANGED
|
@@ -1,23 +1,3 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
-
var __publicField = (obj, key, value) => {
|
|
4
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
-
return value;
|
|
6
|
-
};
|
|
7
|
-
var __accessCheck = (obj, member, msg) => {
|
|
8
|
-
if (!member.has(obj))
|
|
9
|
-
throw TypeError("Cannot " + msg);
|
|
10
|
-
};
|
|
11
|
-
var __privateGet = (obj, member, getter) => {
|
|
12
|
-
__accessCheck(obj, member, "read from private field");
|
|
13
|
-
return getter ? getter.call(obj) : member.get(obj);
|
|
14
|
-
};
|
|
15
|
-
var __privateAdd = (obj, member, value) => {
|
|
16
|
-
if (member.has(obj))
|
|
17
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
18
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
19
|
-
};
|
|
20
|
-
var _plugins, _plugins2;
|
|
21
1
|
import { pipe, map, isNodeModel, isForwardRefNode, createModelsContext } from "@ts-graphviz/common";
|
|
22
2
|
class Builder {
|
|
23
3
|
/**
|
|
@@ -32,8 +12,7 @@ class Builder {
|
|
|
32
12
|
* @internal
|
|
33
13
|
*/
|
|
34
14
|
getLocation() {
|
|
35
|
-
|
|
36
|
-
return ((_b = (_a = this.options) == null ? void 0 : _a.locationFunction) == null ? void 0 : _b.call(_a)) ?? null;
|
|
15
|
+
return this.options?.locationFunction?.() ?? null;
|
|
37
16
|
}
|
|
38
17
|
/**
|
|
39
18
|
* Create an {@link ASTNode} of the specified type
|
|
@@ -295,17 +274,17 @@ class Printer {
|
|
|
295
274
|
* @param options Options to be used when generating the DOT string.
|
|
296
275
|
*/
|
|
297
276
|
constructor(options = {}) {
|
|
298
|
-
/** @internal */
|
|
299
|
-
__privateAdd(this, _plugins, [...defaultPlugins$2]);
|
|
300
277
|
this.options = options;
|
|
301
278
|
}
|
|
279
|
+
/** @internal */
|
|
280
|
+
#plugins = [...defaultPlugins$2];
|
|
302
281
|
/**
|
|
303
282
|
* Generates a DOT string from an ASTNode.
|
|
304
283
|
* @param ast The ASTNode to be converted into a DOT string.
|
|
305
284
|
* @returns The DOT string generated from the ASTNode.
|
|
306
285
|
*/
|
|
307
286
|
print(ast) {
|
|
308
|
-
const plugins = [...
|
|
287
|
+
const plugins = [...this.#plugins];
|
|
309
288
|
const {
|
|
310
289
|
indentSize = 2,
|
|
311
290
|
indentStyle = "space",
|
|
@@ -328,7 +307,6 @@ class Printer {
|
|
|
328
307
|
return context.print(ast);
|
|
329
308
|
}
|
|
330
309
|
}
|
|
331
|
-
_plugins = new WeakMap();
|
|
332
310
|
function stringify(ast, options) {
|
|
333
311
|
const result = new Printer(options).print(ast);
|
|
334
312
|
if (!result) {
|
|
@@ -3299,10 +3277,7 @@ const peggyParser = (
|
|
|
3299
3277
|
const str = value.trim();
|
|
3300
3278
|
const matches = str.match(/\n([\t ]+|(?!\s).)/g);
|
|
3301
3279
|
if (matches) {
|
|
3302
|
-
const indentLengths = matches.map((match) =>
|
|
3303
|
-
var _a;
|
|
3304
|
-
return ((_a = match.match(/[\t ]/g)) == null ? void 0 : _a.length) ?? 0;
|
|
3305
|
-
});
|
|
3280
|
+
const indentLengths = matches.map((match) => match.match(/[\t ]/g)?.length ?? 0);
|
|
3306
3281
|
const pattern = new RegExp(`
|
|
3307
3282
|
[ ]{${Math.min(...indentLengths)}}`, "g");
|
|
3308
3283
|
return str.replace(pattern, "\n");
|
|
@@ -3657,10 +3632,10 @@ const defaultPlugins$1 = [
|
|
|
3657
3632
|
];
|
|
3658
3633
|
class FromModelConverter {
|
|
3659
3634
|
constructor(options = {}) {
|
|
3660
|
-
/** @hidden */
|
|
3661
|
-
__privateAdd(this, _plugins2, [...defaultPlugins$1]);
|
|
3662
3635
|
this.options = options;
|
|
3663
3636
|
}
|
|
3637
|
+
/** @hidden */
|
|
3638
|
+
#plugins = [...defaultPlugins$1];
|
|
3664
3639
|
/**
|
|
3665
3640
|
* Converts a DotObjectModel into an AST.
|
|
3666
3641
|
*
|
|
@@ -3668,7 +3643,7 @@ class FromModelConverter {
|
|
|
3668
3643
|
* @returns The AST generated from the model.
|
|
3669
3644
|
*/
|
|
3670
3645
|
convert(model) {
|
|
3671
|
-
const plugins = [...
|
|
3646
|
+
const plugins = [...this.#plugins];
|
|
3672
3647
|
const { commentKind = "Slash" } = this.options;
|
|
3673
3648
|
const context = {
|
|
3674
3649
|
commentKind,
|
|
@@ -3684,14 +3659,11 @@ class FromModelConverter {
|
|
|
3684
3659
|
return context.convert(model);
|
|
3685
3660
|
}
|
|
3686
3661
|
}
|
|
3687
|
-
_plugins2 = new WeakMap();
|
|
3688
3662
|
function fromModel(model, options) {
|
|
3689
3663
|
return new FromModelConverter(options).convert(model);
|
|
3690
3664
|
}
|
|
3691
3665
|
class CommentHolder {
|
|
3692
|
-
|
|
3693
|
-
__publicField(this, "comment", null);
|
|
3694
|
-
}
|
|
3666
|
+
comment = null;
|
|
3695
3667
|
set(comment) {
|
|
3696
3668
|
this.comment = comment;
|
|
3697
3669
|
}
|
|
@@ -3699,9 +3671,8 @@ class CommentHolder {
|
|
|
3699
3671
|
this.comment = null;
|
|
3700
3672
|
}
|
|
3701
3673
|
apply(model, location) {
|
|
3702
|
-
|
|
3703
|
-
|
|
3704
|
-
if (((_b = this.comment) == null ? void 0 : _b.kind) === "Block") {
|
|
3674
|
+
if (location && this.comment?.location) {
|
|
3675
|
+
if (this.comment?.kind === "Block") {
|
|
3705
3676
|
if (this.comment.location.end.line === location.start.line - 1) {
|
|
3706
3677
|
model.comment = this.comment.value;
|
|
3707
3678
|
}
|
|
@@ -3711,7 +3682,7 @@ class CommentHolder {
|
|
|
3711
3682
|
}
|
|
3712
3683
|
}
|
|
3713
3684
|
} else {
|
|
3714
|
-
model.comment =
|
|
3685
|
+
model.comment = this.comment?.value;
|
|
3715
3686
|
}
|
|
3716
3687
|
this.reset();
|
|
3717
3688
|
}
|
|
@@ -3739,23 +3710,19 @@ const DotPlugin = {
|
|
|
3739
3710
|
};
|
|
3740
3711
|
function convertToEdgeTargetTuple(edge) {
|
|
3741
3712
|
return edge.targets.map((t) => {
|
|
3742
|
-
var _a, _b;
|
|
3743
3713
|
switch (t.type) {
|
|
3744
3714
|
case "NodeRef":
|
|
3745
3715
|
return {
|
|
3746
3716
|
id: t.id.value,
|
|
3747
|
-
port:
|
|
3748
|
-
compass:
|
|
3717
|
+
port: t.port?.value,
|
|
3718
|
+
compass: t.compass?.value
|
|
3749
3719
|
};
|
|
3750
3720
|
case "NodeRefGroup":
|
|
3751
|
-
return t.children.map((t2) => {
|
|
3752
|
-
|
|
3753
|
-
|
|
3754
|
-
|
|
3755
|
-
|
|
3756
|
-
compass: (_b2 = t2.compass) == null ? void 0 : _b2.value
|
|
3757
|
-
};
|
|
3758
|
-
});
|
|
3721
|
+
return t.children.map((t2) => ({
|
|
3722
|
+
id: t2.id.value,
|
|
3723
|
+
port: t2.port?.value,
|
|
3724
|
+
compass: t2.compass?.value
|
|
3725
|
+
}));
|
|
3759
3726
|
}
|
|
3760
3727
|
});
|
|
3761
3728
|
}
|
|
@@ -3861,9 +3828,8 @@ const GraphPlugin = {
|
|
|
3861
3828
|
return ast.type === "Graph";
|
|
3862
3829
|
},
|
|
3863
3830
|
convert(context, ast) {
|
|
3864
|
-
var _a;
|
|
3865
3831
|
const G = ast.directed ? context.models.Digraph : context.models.Graph;
|
|
3866
|
-
const graph = new G(
|
|
3832
|
+
const graph = new G(ast.id?.value, ast.strict);
|
|
3867
3833
|
applyStatements(graph, ast.children);
|
|
3868
3834
|
return graph;
|
|
3869
3835
|
}
|
|
@@ -3893,8 +3859,7 @@ const SubgraphPlugin = {
|
|
|
3893
3859
|
return ast.type === "Subgraph";
|
|
3894
3860
|
},
|
|
3895
3861
|
convert(context, ast) {
|
|
3896
|
-
|
|
3897
|
-
const subgraph = new context.models.Subgraph((_a = ast.id) == null ? void 0 : _a.value);
|
|
3862
|
+
const subgraph = new context.models.Subgraph(ast.id?.value);
|
|
3898
3863
|
applyStatements(subgraph, ast.children);
|
|
3899
3864
|
return subgraph;
|
|
3900
3865
|
}
|
|
@@ -3908,12 +3873,12 @@ const defaultPlugins = [
|
|
|
3908
3873
|
];
|
|
3909
3874
|
class ToModelConverter {
|
|
3910
3875
|
constructor(options = {}) {
|
|
3911
|
-
/** @hidden */
|
|
3912
|
-
__publicField(this, "plugins", [
|
|
3913
|
-
...defaultPlugins
|
|
3914
|
-
]);
|
|
3915
3876
|
this.options = options;
|
|
3916
3877
|
}
|
|
3878
|
+
/** @hidden */
|
|
3879
|
+
plugins = [
|
|
3880
|
+
...defaultPlugins
|
|
3881
|
+
];
|
|
3917
3882
|
/**
|
|
3918
3883
|
* Convert AST to Model.
|
|
3919
3884
|
*
|
package/package.json
CHANGED
|
@@ -1,11 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ts-graphviz/ast",
|
|
3
|
-
"version": "0.0.0-pr956-
|
|
4
|
-
"description": "",
|
|
3
|
+
"version": "0.0.0-pr956-20240225160253",
|
|
4
|
+
"description": "Graphviz AST(Abstract Syntax Tree) Utilities",
|
|
5
|
+
"keywords": [],
|
|
6
|
+
"homepage": "https://github.com/ts-graphviz/ts-graphviz#readme",
|
|
7
|
+
"bugs": {
|
|
8
|
+
"url": "https://github.com/ts-graphviz/ts-graphviz/issues"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/ts-graphviz/ts-graphviz.git",
|
|
13
|
+
"directory": "packages/ast"
|
|
14
|
+
},
|
|
15
|
+
"funding": [
|
|
16
|
+
{
|
|
17
|
+
"type": "github",
|
|
18
|
+
"url": "https://github.com/sponsors/ts-graphviz"
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
"type": "opencollective",
|
|
22
|
+
"url": "https://opencollective.com/ts-graphviz"
|
|
23
|
+
}
|
|
24
|
+
],
|
|
25
|
+
"license": "NIT",
|
|
26
|
+
"author": "",
|
|
5
27
|
"type": "module",
|
|
6
|
-
"main": "lib/ast.cjs",
|
|
7
|
-
"module": "lib/ast.js",
|
|
8
|
-
"types": "lib/ast.d.ts",
|
|
9
28
|
"exports": {
|
|
10
29
|
".": {
|
|
11
30
|
"require": {
|
|
@@ -19,24 +38,17 @@
|
|
|
19
38
|
},
|
|
20
39
|
"./package.json": "./package.json"
|
|
21
40
|
},
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
|
|
31
|
-
"keywords": [],
|
|
32
|
-
"author": "",
|
|
33
|
-
"license": "NIT",
|
|
34
|
-
"bugs": {
|
|
35
|
-
"url": "https://github.com/ts-graphviz/ts-graphviz/issues"
|
|
36
|
-
},
|
|
37
|
-
"homepage": "https://github.com/ts-graphviz/ts-graphviz#readme",
|
|
41
|
+
"main": "lib/ast.cjs",
|
|
42
|
+
"module": "lib/ast.js",
|
|
43
|
+
"types": "lib/ast.d.ts",
|
|
44
|
+
"files": [
|
|
45
|
+
"lib",
|
|
46
|
+
"README.md",
|
|
47
|
+
"CHANGELOG.md",
|
|
48
|
+
"LICENSE"
|
|
49
|
+
],
|
|
38
50
|
"dependencies": {
|
|
39
|
-
"@ts-graphviz/common": "^0.0.0-pr956-
|
|
51
|
+
"@ts-graphviz/common": "^0.0.0-pr956-20240225160253"
|
|
40
52
|
},
|
|
41
53
|
"devDependencies": {
|
|
42
54
|
"peggy": "^3.0.2",
|
|
@@ -45,8 +57,15 @@
|
|
|
45
57
|
"vite": "^5.1.3",
|
|
46
58
|
"vite-plugin-dts": "^3.7.2"
|
|
47
59
|
},
|
|
60
|
+
"engines": {
|
|
61
|
+
"node": ">=18"
|
|
62
|
+
},
|
|
63
|
+
"publishConfig": {
|
|
64
|
+
"access": "public",
|
|
65
|
+
"provenance": true
|
|
66
|
+
},
|
|
48
67
|
"scripts": {
|
|
49
|
-
"
|
|
50
|
-
"
|
|
68
|
+
"build": "vite build",
|
|
69
|
+
"codegen": "peggy --plugin ts-pegjs --extra-options-file src/dot-shim/parser/peggy.options.json -o src/dot-shim/parser/_parse.ts src/dot-shim/parser/dot.peggy"
|
|
51
70
|
}
|
|
52
71
|
}
|
package/src/ast.ts
DELETED
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
|
2
|
-
|
|
3
|
-
exports[`createElement > Attribute 1`] = `
|
|
4
|
-
{
|
|
5
|
-
"children": [],
|
|
6
|
-
"key": {
|
|
7
|
-
"children": [],
|
|
8
|
-
"location": null,
|
|
9
|
-
"quoted": false,
|
|
10
|
-
"type": "Literal",
|
|
11
|
-
"value": "color",
|
|
12
|
-
},
|
|
13
|
-
"location": null,
|
|
14
|
-
"type": "Attribute",
|
|
15
|
-
"value": {
|
|
16
|
-
"children": [],
|
|
17
|
-
"location": null,
|
|
18
|
-
"quoted": false,
|
|
19
|
-
"type": "Literal",
|
|
20
|
-
"value": "red",
|
|
21
|
-
},
|
|
22
|
-
}
|
|
23
|
-
`;
|
|
24
|
-
|
|
25
|
-
exports[`createElement > Coment 1`] = `
|
|
26
|
-
{
|
|
27
|
-
"children": [],
|
|
28
|
-
"kind": "Macro",
|
|
29
|
-
"location": null,
|
|
30
|
-
"type": "Comment",
|
|
31
|
-
"value": "This is comment",
|
|
32
|
-
}
|
|
33
|
-
`;
|
|
34
|
-
|
|
35
|
-
exports[`createElement > Dot 1`] = `
|
|
36
|
-
{
|
|
37
|
-
"children": [],
|
|
38
|
-
"location": null,
|
|
39
|
-
"type": "Dot",
|
|
40
|
-
}
|
|
41
|
-
`;
|
|
42
|
-
|
|
43
|
-
exports[`createElement > Edge 1`] = `
|
|
44
|
-
{
|
|
45
|
-
"children": [],
|
|
46
|
-
"location": null,
|
|
47
|
-
"targets": [
|
|
48
|
-
{
|
|
49
|
-
"children": [],
|
|
50
|
-
"id": {
|
|
51
|
-
"children": [],
|
|
52
|
-
"location": null,
|
|
53
|
-
"quoted": true,
|
|
54
|
-
"type": "Literal",
|
|
55
|
-
"value": "a",
|
|
56
|
-
},
|
|
57
|
-
"location": null,
|
|
58
|
-
"type": "NodeRef",
|
|
59
|
-
},
|
|
60
|
-
{
|
|
61
|
-
"children": [],
|
|
62
|
-
"id": {
|
|
63
|
-
"children": [],
|
|
64
|
-
"location": null,
|
|
65
|
-
"quoted": true,
|
|
66
|
-
"type": "Literal",
|
|
67
|
-
"value": "b",
|
|
68
|
-
},
|
|
69
|
-
"location": null,
|
|
70
|
-
"type": "NodeRef",
|
|
71
|
-
},
|
|
72
|
-
],
|
|
73
|
-
"type": "Edge",
|
|
74
|
-
}
|
|
75
|
-
`;
|
|
76
|
-
|
|
77
|
-
exports[`createElement > Graph 1`] = `
|
|
78
|
-
{
|
|
79
|
-
"children": [],
|
|
80
|
-
"directed": true,
|
|
81
|
-
"location": null,
|
|
82
|
-
"strict": false,
|
|
83
|
-
"type": "Graph",
|
|
84
|
-
}
|
|
85
|
-
`;
|
|
86
|
-
|
|
87
|
-
exports[`createElement > Literal 1`] = `
|
|
88
|
-
{
|
|
89
|
-
"children": [],
|
|
90
|
-
"location": null,
|
|
91
|
-
"quoted": true,
|
|
92
|
-
"type": "Literal",
|
|
93
|
-
"value": "literal value",
|
|
94
|
-
}
|
|
95
|
-
`;
|
|
96
|
-
|
|
97
|
-
exports[`createElement > Node 1`] = `
|
|
98
|
-
{
|
|
99
|
-
"children": [],
|
|
100
|
-
"id": {
|
|
101
|
-
"children": [],
|
|
102
|
-
"location": null,
|
|
103
|
-
"quoted": false,
|
|
104
|
-
"type": "Literal",
|
|
105
|
-
"value": "node_id",
|
|
106
|
-
},
|
|
107
|
-
"location": null,
|
|
108
|
-
"type": "Node",
|
|
109
|
-
}
|
|
110
|
-
`;
|
|
111
|
-
|
|
112
|
-
exports[`createElement > NodeRef 1`] = `
|
|
113
|
-
{
|
|
114
|
-
"children": [],
|
|
115
|
-
"id": {
|
|
116
|
-
"children": [],
|
|
117
|
-
"location": null,
|
|
118
|
-
"quoted": true,
|
|
119
|
-
"type": "Literal",
|
|
120
|
-
"value": "b",
|
|
121
|
-
},
|
|
122
|
-
"location": null,
|
|
123
|
-
"type": "NodeRef",
|
|
124
|
-
}
|
|
125
|
-
`;
|
|
126
|
-
|
|
127
|
-
exports[`createElement > NodeRefGroup 1`] = `
|
|
128
|
-
{
|
|
129
|
-
"children": [
|
|
130
|
-
{
|
|
131
|
-
"children": [],
|
|
132
|
-
"id": {
|
|
133
|
-
"children": [],
|
|
134
|
-
"location": null,
|
|
135
|
-
"quoted": true,
|
|
136
|
-
"type": "Literal",
|
|
137
|
-
"value": "a",
|
|
138
|
-
},
|
|
139
|
-
"location": null,
|
|
140
|
-
"type": "NodeRef",
|
|
141
|
-
},
|
|
142
|
-
{
|
|
143
|
-
"children": [],
|
|
144
|
-
"id": {
|
|
145
|
-
"children": [],
|
|
146
|
-
"location": null,
|
|
147
|
-
"quoted": true,
|
|
148
|
-
"type": "Literal",
|
|
149
|
-
"value": "b",
|
|
150
|
-
},
|
|
151
|
-
"location": null,
|
|
152
|
-
"type": "NodeRef",
|
|
153
|
-
},
|
|
154
|
-
],
|
|
155
|
-
"location": null,
|
|
156
|
-
"type": "NodeRefGroup",
|
|
157
|
-
}
|
|
158
|
-
`;
|
|
159
|
-
|
|
160
|
-
exports[`createElement > Subgraph 1`] = `
|
|
161
|
-
{
|
|
162
|
-
"children": [],
|
|
163
|
-
"location": null,
|
|
164
|
-
"type": "Subgraph",
|
|
165
|
-
}
|
|
166
|
-
`;
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from 'vitest';
|
|
2
|
-
import { Builder } from './builder.js';
|
|
3
|
-
|
|
4
|
-
describe('Builder', () => {
|
|
5
|
-
test('should create an ASTNode with specified properties', () => {
|
|
6
|
-
const location = {
|
|
7
|
-
start: {
|
|
8
|
-
offset: 0,
|
|
9
|
-
line: 1,
|
|
10
|
-
column: 1,
|
|
11
|
-
},
|
|
12
|
-
end: {
|
|
13
|
-
offset: 3,
|
|
14
|
-
line: 1,
|
|
15
|
-
column: 3,
|
|
16
|
-
},
|
|
17
|
-
};
|
|
18
|
-
const builder = new Builder({
|
|
19
|
-
locationFunction: () => location,
|
|
20
|
-
});
|
|
21
|
-
const type = 'TestType' as any;
|
|
22
|
-
const props = { prop1: 'test1', prop2: 'test2' };
|
|
23
|
-
const children = [
|
|
24
|
-
{ type: 'TestType', props: { prop1: 'test1', prop2: 'test2' } },
|
|
25
|
-
] as any[];
|
|
26
|
-
const expectedResult = {
|
|
27
|
-
type,
|
|
28
|
-
...props,
|
|
29
|
-
children,
|
|
30
|
-
location,
|
|
31
|
-
};
|
|
32
|
-
expect(builder.createElement(type, props, children)).toStrictEqual(
|
|
33
|
-
expectedResult,
|
|
34
|
-
);
|
|
35
|
-
});
|
|
36
|
-
});
|
package/src/builder/builder.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import type { ASTChildNode, ASTNode, FileRange } from '../types.js';
|
|
2
|
-
import { ASTBuilder, BuilderOptions } from './types.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Builder is an ASTBuilder that provides a method to create an ASTNode.
|
|
6
|
-
*
|
|
7
|
-
* @group Create AST
|
|
8
|
-
*/
|
|
9
|
-
export class Builder implements ASTBuilder {
|
|
10
|
-
/**
|
|
11
|
-
* Get the current file range or null
|
|
12
|
-
* @internal
|
|
13
|
-
*/
|
|
14
|
-
private getLocation(): FileRange | null {
|
|
15
|
-
return this.options?.locationFunction?.() ?? null;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Constructor of Builder
|
|
20
|
-
* @param options - Options to initialize Builder
|
|
21
|
-
*/
|
|
22
|
-
constructor(private options?: Partial<BuilderOptions>) {}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Create an {@link ASTNode} of the specified type
|
|
26
|
-
*
|
|
27
|
-
* @param type - Type of the {@link ASTNode}
|
|
28
|
-
* @param props - Properties of the {@link ASTNode}
|
|
29
|
-
* @param children - Children of the {@link ASTNode}
|
|
30
|
-
* @returns An {@link ASTNode}
|
|
31
|
-
*/
|
|
32
|
-
public createElement<T extends ASTNode>(
|
|
33
|
-
type: T['type'],
|
|
34
|
-
props: any,
|
|
35
|
-
children: ASTChildNode<T>[] = [],
|
|
36
|
-
): T {
|
|
37
|
-
return {
|
|
38
|
-
location: this.getLocation(),
|
|
39
|
-
...props,
|
|
40
|
-
type,
|
|
41
|
-
children,
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
}
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from 'vitest';
|
|
2
|
-
import { createElement } from './create-element.js';
|
|
3
|
-
|
|
4
|
-
describe('createElement', () => {
|
|
5
|
-
test('Dot', () => {
|
|
6
|
-
const ast = createElement('Dot', {}, []);
|
|
7
|
-
expect(ast).toMatchSnapshot();
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
test('Graph', () => {
|
|
11
|
-
const ast = createElement('Graph', {
|
|
12
|
-
strict: false,
|
|
13
|
-
directed: true,
|
|
14
|
-
});
|
|
15
|
-
expect(ast).toMatchSnapshot();
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
test('Node', () => {
|
|
19
|
-
const ast = createElement('Node', {
|
|
20
|
-
id: createElement('Literal', {
|
|
21
|
-
value: 'node_id',
|
|
22
|
-
quoted: false,
|
|
23
|
-
}),
|
|
24
|
-
});
|
|
25
|
-
expect(ast).toMatchSnapshot();
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
test('Edge', () => {
|
|
29
|
-
const ast = createElement('Edge', {
|
|
30
|
-
targets: [
|
|
31
|
-
createElement('NodeRef', {
|
|
32
|
-
id: createElement('Literal', {
|
|
33
|
-
value: 'a',
|
|
34
|
-
quoted: true,
|
|
35
|
-
}),
|
|
36
|
-
}),
|
|
37
|
-
createElement('NodeRef', {
|
|
38
|
-
id: createElement('Literal', {
|
|
39
|
-
value: 'b',
|
|
40
|
-
quoted: true,
|
|
41
|
-
}),
|
|
42
|
-
}),
|
|
43
|
-
],
|
|
44
|
-
});
|
|
45
|
-
expect(ast).toMatchSnapshot();
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
test('NodeRef', () => {
|
|
49
|
-
const ast = createElement('NodeRef', {
|
|
50
|
-
id: createElement('Literal', {
|
|
51
|
-
value: 'b',
|
|
52
|
-
quoted: true,
|
|
53
|
-
}),
|
|
54
|
-
});
|
|
55
|
-
expect(ast).toMatchSnapshot();
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
test('NodeRefGroup', () => {
|
|
59
|
-
const ast = createElement('NodeRefGroup', {}, [
|
|
60
|
-
createElement('NodeRef', {
|
|
61
|
-
id: createElement('Literal', {
|
|
62
|
-
value: 'a',
|
|
63
|
-
quoted: true,
|
|
64
|
-
}),
|
|
65
|
-
}),
|
|
66
|
-
createElement('NodeRef', {
|
|
67
|
-
id: createElement('Literal', {
|
|
68
|
-
value: 'b',
|
|
69
|
-
quoted: true,
|
|
70
|
-
}),
|
|
71
|
-
}),
|
|
72
|
-
]);
|
|
73
|
-
expect(ast).toMatchSnapshot();
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
test('Subgraph', () => {
|
|
77
|
-
const ast = createElement('Subgraph', {}, []);
|
|
78
|
-
expect(ast).toMatchSnapshot();
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
test('Literal', () => {
|
|
82
|
-
const ast = createElement('Literal', {
|
|
83
|
-
value: 'literal value',
|
|
84
|
-
quoted: true,
|
|
85
|
-
});
|
|
86
|
-
expect(ast).toMatchSnapshot();
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
test('Coment', () => {
|
|
90
|
-
const ast = createElement('Comment', {
|
|
91
|
-
kind: 'Macro',
|
|
92
|
-
value: 'This is comment',
|
|
93
|
-
});
|
|
94
|
-
expect(ast).toMatchSnapshot();
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
test('Attribute', () => {
|
|
98
|
-
const ast = createElement('Attribute', {
|
|
99
|
-
key: createElement('Literal', {
|
|
100
|
-
value: 'color',
|
|
101
|
-
quoted: false,
|
|
102
|
-
}),
|
|
103
|
-
value: createElement('Literal', {
|
|
104
|
-
value: 'red',
|
|
105
|
-
quoted: false,
|
|
106
|
-
}),
|
|
107
|
-
});
|
|
108
|
-
expect(ast).toMatchSnapshot();
|
|
109
|
-
});
|
|
110
|
-
});
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { Builder } from './builder.js';
|
|
2
|
-
import { CreateElement } from './types.js';
|
|
3
|
-
/**
|
|
4
|
-
* Create an {@link ASTNode} of the specified type
|
|
5
|
-
*
|
|
6
|
-
* @param type - Type of the {@link ASTNode}
|
|
7
|
-
* @param props - Properties of the {@link ASTNode}
|
|
8
|
-
* @param children - Children of the {@link ASTNode}
|
|
9
|
-
* @group Create AST
|
|
10
|
-
* @returns An {@link ASTNode}
|
|
11
|
-
*/
|
|
12
|
-
export const createElement: CreateElement =
|
|
13
|
-
Builder.prototype.createElement.bind(new Builder());
|
package/src/builder/index.ts
DELETED