-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructure.js
More file actions
75 lines (69 loc) · 2.09 KB
/
Copy pathstructure.js
File metadata and controls
75 lines (69 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const { parseTraversal } = require("./syntax");
function summarize(children, groupDepth) {
const pointerCount = children.reduce(
(total, child) => total + (child.kind === "pointer" ? 1 : child.pointerCount),
0
);
const groupCount = children.reduce(
(total, child) => total + (child.kind === "group" ? 1 + child.groupCount : 0),
0
);
const depth = children.reduce(
(maximum, child) => Math.max(maximum, child.kind === "group" ? child.depth : groupDepth),
groupDepth
);
return { pointerCount, groupCount, depth };
}
/**
* Build a stable, source-linked tree projection of one traversal expression.
* This is deliberately a structural projection, not recursive graph expansion.
*/
function buildTraversalStructure(
compilation,
source,
context = {},
options = {}
) {
const baseOffset = options.baseOffset ?? 0;
const layers = options.layers ?? ["document", "profile"];
const traversal = parseTraversal(source, { baseOffset });
function buildItems(items, path, groupDepth) {
return items.map((item, ordinal) => {
const itemPath = [...path, ordinal];
if (item.kind === "pointer") {
const bound = compilation.resolve(
item.id,
{ ...context, offset: item.span.start },
{ layers }
);
return {
kind: "pointer",
path: itemPath,
span: item.span,
wrapperSpan: item.wrapperSpan,
pointer: item.id,
title: bound?.declaration.title ?? "",
resolved: Boolean(bound),
provenance: bound?.provenance,
declarationIdentity: bound?.declaration.identity
};
}
const children = buildItems(item.items, itemPath, groupDepth + 1);
return {
kind: "group",
path: itemPath,
span: item.span,
children,
...summarize(children, groupDepth + 1)
};
});
}
const children = buildItems(traversal.items, [], 0);
return {
kind: "traversal-structure",
span: traversal.span,
children,
...summarize(children, 0)
};
}
module.exports = { buildTraversalStructure };