@akanjs/cli 3.1.2-rc.4 → 3.2.0-rc.0
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/.build-stamp +1 -1
- package/{CodeAgent-z34p8m91.js → CodeAgent-h6g8dg3a.js} +2 -2
- package/{CodeTui-nvd9fk3g.js → CodeTui-n9gacepn.js} +1 -1
- package/README.ko.md +9 -0
- package/README.md +9 -0
- package/TunnelShare-k3ja4w9x.js +14 -0
- package/{agent.command-962mp3kg.js → agent.command-2f657613.js} +19 -17
- package/akanCode.js +2 -2
- package/{application.command-1nr0bqq1.js → application.command-yb8v177e.js} +35 -21
- package/{applicationBuildRunner-xy7hnfzq.js → applicationBuildRunner-sctcmkyx.js} +55 -16
- package/buildBatch.proc.js +7 -7
- package/{cloud.command-cj7b3m6y.js → cloud.command-et06ja22.js} +35 -18
- package/cloud.script-s6rbrwer.js +30 -0
- package/{code.command-asq7xrr6.js → code.command-7m67c7zc.js} +6 -6
- package/commandManifest.json +1 -1
- package/{context.command-fw4g2pw1.js → context.command-569z0rqf.js} +20 -19
- package/context.runner-ms8v1rxy.js +29 -0
- package/{guideline.command-43gfjsjv.js → guideline.command-gnv3e8p8.js} +2 -2
- package/guidelines/akanApi/akanApi.instruction.md +317 -0
- package/guidelines/conventions/rules/app.md +5 -5
- package/guidelines/conventions/rules/config.md +1 -1
- package/guidelines/conventions/rules/gates.md +10 -5
- package/incrementalBuilder.proc.js +69 -39
- package/{index-0tkj05cz.js → index-0fb3cpbt.js} +3 -3
- package/{index-6t7fkx73.js → index-1q1cjn8s.js} +21 -11
- package/{index-90q0w90q.js → index-3wjbhj2n.js} +1 -1
- package/{index-6zgt7zmx.js → index-48d5qsj3.js} +4 -4
- package/{index-z47rw9ez.js → index-5w9my9qe.js} +8 -8
- package/{index-qwe079ww.js → index-6wpaq392.js} +61 -15
- package/{index-kc871vpc.js → index-7xdpww43.js} +69 -12
- package/index-7y5yghjg.js +2969 -0
- package/{index-38patcx6.js → index-8g7tdsk4.js} +249 -696
- package/{index-kdswbvys.js → index-8pb9yvp8.js} +63 -21
- package/{index-hmj94es2.js → index-8ts9774g.js} +4 -4
- package/{index-r0q2ka2b.js → index-93ewzmp6.js} +43 -14
- package/{index-y0y0pnx8.js → index-9tbgb4d8.js} +1 -1
- package/index-abgh0jve.js +126 -0
- package/{index-t0hk1kxq.js → index-aj3ehkcx.js} +1 -1
- package/{index-demt944k.js → index-d4n1wmrz.js} +37 -4
- package/{index-sra1a5f8.js → index-drz9e84d.js} +22 -8
- package/{index-gfh90r3t.js → index-e3vmjtph.js} +2 -2
- package/{index-k64mhwbw.js → index-eajbmk10.js} +2 -1
- package/{index-g0yds382.js → index-ed6ea0qp.js} +1 -1
- package/{index-ap6ac70m.js → index-fjr4d6yd.js} +444 -368
- package/index-hqrkqxx2.js +176 -0
- package/{index-jzpeny42.js → index-jdamtr7e.js} +3 -3
- package/{index-ampz4vgv.js → index-kpdv34rq.js} +4 -4
- package/{index-kc6ffq0s.js → index-ktb3wcqz.js} +73 -34
- package/{index-dcm66hss.js → index-mmym5xmb.js} +4 -2
- package/{index-8d9r465x.js → index-mqntfzv5.js} +2 -2
- package/{index-4am4dvmf.js → index-ny2k80mv.js} +15 -3
- package/index-p6hjwt2d.js +1668 -0
- package/{index-53h0053j.js → index-rc5fvqj2.js} +12 -14
- package/{index-r8t5sq7z.js → index-ryrm5z2c.js} +66 -19
- package/{index-jzz6ye8x.js → index-s2grpzef.js} +40 -8
- package/{index-38by07nw.js → index-sr5da35e.js} +32 -9
- package/{index-qev8p5wp.js → index-sy481q2x.js} +1 -1
- package/{index-ck64kxrn.js → index-thckcabc.js} +2 -2
- package/{index-wymcdsas.js → index-vkzfk7qt.js} +25 -23
- package/{index-vkpqe29t.js → index-w639tvkd.js} +24 -125
- package/index-ygdbn9jn.js +122 -0
- package/{index-w60zxxye.js → index-zpv14wpy.js} +3 -3
- package/{index-2yan1ajk.js → index-zxsygkda.js} +18 -9
- package/index.js +24 -23
- package/{library.command-zh29tjdd.js → library.command-1gp5qe3r.js} +3 -3
- package/{localRegistry.command-k9pn9ahj.js → localRegistry.command-v3g21rex.js} +25 -16
- package/{module.command-dgpqs9fz.js → module.command-4xz9he6h.js} +6 -6
- package/{package.command-1je4hsca.js → package.command-s7qpnn1g.js} +12 -3
- package/package.json +2 -2
- package/{page.command-cgsasanh.js → page.command-fan34c49.js} +3 -3
- package/{pipeline.command-tb46sa8v.js → pipeline.command-ph4fnn9w.js} +2 -2
- package/{primitive.command-bzsy23x5.js → primitive.command-3fqh8emf.js} +7 -7
- package/{projectTypecheck-h8rgaczy.js → projectTypecheck-vkb40hjc.js} +1 -1
- package/{quality.command-120m9h6v.js → quality.command-ecj6exsh.js} +15 -10
- package/{repair.command-6m4wd0qv.js → repair.command-fe9jw719.js} +6 -6
- package/{scalar.command-3dn5hsc6.js → scalar.command-cnvfhk0n.js} +5 -5
- package/skills/akan-debug/SKILL.md +2 -1
- package/skills/akan-page/SKILL.md +16 -7
- package/skills/akan-service/SKILL.md +48 -19
- package/{subspace.command-s8nyfqbn.js → subspace.command-yxtt3v3e.js} +8 -8
- package/templates/app/lib/___appName__/__appName__.abstract.md.template +6 -0
- package/templates/app/page/_index.tsx +4 -284
- package/templates/app/page/_layout.tsx +1 -26
- package/templates/app/page/styles.css.template +58 -318
- package/templates/appSample/page/_index.tsx +281 -0
- package/templates/appSample/page/_layout.tsx +55 -0
- package/templates/appSample/page/styles.css.template +327 -0
- package/templates/libRoot/lib/___libName__/__libName__.abstract.md.template +6 -0
- package/templates/workspaceRoot/package.json.template +1 -12
- package/{tunnel.command-8mwrqkr3.js → tunnel.command-ggj38mqf.js} +7 -7
- package/{verify.command-2pspfy7k.js → verify.command-w9ttxkdz.js} +16 -14
- package/{workflow.command-7sy5vnsj.js → workflow.command-ss8devj0.js} +12 -12
- package/{workspace.command-x518tdv4.js → workspace.command-rw1p0xeq.js} +50 -38
- package/TunnelShare-jwbb50kz.js +0 -14
- package/cloud.script-8kr7qgfa.js +0 -27
- package/context.runner-p6m5cxk3.js +0 -28
- package/index-d894868w.js +0 -842
- package/index-re2kfscb.js +0 -1363
- /package/templates/{app → appSample}/page/_overrides.tsx +0 -0
- /package/templates/{app → appSample}/public/fonts/Pretendard-Black.woff2 +0 -0
- /package/templates/{app → appSample}/public/fonts/Pretendard-Bold.woff2 +0 -0
- /package/templates/{app → appSample}/public/fonts/Pretendard-ExtraBold.woff2 +0 -0
- /package/templates/{app → appSample}/public/fonts/Pretendard-ExtraLight.woff2 +0 -0
- /package/templates/{app → appSample}/public/fonts/Pretendard-Light.woff2 +0 -0
- /package/templates/{app → appSample}/public/fonts/Pretendard-Medium.woff2 +0 -0
- /package/templates/{app → appSample}/public/fonts/Pretendard-Regular.woff2 +0 -0
- /package/templates/{app → appSample}/public/fonts/Pretendard-SemiBold.woff2 +0 -0
- /package/templates/{app → appSample}/public/fonts/Pretendard-Thin.woff2 +0 -0
- /package/templates/{app → appSample}/public/jelly/cloud.webp +0 -0
- /package/templates/{app → appSample}/public/jelly/comet.webp +0 -0
- /package/templates/{app → appSample}/public/jelly/moon.webp +0 -0
- /package/templates/{app → appSample}/public/jelly/planet.webp +0 -0
- /package/templates/{app → appSample}/public/jelly/rocket.webp +0 -0
- /package/templates/{app → appSample}/public/jelly/star.webp +0 -0
- /package/templates/{app → appSample}/public/jelly/sugar.webp +0 -0
- /package/templates/{app → appSample}/public/logo.png +0 -0
- /package/templates/{app → appSample}/ui/Recipe/index.ts +0 -0
- /package/templates/{app → appSample}/ui/Recipe/jellyButton.ts +0 -0
|
@@ -0,0 +1,2969 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// pkgs/@akanjs/language/akanGraphDiff.ts
|
|
3
|
+
var edgeKeyOf = ({ from, to, kind, label }) => `${from}
|
|
4
|
+
${to}
|
|
5
|
+
${kind}
|
|
6
|
+
${label ?? ""}`;
|
|
7
|
+
var shapeOf = ({ type, array, optional, declaredIn }) => `${type ?? ""}${array ? "[]" : ""}${optional ? "?" : ""} ${declaredIn}`;
|
|
8
|
+
var isEdited = (before, after) => before.digest !== undefined && after.digest !== undefined && before.digest !== after.digest;
|
|
9
|
+
var byId = (a, b) => a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
|
|
10
|
+
|
|
11
|
+
class AkanGraphDiff {
|
|
12
|
+
static between(base, head) {
|
|
13
|
+
const baseNodes = new Map(base.nodes.map((node) => [node.id, node]));
|
|
14
|
+
const headNodes = new Map(head.nodes.map((node) => [node.id, node]));
|
|
15
|
+
const marked = new Map;
|
|
16
|
+
for (const node of head.nodes) {
|
|
17
|
+
const before = baseNodes.get(node.id);
|
|
18
|
+
const next = before ? AkanGraphDiff.#compare(before, node) : AkanGraphDiff.#mark(node, "added");
|
|
19
|
+
if (next)
|
|
20
|
+
marked.set(node.id, next);
|
|
21
|
+
}
|
|
22
|
+
for (const node of base.nodes)
|
|
23
|
+
if (!headNodes.has(node.id))
|
|
24
|
+
marked.set(node.id, AkanGraphDiff.#mark(node, "removed"));
|
|
25
|
+
const baseKeys = new Set(base.edges.map(edgeKeyOf));
|
|
26
|
+
const headKeys = new Set(head.edges.map(edgeKeyOf));
|
|
27
|
+
const edges = [
|
|
28
|
+
...head.edges.flatMap((edge) => !baseKeys.has(edgeKeyOf(edge)) ? [{ ...edge, change: "added" }] : marked.has(edge.from) || marked.has(edge.to) ? [edge] : []),
|
|
29
|
+
...base.edges.filter((edge) => !headKeys.has(edgeKeyOf(edge))).map((edge) => ({ ...edge, change: "removed" }))
|
|
30
|
+
];
|
|
31
|
+
const context = edges.flatMap((edge) => [edge.from, edge.to]).filter((id) => !marked.has(id)).flatMap((id) => {
|
|
32
|
+
const node = headNodes.get(id) ?? baseNodes.get(id);
|
|
33
|
+
return node ? [node] : [];
|
|
34
|
+
});
|
|
35
|
+
const nodes = [...new Map([...marked.values(), ...context].map((node) => [node.id, node])).values()].map(AkanGraphDiff.#withoutDigest).sort(byId);
|
|
36
|
+
const summary = { added: 0, removed: 0, changed: 0 };
|
|
37
|
+
for (const node of marked.values())
|
|
38
|
+
if (node.change)
|
|
39
|
+
summary[node.change] += 1;
|
|
40
|
+
return { version: 1, nodes, edges, summary };
|
|
41
|
+
}
|
|
42
|
+
static #mark(node, change) {
|
|
43
|
+
return node.fields ? { ...node, change, fields: node.fields.map((field) => ({ ...field, change })) } : { ...node, change };
|
|
44
|
+
}
|
|
45
|
+
static #compare(before, after) {
|
|
46
|
+
const fields = AkanGraphDiff.#fieldsBetween(before.fields, after.fields);
|
|
47
|
+
const isChanged = isEdited(before, after) || before.label !== after.label || before.detail.join(`
|
|
48
|
+
`) !== after.detail.join(`
|
|
49
|
+
`) || (before.values ?? []).join(`
|
|
50
|
+
`) !== (after.values ?? []).join(`
|
|
51
|
+
`) || !!fields?.some((field) => field.change);
|
|
52
|
+
if (!isChanged)
|
|
53
|
+
return null;
|
|
54
|
+
return fields ? { ...after, change: "changed", fields } : { ...after, change: "changed" };
|
|
55
|
+
}
|
|
56
|
+
static #fieldsBetween(before, after) {
|
|
57
|
+
if (!before && !after)
|
|
58
|
+
return;
|
|
59
|
+
const previous = new Map((before ?? []).map((field) => [field.name, field]));
|
|
60
|
+
const names = new Set((after ?? []).map((field) => field.name));
|
|
61
|
+
return [
|
|
62
|
+
...(after ?? []).map((field) => {
|
|
63
|
+
const old = previous.get(field.name);
|
|
64
|
+
if (!old)
|
|
65
|
+
return { ...field, change: "added" };
|
|
66
|
+
return shapeOf(old) === shapeOf(field) && !isEdited(old, field) ? field : { ...field, change: "changed" };
|
|
67
|
+
}),
|
|
68
|
+
...(before ?? []).filter((field) => !names.has(field.name)).map((field) => ({ ...field, change: "removed" }))
|
|
69
|
+
];
|
|
70
|
+
}
|
|
71
|
+
static #withoutDigest({ digest: _digest, ...node }) {
|
|
72
|
+
return node.fields ? { ...node, fields: node.fields.map(({ digest: _field, ...field }) => field) } : node;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
// pkgs/@akanjs/language/moduleFacts.ts
|
|
76
|
+
var testFilePattern = /\.(?:test|spec)\.tsx?$/;
|
|
77
|
+
var moduleCallees = new Set([
|
|
78
|
+
"via",
|
|
79
|
+
"from",
|
|
80
|
+
"into",
|
|
81
|
+
"serve",
|
|
82
|
+
"slice",
|
|
83
|
+
"endpoint",
|
|
84
|
+
"internal",
|
|
85
|
+
"store",
|
|
86
|
+
"enumOf",
|
|
87
|
+
"adapt",
|
|
88
|
+
"dangerouslyAdapt"
|
|
89
|
+
]);
|
|
90
|
+
var intervalUnits = [
|
|
91
|
+
[86400000, "d"],
|
|
92
|
+
[3600000, "h"],
|
|
93
|
+
[60000, "m"],
|
|
94
|
+
[1000, "s"]
|
|
95
|
+
];
|
|
96
|
+
var argMethods = new Set(["param", "body", "search", "room", "msg"]);
|
|
97
|
+
var ownerOf = (relativePath) => {
|
|
98
|
+
const [root, name] = relativePath.split("/");
|
|
99
|
+
return (root === "apps" || root === "libs") && name ? `${root}/${name}` : null;
|
|
100
|
+
};
|
|
101
|
+
var createFactReader = (ts, ast) => {
|
|
102
|
+
const spanOf = (source, node) => {
|
|
103
|
+
const start = node.getStart(source);
|
|
104
|
+
const end = node.getEnd();
|
|
105
|
+
const head = source.getLineAndCharacterOfPosition(start);
|
|
106
|
+
const tail = source.getLineAndCharacterOfPosition(end);
|
|
107
|
+
return {
|
|
108
|
+
fileName: source.fileName,
|
|
109
|
+
start,
|
|
110
|
+
end,
|
|
111
|
+
line: head.line,
|
|
112
|
+
character: head.character,
|
|
113
|
+
endLine: tail.line,
|
|
114
|
+
endCharacter: tail.character
|
|
115
|
+
};
|
|
116
|
+
};
|
|
117
|
+
const extentOf = (source, node) => ({
|
|
118
|
+
start: node.getStart(source),
|
|
119
|
+
end: node.getEnd()
|
|
120
|
+
});
|
|
121
|
+
const unwrap = (expression) => {
|
|
122
|
+
let current = expression;
|
|
123
|
+
while (ts.isParenthesizedExpression(current) || ts.isAsExpression(current) || ts.isSatisfiesExpression(current))
|
|
124
|
+
current = current.expression;
|
|
125
|
+
return current;
|
|
126
|
+
};
|
|
127
|
+
const moduleRefOf = (expression, holder) => {
|
|
128
|
+
let current = expression && unwrap(expression);
|
|
129
|
+
while (current && ts.isCallExpression(current) && ts.isPropertyAccessExpression(current.expression))
|
|
130
|
+
current = current.expression.expression;
|
|
131
|
+
if (current && ts.isStringLiteral(current))
|
|
132
|
+
return current.text;
|
|
133
|
+
if (!current || !ts.isPropertyAccessExpression(current))
|
|
134
|
+
return null;
|
|
135
|
+
return ts.isIdentifier(current.expression) && current.expression.text === holder ? current.name.text : null;
|
|
136
|
+
};
|
|
137
|
+
const returnedObject = (builder) => builder ? ast.firstObjectReturnedByArrow(builder) : null;
|
|
138
|
+
const builderOf = (call) => [...call.arguments].reverse().find((arg) => ts.isArrowFunction(arg) || ts.isFunctionExpression(arg));
|
|
139
|
+
const guardNameOf = (expression) => {
|
|
140
|
+
const value = unwrap(expression);
|
|
141
|
+
return ts.isPropertyAccessExpression(value) && ts.isIdentifier(value.expression) ? `${value.expression.text}.${value.name.text}` : ast.expressionName(value);
|
|
142
|
+
};
|
|
143
|
+
const guardsIn = (object) => {
|
|
144
|
+
const guards = object?.properties.find((property) => ts.isPropertyAssignment(property) && ast.nodeName(property.name) === "guards")?.initializer;
|
|
145
|
+
if (!guards)
|
|
146
|
+
return [];
|
|
147
|
+
if (ts.isArrayLiteralExpression(guards))
|
|
148
|
+
return guards.elements.map(guardNameOf).filter((name) => name !== null);
|
|
149
|
+
if (ts.isObjectLiteralExpression(guards))
|
|
150
|
+
return guards.properties.filter(ts.isPropertyAssignment).flatMap((property) => {
|
|
151
|
+
const value = unwrap(property.initializer);
|
|
152
|
+
const names = ts.isArrayLiteralExpression(value) ? value.elements.map(guardNameOf) : [guardNameOf(value)];
|
|
153
|
+
return names.map((name) => `${ast.nodeName(property.name)} ${name ?? "?"}`);
|
|
154
|
+
});
|
|
155
|
+
const single = guardNameOf(guards);
|
|
156
|
+
return single ? [single] : [];
|
|
157
|
+
};
|
|
158
|
+
const optionObjectOf = (call) => call.arguments.find((arg) => ts.isObjectLiteralExpression(arg) && arg.properties.some((property) => ast.propertyName(property) === "guards"));
|
|
159
|
+
const chainOf = (initializer) => {
|
|
160
|
+
let root = unwrap(initializer);
|
|
161
|
+
const methods = [];
|
|
162
|
+
while (ts.isCallExpression(root) && ts.isPropertyAccessExpression(root.expression) && ts.isCallExpression(unwrap(root.expression.expression))) {
|
|
163
|
+
methods.push(root.expression.name.text);
|
|
164
|
+
root = unwrap(root.expression.expression);
|
|
165
|
+
}
|
|
166
|
+
return { root: ts.isCallExpression(root) ? root : null, methods };
|
|
167
|
+
};
|
|
168
|
+
const keyName = (property) => ts.isPropertyAssignment(property) || ts.isMethodDeclaration(property) || ts.isShorthandPropertyAssignment(property) ? property.name : null;
|
|
169
|
+
const objectKeys = (source, object) => (object?.properties ?? []).flatMap((property) => {
|
|
170
|
+
const name = ast.propertyName(property);
|
|
171
|
+
const node = keyName(property);
|
|
172
|
+
return name && node ? [{ name, span: spanOf(source, node), extent: extentOf(source, property) }] : [];
|
|
173
|
+
});
|
|
174
|
+
const sliceKeysOf = (source, builder) => (returnedObject(builder)?.properties ?? []).flatMap((property) => {
|
|
175
|
+
if (!ts.isPropertyAssignment(property))
|
|
176
|
+
return [];
|
|
177
|
+
const name = ast.nodeName(property.name);
|
|
178
|
+
if (!name)
|
|
179
|
+
return [];
|
|
180
|
+
const { root, methods } = chainOf(property.initializer);
|
|
181
|
+
return [
|
|
182
|
+
{
|
|
183
|
+
name,
|
|
184
|
+
span: spanOf(source, property.name),
|
|
185
|
+
extent: extentOf(source, property),
|
|
186
|
+
guards: root ? guardsIn(optionObjectOf(root)) : [],
|
|
187
|
+
live: methods.includes("live"),
|
|
188
|
+
argCount: methods.filter((method) => argMethods.has(method)).length
|
|
189
|
+
}
|
|
190
|
+
];
|
|
191
|
+
});
|
|
192
|
+
const verbReaderOf = (parameter) => {
|
|
193
|
+
const verbs = new Map;
|
|
194
|
+
if (parameter && ts.isObjectBindingPattern(parameter.name))
|
|
195
|
+
for (const element of parameter.name.elements) {
|
|
196
|
+
const verb = element.propertyName ?? element.name;
|
|
197
|
+
if (ts.isIdentifier(verb) && ts.isIdentifier(element.name))
|
|
198
|
+
verbs.set(element.name.text, verb.text);
|
|
199
|
+
}
|
|
200
|
+
const holder = parameter && ts.isIdentifier(parameter.name) ? parameter.name.text : null;
|
|
201
|
+
return (callee) => {
|
|
202
|
+
if (callee && ts.isIdentifier(callee))
|
|
203
|
+
return verbs.get(callee.text) ?? null;
|
|
204
|
+
if (callee && ts.isPropertyAccessExpression(callee) && ts.isIdentifier(callee.expression) && callee.expression.text === holder)
|
|
205
|
+
return callee.name.text;
|
|
206
|
+
return null;
|
|
207
|
+
};
|
|
208
|
+
};
|
|
209
|
+
const numberOf = (expression) => {
|
|
210
|
+
const value = unwrap(expression);
|
|
211
|
+
if (ts.isNumericLiteral(value))
|
|
212
|
+
return Number(value.text.replace(/_/g, ""));
|
|
213
|
+
if (!ts.isBinaryExpression(value))
|
|
214
|
+
return null;
|
|
215
|
+
const [left, right] = [numberOf(value.left), numberOf(value.right)];
|
|
216
|
+
if (left === null || right === null)
|
|
217
|
+
return null;
|
|
218
|
+
const operator = value.operatorToken.kind;
|
|
219
|
+
if (operator === ts.SyntaxKind.AsteriskToken)
|
|
220
|
+
return left * right;
|
|
221
|
+
if (operator === ts.SyntaxKind.PlusToken)
|
|
222
|
+
return left + right;
|
|
223
|
+
if (operator === ts.SyntaxKind.MinusToken)
|
|
224
|
+
return left - right;
|
|
225
|
+
return operator === ts.SyntaxKind.SlashToken && right ? left / right : null;
|
|
226
|
+
};
|
|
227
|
+
const durationOf = (expression, source) => {
|
|
228
|
+
if (!expression)
|
|
229
|
+
return null;
|
|
230
|
+
const ms = numberOf(expression);
|
|
231
|
+
if (ms === null)
|
|
232
|
+
return expression.getText(source);
|
|
233
|
+
const [size, unit] = intervalUnits.find(([size]) => ms >= size && ms % size === 0) ?? [1, "ms"];
|
|
234
|
+
return `${ms / size}${unit}`;
|
|
235
|
+
};
|
|
236
|
+
const modesOf = (option) => {
|
|
237
|
+
const object = option && unwrap(option);
|
|
238
|
+
if (!object || !ts.isObjectLiteralExpression(object))
|
|
239
|
+
return [];
|
|
240
|
+
return object.properties.filter(ts.isPropertyAssignment).flatMap((property) => {
|
|
241
|
+
const name = ast.nodeName(property.name);
|
|
242
|
+
const value = unwrap(property.initializer);
|
|
243
|
+
if (name === "enabled")
|
|
244
|
+
return value.kind === ts.SyntaxKind.FalseKeyword ? ["disabled"] : [];
|
|
245
|
+
if (name === "once")
|
|
246
|
+
return value.kind === ts.SyntaxKind.TrueKeyword ? ["once"] : [];
|
|
247
|
+
if (name === "lock")
|
|
248
|
+
return value.kind === ts.SyntaxKind.FalseKeyword ? ["unlocked"] : [];
|
|
249
|
+
if (name !== "serverMode" && name !== "operationMode")
|
|
250
|
+
return [];
|
|
251
|
+
const values = ts.isArrayLiteralExpression(value) ? [...value.elements] : [value];
|
|
252
|
+
return values.flatMap((element) => ts.isStringLiteralLike(element) ? [element.text] : ts.isPropertyAccessExpression(element) ? [element.name.text] : []);
|
|
253
|
+
});
|
|
254
|
+
};
|
|
255
|
+
const internalKeysOf = (source, builder) => {
|
|
256
|
+
const verbOf = verbReaderOf(builder?.parameters[0]);
|
|
257
|
+
return (returnedObject(builder)?.properties ?? []).flatMap((property) => {
|
|
258
|
+
if (!ts.isPropertyAssignment(property))
|
|
259
|
+
return [];
|
|
260
|
+
const name = ast.nodeName(property.name);
|
|
261
|
+
if (!name)
|
|
262
|
+
return [];
|
|
263
|
+
const { root } = chainOf(property.initializer);
|
|
264
|
+
const type = root ? verbOf(root.expression) : null;
|
|
265
|
+
const [first, second] = root?.arguments ?? [];
|
|
266
|
+
const schedule = type === "cron" && first ? ts.isStringLiteralLike(first) ? first.text : first.getText(source) : type === "interval" || type === "timeout" ? durationOf(first, source) : null;
|
|
267
|
+
const option = type === "initialize" || type === "destroy" ? first : second;
|
|
268
|
+
return [
|
|
269
|
+
{
|
|
270
|
+
name,
|
|
271
|
+
span: spanOf(source, property.name),
|
|
272
|
+
extent: extentOf(source, property),
|
|
273
|
+
type,
|
|
274
|
+
schedule,
|
|
275
|
+
modes: modesOf(option)
|
|
276
|
+
}
|
|
277
|
+
];
|
|
278
|
+
});
|
|
279
|
+
};
|
|
280
|
+
const plugsOf = (source, builder) => {
|
|
281
|
+
const verbOf = verbReaderOf(builder?.parameters[0]);
|
|
282
|
+
return (returnedObject(builder)?.properties ?? []).flatMap((property) => {
|
|
283
|
+
if (!ts.isPropertyAssignment(property))
|
|
284
|
+
return [];
|
|
285
|
+
const name = ast.nodeName(property.name);
|
|
286
|
+
const { root } = chainOf(property.initializer);
|
|
287
|
+
const [target] = root && verbOf(root.expression) === "plug" ? root.arguments : [];
|
|
288
|
+
const targetName = target && guardNameOf(target);
|
|
289
|
+
return name && targetName ? [{ name, span: spanOf(source, property.name), target: targetName }] : [];
|
|
290
|
+
});
|
|
291
|
+
};
|
|
292
|
+
const endpointKeysOf = (source, builder) => {
|
|
293
|
+
const verbOf = verbReaderOf(builder?.parameters[0]);
|
|
294
|
+
return (returnedObject(builder)?.properties ?? []).flatMap((property) => {
|
|
295
|
+
if (!ts.isPropertyAssignment(property))
|
|
296
|
+
return [];
|
|
297
|
+
const name = ast.nodeName(property.name);
|
|
298
|
+
if (!name)
|
|
299
|
+
return [];
|
|
300
|
+
const { root, methods } = chainOf(property.initializer);
|
|
301
|
+
return [
|
|
302
|
+
{
|
|
303
|
+
name,
|
|
304
|
+
span: spanOf(source, property.name),
|
|
305
|
+
extent: extentOf(source, property),
|
|
306
|
+
verb: root ? verbOf(root.expression) : null,
|
|
307
|
+
guards: root ? guardsIn(optionObjectOf(root)) : [],
|
|
308
|
+
argCount: methods.filter((method) => argMethods.has(method)).length
|
|
309
|
+
}
|
|
310
|
+
];
|
|
311
|
+
});
|
|
312
|
+
};
|
|
313
|
+
const filterQueriesOf = (source, builder) => {
|
|
314
|
+
const query = returnedObject(builder)?.properties.find((property) => ts.isPropertyAssignment(property) && ast.nodeName(property.name) === "query")?.initializer;
|
|
315
|
+
return query && ts.isObjectLiteralExpression(query) ? objectKeys(source, query) : [];
|
|
316
|
+
};
|
|
317
|
+
const typeOf = (expression) => {
|
|
318
|
+
const value = expression && unwrap(expression);
|
|
319
|
+
if (value && ts.isArrayLiteralExpression(value))
|
|
320
|
+
return { type: typeOf(value.elements[0]).type, array: true };
|
|
321
|
+
return { type: value ? ast.expressionName(value) : null, array: false };
|
|
322
|
+
};
|
|
323
|
+
const fieldsOf = (source, object) => (object?.properties ?? []).flatMap((property) => {
|
|
324
|
+
const name = ast.propertyName(property);
|
|
325
|
+
const node = keyName(property);
|
|
326
|
+
if (!name || !node)
|
|
327
|
+
return [];
|
|
328
|
+
const { root, methods } = ts.isPropertyAssignment(property) ? chainOf(property.initializer) : { root: null, methods: [] };
|
|
329
|
+
return [
|
|
330
|
+
{
|
|
331
|
+
name,
|
|
332
|
+
span: spanOf(source, node),
|
|
333
|
+
extent: extentOf(source, property),
|
|
334
|
+
...typeOf(root?.arguments[0]),
|
|
335
|
+
optional: methods.includes("optional")
|
|
336
|
+
}
|
|
337
|
+
];
|
|
338
|
+
});
|
|
339
|
+
const constantOf = (source, name, call) => {
|
|
340
|
+
const parents = call.arguments.map(unwrap).filter((arg) => ts.isIdentifier(arg) || ts.isPropertyAccessExpression(arg)).map((arg) => ast.expressionName(arg)).filter((parent) => parent !== null);
|
|
341
|
+
const fields = call.arguments.flatMap((arg) => fieldsOf(source, ast.firstObjectReturnedByArrow(arg)));
|
|
342
|
+
return { ...name, parents, fields };
|
|
343
|
+
};
|
|
344
|
+
const enumValuesOf = (call) => {
|
|
345
|
+
const list = call.arguments[1] && unwrap(call.arguments[1]);
|
|
346
|
+
if (!list || !ts.isArrayLiteralExpression(list))
|
|
347
|
+
return [];
|
|
348
|
+
return list.elements.flatMap((element) => ts.isStringLiteralLike(element) ? [element.text] : []);
|
|
349
|
+
};
|
|
350
|
+
const methodsOf = (source, statement) => statement.members.flatMap((member) => {
|
|
351
|
+
if (!ts.isMethodDeclaration(member) || !member.body)
|
|
352
|
+
return [];
|
|
353
|
+
const name = ast.nodeName(member.name);
|
|
354
|
+
if (!name || ts.getModifiers(member)?.some(({ kind }) => kind === ts.SyntaxKind.StaticKeyword))
|
|
355
|
+
return [];
|
|
356
|
+
return [{ name, span: spanOf(source, member.name), extent: extentOf(source, member) }];
|
|
357
|
+
});
|
|
358
|
+
const importsOf = (statement) => {
|
|
359
|
+
const { importClause, moduleSpecifier } = statement;
|
|
360
|
+
if (!importClause || !ts.isStringLiteral(moduleSpecifier))
|
|
361
|
+
return [];
|
|
362
|
+
const from = moduleSpecifier.text;
|
|
363
|
+
const bindings = importClause.namedBindings;
|
|
364
|
+
return [
|
|
365
|
+
...importClause.name ? [{ name: importClause.name.text, imported: "default", from }] : [],
|
|
366
|
+
...bindings && ts.isNamespaceImport(bindings) ? [{ name: bindings.name.text, imported: "*", from }] : [],
|
|
367
|
+
...bindings && ts.isNamedImports(bindings) ? bindings.elements.map((element) => ({
|
|
368
|
+
name: element.name.text,
|
|
369
|
+
imported: (element.propertyName ?? element.name).text,
|
|
370
|
+
from
|
|
371
|
+
})) : []
|
|
372
|
+
];
|
|
373
|
+
};
|
|
374
|
+
const implementsGuard = (node, guardNames) => !!node.heritageClauses?.some((clause) => clause.token === ts.SyntaxKind.ImplementsKeyword && clause.types.some(({ expression }) => guardNames.has(ast.expressionName(expression) ?? "")));
|
|
375
|
+
const guardClassIn = (node, guardNames) => {
|
|
376
|
+
if ((ts.isClassExpression(node) || ts.isClassDeclaration(node)) && implementsGuard(node, guardNames))
|
|
377
|
+
return node;
|
|
378
|
+
return ts.forEachChild(node, (child) => guardClassIn(child, guardNames)) ?? null;
|
|
379
|
+
};
|
|
380
|
+
const callsIn = (node, names) => {
|
|
381
|
+
if (ts.isCallExpression(node) && ts.isIdentifier(node.expression) && names.has(node.expression.text))
|
|
382
|
+
return node.expression.text;
|
|
383
|
+
return ts.forEachChild(node, (child) => callsIn(child, names)) ?? null;
|
|
384
|
+
};
|
|
385
|
+
const scopeOf = (node) => {
|
|
386
|
+
const scope = node.members.find((member) => ts.isPropertyDeclaration(member) && ast.nodeName(member.name) === "scope" && !!ts.getModifiers(member)?.some(({ kind }) => kind === ts.SyntaxKind.StaticKeyword))?.initializer;
|
|
387
|
+
const value = scope && unwrap(scope);
|
|
388
|
+
return value && ts.isStringLiteralLike(value) ? value.text : null;
|
|
389
|
+
};
|
|
390
|
+
const docOf = (source, node) => {
|
|
391
|
+
const { text } = source;
|
|
392
|
+
const ranges = ts.getLeadingCommentRanges(text, node.getFullStart()) ?? [];
|
|
393
|
+
const comments = [];
|
|
394
|
+
let end = node.getStart(source);
|
|
395
|
+
for (const range of [...ranges].reverse()) {
|
|
396
|
+
if ((text.slice(range.end, end).match(/\n/g)?.length ?? 0) > 1)
|
|
397
|
+
break;
|
|
398
|
+
comments.unshift(text.slice(range.pos, range.end));
|
|
399
|
+
end = range.pos;
|
|
400
|
+
}
|
|
401
|
+
const lines = comments.flatMap((comment) => comment.split(`
|
|
402
|
+
`)).map((line) => line.trim().replace(/^\/\*\*?|\*\/$/g, "").replace(/^(?:\/\/[?*!]?|\*)/, "").replace(/\{@link\s+([^}\s|]+)[^}]*\}/g, "$1").trim()).filter((line) => line && !/^(?:biome-ignore|eslint-|@ts-)/.test(line));
|
|
403
|
+
return lines.length ? lines.join(" ") : null;
|
|
404
|
+
};
|
|
405
|
+
const functionsOf = (source) => {
|
|
406
|
+
const functions = new Map;
|
|
407
|
+
for (const statement of source.statements) {
|
|
408
|
+
if (ts.isFunctionDeclaration(statement) && statement.name && statement.body)
|
|
409
|
+
functions.set(statement.name.text, { statement, name: statement.name, body: statement.body });
|
|
410
|
+
if (!ts.isVariableStatement(statement))
|
|
411
|
+
continue;
|
|
412
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
413
|
+
const initializer = declaration.initializer && unwrap(declaration.initializer);
|
|
414
|
+
if (initializer && ts.isIdentifier(declaration.name) && (ts.isArrowFunction(initializer) || ts.isFunctionExpression(initializer)))
|
|
415
|
+
functions.set(declaration.name.text, { statement, name: declaration.name, body: initializer.body });
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
return functions;
|
|
419
|
+
};
|
|
420
|
+
const guardFactoriesOf = (source, guardNames) => {
|
|
421
|
+
const functions = functionsOf(source);
|
|
422
|
+
const factories = new Map;
|
|
423
|
+
const factoryOf = (name, scope, via) => {
|
|
424
|
+
const found = functions.get(name);
|
|
425
|
+
if (!found)
|
|
426
|
+
return;
|
|
427
|
+
factories.set(name, {
|
|
428
|
+
name,
|
|
429
|
+
span: spanOf(source, found.name),
|
|
430
|
+
extent: extentOf(source, found.statement),
|
|
431
|
+
scope,
|
|
432
|
+
doc: docOf(source, found.statement),
|
|
433
|
+
via
|
|
434
|
+
});
|
|
435
|
+
};
|
|
436
|
+
for (const [name, { body }] of functions) {
|
|
437
|
+
const guard = guardClassIn(body, guardNames);
|
|
438
|
+
if (guard)
|
|
439
|
+
factoryOf(name, scopeOf(guard), null);
|
|
440
|
+
}
|
|
441
|
+
for (let isGrowing = true;isGrowing; ) {
|
|
442
|
+
isGrowing = false;
|
|
443
|
+
for (const [name, { body }] of functions) {
|
|
444
|
+
const called = factories.has(name) ? null : callsIn(body, new Set(factories.keys()));
|
|
445
|
+
if (!called)
|
|
446
|
+
continue;
|
|
447
|
+
factoryOf(name, factories.get(called)?.scope ?? null, called);
|
|
448
|
+
isGrowing = true;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
return factories;
|
|
452
|
+
};
|
|
453
|
+
const guardsOf = (source, imports) => {
|
|
454
|
+
const guardNames = new Set([
|
|
455
|
+
"Guard",
|
|
456
|
+
...imports.filter(({ imported }) => imported === "Guard").map(({ name }) => name)
|
|
457
|
+
]);
|
|
458
|
+
const imported = new Set(imports.map(({ name }) => name));
|
|
459
|
+
const factories = guardFactoriesOf(source, guardNames);
|
|
460
|
+
const guards = [];
|
|
461
|
+
const calls = [];
|
|
462
|
+
for (const statement of source.statements) {
|
|
463
|
+
if (ts.isClassDeclaration(statement) && statement.name && implementsGuard(statement, guardNames))
|
|
464
|
+
guards.push({
|
|
465
|
+
name: statement.name.text,
|
|
466
|
+
span: spanOf(source, statement.name),
|
|
467
|
+
extent: extentOf(source, statement),
|
|
468
|
+
scope: scopeOf(statement),
|
|
469
|
+
doc: docOf(source, statement),
|
|
470
|
+
factory: null
|
|
471
|
+
});
|
|
472
|
+
if (!ts.isVariableStatement(statement))
|
|
473
|
+
continue;
|
|
474
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
475
|
+
const initializer = declaration.initializer && unwrap(declaration.initializer);
|
|
476
|
+
if (!initializer || !ts.isIdentifier(declaration.name))
|
|
477
|
+
continue;
|
|
478
|
+
const made = {
|
|
479
|
+
name: declaration.name.text,
|
|
480
|
+
span: spanOf(source, declaration.name),
|
|
481
|
+
extent: extentOf(source, statement),
|
|
482
|
+
doc: docOf(source, statement)
|
|
483
|
+
};
|
|
484
|
+
if (ts.isClassExpression(initializer) && implementsGuard(initializer, guardNames))
|
|
485
|
+
guards.push({ ...made, scope: scopeOf(initializer), factory: null });
|
|
486
|
+
if (!ts.isCallExpression(initializer) || !ts.isIdentifier(initializer.expression))
|
|
487
|
+
continue;
|
|
488
|
+
const callee = initializer.expression.text;
|
|
489
|
+
const factory = factories.get(callee);
|
|
490
|
+
if (factory)
|
|
491
|
+
guards.push({ ...made, scope: factory.scope, factory: callee });
|
|
492
|
+
else if (imported.has(callee))
|
|
493
|
+
calls.push({ ...made, callee });
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
return { guards, guardFactories: [...factories.values()], guardCalls: calls };
|
|
497
|
+
};
|
|
498
|
+
const lowerFirst = (text) => text.charAt(0).toLowerCase() + text.slice(1);
|
|
499
|
+
const importsIn = (source) => source.statements.filter(ts.isImportDeclaration).flatMap(importsOf);
|
|
500
|
+
const read = (source, owner) => {
|
|
501
|
+
const imports = importsIn(source);
|
|
502
|
+
const facts = {
|
|
503
|
+
fileName: source.fileName,
|
|
504
|
+
owner,
|
|
505
|
+
imports,
|
|
506
|
+
...guardsOf(source, imports),
|
|
507
|
+
constants: [],
|
|
508
|
+
enums: [],
|
|
509
|
+
filters: [],
|
|
510
|
+
models: [],
|
|
511
|
+
services: [],
|
|
512
|
+
slices: [],
|
|
513
|
+
endpoints: [],
|
|
514
|
+
internals: [],
|
|
515
|
+
adaptors: [],
|
|
516
|
+
stores: []
|
|
517
|
+
};
|
|
518
|
+
for (const statement of source.statements) {
|
|
519
|
+
if (!ts.isClassDeclaration(statement) || !statement.name)
|
|
520
|
+
continue;
|
|
521
|
+
const heritage = ast.heritageCallNamed(statement, moduleCallees);
|
|
522
|
+
if (!heritage)
|
|
523
|
+
continue;
|
|
524
|
+
const { callee, call } = heritage;
|
|
525
|
+
const name = { name: statement.name.text, span: spanOf(source, statement.name) };
|
|
526
|
+
const builder = builderOf(call);
|
|
527
|
+
if (callee === "via")
|
|
528
|
+
facts.constants.push(constantOf(source, name, call));
|
|
529
|
+
else if (callee === "enumOf")
|
|
530
|
+
facts.enums.push({ ...name, values: enumValuesOf(call) });
|
|
531
|
+
else if (callee === "from")
|
|
532
|
+
facts.filters.push({ ...name, queries: filterQueriesOf(source, builder) });
|
|
533
|
+
else if (callee === "into")
|
|
534
|
+
facts.models.push({ ...name, model: name.name.endsWith("Model") ? lowerFirst(name.name.slice(0, -5)) : null });
|
|
535
|
+
else if (callee === "serve")
|
|
536
|
+
facts.services.push({
|
|
537
|
+
...name,
|
|
538
|
+
model: moduleRefOf(call.arguments[0], "db"),
|
|
539
|
+
methods: methodsOf(source, statement),
|
|
540
|
+
plugs: plugsOf(source, builder)
|
|
541
|
+
});
|
|
542
|
+
else if (callee === "slice")
|
|
543
|
+
facts.slices.push({
|
|
544
|
+
...name,
|
|
545
|
+
model: moduleRefOf(call.arguments[0], "srv"),
|
|
546
|
+
guards: guardsIn(call.arguments.find(ts.isObjectLiteralExpression)),
|
|
547
|
+
keys: sliceKeysOf(source, builder)
|
|
548
|
+
});
|
|
549
|
+
else if (callee === "internal")
|
|
550
|
+
facts.internals.push({
|
|
551
|
+
...name,
|
|
552
|
+
model: moduleRefOf(call.arguments[0], "srv"),
|
|
553
|
+
keys: internalKeysOf(source, builder)
|
|
554
|
+
});
|
|
555
|
+
else if (callee === "adapt" || callee === "dangerouslyAdapt") {
|
|
556
|
+
const refName = call.arguments[0] && unwrap(call.arguments[0]);
|
|
557
|
+
facts.adaptors.push({
|
|
558
|
+
...name,
|
|
559
|
+
extent: extentOf(source, statement),
|
|
560
|
+
refName: refName && ts.isStringLiteralLike(refName) ? refName.text : null,
|
|
561
|
+
plugs: plugsOf(source, builder),
|
|
562
|
+
doc: docOf(source, statement)
|
|
563
|
+
});
|
|
564
|
+
} else if (callee === "endpoint")
|
|
565
|
+
facts.endpoints.push({
|
|
566
|
+
...name,
|
|
567
|
+
model: moduleRefOf(call.arguments[0], "srv"),
|
|
568
|
+
keys: endpointKeysOf(source, builder)
|
|
569
|
+
});
|
|
570
|
+
else if (callee === "store")
|
|
571
|
+
facts.stores.push({
|
|
572
|
+
...name,
|
|
573
|
+
model: moduleRefOf(call.arguments[0], "sig"),
|
|
574
|
+
stateKeys: objectKeys(source, returnedObject(builder)),
|
|
575
|
+
actions: methodsOf(source, statement)
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
return facts;
|
|
579
|
+
};
|
|
580
|
+
return { read, spanOf, extentOf, docOf, importsIn, moduleRefOf, chainOf, unwrap };
|
|
581
|
+
};
|
|
582
|
+
|
|
583
|
+
// pkgs/@akanjs/language/routeFacts.ts
|
|
584
|
+
import { normalizeRoutePattern, tryParseRouteModuleKey } from "akanjs/common/routeConvention.ts";
|
|
585
|
+
|
|
586
|
+
class RouteFactReader {
|
|
587
|
+
static #pagePattern = /^((?:apps|libs)\/[^/]+)\/page\/((?!(?:.*\/)?\(libs\)\/).+\.tsx?)$/;
|
|
588
|
+
static #guardPattern = /\.guard\s*\(/;
|
|
589
|
+
static #aliasPattern = /^export default ([A-Za-z_$][\w$]*)\s*;?\s*$/m;
|
|
590
|
+
static #workspacePattern = /^(?:\.|@(?:apps|libs)\/)/;
|
|
591
|
+
#ts;
|
|
592
|
+
#reader;
|
|
593
|
+
#parse;
|
|
594
|
+
constructor(ts, reader, parse) {
|
|
595
|
+
this.#ts = ts;
|
|
596
|
+
this.#reader = reader;
|
|
597
|
+
this.#parse = parse;
|
|
598
|
+
}
|
|
599
|
+
static fileOf(relative) {
|
|
600
|
+
const match = relative.match(RouteFactReader.#pagePattern);
|
|
601
|
+
return match ? { owner: match[1], key: `./${match[2]}` } : null;
|
|
602
|
+
}
|
|
603
|
+
read(fileName, info, text, words) {
|
|
604
|
+
const ts = this.#ts;
|
|
605
|
+
const parsed = tryParseRouteModuleKey(info.key);
|
|
606
|
+
if (!parsed || parsed.kind === "overrides" || parsed.isInternalRootLayout)
|
|
607
|
+
return null;
|
|
608
|
+
const starts = [...text.matchAll(/^export default\b/gm)].map(({ index }) => index);
|
|
609
|
+
const source = RouteFactReader.#guardPattern.test(text) || starts.length > 1 ? this.#parse(fileName, text) : null;
|
|
610
|
+
const exported = source ? this.#exportOf(source) : null;
|
|
611
|
+
const start = exported ? exported.getStart(source ?? undefined) : starts[0] ?? 0;
|
|
612
|
+
const head = text.slice(0, start);
|
|
613
|
+
return {
|
|
614
|
+
fileName,
|
|
615
|
+
owner: info.owner,
|
|
616
|
+
kind: parsed.kind,
|
|
617
|
+
pattern: normalizeRoutePattern(parsed.sourceRouteSegments),
|
|
618
|
+
dir: parsed.moduleSegments.slice(0, -1),
|
|
619
|
+
isRoot: words.includes(`
|
|
620
|
+
rootLayout
|
|
621
|
+
`),
|
|
622
|
+
line: head.split(`
|
|
623
|
+
`).length - 1,
|
|
624
|
+
character: start - head.lastIndexOf(`
|
|
625
|
+
`) - 1,
|
|
626
|
+
guard: source && exported ? this.#guardOf(source, exported) : null,
|
|
627
|
+
imports: source ? this.#reader.importsIn(source) : [],
|
|
628
|
+
alias: source ? exported && ts.isExportAssignment(exported) && ts.isIdentifier(exported.expression) ? exported.expression.text : null : text.match(RouteFactReader.#aliasPattern)?.[1] ?? null
|
|
629
|
+
};
|
|
630
|
+
}
|
|
631
|
+
#exportOf(source) {
|
|
632
|
+
const ts = this.#ts;
|
|
633
|
+
return source.statements.find((statement) => ts.isExportAssignment(statement) || ts.canHaveModifiers(statement) && !!ts.getModifiers(statement)?.some(({ kind }) => kind === ts.SyntaxKind.DefaultKeyword)) ?? null;
|
|
634
|
+
}
|
|
635
|
+
declarationIn(source, name) {
|
|
636
|
+
const ts = this.#ts;
|
|
637
|
+
for (const statement of source.statements) {
|
|
638
|
+
if (ts.isFunctionDeclaration(statement) && statement.name?.text === name)
|
|
639
|
+
return { statement, declared: statement.name, initializer: null };
|
|
640
|
+
if (!ts.isVariableStatement(statement))
|
|
641
|
+
continue;
|
|
642
|
+
const declaration = statement.declarationList.declarations.find((item) => ts.isIdentifier(item.name) && item.name.text === name);
|
|
643
|
+
if (declaration)
|
|
644
|
+
return { statement, declared: declaration.name, initializer: declaration.initializer ?? null };
|
|
645
|
+
}
|
|
646
|
+
return null;
|
|
647
|
+
}
|
|
648
|
+
#guardOf(source, exported) {
|
|
649
|
+
const ts = this.#ts;
|
|
650
|
+
let current = ts.isExportAssignment(exported) ? this.#reader.unwrap(exported.expression) : null;
|
|
651
|
+
if (current && ts.isIdentifier(current)) {
|
|
652
|
+
const initializer = this.declarationIn(source, current.text)?.initializer;
|
|
653
|
+
current = initializer ? this.#reader.unwrap(initializer) : null;
|
|
654
|
+
}
|
|
655
|
+
while (current && ts.isCallExpression(current) && ts.isPropertyAccessExpression(current.expression)) {
|
|
656
|
+
const { name, expression } = current.expression;
|
|
657
|
+
const [stage] = current.arguments;
|
|
658
|
+
if (name.text === "guard" && stage)
|
|
659
|
+
return { calls: this.#callsIn(source, stage), span: this.#reader.spanOf(source, name) };
|
|
660
|
+
current = this.#reader.unwrap(expression);
|
|
661
|
+
}
|
|
662
|
+
return null;
|
|
663
|
+
}
|
|
664
|
+
#callsIn(source, stage) {
|
|
665
|
+
const ts = this.#ts;
|
|
666
|
+
const imported = new Set(this.#reader.importsIn(source).filter(({ from }) => RouteFactReader.#workspacePattern.test(from)).map(({ name }) => name));
|
|
667
|
+
const calls = new Map;
|
|
668
|
+
const callOf = (name) => {
|
|
669
|
+
if (calls.has(name))
|
|
670
|
+
return true;
|
|
671
|
+
const declared = this.declarationIn(source, name);
|
|
672
|
+
if (!declared && !imported.has(name))
|
|
673
|
+
return false;
|
|
674
|
+
calls.set(name, {
|
|
675
|
+
name,
|
|
676
|
+
local: declared && {
|
|
677
|
+
span: this.#reader.spanOf(source, declared.declared),
|
|
678
|
+
extent: this.#reader.extentOf(source, declared.statement),
|
|
679
|
+
doc: this.#reader.docOf(source, declared.statement)
|
|
680
|
+
}
|
|
681
|
+
});
|
|
682
|
+
return true;
|
|
683
|
+
};
|
|
684
|
+
const visit = (node) => {
|
|
685
|
+
if (ts.isCallExpression(node) && ts.isIdentifier(node.expression) && callOf(node.expression.text))
|
|
686
|
+
return;
|
|
687
|
+
ts.forEachChild(node, visit);
|
|
688
|
+
};
|
|
689
|
+
if (ts.isIdentifier(stage))
|
|
690
|
+
callOf(stage.text);
|
|
691
|
+
else
|
|
692
|
+
visit(stage);
|
|
693
|
+
return [...calls.values()];
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
// pkgs/@akanjs/language/akanGraph.ts
|
|
698
|
+
var capitalize = (text) => text.charAt(0).toUpperCase() + text.slice(1);
|
|
699
|
+
var lowerFirst = (text) => text.charAt(0).toLowerCase() + text.slice(1);
|
|
700
|
+
var memberSourcePattern = /\.(signal|service|store)\.ts$/;
|
|
701
|
+
var skippedPattern = /(?:\.(?:constant|document|dictionary|test|spec)\.tsx?|\/lib\/(?:cnst|db|dict|sig|srv|st|useClient|useServer)\.ts|\/index\.tsx?|^(?:apps|libs)\/[^/]+\/(?:client|server)\.ts)$/;
|
|
702
|
+
var scalarPattern = /\/lib\/__scalar\/([^/]+)\/[^/]+\.constant\.ts$/;
|
|
703
|
+
var moduleDirPattern = /\/lib\/([^/]+)\//;
|
|
704
|
+
var typeKinds = new Set(["model", "scalar", "enum"]);
|
|
705
|
+
var clientRoots = new Set(["fetch", "st", "sig"]);
|
|
706
|
+
var builtinGuards = {
|
|
707
|
+
Public: { scope: "account", doc: "Passes every caller, signed in or not." },
|
|
708
|
+
None: { scope: "account", doc: "Refuses every caller." }
|
|
709
|
+
};
|
|
710
|
+
var frameworkOwner = "akanjs";
|
|
711
|
+
var frameworkPattern = /^akanjs(?:\/|$)/;
|
|
712
|
+
var aliasPattern = /^@(apps|libs)\/([^/]+)/;
|
|
713
|
+
var maxMapDepth = 4;
|
|
714
|
+
|
|
715
|
+
class GraphBuilder {
|
|
716
|
+
#source;
|
|
717
|
+
#nodes = new Map;
|
|
718
|
+
#edges = new Map;
|
|
719
|
+
#contexts = new Map;
|
|
720
|
+
#members = new Map;
|
|
721
|
+
#guards = new Map;
|
|
722
|
+
#mapEntries = new Map;
|
|
723
|
+
#routeFiles = new Map;
|
|
724
|
+
#declarations = new Map;
|
|
725
|
+
#routes;
|
|
726
|
+
#adaptors = new Map;
|
|
727
|
+
#plugs = new Map;
|
|
728
|
+
constructor(source) {
|
|
729
|
+
this.#source = source;
|
|
730
|
+
this.#routes = new RouteFactReader(source.ts, source.reader, source.parse);
|
|
731
|
+
}
|
|
732
|
+
build() {
|
|
733
|
+
const { facts } = this.#source;
|
|
734
|
+
const constantsByOwner = new Map;
|
|
735
|
+
for (const file of facts)
|
|
736
|
+
constantsByOwner.set(file.owner, [...constantsByOwner.get(file.owner) ?? [], ...file.constants]);
|
|
737
|
+
const scopeOf = (owner) => [
|
|
738
|
+
...constantsByOwner.get(owner) ?? [],
|
|
739
|
+
...[...constantsByOwner].filter(([key]) => key !== owner && key?.startsWith("libs/")).flatMap(([, list]) => list)
|
|
740
|
+
];
|
|
741
|
+
const typed = [];
|
|
742
|
+
for (const file of facts)
|
|
743
|
+
for (const fact of file.adaptors) {
|
|
744
|
+
const declared = { fact, owner: file.owner, fileName: file.fileName };
|
|
745
|
+
this.#adaptors.set(fact.name, [...this.#adaptors.get(fact.name) ?? [], declared]);
|
|
746
|
+
this.#adaptorNodeOf(declared);
|
|
747
|
+
}
|
|
748
|
+
for (const file of facts)
|
|
749
|
+
for (const fact of file.adaptors)
|
|
750
|
+
for (const plug of fact.plugs)
|
|
751
|
+
this.#addEdge(this.#key("adaptor", file.owner, fact.name), this.#adaptorIdOf(plug.target, file), "plugs", plug.name, this.#location(plug.span));
|
|
752
|
+
this.#declareGuards(facts);
|
|
753
|
+
for (const file of facts) {
|
|
754
|
+
const { owner } = file;
|
|
755
|
+
const scalar = this.#source.relative(file.fileName).match(scalarPattern)?.[1];
|
|
756
|
+
const scalarClass = scalar ? file.constants.find((constant) => constant.name === capitalize(scalar)) : undefined;
|
|
757
|
+
if (scalar && scalarClass) {
|
|
758
|
+
const id = this.#key("scalar", owner, scalar);
|
|
759
|
+
const node = this.#addNode(id, "scalar", owner, scalar, scalarClass.name, scalarClass.span);
|
|
760
|
+
node.fields = this.#fieldsOf(scalarClass, scopeOf(owner));
|
|
761
|
+
typed.push(node);
|
|
762
|
+
}
|
|
763
|
+
for (const item of file.enums) {
|
|
764
|
+
const id = this.#key("enum", owner, item.name);
|
|
765
|
+
this.#addNode(id, "enum", owner, this.#moduleDirOf(file.fileName), item.name, item.span).values = item.values;
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
for (const file of facts) {
|
|
769
|
+
const { owner } = file;
|
|
770
|
+
for (const service of file.services) {
|
|
771
|
+
const refName = service.model;
|
|
772
|
+
if (!refName)
|
|
773
|
+
continue;
|
|
774
|
+
const modelId = this.#key("model", owner, refName);
|
|
775
|
+
const modelClass = constantsByOwner.get(owner)?.find((constant) => constant.name === capitalize(refName));
|
|
776
|
+
if (modelClass && !this.#nodes.has(modelId)) {
|
|
777
|
+
const node = this.#addNode(modelId, "model", owner, refName, modelClass.name, modelClass.span);
|
|
778
|
+
node.fields = this.#fieldsOf(modelClass, scopeOf(owner));
|
|
779
|
+
typed.push(node);
|
|
780
|
+
}
|
|
781
|
+
for (const method of service.methods)
|
|
782
|
+
this.#addMember("method", owner, refName, method, `${service.name}.${method.name}`, file.fileName);
|
|
783
|
+
for (const plug of service.plugs)
|
|
784
|
+
this.#plugs.set(`${owner ?? ""}
|
|
785
|
+
${refName}
|
|
786
|
+
${plug.name}`, this.#adaptorIdOf(plug.target, file));
|
|
787
|
+
}
|
|
788
|
+
for (const filter of file.filters) {
|
|
789
|
+
if (!filter.name.endsWith("Filter"))
|
|
790
|
+
continue;
|
|
791
|
+
const refName = lowerFirst(filter.name.slice(0, -"Filter".length));
|
|
792
|
+
this.#addNode(this.#key("query", owner, refName), "query", owner, refName, "", filter.span);
|
|
793
|
+
for (const query of filter.queries)
|
|
794
|
+
this.#digest(this.#addNode(this.#key("query", owner, refName, query.name), "query", owner, refName, query.name, query.span), this.#source.readText(file.fileName), query.extent);
|
|
795
|
+
}
|
|
796
|
+
for (const slice of file.slices) {
|
|
797
|
+
const refName = slice.model;
|
|
798
|
+
if (!refName)
|
|
799
|
+
continue;
|
|
800
|
+
const fallback = this.#addNode(this.#key("slice", owner, refName), "slice", owner, refName, "", slice.span, slice.guards);
|
|
801
|
+
for (const guard of slice.guards)
|
|
802
|
+
this.#guard(fallback, guard, file);
|
|
803
|
+
for (const key of slice.keys) {
|
|
804
|
+
const detail = [...key.guards, ...key.live ? ["live"] : []];
|
|
805
|
+
const node = this.#addMember("slice", owner, refName, key, `${refName}.${key.name}`, file.fileName, detail);
|
|
806
|
+
for (const guard of key.guards)
|
|
807
|
+
this.#guard(node, guard, file);
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
for (const endpoint of file.endpoints) {
|
|
811
|
+
const refName = endpoint.model;
|
|
812
|
+
if (!refName)
|
|
813
|
+
continue;
|
|
814
|
+
for (const key of endpoint.keys) {
|
|
815
|
+
const detail = [...key.verb ? [key.verb] : [], ...key.guards];
|
|
816
|
+
const node = this.#addMember("endpoint", owner, refName, key, `${refName}.${key.name}`, file.fileName, detail);
|
|
817
|
+
for (const guard of key.guards)
|
|
818
|
+
this.#guard(node, guard, file);
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
for (const internal of file.internals)
|
|
822
|
+
for (const key of internal.keys)
|
|
823
|
+
if (internal.model && key.type !== "resolveField")
|
|
824
|
+
this.#addMember("internal", owner, internal.model, key, `${internal.name}.${key.name}`, file.fileName, [key.type, key.schedule, ...key.modes].filter((item) => item !== null));
|
|
825
|
+
for (const store of file.stores)
|
|
826
|
+
if (store.model)
|
|
827
|
+
for (const action of store.actions)
|
|
828
|
+
this.#addMember("action", owner, store.model, action, `${store.name}.${action.name}`, file.fileName);
|
|
829
|
+
}
|
|
830
|
+
const types = new Map;
|
|
831
|
+
for (const node of this.#nodes.values())
|
|
832
|
+
if (typeKinds.has(node.kind))
|
|
833
|
+
types.set(node.label, [...types.get(node.label) ?? [], node]);
|
|
834
|
+
for (const node of typed)
|
|
835
|
+
for (const field of node.fields ?? []) {
|
|
836
|
+
const candidates = (field.type ? types.get(field.type) ?? [] : []).filter((type) => this.#visible(type.owner, node.owner));
|
|
837
|
+
field.ref = (candidates.find((type) => type.owner === node.owner) ?? candidates[0])?.id ?? null;
|
|
838
|
+
if (field.ref)
|
|
839
|
+
this.#addEdge(node.id, field.ref, "field", `${field.name}${field.array ? "[]" : ""}`, field.at);
|
|
840
|
+
}
|
|
841
|
+
this.#addRoutes();
|
|
842
|
+
for (const [fileName, words] of this.#source.files)
|
|
843
|
+
this.#walk(fileName, words);
|
|
844
|
+
return {
|
|
845
|
+
version: 1,
|
|
846
|
+
nodes: [...this.#nodes.values()].sort((a, b) => a.id < b.id ? -1 : a.id > b.id ? 1 : 0),
|
|
847
|
+
edges: [...this.#edges.values()]
|
|
848
|
+
};
|
|
849
|
+
}
|
|
850
|
+
#key(kind, owner, scope, name = "") {
|
|
851
|
+
return `${kind}:${owner ?? ""}:${scope}${name ? `.${name}` : ""}`;
|
|
852
|
+
}
|
|
853
|
+
#location(span) {
|
|
854
|
+
return { file: this.#source.relative(span.fileName), line: span.line + 1, column: span.character + 1 };
|
|
855
|
+
}
|
|
856
|
+
#addNode(id, kind, owner, module, name, span, detail = [], label = (kind === "query" || kind === "slice") && module ? `${module}${name ? `.${name}` : " (default)"}` : name) {
|
|
857
|
+
const existing = this.#nodes.get(id);
|
|
858
|
+
if (existing)
|
|
859
|
+
return existing;
|
|
860
|
+
const node = {
|
|
861
|
+
id,
|
|
862
|
+
kind,
|
|
863
|
+
name,
|
|
864
|
+
label,
|
|
865
|
+
owner,
|
|
866
|
+
module,
|
|
867
|
+
detail,
|
|
868
|
+
at: span ? this.#location(span) : null
|
|
869
|
+
};
|
|
870
|
+
this.#nodes.set(id, node);
|
|
871
|
+
return node;
|
|
872
|
+
}
|
|
873
|
+
#addMember(kind, owner, refName, member, label, fileName, detail = []) {
|
|
874
|
+
const id = this.#key(kind, owner, refName, member.name);
|
|
875
|
+
const node = this.#addNode(id, kind, owner, refName, member.name, member.span, detail, label);
|
|
876
|
+
this.#digest(node, this.#source.readText(fileName), member.extent);
|
|
877
|
+
this.#contexts.set(fileName, [
|
|
878
|
+
...this.#contexts.get(fileName) ?? [],
|
|
879
|
+
{ id, kind, owner, module: refName, extent: member.extent }
|
|
880
|
+
]);
|
|
881
|
+
const byName = `${kind}:${member.name}`;
|
|
882
|
+
this.#members.set(byName, [...this.#members.get(byName) ?? [], node]);
|
|
883
|
+
return node;
|
|
884
|
+
}
|
|
885
|
+
#hashOf(...parts) {
|
|
886
|
+
if (!this.#source.digests)
|
|
887
|
+
return null;
|
|
888
|
+
let hash = 2166136261;
|
|
889
|
+
for (const [text, { start, end }] of parts) {
|
|
890
|
+
if (text === undefined)
|
|
891
|
+
return null;
|
|
892
|
+
for (let idx = start;idx < end; idx++) {
|
|
893
|
+
const code = text.charCodeAt(idx);
|
|
894
|
+
if (code !== 13)
|
|
895
|
+
hash = Math.imul(hash ^ code, 16777619);
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
return (hash >>> 0).toString(16).padStart(8, "0");
|
|
899
|
+
}
|
|
900
|
+
#digest(node, text, extent) {
|
|
901
|
+
const digest = node.digest ? null : this.#hashOf([text, extent]);
|
|
902
|
+
if (digest)
|
|
903
|
+
node.digest = digest;
|
|
904
|
+
}
|
|
905
|
+
#addEdge(from, to, kind, label, at) {
|
|
906
|
+
if (from === to)
|
|
907
|
+
return;
|
|
908
|
+
const key = `${from}
|
|
909
|
+
${to}
|
|
910
|
+
${kind}
|
|
911
|
+
${label ?? ""}`;
|
|
912
|
+
const existing = this.#edges.get(key);
|
|
913
|
+
if (existing)
|
|
914
|
+
existing.count += 1;
|
|
915
|
+
else
|
|
916
|
+
this.#edges.set(key, { from, to, kind, label, count: 1, at });
|
|
917
|
+
}
|
|
918
|
+
#guard(node, guard, file) {
|
|
919
|
+
const [role, local] = guard.includes(" ") ? guard.split(" ") : [null, guard];
|
|
920
|
+
if (!local || local === "?")
|
|
921
|
+
return;
|
|
922
|
+
this.#addEdge(node.id, this.#guardIdOf(local, file), "guard", role, node.at);
|
|
923
|
+
}
|
|
924
|
+
#declareGuards(facts) {
|
|
925
|
+
const factories = new Map;
|
|
926
|
+
for (const file of facts)
|
|
927
|
+
for (const fact of file.guardFactories) {
|
|
928
|
+
const factory = { fact, owner: file.owner, fileName: file.fileName, made: [] };
|
|
929
|
+
factories.set(fact.name, [...factories.get(fact.name) ?? [], factory]);
|
|
930
|
+
}
|
|
931
|
+
const sameFile = (factory, name) => name ? factories.get(name)?.find((other) => other.fileName === factory.fileName) : undefined;
|
|
932
|
+
const declare = (guard, factory) => {
|
|
933
|
+
this.#guards.set(guard.name, [...this.#guards.get(guard.name) ?? [], guard]);
|
|
934
|
+
factory?.made.push(guard);
|
|
935
|
+
};
|
|
936
|
+
const madeBy = (factory, fileName, extent) => {
|
|
937
|
+
const parts = [{ fileName, extent }];
|
|
938
|
+
const seen = new Set;
|
|
939
|
+
for (let current = factory;current && !seen.has(current); current = sameFile(current, current.fact.via)) {
|
|
940
|
+
seen.add(current);
|
|
941
|
+
parts.push({ fileName: current.fileName, extent: current.fact.extent });
|
|
942
|
+
}
|
|
943
|
+
return parts;
|
|
944
|
+
};
|
|
945
|
+
for (const file of facts) {
|
|
946
|
+
const { owner, fileName } = file;
|
|
947
|
+
for (const { name, span, extent, scope, doc, factory: callee } of file.guards) {
|
|
948
|
+
const factory = callee ? factories.get(callee)?.find((other) => other.fileName === fileName) : undefined;
|
|
949
|
+
declare({ name, owner, span, scope, doc, parts: madeBy(factory, fileName, extent) }, factory);
|
|
950
|
+
}
|
|
951
|
+
for (const { name, span, extent, doc, callee } of file.guardCalls) {
|
|
952
|
+
const imported = file.imports.find((item) => item.name === callee);
|
|
953
|
+
if (!imported)
|
|
954
|
+
continue;
|
|
955
|
+
const from = this.#ownerOfSpecifier(fileName, imported.from);
|
|
956
|
+
const candidates = factories.get(imported.imported === "default" || imported.imported === "*" ? callee : imported.imported) ?? [];
|
|
957
|
+
const factory = candidates.find((other) => other.owner === from) ?? this.#soleVisible(candidates, from ?? owner);
|
|
958
|
+
if (factory)
|
|
959
|
+
declare({ name, owner, span, scope: factory.fact.scope, doc, parts: madeBy(factory, fileName, extent) }, factory);
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
for (const list of factories.values())
|
|
963
|
+
for (const { fact, made } of list)
|
|
964
|
+
if (made.every((guard) => !guard.doc))
|
|
965
|
+
for (const guard of made)
|
|
966
|
+
guard.doc = fact.doc;
|
|
967
|
+
for (const list of this.#guards.values())
|
|
968
|
+
for (const guard of list)
|
|
969
|
+
this.#guardNodeOf(guard);
|
|
970
|
+
}
|
|
971
|
+
#guardIdOf(local, site) {
|
|
972
|
+
const found = this.#findGuard(local, site, 0);
|
|
973
|
+
if ("guard" in found)
|
|
974
|
+
return this.#guardNodeOf(found.guard);
|
|
975
|
+
const { name, from } = found;
|
|
976
|
+
const isFramework = from === frameworkOwner || from === undefined && !!builtinGuards[name];
|
|
977
|
+
const builtin = isFramework ? builtinGuards[name] : undefined;
|
|
978
|
+
const owner = isFramework ? null : from ?? null;
|
|
979
|
+
const detail = [...builtin ? [builtin.scope] : [], ...isFramework ? [frameworkOwner] : []];
|
|
980
|
+
const node = this.#addNode(this.#key("guard", owner, name), "guard", owner, null, name, null, detail);
|
|
981
|
+
if (builtin)
|
|
982
|
+
node.doc = builtin.doc;
|
|
983
|
+
return node.id;
|
|
984
|
+
}
|
|
985
|
+
#findGuard(local, site, depth) {
|
|
986
|
+
const [head = local, member] = local.split(".");
|
|
987
|
+
const imported = site.imports.find((item) => item.name === head);
|
|
988
|
+
const from = imported ? this.#ownerOfSpecifier(site.fileName, imported.from) : undefined;
|
|
989
|
+
const holder = imported ? from : site.owner;
|
|
990
|
+
if (member && imported?.imported !== "*" && holder !== undefined && holder !== frameworkOwner && depth < maxMapDepth) {
|
|
991
|
+
const objectName = imported && imported.imported !== "default" ? imported.imported : head;
|
|
992
|
+
for (const entry of this.#mapEntriesOf(objectName, member, imported ? null : site.fileName, holder)) {
|
|
993
|
+
const found = this.#findGuard(entry.name, entry.site, depth + 1);
|
|
994
|
+
if ("guard" in found)
|
|
995
|
+
return found;
|
|
996
|
+
}
|
|
997
|
+
}
|
|
998
|
+
const name = member ?? (imported && imported.imported !== "default" && imported.imported !== "*" ? imported.imported : head);
|
|
999
|
+
const declared = this.#guards.get(name) ?? [];
|
|
1000
|
+
const guard = from === undefined ? declared.find((other) => other.owner === site.owner) ?? declared.find((other) => this.#visible(other.owner, site.owner)) : declared.find((other) => other.owner === from) ?? (from === frameworkOwner ? undefined : this.#soleVisible(declared, from));
|
|
1001
|
+
return guard ? { guard } : { name, from };
|
|
1002
|
+
}
|
|
1003
|
+
#mapEntriesOf(objectName, member, fileName, owner) {
|
|
1004
|
+
const key = `${objectName}
|
|
1005
|
+
${member}
|
|
1006
|
+
${fileName ?? ""}
|
|
1007
|
+
${owner ?? ""}`;
|
|
1008
|
+
const cached = this.#mapEntries.get(key);
|
|
1009
|
+
if (cached)
|
|
1010
|
+
return cached;
|
|
1011
|
+
const { ts, reader } = this.#source;
|
|
1012
|
+
const entries = [];
|
|
1013
|
+
const fileNames = [
|
|
1014
|
+
...fileName ? [fileName] : [],
|
|
1015
|
+
...this.#source.files.filter(([other, words]) => other !== fileName && owner !== null && !testFilePattern.test(other) && words.includes(`
|
|
1016
|
+
${objectName}
|
|
1017
|
+
`) && words.includes(`
|
|
1018
|
+
${member}
|
|
1019
|
+
`) && this.#source.ownerOf(other) === owner).map(([other]) => other)
|
|
1020
|
+
];
|
|
1021
|
+
for (const holder of fileNames) {
|
|
1022
|
+
const text = this.#source.readText(holder);
|
|
1023
|
+
if (text === undefined)
|
|
1024
|
+
continue;
|
|
1025
|
+
const source = this.#source.parse(holder, text);
|
|
1026
|
+
const site = { fileName: holder, owner: this.#source.ownerOf(holder), imports: reader.importsIn(source) };
|
|
1027
|
+
for (const statement of source.statements) {
|
|
1028
|
+
if (!ts.isVariableStatement(statement))
|
|
1029
|
+
continue;
|
|
1030
|
+
const map = statement.declarationList.declarations.find((declaration) => ts.isIdentifier(declaration.name) && declaration.name.text === objectName)?.initializer;
|
|
1031
|
+
const object = map && reader.unwrap(map);
|
|
1032
|
+
if (!object || !ts.isObjectLiteralExpression(object))
|
|
1033
|
+
continue;
|
|
1034
|
+
for (const property of object.properties) {
|
|
1035
|
+
if (ts.isShorthandPropertyAssignment(property) && property.name.text === member)
|
|
1036
|
+
entries.push({ name: member, site });
|
|
1037
|
+
if (!ts.isPropertyAssignment(property) || !(ts.isIdentifier(property.name) || ts.isStringLiteral(property.name)) || property.name.text !== member)
|
|
1038
|
+
continue;
|
|
1039
|
+
const value = reader.unwrap(property.initializer);
|
|
1040
|
+
if (ts.isIdentifier(value))
|
|
1041
|
+
entries.push({ name: value.text, site });
|
|
1042
|
+
else if (ts.isPropertyAccessExpression(value) && ts.isIdentifier(value.expression))
|
|
1043
|
+
entries.push({ name: `${value.expression.text}.${value.name.text}`, site });
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
this.#mapEntries.set(key, entries);
|
|
1048
|
+
return entries;
|
|
1049
|
+
}
|
|
1050
|
+
#declarationOf(owner, name, near) {
|
|
1051
|
+
const key = `${owner}
|
|
1052
|
+
${name}
|
|
1053
|
+
${near ?? ""}`;
|
|
1054
|
+
const cached = this.#declarations.get(key);
|
|
1055
|
+
if (cached !== undefined)
|
|
1056
|
+
return cached;
|
|
1057
|
+
const isNear = (fileName) => {
|
|
1058
|
+
const relative = this.#source.relative(fileName);
|
|
1059
|
+
return !!near && (relative.startsWith(`${near}/`) || relative.replace(/\.tsx?$/, "") === near);
|
|
1060
|
+
};
|
|
1061
|
+
const candidates = this.#source.files.filter(([fileName, words]) => !testFilePattern.test(fileName) && words.includes(`
|
|
1062
|
+
${name}
|
|
1063
|
+
`) && this.#source.ownerOf(fileName) === owner).map(([fileName]) => fileName);
|
|
1064
|
+
let found = null;
|
|
1065
|
+
for (const fileName of [...candidates.filter(isNear), ...candidates.filter((other) => !isNear(other))]) {
|
|
1066
|
+
const text = this.#source.readText(fileName);
|
|
1067
|
+
if (text === undefined)
|
|
1068
|
+
continue;
|
|
1069
|
+
const source = this.#source.parse(fileName, text);
|
|
1070
|
+
const declared = this.#routes.declarationIn(source, name);
|
|
1071
|
+
if (!declared)
|
|
1072
|
+
continue;
|
|
1073
|
+
found = { source, statement: declared.statement, declared: declared.declared };
|
|
1074
|
+
break;
|
|
1075
|
+
}
|
|
1076
|
+
this.#declarations.set(key, found);
|
|
1077
|
+
return found;
|
|
1078
|
+
}
|
|
1079
|
+
#addRoutes() {
|
|
1080
|
+
const routes = [];
|
|
1081
|
+
const layouts = new Map;
|
|
1082
|
+
for (const [fileName, words] of this.#source.files) {
|
|
1083
|
+
const relative = this.#source.relative(fileName);
|
|
1084
|
+
const info = RouteFactReader.fileOf(relative);
|
|
1085
|
+
const text = info ? this.#source.readText(fileName) : undefined;
|
|
1086
|
+
const fact = info && text !== undefined ? this.#routes.read(fileName, info, text, words) : null;
|
|
1087
|
+
if (!fact || text === undefined)
|
|
1088
|
+
continue;
|
|
1089
|
+
const span = {
|
|
1090
|
+
fileName,
|
|
1091
|
+
start: 0,
|
|
1092
|
+
end: 0,
|
|
1093
|
+
line: fact.line,
|
|
1094
|
+
character: fact.character,
|
|
1095
|
+
endLine: 0,
|
|
1096
|
+
endCharacter: 0
|
|
1097
|
+
};
|
|
1098
|
+
const dir = `/${fact.dir.join("/")}`;
|
|
1099
|
+
const guard = fact.guard ? ["guard"] : [];
|
|
1100
|
+
const node = fact.kind === "layout" ? this.#addNode(this.#key("layout", fact.owner, dir), "layout", fact.owner, null, dir, span, [...fact.isRoot ? ["root"] : [], ...guard], `${dir === "/" ? "" : dir}/_layout`) : this.#addNode(this.#key("route", fact.owner, fact.pattern), "route", fact.owner, null, fact.pattern, span, guard);
|
|
1101
|
+
this.#digest(node, text, { start: 0, end: text.length });
|
|
1102
|
+
this.#routeFiles.set(relative, { id: node.id, alias: fact.alias });
|
|
1103
|
+
if (fact.kind === "layout")
|
|
1104
|
+
layouts.set(`${fact.owner}
|
|
1105
|
+
${dir}`, node);
|
|
1106
|
+
routes.push({ fact, node });
|
|
1107
|
+
for (const call of fact.guard?.calls ?? []) {
|
|
1108
|
+
const id = this.#routeGuardIdOf(call, fact, text);
|
|
1109
|
+
if (id && fact.guard)
|
|
1110
|
+
this.#addEdge(node.id, id, "guard", null, this.#location(fact.guard.span));
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
const parentOf = ({ owner, dir, kind }) => {
|
|
1114
|
+
for (let depth = kind === "layout" ? dir.length - 1 : dir.length;depth >= 0; depth--) {
|
|
1115
|
+
const layout = layouts.get(`${owner}
|
|
1116
|
+
/${dir.slice(0, depth).join("/")}`);
|
|
1117
|
+
if (layout)
|
|
1118
|
+
return layout;
|
|
1119
|
+
}
|
|
1120
|
+
return null;
|
|
1121
|
+
};
|
|
1122
|
+
for (const { fact, node } of routes) {
|
|
1123
|
+
const parent = parentOf(fact);
|
|
1124
|
+
if (parent)
|
|
1125
|
+
this.#addEdge(parent.id, node.id, "wraps", null, node.at);
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
#routeGuardIdOf({ name: local, local: declared }, fact, text) {
|
|
1129
|
+
if (declared) {
|
|
1130
|
+
const file = this.#source.relative(fact.fileName).slice(fact.owner.length + 1);
|
|
1131
|
+
const id = this.#key("guard", fact.owner, `${file}#${local}`);
|
|
1132
|
+
return this.#routeGuardNodeOf(id, fact.owner, local, declared.span, declared.doc, text, declared.extent);
|
|
1133
|
+
}
|
|
1134
|
+
const imported = fact.imports.find((item) => item.name === local);
|
|
1135
|
+
if (!imported)
|
|
1136
|
+
return null;
|
|
1137
|
+
const name = imported.imported !== "default" && imported.imported !== "*" ? imported.imported : local;
|
|
1138
|
+
const from = this.#ownerOfSpecifier(fact.fileName, imported.from);
|
|
1139
|
+
if (!from || from === frameworkOwner)
|
|
1140
|
+
return null;
|
|
1141
|
+
const found = this.#declarationOf(from, name, this.#pathOfSpecifier(fact.fileName, imported.from));
|
|
1142
|
+
if (!found)
|
|
1143
|
+
return null;
|
|
1144
|
+
const { reader } = this.#source;
|
|
1145
|
+
return this.#routeGuardNodeOf(this.#key("guard", from, name), from, name, reader.spanOf(found.source, found.declared), reader.docOf(found.source, found.statement), found.source.text, reader.extentOf(found.source, found.statement));
|
|
1146
|
+
}
|
|
1147
|
+
#routeGuardNodeOf(id, owner, name, span, doc, text, extent) {
|
|
1148
|
+
const isNew = !this.#nodes.has(id);
|
|
1149
|
+
const node = this.#addNode(id, "guard", owner, null, name, span, ["route"]);
|
|
1150
|
+
if (isNew && doc)
|
|
1151
|
+
node.doc = doc;
|
|
1152
|
+
this.#digest(node, text, extent);
|
|
1153
|
+
return node.id;
|
|
1154
|
+
}
|
|
1155
|
+
#adaptorIdOf(local, file) {
|
|
1156
|
+
const [head, member] = local.split(".");
|
|
1157
|
+
const imported = file.imports.find((item) => item.name === head);
|
|
1158
|
+
const from = imported ? this.#ownerOfSpecifier(file.fileName, imported.from) : undefined;
|
|
1159
|
+
const name = member ?? (imported && imported.imported !== "default" && imported.imported !== "*" ? imported.imported : head);
|
|
1160
|
+
const declared = this.#adaptors.get(name) ?? [];
|
|
1161
|
+
const found = from === undefined ? declared.find((adaptor) => adaptor.owner === file.owner) ?? declared.find((adaptor) => this.#visible(adaptor.owner, file.owner)) : declared.find((adaptor) => adaptor.owner === from) ?? (from === frameworkOwner ? undefined : declared.find((adaptor) => this.#visible(adaptor.owner, from)));
|
|
1162
|
+
if (found)
|
|
1163
|
+
return this.#adaptorNodeOf(found);
|
|
1164
|
+
const owner = from === frameworkOwner ? null : imported ? from ?? null : file.owner;
|
|
1165
|
+
const detail = from === frameworkOwner ? [frameworkOwner] : [];
|
|
1166
|
+
return this.#addNode(this.#key("adaptor", owner, name), "adaptor", owner, null, name, null, detail).id;
|
|
1167
|
+
}
|
|
1168
|
+
#adaptorNodeOf({ fact, owner, fileName }) {
|
|
1169
|
+
const id = this.#key("adaptor", owner, fact.name);
|
|
1170
|
+
const detail = fact.refName ? [fact.refName] : [];
|
|
1171
|
+
const node = this.#addNode(id, "adaptor", owner, null, fact.name, fact.span, detail);
|
|
1172
|
+
if (fact.doc && !node.doc)
|
|
1173
|
+
node.doc = fact.doc;
|
|
1174
|
+
this.#digest(node, this.#source.readText(fileName), fact.extent);
|
|
1175
|
+
return node.id;
|
|
1176
|
+
}
|
|
1177
|
+
#guardNodeOf({ name, owner, span, scope, doc, parts }) {
|
|
1178
|
+
const node = this.#addNode(this.#key("guard", owner, name), "guard", owner, null, name, span, scope ? [scope] : []);
|
|
1179
|
+
if (doc && !node.doc)
|
|
1180
|
+
node.doc = doc;
|
|
1181
|
+
const digest = node.digest ? null : this.#hashOf(...parts.map(({ fileName, extent }) => [
|
|
1182
|
+
this.#source.readText(fileName),
|
|
1183
|
+
extent
|
|
1184
|
+
]));
|
|
1185
|
+
if (digest)
|
|
1186
|
+
node.digest = digest;
|
|
1187
|
+
return node.id;
|
|
1188
|
+
}
|
|
1189
|
+
#ownerOfSpecifier(fileName, specifier) {
|
|
1190
|
+
if (frameworkPattern.test(specifier))
|
|
1191
|
+
return frameworkOwner;
|
|
1192
|
+
const path = this.#pathOfSpecifier(fileName, specifier);
|
|
1193
|
+
return path === null ? undefined : ownerOf(path) ?? undefined;
|
|
1194
|
+
}
|
|
1195
|
+
#pathOfSpecifier(fileName, specifier) {
|
|
1196
|
+
const alias = specifier.match(aliasPattern);
|
|
1197
|
+
if (alias)
|
|
1198
|
+
return `${alias[1]}/${alias[2]}${specifier.slice(alias[0].length)}`;
|
|
1199
|
+
if (!specifier.startsWith("."))
|
|
1200
|
+
return null;
|
|
1201
|
+
const segments = this.#source.relative(fileName).split("/").slice(0, -1);
|
|
1202
|
+
for (const segment of specifier.split("/"))
|
|
1203
|
+
if (segment === "..")
|
|
1204
|
+
segments.pop();
|
|
1205
|
+
else if (segment !== ".")
|
|
1206
|
+
segments.push(segment);
|
|
1207
|
+
return segments.join("/");
|
|
1208
|
+
}
|
|
1209
|
+
#moduleDirOf(fileName) {
|
|
1210
|
+
const dir = this.#source.relative(fileName).match(moduleDirPattern)?.[1] ?? null;
|
|
1211
|
+
return dir?.startsWith("_") && !dir.startsWith("__") ? dir.slice(1) : dir;
|
|
1212
|
+
}
|
|
1213
|
+
#fieldsOf(constant, scope) {
|
|
1214
|
+
const fields = new Map;
|
|
1215
|
+
const seen = new Set;
|
|
1216
|
+
const visit = (current) => {
|
|
1217
|
+
if (seen.has(current))
|
|
1218
|
+
return;
|
|
1219
|
+
seen.add(current);
|
|
1220
|
+
for (const parent of current.parents) {
|
|
1221
|
+
const parentClass = scope.find((other) => other.name === parent && other.span.fileName === current.span.fileName) ?? scope.find((other) => other.name === parent);
|
|
1222
|
+
if (parentClass)
|
|
1223
|
+
visit(parentClass);
|
|
1224
|
+
}
|
|
1225
|
+
for (const field of current.fields) {
|
|
1226
|
+
if (fields.has(field.name))
|
|
1227
|
+
continue;
|
|
1228
|
+
const digest = this.#hashOf([this.#source.readText(field.span.fileName), field.extent]);
|
|
1229
|
+
fields.set(field.name, {
|
|
1230
|
+
name: field.name,
|
|
1231
|
+
type: field.type,
|
|
1232
|
+
array: field.array,
|
|
1233
|
+
optional: field.optional,
|
|
1234
|
+
ref: null,
|
|
1235
|
+
declaredIn: current.name,
|
|
1236
|
+
at: this.#location(field.span),
|
|
1237
|
+
...digest ? { digest } : {}
|
|
1238
|
+
});
|
|
1239
|
+
}
|
|
1240
|
+
};
|
|
1241
|
+
visit(constant);
|
|
1242
|
+
return [...fields.values()];
|
|
1243
|
+
}
|
|
1244
|
+
#visible(target, from) {
|
|
1245
|
+
return !from || !target || target === from || target.startsWith("libs/");
|
|
1246
|
+
}
|
|
1247
|
+
#soleVisible(declared, from) {
|
|
1248
|
+
const visible = declared.filter((other) => this.#visible(other.owner, from));
|
|
1249
|
+
return visible.length === 1 ? visible[0] : undefined;
|
|
1250
|
+
}
|
|
1251
|
+
#targetNodeOf({ family, owner, refName, sourceKey }) {
|
|
1252
|
+
const isDefault = /^[A-Z]/.test(sourceKey);
|
|
1253
|
+
const id = family === "slice" || family === "query" ? this.#key(family, owner, refName, isDefault ? "" : sourceKey) : family === "model" || family === "formSetter" ? this.#key("model", owner, refName) : family === "endpoint" ? this.#key("endpoint", owner, refName, sourceKey) : null;
|
|
1254
|
+
return id && this.#nodes.has(id) ? id : null;
|
|
1255
|
+
}
|
|
1256
|
+
#membersNamed(kind, name, owner, refName) {
|
|
1257
|
+
const nodes = (this.#members.get(`${kind}:${name}`) ?? []).filter((node) => (!refName || node.module === refName) && this.#visible(node.owner, owner));
|
|
1258
|
+
const own = nodes.filter((node) => node.owner === owner);
|
|
1259
|
+
return (own.length ? own : nodes).map((node) => node.id);
|
|
1260
|
+
}
|
|
1261
|
+
#plainTargetsOf(chain, name, context, owner) {
|
|
1262
|
+
const [head, second] = chain ?? [];
|
|
1263
|
+
if (chain?.length === 1 && head === "fetch")
|
|
1264
|
+
return this.#membersNamed("endpoint", name, owner);
|
|
1265
|
+
if (chain?.length === 2 && head === "st" && second === "do")
|
|
1266
|
+
return this.#membersNamed("action", name, owner);
|
|
1267
|
+
if (chain?.length === 1 && head === "this" && (context.kind === "method" || context.kind === "action"))
|
|
1268
|
+
return this.#membersNamed(context.kind, name, context.owner, context.module).filter((id) => id.startsWith(`${context.kind}:${context.owner ?? ""}:`));
|
|
1269
|
+
if (chain?.length === 2 && head === "this" && second && /.Service$/.test(second))
|
|
1270
|
+
return this.#membersNamed("method", name, owner, second.slice(0, -"Service".length));
|
|
1271
|
+
return [];
|
|
1272
|
+
}
|
|
1273
|
+
#walk(fileName, words) {
|
|
1274
|
+
const relative = this.#source.relative(fileName);
|
|
1275
|
+
const contexts = this.#contexts.get(fileName);
|
|
1276
|
+
const isComponentFile = !memberSourcePattern.test(fileName) && !skippedPattern.test(relative) && (words.includes(`
|
|
1277
|
+
fetch
|
|
1278
|
+
`) || words.includes(`
|
|
1279
|
+
st
|
|
1280
|
+
`));
|
|
1281
|
+
if (!contexts && !isComponentFile)
|
|
1282
|
+
return;
|
|
1283
|
+
const text = this.#source.readText(fileName);
|
|
1284
|
+
if (text === undefined)
|
|
1285
|
+
return;
|
|
1286
|
+
const { ts } = this.#source;
|
|
1287
|
+
const owner = this.#source.ownerOf(fileName);
|
|
1288
|
+
const source = this.#source.parse(fileName, text);
|
|
1289
|
+
const visit = (node) => {
|
|
1290
|
+
if (ts.isPropertyAccessExpression(node) && ts.isIdentifier(node.name)) {
|
|
1291
|
+
const start = node.name.getStart(source);
|
|
1292
|
+
const chain = this.#chainOf(node.expression);
|
|
1293
|
+
const context = contexts ? contexts.find(({ extent }) => extent.start <= start && start < extent.end) : chain && clientRoots.has(chain[0]) ? this.#componentOf(source, node) : undefined;
|
|
1294
|
+
if (context)
|
|
1295
|
+
this.#link(fileName, source, node.name, chain, context, owner);
|
|
1296
|
+
}
|
|
1297
|
+
ts.forEachChild(node, visit);
|
|
1298
|
+
};
|
|
1299
|
+
visit(source);
|
|
1300
|
+
}
|
|
1301
|
+
#link(fileName, source, identifier, chain, context, owner) {
|
|
1302
|
+
const plugged = chain?.length === 2 && chain[0] === "this" && context.kind === "method" && this.#isServiceThis(identifier) ? this.#plugs.get(`${context.owner ?? ""}
|
|
1303
|
+
${context.module}
|
|
1304
|
+
${chain[1]}`) : undefined;
|
|
1305
|
+
const resolved = plugged ? [plugged] : this.#source.resolve(fileName, identifier).map((target) => this.#targetNodeOf(target)).filter((id) => id !== null);
|
|
1306
|
+
const targets = resolved.length ? resolved : this.#plainTargetsOf(chain, identifier.text, context, owner);
|
|
1307
|
+
if (!targets.length)
|
|
1308
|
+
return;
|
|
1309
|
+
if (context.pending && !this.#nodes.has(context.id)) {
|
|
1310
|
+
this.#nodes.set(context.id, context.pending);
|
|
1311
|
+
this.#digest(context.pending, source.text, context.extent);
|
|
1312
|
+
}
|
|
1313
|
+
const own = targets.filter((id) => this.#nodes.get(id)?.owner === owner);
|
|
1314
|
+
const kind = context.kind === "component" ? "uses" : chain?.[0] === "fetch" ? "fetches" : "calls";
|
|
1315
|
+
const position = source.getLineAndCharacterOfPosition(identifier.getStart(source));
|
|
1316
|
+
const at = { file: this.#source.relative(fileName), line: position.line + 1, column: position.character + 1 };
|
|
1317
|
+
for (const target of new Set(own.length ? own : targets))
|
|
1318
|
+
this.#addEdge(context.id, target, kind, identifier.text, at);
|
|
1319
|
+
}
|
|
1320
|
+
#isServiceThis(identifier) {
|
|
1321
|
+
const { ts } = this.#source;
|
|
1322
|
+
for (let current = identifier.parent;!ts.isSourceFile(current); current = current.parent) {
|
|
1323
|
+
if (ts.isMethodDeclaration(current) || ts.isAccessor(current) || ts.isConstructorDeclaration(current))
|
|
1324
|
+
return ts.isClassDeclaration(current.parent) && ts.isSourceFile(current.parent.parent);
|
|
1325
|
+
if (ts.isFunctionExpression(current) || ts.isFunctionDeclaration(current) || ts.isClassLike(current))
|
|
1326
|
+
return false;
|
|
1327
|
+
}
|
|
1328
|
+
return false;
|
|
1329
|
+
}
|
|
1330
|
+
#chainOf(expression) {
|
|
1331
|
+
const { ts } = this.#source;
|
|
1332
|
+
if (ts.isNonNullExpression(expression) || ts.isParenthesizedExpression(expression))
|
|
1333
|
+
return this.#chainOf(expression.expression);
|
|
1334
|
+
if (ts.isIdentifier(expression))
|
|
1335
|
+
return [expression.text];
|
|
1336
|
+
if (expression.kind === ts.SyntaxKind.ThisKeyword)
|
|
1337
|
+
return ["this"];
|
|
1338
|
+
if (!ts.isPropertyAccessExpression(expression))
|
|
1339
|
+
return null;
|
|
1340
|
+
const head = this.#chainOf(expression.expression);
|
|
1341
|
+
return head ? [...head, expression.name.text] : null;
|
|
1342
|
+
}
|
|
1343
|
+
#componentOf(source, node) {
|
|
1344
|
+
const { ts } = this.#source;
|
|
1345
|
+
let top = node;
|
|
1346
|
+
while (top.parent && !ts.isSourceFile(top.parent))
|
|
1347
|
+
top = top.parent;
|
|
1348
|
+
const declared = ts.isVariableStatement(top) ? top.declarationList.declarations[0]?.name : ts.isFunctionDeclaration(top) || ts.isClassDeclaration(top) ? top.name : undefined;
|
|
1349
|
+
const isDefaultExport = ts.canHaveModifiers(top) && !!ts.getModifiers(top)?.some(({ kind }) => kind === ts.SyntaxKind.DefaultKeyword);
|
|
1350
|
+
const name = declared && ts.isIdentifier(declared) && !isDefaultExport ? declared.text : "default";
|
|
1351
|
+
const relative = this.#source.relative(source.fileName);
|
|
1352
|
+
const owner = this.#source.ownerOf(source.fileName);
|
|
1353
|
+
const extent = { start: top.getStart(source), end: top.getEnd() };
|
|
1354
|
+
const routeFile = this.#routeFiles.get(relative);
|
|
1355
|
+
const routeId = routeFile && (name === "default" || name === routeFile.alias) ? routeFile.id : undefined;
|
|
1356
|
+
if (routeId)
|
|
1357
|
+
return { id: routeId, kind: "component", owner, module: null, extent };
|
|
1358
|
+
const id = `component:${relative}#${name}`;
|
|
1359
|
+
const context = { id, kind: "component", owner, module: null, extent };
|
|
1360
|
+
if (this.#nodes.has(id))
|
|
1361
|
+
return context;
|
|
1362
|
+
const pagePrefix = owner ? `${owner}/page/` : null;
|
|
1363
|
+
const route = pagePrefix && relative.startsWith(pagePrefix) ? relative.slice(pagePrefix.length).replace(/\.tsx?$/, "") : null;
|
|
1364
|
+
const base = relative.slice(relative.lastIndexOf("/") + 1).replace(/\.tsx?$/, "");
|
|
1365
|
+
const label = route ? `${route}${name === "default" ? "" : `#${name}`}` : name === "default" ? base : `${base}.${name}`;
|
|
1366
|
+
const position = source.getLineAndCharacterOfPosition((declared ?? top).getStart(source));
|
|
1367
|
+
context.pending = {
|
|
1368
|
+
id,
|
|
1369
|
+
kind: "component",
|
|
1370
|
+
name,
|
|
1371
|
+
label,
|
|
1372
|
+
owner,
|
|
1373
|
+
module: this.#moduleDirOf(source.fileName),
|
|
1374
|
+
detail: route ? ["page"] : [],
|
|
1375
|
+
at: { file: relative, line: position.line + 1, column: position.character + 1 }
|
|
1376
|
+
};
|
|
1377
|
+
return context;
|
|
1378
|
+
}
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
// pkgs/@akanjs/language/ast.ts
|
|
1382
|
+
var createAst = (ts) => {
|
|
1383
|
+
const sourceFileFor = (fileName, content, scriptKind = ts.ScriptKind.TS) => ts.createSourceFile(fileName, content, ts.ScriptTarget.Latest, true, scriptKind);
|
|
1384
|
+
const nodeName = (node) => {
|
|
1385
|
+
if (!node)
|
|
1386
|
+
return null;
|
|
1387
|
+
if (ts.isIdentifier(node) || ts.isStringLiteral(node) || ts.isNumericLiteral(node))
|
|
1388
|
+
return node.text;
|
|
1389
|
+
return null;
|
|
1390
|
+
};
|
|
1391
|
+
const propertyName = (node) => ts.isPropertyAssignment(node) || ts.isShorthandPropertyAssignment(node) || ts.isMethodDeclaration(node) ? nodeName(node.name) : null;
|
|
1392
|
+
const expressionName = (expression) => {
|
|
1393
|
+
if (ts.isIdentifier(expression))
|
|
1394
|
+
return expression.text;
|
|
1395
|
+
if (ts.isPropertyAccessExpression(expression))
|
|
1396
|
+
return expression.name.text;
|
|
1397
|
+
if (ts.isCallExpression(expression))
|
|
1398
|
+
return expressionName(expression.expression);
|
|
1399
|
+
if (ts.isAsExpression(expression))
|
|
1400
|
+
return expressionName(expression.expression);
|
|
1401
|
+
return null;
|
|
1402
|
+
};
|
|
1403
|
+
const callExpressionName = (node) => ts.isPropertyAccessExpression(node.expression) ? node.expression.name.text : expressionName(node.expression);
|
|
1404
|
+
const firstObjectReturnedByArrow = (node) => {
|
|
1405
|
+
if (!ts.isArrowFunction(node) && !ts.isFunctionExpression(node))
|
|
1406
|
+
return null;
|
|
1407
|
+
if (ts.isObjectLiteralExpression(node.body))
|
|
1408
|
+
return node.body;
|
|
1409
|
+
if (ts.isParenthesizedExpression(node.body) && ts.isObjectLiteralExpression(node.body.expression)) {
|
|
1410
|
+
return node.body.expression;
|
|
1411
|
+
}
|
|
1412
|
+
if (!ts.isBlock(node.body))
|
|
1413
|
+
return null;
|
|
1414
|
+
for (const statement of node.body.statements) {
|
|
1415
|
+
if (ts.isReturnStatement(statement) && statement.expression && ts.isObjectLiteralExpression(statement.expression)) {
|
|
1416
|
+
return statement.expression;
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1419
|
+
return null;
|
|
1420
|
+
};
|
|
1421
|
+
const heritageCallNamed = (node, names) => {
|
|
1422
|
+
const heritage = node.heritageClauses?.flatMap((clause) => [...clause.types]) ?? [];
|
|
1423
|
+
for (const { expression } of heritage) {
|
|
1424
|
+
if (!ts.isCallExpression(expression))
|
|
1425
|
+
continue;
|
|
1426
|
+
const callee = expressionName(expression.expression);
|
|
1427
|
+
if (callee && names.has(callee))
|
|
1428
|
+
return { callee, call: expression };
|
|
1429
|
+
}
|
|
1430
|
+
return null;
|
|
1431
|
+
};
|
|
1432
|
+
const viaNames = new Set(["via"]);
|
|
1433
|
+
const heritageCall = (node) => heritageCallNamed(node, viaNames)?.call ?? null;
|
|
1434
|
+
return {
|
|
1435
|
+
sourceFileFor,
|
|
1436
|
+
nodeName,
|
|
1437
|
+
propertyName,
|
|
1438
|
+
expressionName,
|
|
1439
|
+
callExpressionName,
|
|
1440
|
+
firstObjectReturnedByArrow,
|
|
1441
|
+
heritageCallNamed,
|
|
1442
|
+
heritageCall
|
|
1443
|
+
};
|
|
1444
|
+
};
|
|
1445
|
+
|
|
1446
|
+
// pkgs/@akanjs/language/nameTable.ts
|
|
1447
|
+
import {
|
|
1448
|
+
databaseStateNames,
|
|
1449
|
+
endpointNames,
|
|
1450
|
+
formSetterNames,
|
|
1451
|
+
modelNames,
|
|
1452
|
+
moduleNames,
|
|
1453
|
+
queryNames,
|
|
1454
|
+
sliceNames,
|
|
1455
|
+
stateSetterName
|
|
1456
|
+
} from "akanjs/common/akanNames.ts";
|
|
1457
|
+
var capitalize2 = (text) => text.charAt(0).toUpperCase() + text.slice(1);
|
|
1458
|
+
|
|
1459
|
+
class NameTable {
|
|
1460
|
+
#byName = new Map;
|
|
1461
|
+
#byFile = new Map;
|
|
1462
|
+
constructor(files) {
|
|
1463
|
+
const all = [...files];
|
|
1464
|
+
const constantsByOwner = new Map;
|
|
1465
|
+
for (const facts of all) {
|
|
1466
|
+
const constants = constantsByOwner.get(facts.owner) ?? [];
|
|
1467
|
+
constants.push(...facts.constants);
|
|
1468
|
+
constantsByOwner.set(facts.owner, constants);
|
|
1469
|
+
}
|
|
1470
|
+
const libConstants = [...constantsByOwner].filter(([owner]) => owner?.startsWith("libs/"));
|
|
1471
|
+
const scope = (owner) => ({
|
|
1472
|
+
own: constantsByOwner.get(owner) ?? [],
|
|
1473
|
+
libs: libConstants.filter(([key]) => key !== owner).flatMap(([, constants]) => constants)
|
|
1474
|
+
});
|
|
1475
|
+
const scopes = new Map;
|
|
1476
|
+
for (const group of NameTable.#groupsOf(all)) {
|
|
1477
|
+
const constants = scopes.get(group.owner) ?? scope(group.owner);
|
|
1478
|
+
scopes.set(group.owner, constants);
|
|
1479
|
+
this.#addGroup(group, constants);
|
|
1480
|
+
}
|
|
1481
|
+
}
|
|
1482
|
+
lookup(name) {
|
|
1483
|
+
return this.#byName.get(name) ?? [];
|
|
1484
|
+
}
|
|
1485
|
+
entriesTargeting(fileName) {
|
|
1486
|
+
return this.#byFile.get(fileName) ?? [];
|
|
1487
|
+
}
|
|
1488
|
+
static #groupsOf(files) {
|
|
1489
|
+
const groups = new Map;
|
|
1490
|
+
const groupOf = (owner, refName) => {
|
|
1491
|
+
const key = `${owner ?? ""}:${refName}`;
|
|
1492
|
+
const group = groups.get(key) ?? { owner, refName, constants: [], slices: [], facts: [] };
|
|
1493
|
+
groups.set(key, group);
|
|
1494
|
+
return group;
|
|
1495
|
+
};
|
|
1496
|
+
for (const facts of files) {
|
|
1497
|
+
const refs = new Set;
|
|
1498
|
+
for (const item of [...facts.services, ...facts.slices, ...facts.endpoints, ...facts.stores, ...facts.models])
|
|
1499
|
+
if (item.model)
|
|
1500
|
+
refs.add(item.model);
|
|
1501
|
+
for (const filter of facts.filters)
|
|
1502
|
+
if (filter.name.endsWith("Filter"))
|
|
1503
|
+
refs.add(NameTable.#lowerFirst(filter.name.slice(0, -6)));
|
|
1504
|
+
for (const constant of facts.constants)
|
|
1505
|
+
refs.add(NameTable.#lowerFirst(constant.name));
|
|
1506
|
+
for (const refName of refs)
|
|
1507
|
+
groupOf(facts.owner, refName).facts.push(facts);
|
|
1508
|
+
}
|
|
1509
|
+
for (const group of groups.values()) {
|
|
1510
|
+
group.constants = group.facts.flatMap((facts) => facts.constants);
|
|
1511
|
+
group.slices = group.facts.flatMap((facts) => facts.slices.filter((slice) => slice.model === group.refName));
|
|
1512
|
+
}
|
|
1513
|
+
return [...groups.values()].filter((group) => group.facts.some((facts) => facts.services.some((item) => item.model === group.refName) || facts.slices.some((item) => item.model === group.refName) || facts.stores.some((item) => item.model === group.refName) || facts.models.some((item) => item.model === group.refName)));
|
|
1514
|
+
}
|
|
1515
|
+
static #lowerFirst(text) {
|
|
1516
|
+
return text.charAt(0).toLowerCase() + text.slice(1);
|
|
1517
|
+
}
|
|
1518
|
+
#add(entry) {
|
|
1519
|
+
if (entry.name === entry.sourceKey)
|
|
1520
|
+
return;
|
|
1521
|
+
const sameName = this.#byName.get(entry.name) ?? [];
|
|
1522
|
+
if (sameName.some((other) => other.gate === entry.gate && other.span.fileName === entry.span.fileName && other.span.start === entry.span.start))
|
|
1523
|
+
return;
|
|
1524
|
+
sameName.push(entry);
|
|
1525
|
+
this.#byName.set(entry.name, sameName);
|
|
1526
|
+
const sameFile = this.#byFile.get(entry.span.fileName) ?? [];
|
|
1527
|
+
sameFile.push(entry);
|
|
1528
|
+
this.#byFile.set(entry.span.fileName, sameFile);
|
|
1529
|
+
}
|
|
1530
|
+
#addGroup(group, constants) {
|
|
1531
|
+
const { owner, refName } = group;
|
|
1532
|
+
const Model = capitalize2(refName);
|
|
1533
|
+
const base = { owner, refName };
|
|
1534
|
+
const modelClass = group.constants.find((constant) => constant.name === Model);
|
|
1535
|
+
const facts = group.facts;
|
|
1536
|
+
const names = moduleNames(refName);
|
|
1537
|
+
const serviceClass = facts.flatMap((file) => file.services).find((item) => item.model === refName);
|
|
1538
|
+
const sliceClass = group.slices[0];
|
|
1539
|
+
const modelDoc = facts.flatMap((file) => file.models).find((item) => item.model === refName);
|
|
1540
|
+
const filterClass = facts.flatMap((file) => file.filters).find((item) => item.name === names.FilterClass);
|
|
1541
|
+
if (serviceClass) {
|
|
1542
|
+
const target = { ...base, family: "module", sourceKey: serviceClass.name, span: serviceClass.span };
|
|
1543
|
+
this.#add({ ...target, name: names.service, role: "service", gate: null, label: serviceClass.name, detail: [] });
|
|
1544
|
+
this.#add({ ...target, name: refName, role: "srv", gate: "srv", label: serviceClass.name, detail: [] });
|
|
1545
|
+
}
|
|
1546
|
+
if (sliceClass)
|
|
1547
|
+
this.#add({
|
|
1548
|
+
...base,
|
|
1549
|
+
family: "module",
|
|
1550
|
+
sourceKey: sliceClass.name,
|
|
1551
|
+
span: sliceClass.span,
|
|
1552
|
+
name: refName,
|
|
1553
|
+
role: "sig",
|
|
1554
|
+
gate: "sig",
|
|
1555
|
+
label: sliceClass.name,
|
|
1556
|
+
detail: sliceClass.guards.length ? [`guards: ${sliceClass.guards.join(", ")}`] : []
|
|
1557
|
+
});
|
|
1558
|
+
if (modelDoc)
|
|
1559
|
+
this.#add({
|
|
1560
|
+
...base,
|
|
1561
|
+
family: "module",
|
|
1562
|
+
sourceKey: modelDoc.name,
|
|
1563
|
+
span: modelDoc.span,
|
|
1564
|
+
name: refName,
|
|
1565
|
+
role: "db",
|
|
1566
|
+
gate: "db",
|
|
1567
|
+
label: modelDoc.name,
|
|
1568
|
+
detail: []
|
|
1569
|
+
});
|
|
1570
|
+
const modelRoles = modelNames(refName);
|
|
1571
|
+
const modelNameSet = new Set(Object.values(modelRoles));
|
|
1572
|
+
if (modelClass) {
|
|
1573
|
+
const target = { ...base, family: "model", sourceKey: modelClass.name, span: modelClass.span };
|
|
1574
|
+
for (const [role, name] of Object.entries(modelRoles))
|
|
1575
|
+
this.#add({
|
|
1576
|
+
...target,
|
|
1577
|
+
name,
|
|
1578
|
+
role,
|
|
1579
|
+
gate: role === "model" ? "state" : null,
|
|
1580
|
+
label: `model ${Model}`,
|
|
1581
|
+
detail: []
|
|
1582
|
+
});
|
|
1583
|
+
for (const [role, name] of Object.entries(databaseStateNames(refName)))
|
|
1584
|
+
this.#add({
|
|
1585
|
+
...target,
|
|
1586
|
+
name: stateSetterName(name),
|
|
1587
|
+
role: `set:${role}`,
|
|
1588
|
+
gate: null,
|
|
1589
|
+
label: `model ${Model}`,
|
|
1590
|
+
detail: []
|
|
1591
|
+
});
|
|
1592
|
+
for (const field of NameTable.#fieldsOf(modelClass, constants)) {
|
|
1593
|
+
const setters = formSetterNames(Model, field.name);
|
|
1594
|
+
for (const role of [
|
|
1595
|
+
"setFieldOnModel",
|
|
1596
|
+
"addFieldOnModel",
|
|
1597
|
+
"subFieldOnModel",
|
|
1598
|
+
"addOrSubFieldOnModel",
|
|
1599
|
+
"uploadFieldOnModel"
|
|
1600
|
+
])
|
|
1601
|
+
this.#add({
|
|
1602
|
+
...base,
|
|
1603
|
+
family: "formSetter",
|
|
1604
|
+
sourceKey: field.name,
|
|
1605
|
+
span: field.span,
|
|
1606
|
+
name: setters[role],
|
|
1607
|
+
role,
|
|
1608
|
+
gate: null,
|
|
1609
|
+
label: `field ${field.declaredIn}.${field.name}`,
|
|
1610
|
+
detail: []
|
|
1611
|
+
});
|
|
1612
|
+
}
|
|
1613
|
+
}
|
|
1614
|
+
for (const slice of group.slices) {
|
|
1615
|
+
const defaultTarget = {
|
|
1616
|
+
...base,
|
|
1617
|
+
family: "slice",
|
|
1618
|
+
sourceKey: slice.name,
|
|
1619
|
+
span: slice.span,
|
|
1620
|
+
label: `slice ${refName} (default)`,
|
|
1621
|
+
detail: slice.guards.length ? [`guards: ${slice.guards.join(", ")}`] : []
|
|
1622
|
+
};
|
|
1623
|
+
this.#addSliceNames(defaultTarget, refName, "", modelNameSet);
|
|
1624
|
+
for (const key of slice.keys)
|
|
1625
|
+
this.#addSliceNames({
|
|
1626
|
+
...base,
|
|
1627
|
+
family: "slice",
|
|
1628
|
+
sourceKey: key.name,
|
|
1629
|
+
span: key.span,
|
|
1630
|
+
label: `slice ${key.name}`,
|
|
1631
|
+
detail: [...key.guards.length ? [`guards: ${key.guards.join(", ")}`] : [], ...key.live ? ["live"] : []]
|
|
1632
|
+
}, refName, key.name, modelNameSet, slice.name);
|
|
1633
|
+
}
|
|
1634
|
+
if (filterClass) {
|
|
1635
|
+
const defaults = { ...base, family: "query", detail: [] };
|
|
1636
|
+
for (const [role, name] of Object.entries(queryNames("any")))
|
|
1637
|
+
this.#add({
|
|
1638
|
+
...defaults,
|
|
1639
|
+
sourceKey: filterClass.name,
|
|
1640
|
+
span: filterClass.span,
|
|
1641
|
+
name,
|
|
1642
|
+
role,
|
|
1643
|
+
gate: null,
|
|
1644
|
+
label: `query any (default) of ${filterClass.name}`
|
|
1645
|
+
});
|
|
1646
|
+
for (const query of filterClass.queries)
|
|
1647
|
+
for (const [role, name] of Object.entries(queryNames(query.name)))
|
|
1648
|
+
this.#add({
|
|
1649
|
+
...defaults,
|
|
1650
|
+
sourceKey: query.name,
|
|
1651
|
+
span: query.span,
|
|
1652
|
+
name,
|
|
1653
|
+
role,
|
|
1654
|
+
gate: null,
|
|
1655
|
+
label: `query ${query.name}`,
|
|
1656
|
+
renamed: (key) => queryNames(key)[role],
|
|
1657
|
+
dictionary: { section: "query", className: filterClass.name }
|
|
1658
|
+
});
|
|
1659
|
+
}
|
|
1660
|
+
for (const store of facts.flatMap((file) => file.stores).filter((item) => item.model === refName))
|
|
1661
|
+
for (const key of store.stateKeys)
|
|
1662
|
+
this.#add({
|
|
1663
|
+
...base,
|
|
1664
|
+
family: "storeState",
|
|
1665
|
+
sourceKey: key.name,
|
|
1666
|
+
span: key.span,
|
|
1667
|
+
name: stateSetterName(key.name),
|
|
1668
|
+
role: "set",
|
|
1669
|
+
gate: null,
|
|
1670
|
+
label: `store state ${key.name}`,
|
|
1671
|
+
detail: []
|
|
1672
|
+
});
|
|
1673
|
+
for (const endpoint of facts.flatMap((file) => file.endpoints).filter((item) => item.model === refName))
|
|
1674
|
+
for (const key of endpoint.keys) {
|
|
1675
|
+
const derived = endpointNames(key.name);
|
|
1676
|
+
const name = key.verb === "pubsub" ? derived.subscribe : key.verb === "message" ? derived.listen : null;
|
|
1677
|
+
if (!name)
|
|
1678
|
+
continue;
|
|
1679
|
+
this.#add({
|
|
1680
|
+
...base,
|
|
1681
|
+
family: "endpoint",
|
|
1682
|
+
sourceKey: key.name,
|
|
1683
|
+
span: key.span,
|
|
1684
|
+
name,
|
|
1685
|
+
role: key.verb === "pubsub" ? "subscribe" : "listen",
|
|
1686
|
+
gate: null,
|
|
1687
|
+
label: `${key.verb} ${key.name}`,
|
|
1688
|
+
detail: key.guards.length ? [`guards: ${key.guards.join(", ")}`] : []
|
|
1689
|
+
});
|
|
1690
|
+
}
|
|
1691
|
+
}
|
|
1692
|
+
static #sliceRoles(refName, suffix) {
|
|
1693
|
+
const names = sliceNames(refName, suffix);
|
|
1694
|
+
return new Map([
|
|
1695
|
+
["slice", names.slice],
|
|
1696
|
+
...Object.entries(names.state),
|
|
1697
|
+
...Object.entries(names.action),
|
|
1698
|
+
...Object.entries(names.endpoint),
|
|
1699
|
+
...Object.entries(names.state).map(([role, name]) => [`set:${role}`, stateSetterName(name)])
|
|
1700
|
+
]);
|
|
1701
|
+
}
|
|
1702
|
+
#addSliceNames(target, refName, suffix, skip, className) {
|
|
1703
|
+
for (const [role, name] of NameTable.#sliceRoles(refName, suffix)) {
|
|
1704
|
+
if (!suffix && role !== "slice" && skip.has(name))
|
|
1705
|
+
continue;
|
|
1706
|
+
this.#add({
|
|
1707
|
+
...target,
|
|
1708
|
+
name,
|
|
1709
|
+
role,
|
|
1710
|
+
gate: role === "slice" ? "slice" : null,
|
|
1711
|
+
...suffix && className ? {
|
|
1712
|
+
renamed: (key) => NameTable.#sliceRoles(refName, key).get(role) ?? name,
|
|
1713
|
+
dictionary: { section: "slice", className }
|
|
1714
|
+
} : {}
|
|
1715
|
+
});
|
|
1716
|
+
}
|
|
1717
|
+
}
|
|
1718
|
+
static #fieldsOf(modelClass, { own, libs }) {
|
|
1719
|
+
const classNamed = (name, near) => own.find((constant) => constant.name === name && constant.span.fileName === near.span.fileName) ?? own.find((constant) => constant.name === name) ?? libs.find((constant) => constant.name === name);
|
|
1720
|
+
const fields = new Map;
|
|
1721
|
+
const seen = new Set;
|
|
1722
|
+
const visit = (constant) => {
|
|
1723
|
+
if (seen.has(constant))
|
|
1724
|
+
return;
|
|
1725
|
+
seen.add(constant);
|
|
1726
|
+
for (const field of constant.fields)
|
|
1727
|
+
if (!fields.has(field.name))
|
|
1728
|
+
fields.set(field.name, { ...field, declaredIn: constant.name });
|
|
1729
|
+
for (const parent of constant.parents) {
|
|
1730
|
+
const parentClass = classNamed(parent, constant);
|
|
1731
|
+
if (parentClass)
|
|
1732
|
+
visit(parentClass);
|
|
1733
|
+
}
|
|
1734
|
+
};
|
|
1735
|
+
visit(modelClass);
|
|
1736
|
+
return [...fields.values()];
|
|
1737
|
+
}
|
|
1738
|
+
}
|
|
1739
|
+
|
|
1740
|
+
// pkgs/@akanjs/language/akanIndex.ts
|
|
1741
|
+
var sourcePattern = /\.(constant|document|service|signal|store)\.ts$/;
|
|
1742
|
+
var dictionaryPattern = /\.dictionary\.ts$/;
|
|
1743
|
+
var srvkitPattern = /\/srvkit\//;
|
|
1744
|
+
var wordPattern = /[A-Za-z_$][\w$]*/g;
|
|
1745
|
+
var identifierPattern = /^[A-Za-z_$][\w$]*$/;
|
|
1746
|
+
var regExpSpecials = /[\\^$.*+?()[\]{}|]/g;
|
|
1747
|
+
var stateReceivers = new Set(["use", "sel", "get", "do"]);
|
|
1748
|
+
var lensFamilies = new Set(["slice", "query", "formSetter", "storeState", "endpoint"]);
|
|
1749
|
+
var modellessFamilies = new Set(["query", "storeState", "endpoint"]);
|
|
1750
|
+
var gateWords = { srv: "srv", sig: "sig", db: "db", slice: "slice" };
|
|
1751
|
+
var parseCacheSize = 64;
|
|
1752
|
+
var pathOf = (fileName) => fileName.replace(/\\/g, "/");
|
|
1753
|
+
var escapeRegExp = (text) => text.replace(regExpSpecials, (special) => `\\${special}`);
|
|
1754
|
+
var receiverGap = String.raw`(?:\s|\)|\/\*[\s\S]*?\*\/)*`;
|
|
1755
|
+
|
|
1756
|
+
class AkanIndex {
|
|
1757
|
+
#ts;
|
|
1758
|
+
#root;
|
|
1759
|
+
#ast;
|
|
1760
|
+
#reader;
|
|
1761
|
+
#facts = new Map;
|
|
1762
|
+
#fileWords = new Map;
|
|
1763
|
+
#parsed = new Map;
|
|
1764
|
+
#table = null;
|
|
1765
|
+
constructor({ ts, root }) {
|
|
1766
|
+
this.#ts = ts;
|
|
1767
|
+
this.#root = pathOf(root).replace(/\/$/, "");
|
|
1768
|
+
this.#ast = createAst(ts);
|
|
1769
|
+
this.#reader = createFactReader(ts, this.#ast);
|
|
1770
|
+
}
|
|
1771
|
+
static isSourcePath(fileName) {
|
|
1772
|
+
return sourcePattern.test(fileName);
|
|
1773
|
+
}
|
|
1774
|
+
get fileCount() {
|
|
1775
|
+
return this.#fileWords.size;
|
|
1776
|
+
}
|
|
1777
|
+
setFile(givenName, text) {
|
|
1778
|
+
const fileName = pathOf(givenName);
|
|
1779
|
+
const words = `
|
|
1780
|
+
${[...new Set(text.match(wordPattern))].join(`
|
|
1781
|
+
`)}
|
|
1782
|
+
`;
|
|
1783
|
+
this.#fileWords.set(fileName, words);
|
|
1784
|
+
const isSource = AkanIndex.isSourcePath(fileName);
|
|
1785
|
+
if (!isSource && !AkanIndex.#holdsFacts(fileName, words)) {
|
|
1786
|
+
this.#facts.delete(fileName);
|
|
1787
|
+
return;
|
|
1788
|
+
}
|
|
1789
|
+
const source = this.#parsed.get(fileName)?.text === text ? this.#parse(fileName, text) : this.#parseUncached(fileName, text);
|
|
1790
|
+
this.#facts.set(fileName, this.#reader.read(source, this.#ownerOf(fileName)));
|
|
1791
|
+
if (isSource)
|
|
1792
|
+
this.#table = null;
|
|
1793
|
+
}
|
|
1794
|
+
removeFile(givenName) {
|
|
1795
|
+
const fileName = pathOf(givenName);
|
|
1796
|
+
this.#fileWords.delete(fileName);
|
|
1797
|
+
this.#parsed.delete(fileName);
|
|
1798
|
+
if (this.#facts.delete(fileName) && AkanIndex.isSourcePath(fileName))
|
|
1799
|
+
this.#table = null;
|
|
1800
|
+
}
|
|
1801
|
+
definitionAt(givenName, text, offset) {
|
|
1802
|
+
const fileName = pathOf(givenName);
|
|
1803
|
+
const site = this.#useAt(this.#parse(fileName, text), offset);
|
|
1804
|
+
return site ? this.#resolve(fileName, site) : [];
|
|
1805
|
+
}
|
|
1806
|
+
hoverAt(givenName, text, offset) {
|
|
1807
|
+
const fileName = pathOf(givenName);
|
|
1808
|
+
const site = this.#useAt(this.#parse(fileName, text), offset);
|
|
1809
|
+
if (!site)
|
|
1810
|
+
return null;
|
|
1811
|
+
const targets = this.#resolve(fileName, site);
|
|
1812
|
+
return targets.length ? { name: site.identifier.text, targets } : null;
|
|
1813
|
+
}
|
|
1814
|
+
sourceKeysOf(givenName) {
|
|
1815
|
+
const keys = new Map;
|
|
1816
|
+
for (const entry of this.#nameTable().entriesTargeting(pathOf(givenName))) {
|
|
1817
|
+
if (!lensFamilies.has(entry.family) || keys.has(entry.span.start))
|
|
1818
|
+
continue;
|
|
1819
|
+
keys.set(entry.span.start, AkanIndex.#sourceKeyOf(entry));
|
|
1820
|
+
}
|
|
1821
|
+
return [...keys.values()].sort((a, b) => a.span.start - b.span.start);
|
|
1822
|
+
}
|
|
1823
|
+
sourceKeyAt(givenName, offset) {
|
|
1824
|
+
const entries = this.#sourceEntriesAt(pathOf(givenName), offset);
|
|
1825
|
+
const entry = entries.find((candidate) => lensFamilies.has(candidate.family)) ?? entries.at(0);
|
|
1826
|
+
return entry ? AkanIndex.#sourceKeyOf(entry) : null;
|
|
1827
|
+
}
|
|
1828
|
+
referencesAt(givenName, offset, readText) {
|
|
1829
|
+
return this.#usesOf(this.#sourceEntriesAt(pathOf(givenName), offset), readText).map((use) => use.span);
|
|
1830
|
+
}
|
|
1831
|
+
graph(readText, { digests = false } = {}) {
|
|
1832
|
+
return new GraphBuilder({
|
|
1833
|
+
digests,
|
|
1834
|
+
ts: this.#ts,
|
|
1835
|
+
facts: [...this.#facts].sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([, facts]) => facts),
|
|
1836
|
+
files: [...this.#fileWords].sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0),
|
|
1837
|
+
readText,
|
|
1838
|
+
parse: (fileName, text) => this.#parse(fileName, text),
|
|
1839
|
+
resolve: (fileName, identifier) => {
|
|
1840
|
+
if (!this.#nameTable().lookup(identifier.text).length)
|
|
1841
|
+
return [];
|
|
1842
|
+
const site = this.#siteOf(identifier);
|
|
1843
|
+
return site ? this.#resolve(fileName, site) : [];
|
|
1844
|
+
},
|
|
1845
|
+
relative: (fileName) => this.#relative(fileName),
|
|
1846
|
+
ownerOf: (fileName) => this.#ownerOf(fileName),
|
|
1847
|
+
reader: this.#reader
|
|
1848
|
+
}).build();
|
|
1849
|
+
}
|
|
1850
|
+
renameTargetAt(givenName, offset) {
|
|
1851
|
+
const source = this.#sourceEntriesAt(pathOf(givenName), offset).find((entry) => entry.renamed);
|
|
1852
|
+
return source ? { family: source.family, sourceKey: source.sourceKey, span: source.span } : null;
|
|
1853
|
+
}
|
|
1854
|
+
renameAt(givenName, offset, newKey, readText) {
|
|
1855
|
+
const sources = this.#sourceEntriesAt(pathOf(givenName), offset).filter((entry) => entry.renamed);
|
|
1856
|
+
if (!sources.length || !identifierPattern.test(newKey))
|
|
1857
|
+
return null;
|
|
1858
|
+
return [
|
|
1859
|
+
{ span: sources[0].span, newText: newKey },
|
|
1860
|
+
...this.#usesOf(sources, readText).map(({ span, entry, shorthand }) => {
|
|
1861
|
+
const renamed = entry.renamed?.(newKey) ?? entry.name;
|
|
1862
|
+
return { span, newText: shorthand ? `${renamed}: ${entry.name}` : renamed };
|
|
1863
|
+
}),
|
|
1864
|
+
...this.#dictionaryKeysOf(sources[0], readText).map(({ span, quote }) => ({
|
|
1865
|
+
span,
|
|
1866
|
+
newText: `${quote}${newKey}${quote}`
|
|
1867
|
+
}))
|
|
1868
|
+
];
|
|
1869
|
+
}
|
|
1870
|
+
static #holdsFacts(fileName, words) {
|
|
1871
|
+
if (testFilePattern.test(fileName))
|
|
1872
|
+
return false;
|
|
1873
|
+
if (srvkitPattern.test(fileName))
|
|
1874
|
+
return true;
|
|
1875
|
+
const holdsGuards = words.includes(`
|
|
1876
|
+
Guard
|
|
1877
|
+
`) && words.includes(`
|
|
1878
|
+
canPass
|
|
1879
|
+
`);
|
|
1880
|
+
return holdsGuards || words.includes(`
|
|
1881
|
+
adapt
|
|
1882
|
+
`) || words.includes(`
|
|
1883
|
+
dangerouslyAdapt
|
|
1884
|
+
`);
|
|
1885
|
+
}
|
|
1886
|
+
static #sourceKeyOf({ family, sourceKey, label, detail, span }) {
|
|
1887
|
+
return { family, sourceKey, label, detail, span };
|
|
1888
|
+
}
|
|
1889
|
+
#usesOf(sources, readText) {
|
|
1890
|
+
if (!sources.length)
|
|
1891
|
+
return [];
|
|
1892
|
+
const ts = this.#ts;
|
|
1893
|
+
const isSource = (target) => sources.some((source) => source.span.fileName === target.span.fileName && source.span.start === target.span.start);
|
|
1894
|
+
const refNames = new Set(sources.map((source) => source.refName));
|
|
1895
|
+
const byName = new Map(sources.map((source) => [source.name, source]));
|
|
1896
|
+
const naming = new RegExp(`\\n(?:${[...byName.keys()].map(escapeRegExp).join("|")})\\n`);
|
|
1897
|
+
const gateWordsOf = sources.map((source) => source.gate ? gateWords[source.gate] : undefined);
|
|
1898
|
+
const receiverPatterns = sources.map((source, at) => {
|
|
1899
|
+
const gateWord = gateWordsOf[at];
|
|
1900
|
+
return gateWord ? new RegExp(String.raw`(?<![\w$])${gateWord}${receiverGap}[?!]?\.${receiverGap}${escapeRegExp(source.name)}(?![\w$])`) : null;
|
|
1901
|
+
});
|
|
1902
|
+
const named = (words, at) => words.includes(`
|
|
1903
|
+
${sources[at].name}
|
|
1904
|
+
`) && (!gateWordsOf[at] || words.includes(`
|
|
1905
|
+
${gateWordsOf[at]}
|
|
1906
|
+
`));
|
|
1907
|
+
const candidates = [...this.#fileWords].filter(([, words]) => naming.test(words) && sources.some((_, at) => named(words, at)));
|
|
1908
|
+
const found = [];
|
|
1909
|
+
for (const [file, words] of candidates.sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)) {
|
|
1910
|
+
const text = readText(file);
|
|
1911
|
+
if (text === undefined)
|
|
1912
|
+
continue;
|
|
1913
|
+
if (!sources.some((_, at) => named(words, at) && (receiverPatterns[at]?.test(text) ?? true)))
|
|
1914
|
+
continue;
|
|
1915
|
+
const source = this.#parse(file, text);
|
|
1916
|
+
const visit = (node) => {
|
|
1917
|
+
const entry = ts.isIdentifier(node) ? byName.get(node.text) : undefined;
|
|
1918
|
+
const site = entry && ts.isIdentifier(node) ? this.#siteOf(node) : null;
|
|
1919
|
+
const targets = site ? this.#resolve(file, site) : [];
|
|
1920
|
+
if (entry && site && targets.some(isSource) && targets.every((target) => refNames.has(target.refName))) {
|
|
1921
|
+
const parent = site.identifier.parent;
|
|
1922
|
+
const shorthand = ts.isBindingElement(parent) && !parent.propertyName;
|
|
1923
|
+
found.push({ span: this.#reader.spanOf(source, site.identifier), entry, shorthand });
|
|
1924
|
+
}
|
|
1925
|
+
ts.forEachChild(node, visit);
|
|
1926
|
+
};
|
|
1927
|
+
visit(source);
|
|
1928
|
+
}
|
|
1929
|
+
return found;
|
|
1930
|
+
}
|
|
1931
|
+
#dictionaryKeysOf(source, readText) {
|
|
1932
|
+
const { dictionary } = source;
|
|
1933
|
+
if (!dictionary)
|
|
1934
|
+
return [];
|
|
1935
|
+
const ts = this.#ts;
|
|
1936
|
+
const keys = [];
|
|
1937
|
+
const moduleDir = source.span.fileName.slice(0, source.span.fileName.lastIndexOf("/"));
|
|
1938
|
+
for (const [file, words] of this.#fileWords) {
|
|
1939
|
+
if (!dictionaryPattern.test(file) || this.#ownerOf(file) !== source.owner)
|
|
1940
|
+
continue;
|
|
1941
|
+
const sameModule = file.slice(0, file.lastIndexOf("/")) === moduleDir;
|
|
1942
|
+
if (!words.includes(`
|
|
1943
|
+
${dictionary.section}
|
|
1944
|
+
`))
|
|
1945
|
+
continue;
|
|
1946
|
+
if (!sameModule && !words.includes(`
|
|
1947
|
+
${dictionary.className}
|
|
1948
|
+
`))
|
|
1949
|
+
continue;
|
|
1950
|
+
const text = readText(file);
|
|
1951
|
+
if (text === undefined)
|
|
1952
|
+
continue;
|
|
1953
|
+
const parsed = this.#parse(file, text);
|
|
1954
|
+
const visit = (node) => {
|
|
1955
|
+
const typeArgument = ts.isCallExpression(node) ? node.typeArguments?.[0] : undefined;
|
|
1956
|
+
const builder = ts.isCallExpression(node) ? node.arguments.at(-1) : undefined;
|
|
1957
|
+
const typed = !!typeArgument && ts.isTypeReferenceNode(typeArgument) && ts.isIdentifier(typeArgument.typeName) && typeArgument.typeName.text === dictionary.className;
|
|
1958
|
+
const untyped = !typeArgument && sameModule;
|
|
1959
|
+
const object = ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && node.expression.name.text === dictionary.section && (typed || untyped) && builder ? this.#ast.firstObjectReturnedByArrow(builder) : null;
|
|
1960
|
+
for (const property of object?.properties ?? [])
|
|
1961
|
+
if (ts.isPropertyAssignment(property) && this.#ast.nodeName(property.name) === source.sourceKey)
|
|
1962
|
+
keys.push({
|
|
1963
|
+
span: this.#reader.spanOf(parsed, property.name),
|
|
1964
|
+
quote: ts.isStringLiteral(property.name) ? parsed.text.charAt(property.name.getStart(parsed)) : ""
|
|
1965
|
+
});
|
|
1966
|
+
ts.forEachChild(node, visit);
|
|
1967
|
+
};
|
|
1968
|
+
visit(parsed);
|
|
1969
|
+
}
|
|
1970
|
+
return keys;
|
|
1971
|
+
}
|
|
1972
|
+
#sourceEntriesAt(fileName, offset) {
|
|
1973
|
+
return this.#nameTable().entriesTargeting(fileName).filter((entry) => entry.span.start <= offset && offset <= entry.span.end);
|
|
1974
|
+
}
|
|
1975
|
+
#nameTable() {
|
|
1976
|
+
this.#table ??= new NameTable([...this.#facts.values()].filter(({ fileName }) => AkanIndex.isSourcePath(fileName)));
|
|
1977
|
+
return this.#table;
|
|
1978
|
+
}
|
|
1979
|
+
#relative(fileName) {
|
|
1980
|
+
return fileName.startsWith(`${this.#root}/`) ? fileName.slice(this.#root.length + 1) : fileName;
|
|
1981
|
+
}
|
|
1982
|
+
#ownerOf(fileName) {
|
|
1983
|
+
return ownerOf(this.#relative(fileName));
|
|
1984
|
+
}
|
|
1985
|
+
#parseUncached(fileName, text) {
|
|
1986
|
+
const kind = fileName.endsWith(".tsx") ? this.#ts.ScriptKind.TSX : this.#ts.ScriptKind.TS;
|
|
1987
|
+
return this.#ast.sourceFileFor(fileName, text, kind);
|
|
1988
|
+
}
|
|
1989
|
+
#parse(fileName, text) {
|
|
1990
|
+
const cached = this.#parsed.get(fileName);
|
|
1991
|
+
if (cached?.text === text)
|
|
1992
|
+
return cached.source;
|
|
1993
|
+
const source = this.#parseUncached(fileName, text);
|
|
1994
|
+
this.#parsed.delete(fileName);
|
|
1995
|
+
this.#parsed.set(fileName, { text, source });
|
|
1996
|
+
if (this.#parsed.size > parseCacheSize) {
|
|
1997
|
+
const oldest = this.#parsed.keys().next().value;
|
|
1998
|
+
if (oldest !== undefined)
|
|
1999
|
+
this.#parsed.delete(oldest);
|
|
2000
|
+
}
|
|
2001
|
+
return source;
|
|
2002
|
+
}
|
|
2003
|
+
#useAt(source, offset) {
|
|
2004
|
+
const ts = this.#ts;
|
|
2005
|
+
let found = null;
|
|
2006
|
+
const visit = (node) => {
|
|
2007
|
+
if (offset < node.getStart(source) || offset > node.getEnd())
|
|
2008
|
+
return;
|
|
2009
|
+
if (ts.isIdentifier(node))
|
|
2010
|
+
found = node;
|
|
2011
|
+
ts.forEachChild(node, visit);
|
|
2012
|
+
};
|
|
2013
|
+
visit(source);
|
|
2014
|
+
return found ? this.#siteOf(found) : null;
|
|
2015
|
+
}
|
|
2016
|
+
#siteOf(identifier) {
|
|
2017
|
+
const ts = this.#ts;
|
|
2018
|
+
const parent = identifier.parent;
|
|
2019
|
+
let chain;
|
|
2020
|
+
if (ts.isPropertyAccessExpression(parent) && parent.name === identifier)
|
|
2021
|
+
chain = this.#chainOf(parent.expression);
|
|
2022
|
+
else if (ts.isBindingElement(parent) && !parent.dotDotDotToken && ts.isObjectBindingPattern(parent.parent) && (parent.propertyName === identifier || !parent.propertyName && parent.name === identifier))
|
|
2023
|
+
chain = null;
|
|
2024
|
+
else
|
|
2025
|
+
return null;
|
|
2026
|
+
const { model, inStore } = this.#classContextOf(identifier);
|
|
2027
|
+
return { identifier, chain, classModel: model, inStore };
|
|
2028
|
+
}
|
|
2029
|
+
#chainOf(expression) {
|
|
2030
|
+
const ts = this.#ts;
|
|
2031
|
+
if (ts.isNonNullExpression(expression) || ts.isParenthesizedExpression(expression))
|
|
2032
|
+
return this.#chainOf(expression.expression);
|
|
2033
|
+
if (ts.isIdentifier(expression))
|
|
2034
|
+
return [expression.text];
|
|
2035
|
+
if (expression.kind === ts.SyntaxKind.ThisKeyword)
|
|
2036
|
+
return ["this"];
|
|
2037
|
+
if (ts.isPropertyAccessExpression(expression))
|
|
2038
|
+
return [...this.#chainOf(expression.expression), expression.name.text];
|
|
2039
|
+
return ["?"];
|
|
2040
|
+
}
|
|
2041
|
+
#classContextOf(node) {
|
|
2042
|
+
const ts = this.#ts;
|
|
2043
|
+
let current = node.parent;
|
|
2044
|
+
while (current && !ts.isClassDeclaration(current) && !ts.isClassExpression(current))
|
|
2045
|
+
current = current.parent;
|
|
2046
|
+
if (!current)
|
|
2047
|
+
return { model: null, inStore: false };
|
|
2048
|
+
const className = current.name?.text;
|
|
2049
|
+
const heritage = current.heritageClauses?.flatMap((clause) => [...clause.types]) ?? [];
|
|
2050
|
+
for (const { expression } of heritage) {
|
|
2051
|
+
if (!ts.isCallExpression(expression))
|
|
2052
|
+
continue;
|
|
2053
|
+
const callee = this.#ast.expressionName(expression.expression);
|
|
2054
|
+
if (callee === "into" && className?.endsWith("Model"))
|
|
2055
|
+
return { model: className.charAt(0).toLowerCase() + className.slice(1, -5), inStore: false };
|
|
2056
|
+
const holder = callee === "serve" ? "db" : callee === "store" ? "sig" : callee === "slice" || callee === "endpoint" || callee === "internal" ? "srv" : null;
|
|
2057
|
+
if (!holder)
|
|
2058
|
+
continue;
|
|
2059
|
+
return { model: this.#reader.moduleRefOf(expression.arguments[0], holder), inStore: callee === "store" };
|
|
2060
|
+
}
|
|
2061
|
+
return { model: null, inStore: false };
|
|
2062
|
+
}
|
|
2063
|
+
#modelHintOf(site) {
|
|
2064
|
+
const { chain } = site;
|
|
2065
|
+
if (!chain)
|
|
2066
|
+
return null;
|
|
2067
|
+
if (chain.length === 1 && chain[0] === "this")
|
|
2068
|
+
return site.classModel;
|
|
2069
|
+
const holder = chain.at(-1)?.replace(/^#/, "");
|
|
2070
|
+
const suffix = holder?.match(/.(Service|Model)$/)?.[1];
|
|
2071
|
+
if (holder && suffix)
|
|
2072
|
+
return holder.slice(0, -suffix.length);
|
|
2073
|
+
if (chain.length === 2 && (chain[0] === "db" || chain[0] === "srv"))
|
|
2074
|
+
return chain[1];
|
|
2075
|
+
return null;
|
|
2076
|
+
}
|
|
2077
|
+
#passesGate(entry, site) {
|
|
2078
|
+
const { chain } = site;
|
|
2079
|
+
const last = chain?.at(-1);
|
|
2080
|
+
switch (entry.gate) {
|
|
2081
|
+
case "srv":
|
|
2082
|
+
case "sig":
|
|
2083
|
+
case "db":
|
|
2084
|
+
return chain?.length === 1 && last === entry.gate;
|
|
2085
|
+
case "slice":
|
|
2086
|
+
return last === "slice";
|
|
2087
|
+
case "state":
|
|
2088
|
+
return !!last && stateReceivers.has(last) || site.inStore && chain?.length === 1 && last === "this";
|
|
2089
|
+
default:
|
|
2090
|
+
return true;
|
|
2091
|
+
}
|
|
2092
|
+
}
|
|
2093
|
+
#resolve(fileName, site) {
|
|
2094
|
+
const owner = this.#ownerOf(fileName);
|
|
2095
|
+
const visible = this.#nameTable().lookup(site.identifier.text).filter((entry) => this.#passesGate(entry, site) && (!owner || !entry.owner || entry.owner === owner || entry.owner.startsWith("libs/")));
|
|
2096
|
+
const hint = this.#modelHintOf(site);
|
|
2097
|
+
const hinted = hint ? visible.filter((entry) => entry.refName === hint) : [];
|
|
2098
|
+
const chosen = hinted.length ? hinted : hint ? visible.filter((entry) => !modellessFamilies.has(entry.family)) : visible;
|
|
2099
|
+
return chosen.map(({ family, refName, owner: entryOwner, sourceKey, label, detail, span }) => ({
|
|
2100
|
+
family,
|
|
2101
|
+
refName,
|
|
2102
|
+
owner: entryOwner,
|
|
2103
|
+
sourceKey,
|
|
2104
|
+
label,
|
|
2105
|
+
detail,
|
|
2106
|
+
span
|
|
2107
|
+
})).sort((a, b) => Number(b.owner === owner) - Number(a.owner === owner));
|
|
2108
|
+
}
|
|
2109
|
+
}
|
|
2110
|
+
// pkgs/@akanjs/language/frameworkChannelReader.ts
|
|
2111
|
+
class FrameworkChannelReader {
|
|
2112
|
+
#ts;
|
|
2113
|
+
#unions = [];
|
|
2114
|
+
#channels = [];
|
|
2115
|
+
#missing = [];
|
|
2116
|
+
#deferred = [];
|
|
2117
|
+
#spoken = new Map;
|
|
2118
|
+
#sendParameters = new Set;
|
|
2119
|
+
#classes = new Map;
|
|
2120
|
+
#injectedSends = [];
|
|
2121
|
+
#constructed = new Map;
|
|
2122
|
+
constructor(ts, specs, sources) {
|
|
2123
|
+
this.#ts = ts;
|
|
2124
|
+
for (const source of sources.values())
|
|
2125
|
+
for (const statement of source.statements)
|
|
2126
|
+
if (ts.isClassDeclaration(statement) && statement.name)
|
|
2127
|
+
this.#classes.set(statement.name.text, [...this.#classes.get(statement.name.text) ?? [], statement]);
|
|
2128
|
+
for (const source of sources.values())
|
|
2129
|
+
this.#unions.push(...this.#unionsIn(source));
|
|
2130
|
+
const parts = new Set(this.#unions.flatMap((union) => [...union.typeNames].filter((name) => name !== union.name).map((name) => `${union.file}
|
|
2131
|
+
${name}`)));
|
|
2132
|
+
this.#unions.splice(0, this.#unions.length, ...this.#unions.filter((union) => !parts.has(`${union.file}
|
|
2133
|
+
${union.name}`)));
|
|
2134
|
+
for (const spec of specs) {
|
|
2135
|
+
const union = this.#unions.find(({ file, name }) => file === spec.file && name === spec.union);
|
|
2136
|
+
if (!union) {
|
|
2137
|
+
this.#missing.push(`${spec.union} in ${spec.file}`);
|
|
2138
|
+
continue;
|
|
2139
|
+
}
|
|
2140
|
+
union.channel = { spec, union, senders: new Map, handlers: new Map };
|
|
2141
|
+
this.#channels.push(union.channel);
|
|
2142
|
+
}
|
|
2143
|
+
for (const source of sources.values())
|
|
2144
|
+
this.#read(source);
|
|
2145
|
+
for (const { channels, ...site } of this.#deferred) {
|
|
2146
|
+
const spoken = this.#spoken.get(site.source.fileName);
|
|
2147
|
+
for (const channel of channels)
|
|
2148
|
+
if (spoken?.has(channel))
|
|
2149
|
+
this.#record(channel, site);
|
|
2150
|
+
}
|
|
2151
|
+
for (const { owner, ...site } of this.#injectedSends)
|
|
2152
|
+
for (const file of this.#constructed.get(owner) ?? [])
|
|
2153
|
+
for (const channel of [...this.#spoken.get(file) ?? []])
|
|
2154
|
+
if (channel.union.members.has(site.literal) && !this.#holds(channel, site))
|
|
2155
|
+
this.#record(channel, site);
|
|
2156
|
+
}
|
|
2157
|
+
get missing() {
|
|
2158
|
+
return [...this.#missing];
|
|
2159
|
+
}
|
|
2160
|
+
channels() {
|
|
2161
|
+
return this.#channels.map(({ spec, union, senders, handlers }) => ({
|
|
2162
|
+
name: spec.union,
|
|
2163
|
+
label: spec.label,
|
|
2164
|
+
at: union.at,
|
|
2165
|
+
messages: [...union.members].map(([type, member]) => ({
|
|
2166
|
+
type,
|
|
2167
|
+
at: member.at,
|
|
2168
|
+
senders: FrameworkChannelReader.#grouped(senders.get(type) ?? []),
|
|
2169
|
+
handlers: FrameworkChannelReader.#grouped(handlers.get(type) ?? [])
|
|
2170
|
+
}))
|
|
2171
|
+
}));
|
|
2172
|
+
}
|
|
2173
|
+
static #grouped(sites) {
|
|
2174
|
+
const byOwner = new Map;
|
|
2175
|
+
const ordered = [...sites].sort((a, b) => a.at.file === b.at.file ? a.at.line - b.at.line : a.at.file < b.at.file ? -1 : 1);
|
|
2176
|
+
for (const site of ordered) {
|
|
2177
|
+
const existing = byOwner.get(site.owner);
|
|
2178
|
+
if (existing)
|
|
2179
|
+
existing.count += 1;
|
|
2180
|
+
else
|
|
2181
|
+
byOwner.set(site.owner, { ...site, count: 1 });
|
|
2182
|
+
}
|
|
2183
|
+
return [...byOwner.values()].sort((a, b) => a.owner < b.owner ? -1 : a.owner > b.owner ? 1 : 0);
|
|
2184
|
+
}
|
|
2185
|
+
#location(source, node) {
|
|
2186
|
+
const { line, character } = source.getLineAndCharacterOfPosition(node.getStart(source));
|
|
2187
|
+
return { file: source.fileName, line: line + 1, column: character + 1 };
|
|
2188
|
+
}
|
|
2189
|
+
#unionsIn(source) {
|
|
2190
|
+
const ts = this.#ts;
|
|
2191
|
+
const declarations = new Map;
|
|
2192
|
+
for (const statement of source.statements)
|
|
2193
|
+
if (ts.isTypeAliasDeclaration(statement) || ts.isInterfaceDeclaration(statement))
|
|
2194
|
+
declarations.set(statement.name.text, statement);
|
|
2195
|
+
return [...declarations.values()].flatMap((declaration) => {
|
|
2196
|
+
if (!ts.isTypeAliasDeclaration(declaration) || !ts.isUnionTypeNode(declaration.type))
|
|
2197
|
+
return [];
|
|
2198
|
+
const members = new Map;
|
|
2199
|
+
const typeNames = new Set([declaration.name.text]);
|
|
2200
|
+
const isShaped = this.#collect(source, declaration.type, declarations, members, typeNames);
|
|
2201
|
+
if (!isShaped || members.size < 2)
|
|
2202
|
+
return [];
|
|
2203
|
+
return [
|
|
2204
|
+
{
|
|
2205
|
+
file: source.fileName,
|
|
2206
|
+
name: declaration.name.text,
|
|
2207
|
+
at: this.#location(source, declaration.name),
|
|
2208
|
+
members,
|
|
2209
|
+
typeNames,
|
|
2210
|
+
channel: null
|
|
2211
|
+
}
|
|
2212
|
+
];
|
|
2213
|
+
});
|
|
2214
|
+
}
|
|
2215
|
+
#collect(source, type, declarations, members, typeNames) {
|
|
2216
|
+
const ts = this.#ts;
|
|
2217
|
+
if (ts.isParenthesizedTypeNode(type))
|
|
2218
|
+
return this.#collect(source, type.type, declarations, members, typeNames);
|
|
2219
|
+
if (ts.isUnionTypeNode(type))
|
|
2220
|
+
return type.types.every((member) => this.#collect(source, member, declarations, members, typeNames));
|
|
2221
|
+
if (ts.isTypeLiteralNode(type))
|
|
2222
|
+
return this.#addMember(source, type.members, members);
|
|
2223
|
+
if (!ts.isTypeReferenceNode(type) || !ts.isIdentifier(type.typeName))
|
|
2224
|
+
return false;
|
|
2225
|
+
const name = type.typeName.text;
|
|
2226
|
+
const declaration = declarations.get(name);
|
|
2227
|
+
if (!declaration)
|
|
2228
|
+
return false;
|
|
2229
|
+
if (typeNames.has(name))
|
|
2230
|
+
return true;
|
|
2231
|
+
typeNames.add(name);
|
|
2232
|
+
return ts.isTypeAliasDeclaration(declaration) ? this.#collect(source, declaration.type, declarations, members, typeNames) : this.#addMember(source, declaration.members, members);
|
|
2233
|
+
}
|
|
2234
|
+
#addMember(source, elements, members) {
|
|
2235
|
+
const ts = this.#ts;
|
|
2236
|
+
const typeProperty = elements.find((element) => ts.isPropertySignature(element) && this.#nameOf(element.name) === "type");
|
|
2237
|
+
const literal = typeProperty?.type && ts.isLiteralTypeNode(typeProperty.type) && ts.isStringLiteral(typeProperty.type.literal) ? typeProperty.type.literal.text : null;
|
|
2238
|
+
if (!typeProperty || literal === null)
|
|
2239
|
+
return false;
|
|
2240
|
+
const keys = new Set;
|
|
2241
|
+
const required = new Set;
|
|
2242
|
+
for (const element of elements) {
|
|
2243
|
+
const name = ts.isPropertySignature(element) ? this.#nameOf(element.name) : null;
|
|
2244
|
+
if (!name)
|
|
2245
|
+
continue;
|
|
2246
|
+
keys.add(name);
|
|
2247
|
+
if (ts.isPropertySignature(element) && !element.questionToken)
|
|
2248
|
+
required.add(name);
|
|
2249
|
+
}
|
|
2250
|
+
const existing = members.get(literal);
|
|
2251
|
+
if (!existing) {
|
|
2252
|
+
members.set(literal, { at: this.#location(source, typeProperty), keys, required });
|
|
2253
|
+
return true;
|
|
2254
|
+
}
|
|
2255
|
+
for (const key of keys)
|
|
2256
|
+
existing.keys.add(key);
|
|
2257
|
+
for (const key of existing.required)
|
|
2258
|
+
if (!required.has(key))
|
|
2259
|
+
existing.required.delete(key);
|
|
2260
|
+
return true;
|
|
2261
|
+
}
|
|
2262
|
+
#nameOf(name) {
|
|
2263
|
+
const ts = this.#ts;
|
|
2264
|
+
return ts.isIdentifier(name) || ts.isStringLiteral(name) ? name.text : null;
|
|
2265
|
+
}
|
|
2266
|
+
#literalOf(expression) {
|
|
2267
|
+
const ts = this.#ts;
|
|
2268
|
+
return expression && (ts.isStringLiteral(expression) || ts.isNoSubstitutionTemplateLiteral(expression)) ? expression.text : null;
|
|
2269
|
+
}
|
|
2270
|
+
#record(channel, { role, literal, source, node }) {
|
|
2271
|
+
const sites = channel[role];
|
|
2272
|
+
const site = { owner: this.#ownerOf(source, node), count: 1, at: this.#location(source, node) };
|
|
2273
|
+
const relayed = role === "handlers" && !!site.owner && !!channel.spec.relays?.includes(site.owner);
|
|
2274
|
+
if (!relayed)
|
|
2275
|
+
sites.set(literal, [...sites.get(literal) ?? [], site]);
|
|
2276
|
+
const spoken = this.#spoken.get(source.fileName) ?? new Set;
|
|
2277
|
+
spoken.add(channel);
|
|
2278
|
+
this.#spoken.set(source.fileName, spoken);
|
|
2279
|
+
}
|
|
2280
|
+
#settle(candidates, site) {
|
|
2281
|
+
const local = candidates.filter(({ file }) => file === site.source.fileName);
|
|
2282
|
+
const unions = candidates.length > 1 && local.length ? local : candidates;
|
|
2283
|
+
const channels = unions.flatMap(({ channel }) => channel ? [channel] : []);
|
|
2284
|
+
if (unions.length === 1 && channels.length === 1)
|
|
2285
|
+
this.#record(channels[0], site);
|
|
2286
|
+
else if (channels.length)
|
|
2287
|
+
this.#deferred.push({ ...site, channels });
|
|
2288
|
+
}
|
|
2289
|
+
#ownerOf(source, node) {
|
|
2290
|
+
const ts = this.#ts;
|
|
2291
|
+
let top = node;
|
|
2292
|
+
for (let current = node;current; current = current.parent) {
|
|
2293
|
+
if ((ts.isClassDeclaration(current) || ts.isClassExpression(current)) && current.name)
|
|
2294
|
+
return current.name.text;
|
|
2295
|
+
if (current.parent && !ts.isSourceFile(current.parent))
|
|
2296
|
+
top = current.parent;
|
|
2297
|
+
}
|
|
2298
|
+
if (ts.isFunctionDeclaration(top) && top.name)
|
|
2299
|
+
return top.name.text;
|
|
2300
|
+
if (ts.isVariableStatement(top)) {
|
|
2301
|
+
const declaration = top.declarationList.declarations.find(({ initializer, pos, end }) => !!initializer && (ts.isArrowFunction(initializer) || ts.isFunctionExpression(initializer)) && pos <= node.pos && node.end <= end);
|
|
2302
|
+
if (declaration && ts.isIdentifier(declaration.name))
|
|
2303
|
+
return declaration.name.text;
|
|
2304
|
+
}
|
|
2305
|
+
const classes = source.statements.filter(ts.isClassDeclaration);
|
|
2306
|
+
const only = classes.length === 1 ? classes[0].name?.text : undefined;
|
|
2307
|
+
return only ?? source.fileName.slice(source.fileName.lastIndexOf("/") + 1).replace(/\.tsx?$/, "");
|
|
2308
|
+
}
|
|
2309
|
+
#namesIn(type) {
|
|
2310
|
+
const ts = this.#ts;
|
|
2311
|
+
const names = new Set;
|
|
2312
|
+
const visit = (node) => {
|
|
2313
|
+
if (ts.isTypeReferenceNode(node))
|
|
2314
|
+
names.add(ts.isIdentifier(node.typeName) ? node.typeName.text : node.typeName.right.text);
|
|
2315
|
+
if (!ts.isTypeLiteralNode(node))
|
|
2316
|
+
ts.forEachChild(node, visit);
|
|
2317
|
+
};
|
|
2318
|
+
if (type)
|
|
2319
|
+
visit(type);
|
|
2320
|
+
return names;
|
|
2321
|
+
}
|
|
2322
|
+
#literalTypesIn(type) {
|
|
2323
|
+
const ts = this.#ts;
|
|
2324
|
+
const literals = new Set;
|
|
2325
|
+
const visit = (node) => {
|
|
2326
|
+
if (ts.isLiteralTypeNode(node) && ts.isStringLiteral(node.literal))
|
|
2327
|
+
literals.add(node.literal.text);
|
|
2328
|
+
ts.forEachChild(node, visit);
|
|
2329
|
+
};
|
|
2330
|
+
if (type)
|
|
2331
|
+
visit(type);
|
|
2332
|
+
return literals;
|
|
2333
|
+
}
|
|
2334
|
+
#hinted(names) {
|
|
2335
|
+
return this.#unions.filter((union) => [...names].some((name) => union.typeNames.has(name)));
|
|
2336
|
+
}
|
|
2337
|
+
#read(source) {
|
|
2338
|
+
const ts = this.#ts;
|
|
2339
|
+
const comparisons = new Map;
|
|
2340
|
+
const switches = [];
|
|
2341
|
+
const templates = [];
|
|
2342
|
+
const calls = [];
|
|
2343
|
+
const visit = (node) => {
|
|
2344
|
+
if (ts.isObjectLiteralExpression(node)) {
|
|
2345
|
+
const template = this.#readSend(source, node);
|
|
2346
|
+
if (template)
|
|
2347
|
+
templates.push(template);
|
|
2348
|
+
} else if (ts.isCallExpression(node))
|
|
2349
|
+
calls.push(node);
|
|
2350
|
+
else if (ts.isNewExpression(node)) {
|
|
2351
|
+
this.#readRouteSet(source, node);
|
|
2352
|
+
if (ts.isIdentifier(node.expression)) {
|
|
2353
|
+
const files = this.#constructed.get(node.expression.text) ?? new Set;
|
|
2354
|
+
files.add(source.fileName);
|
|
2355
|
+
this.#constructed.set(node.expression.text, files);
|
|
2356
|
+
}
|
|
2357
|
+
} else if (ts.isSwitchStatement(node)) {
|
|
2358
|
+
const chain = this.#switchChain(node);
|
|
2359
|
+
if (chain)
|
|
2360
|
+
switches.push(chain);
|
|
2361
|
+
} else if (ts.isBinaryExpression(node)) {
|
|
2362
|
+
const compared = this.#comparedType(node);
|
|
2363
|
+
if (compared) {
|
|
2364
|
+
const key = `${this.#scopeOf(node)?.pos ?? -1}:${compared.receiver.getText(source)}`;
|
|
2365
|
+
const chain = comparisons.get(key) ?? {
|
|
2366
|
+
literals: [],
|
|
2367
|
+
hint: this.#declaredTypeNames(compared.receiver),
|
|
2368
|
+
parameter: this.#parameterOf(compared.receiver)
|
|
2369
|
+
};
|
|
2370
|
+
chain.literals.push([compared.literal, compared.at]);
|
|
2371
|
+
comparisons.set(key, chain);
|
|
2372
|
+
}
|
|
2373
|
+
}
|
|
2374
|
+
ts.forEachChild(node, visit);
|
|
2375
|
+
};
|
|
2376
|
+
visit(source);
|
|
2377
|
+
for (const { object, fn, index } of templates)
|
|
2378
|
+
for (const call of calls) {
|
|
2379
|
+
const argument = call.arguments[index];
|
|
2380
|
+
const literal = this.#literalOf(argument);
|
|
2381
|
+
if (literal !== null && this.#calleeOf(call) === fn)
|
|
2382
|
+
this.#sendSite(source, object, literal, argument, true);
|
|
2383
|
+
}
|
|
2384
|
+
for (const chain of [...switches, ...comparisons.values()])
|
|
2385
|
+
if (!chain.parameter || !this.#sendParameters.has(chain.parameter))
|
|
2386
|
+
this.#readChain(source, chain);
|
|
2387
|
+
}
|
|
2388
|
+
#readSend(source, object) {
|
|
2389
|
+
const ts = this.#ts;
|
|
2390
|
+
const typeProperty = object.properties.find((property) => (ts.isPropertyAssignment(property) || ts.isShorthandPropertyAssignment(property)) && this.#nameOf(property.name) === "type");
|
|
2391
|
+
if (!typeProperty)
|
|
2392
|
+
return null;
|
|
2393
|
+
const value = ts.isPropertyAssignment(typeProperty) ? typeProperty.initializer : typeProperty.name;
|
|
2394
|
+
const literal = this.#literalOf(value);
|
|
2395
|
+
if (literal !== null)
|
|
2396
|
+
this.#sendSite(source, object, literal, typeProperty, false);
|
|
2397
|
+
if (literal !== null || !ts.isIdentifier(value))
|
|
2398
|
+
return null;
|
|
2399
|
+
const fn = this.#scopeOf(object);
|
|
2400
|
+
const index = fn?.parameters.findIndex(({ name }) => ts.isIdentifier(name) && name.text === value.text) ?? -1;
|
|
2401
|
+
return fn && index >= 0 ? { object, fn, index } : null;
|
|
2402
|
+
}
|
|
2403
|
+
#sendSite(source, object, literal, node, forwarded) {
|
|
2404
|
+
const context = this.#sendContextOf(object, forwarded ? null : literal);
|
|
2405
|
+
if (!context)
|
|
2406
|
+
return;
|
|
2407
|
+
const site = { role: "senders", literal, source, node };
|
|
2408
|
+
const injector = this.#injectorOf(object);
|
|
2409
|
+
if (injector)
|
|
2410
|
+
this.#injectedSends.push({ ...site, owner: injector });
|
|
2411
|
+
const hinted = this.#hinted(context.names).filter((union) => union.members.has(literal));
|
|
2412
|
+
const candidates = hinted.length ? hinted : this.#fitting(object, literal);
|
|
2413
|
+
if (context.parameter && candidates.some(({ channel }) => channel))
|
|
2414
|
+
this.#sendParameters.add(context.parameter);
|
|
2415
|
+
this.#settle(candidates, site);
|
|
2416
|
+
}
|
|
2417
|
+
#injectorOf(object) {
|
|
2418
|
+
const ts = this.#ts;
|
|
2419
|
+
const call = object.parent;
|
|
2420
|
+
if (!ts.isCallExpression(call) || !call.arguments.includes(object))
|
|
2421
|
+
return null;
|
|
2422
|
+
const callee = call.expression;
|
|
2423
|
+
if (!ts.isPropertyAccessExpression(callee) || callee.expression.kind !== ts.SyntaxKind.ThisKeyword)
|
|
2424
|
+
return null;
|
|
2425
|
+
const owner = this.#classOf(call);
|
|
2426
|
+
const constructor = owner?.members.find(ts.isConstructorDeclaration);
|
|
2427
|
+
const parameters = new Set(constructor?.parameters.flatMap(({ name }) => ts.isIdentifier(name) ? [name.text] : []) ?? []);
|
|
2428
|
+
const isInjected = !!constructor?.body?.statements.some((statement) => {
|
|
2429
|
+
if (!ts.isExpressionStatement(statement) || !ts.isBinaryExpression(statement.expression))
|
|
2430
|
+
return false;
|
|
2431
|
+
const { left, operatorToken, right } = statement.expression;
|
|
2432
|
+
return operatorToken.kind === ts.SyntaxKind.EqualsToken && ts.isPropertyAccessExpression(left) && left.expression.kind === ts.SyntaxKind.ThisKeyword && this.#memberName(left.name) === callee.name.text && ts.isIdentifier(right) && parameters.has(right.text);
|
|
2433
|
+
});
|
|
2434
|
+
return isInjected && owner?.name ? owner.name.text : null;
|
|
2435
|
+
}
|
|
2436
|
+
#holds(channel, site) {
|
|
2437
|
+
const { file, line, column } = this.#location(site.source, site.node);
|
|
2438
|
+
return (channel[site.role].get(site.literal) ?? []).some(({ at }) => at.file === file && at.line === line && at.column === column);
|
|
2439
|
+
}
|
|
2440
|
+
#fitting(object, literal) {
|
|
2441
|
+
const ts = this.#ts;
|
|
2442
|
+
const isLoose = object.properties.some((property) => ts.isSpreadAssignment(property));
|
|
2443
|
+
const keys = new Set(object.properties.flatMap((property) => {
|
|
2444
|
+
const name = ts.isSpreadAssignment(property) ? null : this.#nameOf(property.name);
|
|
2445
|
+
return name === null ? [] : [name];
|
|
2446
|
+
}));
|
|
2447
|
+
return this.#unions.filter((union) => {
|
|
2448
|
+
const member = union.members.get(literal);
|
|
2449
|
+
return !!member && [...keys].every((key) => member.keys.has(key)) && (isLoose || [...member.required].every((key) => keys.has(key)));
|
|
2450
|
+
});
|
|
2451
|
+
}
|
|
2452
|
+
#sendContextOf(object, literal) {
|
|
2453
|
+
const ts = this.#ts;
|
|
2454
|
+
const hint = new Set;
|
|
2455
|
+
let node = object;
|
|
2456
|
+
while (node.parent) {
|
|
2457
|
+
const parent = node.parent;
|
|
2458
|
+
if (ts.isParenthesizedExpression(parent) || ts.isConditionalExpression(parent))
|
|
2459
|
+
node = parent;
|
|
2460
|
+
else if (ts.isAsExpression(parent) || ts.isSatisfiesExpression(parent)) {
|
|
2461
|
+
for (const name of this.#namesIn(parent.type))
|
|
2462
|
+
hint.add(name);
|
|
2463
|
+
node = parent;
|
|
2464
|
+
} else if (ts.isCallExpression(parent) && this.#calleeName(parent) === "stringify")
|
|
2465
|
+
node = parent;
|
|
2466
|
+
else if (ts.isArrowFunction(parent) && parent.body === node && !parent.type)
|
|
2467
|
+
node = parent;
|
|
2468
|
+
else
|
|
2469
|
+
break;
|
|
2470
|
+
}
|
|
2471
|
+
const parent = node.parent;
|
|
2472
|
+
if (!parent)
|
|
2473
|
+
return null;
|
|
2474
|
+
const scope = this.#scopeOf(object);
|
|
2475
|
+
if (literal !== null && scope?.parameters.some(({ type }) => this.#literalTypesIn(type).has(literal)))
|
|
2476
|
+
return null;
|
|
2477
|
+
const context = (names, parameter = null) => ({
|
|
2478
|
+
names: new Set([...hint, ...names]),
|
|
2479
|
+
parameter
|
|
2480
|
+
});
|
|
2481
|
+
if (ts.isCallExpression(parent)) {
|
|
2482
|
+
const index = parent.arguments.indexOf(node);
|
|
2483
|
+
if (index < 0)
|
|
2484
|
+
return null;
|
|
2485
|
+
const parameter = this.#calleeOf(parent)?.parameters[index] ?? null;
|
|
2486
|
+
return context(this.#namesIn(parameter?.type), parameter);
|
|
2487
|
+
}
|
|
2488
|
+
if (ts.isVariableDeclaration(parent) && parent.type)
|
|
2489
|
+
return context(this.#namesIn(parent.type));
|
|
2490
|
+
if (ts.isPropertyAssignment(parent) && ts.isObjectLiteralExpression(parent.parent)) {
|
|
2491
|
+
const outer = this.#sendContextOf(parent.parent, null);
|
|
2492
|
+
const property = this.#nameOf(parent.name);
|
|
2493
|
+
const names = outer && property ? this.#propertyTypeNames(object.getSourceFile(), outer.names, property) : null;
|
|
2494
|
+
return names?.size ? context(names) : null;
|
|
2495
|
+
}
|
|
2496
|
+
const fn = ts.isReturnStatement(parent) ? this.#scopeOf(parent) : ts.isArrowFunction(parent) ? parent : null;
|
|
2497
|
+
if (fn?.type)
|
|
2498
|
+
return context(this.#namesIn(fn.type));
|
|
2499
|
+
return hint.size ? context(new Set) : null;
|
|
2500
|
+
}
|
|
2501
|
+
#propertyTypeNames(source, typeNames, property) {
|
|
2502
|
+
const ts = this.#ts;
|
|
2503
|
+
for (const statement of source.statements) {
|
|
2504
|
+
if (!(ts.isInterfaceDeclaration(statement) || ts.isTypeAliasDeclaration(statement)))
|
|
2505
|
+
continue;
|
|
2506
|
+
if (!typeNames.has(statement.name.text))
|
|
2507
|
+
continue;
|
|
2508
|
+
const members = ts.isInterfaceDeclaration(statement) ? statement.members : ts.isTypeLiteralNode(statement.type) ? statement.type.members : [];
|
|
2509
|
+
const signature = members.find((member) => ts.isPropertySignature(member) && this.#nameOf(member.name) === property);
|
|
2510
|
+
if (signature?.type)
|
|
2511
|
+
return this.#namesIn(signature.type);
|
|
2512
|
+
}
|
|
2513
|
+
return null;
|
|
2514
|
+
}
|
|
2515
|
+
#calleeOf(call) {
|
|
2516
|
+
const ts = this.#ts;
|
|
2517
|
+
const callee = call.expression;
|
|
2518
|
+
const isFunction = (node) => !!node && (ts.isArrowFunction(node) || ts.isFunctionExpression(node));
|
|
2519
|
+
if (ts.isPropertyAccessExpression(callee) && callee.expression.kind === ts.SyntaxKind.ThisKeyword) {
|
|
2520
|
+
const member = this.#memberOf(this.#classOf(call), callee.name.text);
|
|
2521
|
+
if (!member)
|
|
2522
|
+
return null;
|
|
2523
|
+
if (ts.isMethodDeclaration(member))
|
|
2524
|
+
return member;
|
|
2525
|
+
return ts.isPropertyDeclaration(member) && isFunction(member.initializer) ? member.initializer : null;
|
|
2526
|
+
}
|
|
2527
|
+
if (ts.isPropertyAccessExpression(callee)) {
|
|
2528
|
+
for (const name of this.#receiverTypeNames(callee.expression))
|
|
2529
|
+
for (const declaration of this.#classes.get(name) ?? []) {
|
|
2530
|
+
const member = this.#memberOf(declaration, callee.name.text);
|
|
2531
|
+
if (member && ts.isMethodDeclaration(member))
|
|
2532
|
+
return member;
|
|
2533
|
+
}
|
|
2534
|
+
return null;
|
|
2535
|
+
}
|
|
2536
|
+
if (!ts.isIdentifier(callee))
|
|
2537
|
+
return null;
|
|
2538
|
+
for (const statement of call.getSourceFile().statements) {
|
|
2539
|
+
if (ts.isFunctionDeclaration(statement) && statement.name?.text === callee.text)
|
|
2540
|
+
return statement;
|
|
2541
|
+
if (!ts.isVariableStatement(statement))
|
|
2542
|
+
continue;
|
|
2543
|
+
for (const declaration of statement.declarationList.declarations)
|
|
2544
|
+
if (ts.isIdentifier(declaration.name) && declaration.name.text === callee.text)
|
|
2545
|
+
return isFunction(declaration.initializer) ? declaration.initializer : null;
|
|
2546
|
+
}
|
|
2547
|
+
return null;
|
|
2548
|
+
}
|
|
2549
|
+
#classOf(node) {
|
|
2550
|
+
let owner = node.parent;
|
|
2551
|
+
while (owner && !this.#ts.isClassLike(owner))
|
|
2552
|
+
owner = owner.parent;
|
|
2553
|
+
return owner ?? null;
|
|
2554
|
+
}
|
|
2555
|
+
#memberOf(owner, name) {
|
|
2556
|
+
return owner?.members.find((member) => member.name && this.#memberName(member.name) === name) ?? null;
|
|
2557
|
+
}
|
|
2558
|
+
#receiverTypeNames(receiver) {
|
|
2559
|
+
const ts = this.#ts;
|
|
2560
|
+
if (ts.isPropertyAccessExpression(receiver) && receiver.expression.kind === ts.SyntaxKind.ThisKeyword) {
|
|
2561
|
+
const field = this.#memberOf(this.#classOf(receiver), receiver.name.text);
|
|
2562
|
+
return field && ts.isPropertyDeclaration(field) ? this.#namesIn(field.type) : new Set;
|
|
2563
|
+
}
|
|
2564
|
+
return this.#declaredTypeNames(receiver) ?? new Set;
|
|
2565
|
+
}
|
|
2566
|
+
#memberName(name) {
|
|
2567
|
+
const ts = this.#ts;
|
|
2568
|
+
return ts.isIdentifier(name) || ts.isPrivateIdentifier(name) || ts.isStringLiteral(name) ? name.text : null;
|
|
2569
|
+
}
|
|
2570
|
+
#calleeName(call) {
|
|
2571
|
+
const ts = this.#ts;
|
|
2572
|
+
const callee = call.expression;
|
|
2573
|
+
if (ts.isIdentifier(callee))
|
|
2574
|
+
return callee.text;
|
|
2575
|
+
if (ts.isPropertyAccessExpression(callee))
|
|
2576
|
+
return callee.name.text;
|
|
2577
|
+
return null;
|
|
2578
|
+
}
|
|
2579
|
+
#scopeOf(node) {
|
|
2580
|
+
const ts = this.#ts;
|
|
2581
|
+
for (let current = node.parent;current; current = current.parent)
|
|
2582
|
+
if (ts.isFunctionLike(current))
|
|
2583
|
+
return current;
|
|
2584
|
+
return null;
|
|
2585
|
+
}
|
|
2586
|
+
#typeReceiverOf(expression) {
|
|
2587
|
+
const ts = this.#ts;
|
|
2588
|
+
if (ts.isPropertyAccessExpression(expression) && expression.name.text === "type")
|
|
2589
|
+
return expression.expression;
|
|
2590
|
+
if (ts.isElementAccessExpression(expression) && this.#literalOf(expression.argumentExpression) === "type")
|
|
2591
|
+
return expression.expression;
|
|
2592
|
+
return null;
|
|
2593
|
+
}
|
|
2594
|
+
#comparedType(node) {
|
|
2595
|
+
const ts = this.#ts;
|
|
2596
|
+
const { kind } = node.operatorToken;
|
|
2597
|
+
if (kind !== ts.SyntaxKind.EqualsEqualsEqualsToken && kind !== ts.SyntaxKind.ExclamationEqualsEqualsToken && kind !== ts.SyntaxKind.EqualsEqualsToken && kind !== ts.SyntaxKind.ExclamationEqualsToken)
|
|
2598
|
+
return null;
|
|
2599
|
+
for (const [side, other] of [
|
|
2600
|
+
[node.left, node.right],
|
|
2601
|
+
[node.right, node.left]
|
|
2602
|
+
]) {
|
|
2603
|
+
const receiver = this.#typeReceiverOf(side);
|
|
2604
|
+
const literal = this.#literalOf(other);
|
|
2605
|
+
if (receiver && literal !== null)
|
|
2606
|
+
return { receiver, literal, at: other };
|
|
2607
|
+
}
|
|
2608
|
+
return null;
|
|
2609
|
+
}
|
|
2610
|
+
#declaredTypeNames(receiver) {
|
|
2611
|
+
const ts = this.#ts;
|
|
2612
|
+
if (!ts.isIdentifier(receiver))
|
|
2613
|
+
return null;
|
|
2614
|
+
const name = receiver.text;
|
|
2615
|
+
for (let current = receiver.parent;current; current = current.parent) {
|
|
2616
|
+
if (ts.isFunctionLike(current)) {
|
|
2617
|
+
for (const parameter of current.parameters)
|
|
2618
|
+
if (ts.isIdentifier(parameter.name) && parameter.name.text === name)
|
|
2619
|
+
return parameter.type ? this.#namesIn(parameter.type) : null;
|
|
2620
|
+
}
|
|
2621
|
+
if (ts.isBlock(current) || ts.isSourceFile(current) || ts.isCaseClause(current))
|
|
2622
|
+
for (const statement of current.statements) {
|
|
2623
|
+
if (!ts.isVariableStatement(statement))
|
|
2624
|
+
continue;
|
|
2625
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
2626
|
+
if (!ts.isIdentifier(declaration.name) || declaration.name.text !== name)
|
|
2627
|
+
continue;
|
|
2628
|
+
const { initializer } = declaration;
|
|
2629
|
+
const asType = initializer && (ts.isAsExpression(initializer) || ts.isSatisfiesExpression(initializer)) ? initializer.type : undefined;
|
|
2630
|
+
const type = declaration.type ?? asType;
|
|
2631
|
+
return type ? this.#namesIn(type) : null;
|
|
2632
|
+
}
|
|
2633
|
+
}
|
|
2634
|
+
}
|
|
2635
|
+
return null;
|
|
2636
|
+
}
|
|
2637
|
+
#parameterOf(receiver) {
|
|
2638
|
+
const ts = this.#ts;
|
|
2639
|
+
if (!ts.isIdentifier(receiver))
|
|
2640
|
+
return null;
|
|
2641
|
+
for (let current = receiver.parent;current; current = current.parent)
|
|
2642
|
+
if (ts.isFunctionLike(current)) {
|
|
2643
|
+
const parameter = current.parameters.find(({ name }) => ts.isIdentifier(name) && name.text === receiver.text);
|
|
2644
|
+
if (parameter)
|
|
2645
|
+
return parameter;
|
|
2646
|
+
}
|
|
2647
|
+
return null;
|
|
2648
|
+
}
|
|
2649
|
+
#readChain(source, { literals, hint }) {
|
|
2650
|
+
const hinted = hint?.size ? this.#hinted(hint) : [];
|
|
2651
|
+
if (hint?.size && !hinted.length)
|
|
2652
|
+
return;
|
|
2653
|
+
const scored = this.#unions.map((union) => ({ union, hits: literals.filter(([literal]) => union.members.has(literal)).length })).filter(({ hits }) => hits > 0);
|
|
2654
|
+
const best = Math.max(0, ...scored.map(({ hits }) => hits));
|
|
2655
|
+
const unions = hinted.length ? hinted : best * 2 >= literals.length ? scored.filter(({ hits }) => hits === best).map(({ union }) => union) : [];
|
|
2656
|
+
for (const [literal, node] of literals) {
|
|
2657
|
+
const holding = unions.filter((union) => union.members.has(literal));
|
|
2658
|
+
this.#settle(holding, { role: "handlers", literal, source, node });
|
|
2659
|
+
}
|
|
2660
|
+
}
|
|
2661
|
+
#switchChain(statement) {
|
|
2662
|
+
const ts = this.#ts;
|
|
2663
|
+
const receiver = this.#typeReceiverOf(statement.expression);
|
|
2664
|
+
if (!receiver)
|
|
2665
|
+
return null;
|
|
2666
|
+
const literals = statement.caseBlock.clauses.flatMap((clause) => {
|
|
2667
|
+
if (!ts.isCaseClause(clause))
|
|
2668
|
+
return [];
|
|
2669
|
+
const literal = this.#literalOf(clause.expression);
|
|
2670
|
+
return literal === null ? [] : [[literal, clause.expression]];
|
|
2671
|
+
});
|
|
2672
|
+
return { literals, hint: this.#declaredTypeNames(receiver), parameter: this.#parameterOf(receiver) };
|
|
2673
|
+
}
|
|
2674
|
+
#readRouteSet(source, expression) {
|
|
2675
|
+
const ts = this.#ts;
|
|
2676
|
+
if (!ts.isIdentifier(expression.expression) || expression.expression.text !== "Set")
|
|
2677
|
+
return;
|
|
2678
|
+
const unions = this.#hinted(this.#namesIn(expression.typeArguments?.[0]));
|
|
2679
|
+
const list = expression.arguments?.[0];
|
|
2680
|
+
if (!unions.length || !list || !ts.isArrayLiteralExpression(list))
|
|
2681
|
+
return;
|
|
2682
|
+
for (const element of list.elements) {
|
|
2683
|
+
const literal = this.#literalOf(element);
|
|
2684
|
+
if (literal === null)
|
|
2685
|
+
continue;
|
|
2686
|
+
const holding = unions.filter((union) => union.members.has(literal));
|
|
2687
|
+
this.#settle(holding, { role: "handlers", literal, source, node: element });
|
|
2688
|
+
}
|
|
2689
|
+
}
|
|
2690
|
+
}
|
|
2691
|
+
// pkgs/@akanjs/language/frameworkMapBuilder.ts
|
|
2692
|
+
var extensions = ["", ".ts", ".tsx", "/index.ts", "/index.tsx"];
|
|
2693
|
+
var byText = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
2694
|
+
|
|
2695
|
+
class FrameworkMapBuilder {
|
|
2696
|
+
#source;
|
|
2697
|
+
#fileSet;
|
|
2698
|
+
constructor(source) {
|
|
2699
|
+
this.#source = source;
|
|
2700
|
+
this.#fileSet = new Set(source.files.map(([file]) => file));
|
|
2701
|
+
}
|
|
2702
|
+
static isSourcePath(file) {
|
|
2703
|
+
return /\.tsx?$/.test(file) && !/\.d\.ts$|\.(?:test|spec)\.tsx?$|fixture/i.test(file);
|
|
2704
|
+
}
|
|
2705
|
+
build() {
|
|
2706
|
+
const { ts, packages, files, channels } = this.#source;
|
|
2707
|
+
const sources = new Map;
|
|
2708
|
+
for (const [file, text] of [...files].sort(([a], [b]) => byText(a, b)))
|
|
2709
|
+
sources.set(file, ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true, file.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS));
|
|
2710
|
+
const facets = new Map;
|
|
2711
|
+
const imports = new Map;
|
|
2712
|
+
for (const [file, source] of sources) {
|
|
2713
|
+
const facet = this.#facetOf(file);
|
|
2714
|
+
if (!facet)
|
|
2715
|
+
continue;
|
|
2716
|
+
const pkg = this.#packageOf(file);
|
|
2717
|
+
const current = facets.get(facet) ?? {
|
|
2718
|
+
id: facet,
|
|
2719
|
+
name: pkg?.split ? facet.slice(pkg.name.length + 1) : facet,
|
|
2720
|
+
packageName: pkg?.name ?? facet,
|
|
2721
|
+
layer: 0,
|
|
2722
|
+
files: 0,
|
|
2723
|
+
classes: []
|
|
2724
|
+
};
|
|
2725
|
+
current.files += 1;
|
|
2726
|
+
current.classes.push(...this.#classesOf(source));
|
|
2727
|
+
facets.set(facet, current);
|
|
2728
|
+
for (const site of this.#importsOf(file, source)) {
|
|
2729
|
+
if (site.facet === facet)
|
|
2730
|
+
continue;
|
|
2731
|
+
const key = `${facet}
|
|
2732
|
+
${site.facet}`;
|
|
2733
|
+
const entry = imports.get(key) ?? { from: facet, to: site.facet, value: 0, type: 0, dynamic: 0, at: site.at };
|
|
2734
|
+
if (entry.value === 0 && site.kind === "value" && imports.has(key))
|
|
2735
|
+
entry.at = site.at;
|
|
2736
|
+
entry[site.kind] += 1;
|
|
2737
|
+
imports.set(key, entry);
|
|
2738
|
+
}
|
|
2739
|
+
}
|
|
2740
|
+
const { layerOf, cycles } = FrameworkMapBuilder.#layersOf([...imports.values()]);
|
|
2741
|
+
for (const facet of facets.values()) {
|
|
2742
|
+
facet.classes.sort((a, b) => b.members - a.members || byText(a.name, b.name));
|
|
2743
|
+
facet.layer = layerOf(facet.id);
|
|
2744
|
+
}
|
|
2745
|
+
const reader = new FrameworkChannelReader(ts, channels, sources);
|
|
2746
|
+
return {
|
|
2747
|
+
version: 1,
|
|
2748
|
+
packages: packages.map(({ name, root }) => ({ name, root })),
|
|
2749
|
+
facets: [...facets.values()].sort((a, b) => byText(a.id, b.id)),
|
|
2750
|
+
imports: [...imports.values()].sort((a, b) => byText(a.from, b.from) || byText(a.to, b.to)),
|
|
2751
|
+
channels: reader.channels(),
|
|
2752
|
+
missing: reader.missing,
|
|
2753
|
+
cycles
|
|
2754
|
+
};
|
|
2755
|
+
}
|
|
2756
|
+
static #layersOf(imports) {
|
|
2757
|
+
const deps = new Map;
|
|
2758
|
+
for (const { from, to, value, dynamic } of imports)
|
|
2759
|
+
if (value + dynamic > 0)
|
|
2760
|
+
deps.set(from, [...deps.get(from) ?? [], to]);
|
|
2761
|
+
const components = FrameworkMapBuilder.#stronglyConnected(deps);
|
|
2762
|
+
const componentOf = new Map(components.flatMap((component) => component.map((id) => [id, component])));
|
|
2763
|
+
const layers = new Map;
|
|
2764
|
+
const layerOf = (id) => {
|
|
2765
|
+
const component = componentOf.get(id);
|
|
2766
|
+
if (!component)
|
|
2767
|
+
return 0;
|
|
2768
|
+
const known = layers.get(component);
|
|
2769
|
+
if (known !== undefined)
|
|
2770
|
+
return known;
|
|
2771
|
+
const below = component.flatMap((member) => (deps.get(member) ?? []).filter((to) => !component.includes(to)));
|
|
2772
|
+
const layer = Math.max(-1, ...below.map(layerOf)) + 1;
|
|
2773
|
+
layers.set(component, layer);
|
|
2774
|
+
return layer;
|
|
2775
|
+
};
|
|
2776
|
+
const cycles = components.filter((component) => component.length > 1).map((component) => [...component].sort(byText)).sort((a, b) => byText(a[0], b[0]));
|
|
2777
|
+
return { layerOf, cycles };
|
|
2778
|
+
}
|
|
2779
|
+
static #stronglyConnected(deps) {
|
|
2780
|
+
const ids = [...new Set([...deps.keys(), ...[...deps.values()].flat()])].sort(byText);
|
|
2781
|
+
const index = new Map;
|
|
2782
|
+
const low = new Map;
|
|
2783
|
+
const stack = [];
|
|
2784
|
+
const onStack = new Set;
|
|
2785
|
+
const components = [];
|
|
2786
|
+
const visit = (id) => {
|
|
2787
|
+
index.set(id, index.size);
|
|
2788
|
+
low.set(id, index.get(id) ?? 0);
|
|
2789
|
+
stack.push(id);
|
|
2790
|
+
onStack.add(id);
|
|
2791
|
+
for (const to of deps.get(id) ?? []) {
|
|
2792
|
+
if (!index.has(to)) {
|
|
2793
|
+
visit(to);
|
|
2794
|
+
low.set(id, Math.min(low.get(id) ?? 0, low.get(to) ?? 0));
|
|
2795
|
+
} else if (onStack.has(to))
|
|
2796
|
+
low.set(id, Math.min(low.get(id) ?? 0, index.get(to) ?? 0));
|
|
2797
|
+
}
|
|
2798
|
+
if (low.get(id) !== index.get(id))
|
|
2799
|
+
return;
|
|
2800
|
+
const component = [];
|
|
2801
|
+
for (let member = stack.pop();member !== undefined; member = stack.pop()) {
|
|
2802
|
+
onStack.delete(member);
|
|
2803
|
+
component.push(member);
|
|
2804
|
+
if (member === id)
|
|
2805
|
+
break;
|
|
2806
|
+
}
|
|
2807
|
+
components.push(component);
|
|
2808
|
+
};
|
|
2809
|
+
for (const id of ids)
|
|
2810
|
+
if (!index.has(id))
|
|
2811
|
+
visit(id);
|
|
2812
|
+
return components;
|
|
2813
|
+
}
|
|
2814
|
+
#packageOf(file) {
|
|
2815
|
+
return this.#source.packages.filter(({ root }) => file.startsWith(`${root}/`)).sort((a, b) => b.root.length - a.root.length)[0];
|
|
2816
|
+
}
|
|
2817
|
+
#facetOf(file) {
|
|
2818
|
+
const pkg = this.#packageOf(file);
|
|
2819
|
+
if (!pkg)
|
|
2820
|
+
return null;
|
|
2821
|
+
if (!pkg.split)
|
|
2822
|
+
return pkg.name;
|
|
2823
|
+
const rest = file.slice(pkg.root.length + 1);
|
|
2824
|
+
const slash = rest.indexOf("/");
|
|
2825
|
+
return slash < 0 ? null : `${pkg.name}/${rest.slice(0, slash)}`;
|
|
2826
|
+
}
|
|
2827
|
+
#location(source, node) {
|
|
2828
|
+
const { line, character } = source.getLineAndCharacterOfPosition(node.getStart(source));
|
|
2829
|
+
return { file: source.fileName, line: line + 1, column: character + 1 };
|
|
2830
|
+
}
|
|
2831
|
+
#classesOf(source) {
|
|
2832
|
+
const { ts } = this.#source;
|
|
2833
|
+
return source.statements.flatMap((statement) => ts.isClassDeclaration(statement) && statement.name ? [{ name: statement.name.text, members: statement.members.length, at: this.#location(source, statement.name) }] : []);
|
|
2834
|
+
}
|
|
2835
|
+
#importsOf(file, source) {
|
|
2836
|
+
const { ts } = this.#source;
|
|
2837
|
+
const sites = [];
|
|
2838
|
+
const push = (specifier, kind, node) => {
|
|
2839
|
+
const target = this.#resolve(file, specifier);
|
|
2840
|
+
const facet = target ? this.#facetOf(target) : null;
|
|
2841
|
+
if (facet)
|
|
2842
|
+
sites.push({ facet, kind, at: this.#location(source, node) });
|
|
2843
|
+
};
|
|
2844
|
+
const visit = (node) => {
|
|
2845
|
+
if (ts.isImportDeclaration(node) && ts.isStringLiteral(node.moduleSpecifier)) {
|
|
2846
|
+
const clause = node.importClause;
|
|
2847
|
+
const named = clause?.namedBindings;
|
|
2848
|
+
const isTypeOnly = !!clause?.isTypeOnly || !!clause && !clause.name && !!named && ts.isNamedImports(named) && named.elements.length > 0 && named.elements.every((element) => element.isTypeOnly);
|
|
2849
|
+
push(node.moduleSpecifier.text, isTypeOnly ? "type" : "value", node);
|
|
2850
|
+
} else if (ts.isExportDeclaration(node) && node.moduleSpecifier && ts.isStringLiteral(node.moduleSpecifier)) {
|
|
2851
|
+
const clause = node.exportClause;
|
|
2852
|
+
const isTypeOnly = node.isTypeOnly || !!clause && ts.isNamedExports(clause) && clause.elements.length > 0 && clause.elements.every((element) => element.isTypeOnly);
|
|
2853
|
+
push(node.moduleSpecifier.text, isTypeOnly ? "type" : "value", node);
|
|
2854
|
+
} else if (ts.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword && node.arguments[0] && ts.isStringLiteralLike(node.arguments[0]))
|
|
2855
|
+
push(node.arguments[0].text, "dynamic", node);
|
|
2856
|
+
ts.forEachChild(node, visit);
|
|
2857
|
+
};
|
|
2858
|
+
visit(source);
|
|
2859
|
+
return sites;
|
|
2860
|
+
}
|
|
2861
|
+
#resolve(file, specifier) {
|
|
2862
|
+
const pkg = specifier.startsWith(".") ? null : this.#source.packages.filter(({ name }) => specifier === name || specifier.startsWith(`${name}/`)).sort((a, b) => b.name.length - a.name.length)[0];
|
|
2863
|
+
if (!specifier.startsWith(".") && !pkg)
|
|
2864
|
+
return null;
|
|
2865
|
+
const base = pkg ? `${pkg.root}${specifier.slice(pkg.name.length)}` : FrameworkMapBuilder.#normalize(`${file.slice(0, file.lastIndexOf("/"))}/${specifier}`);
|
|
2866
|
+
return extensions.map((extension) => `${base}${extension}`).find((candidate) => this.#fileSet.has(candidate)) ?? null;
|
|
2867
|
+
}
|
|
2868
|
+
static #normalize(path) {
|
|
2869
|
+
const parts = [];
|
|
2870
|
+
for (const part of path.split("/"))
|
|
2871
|
+
if (part === "..")
|
|
2872
|
+
parts.pop();
|
|
2873
|
+
else if (part !== "." && part !== "")
|
|
2874
|
+
parts.push(part);
|
|
2875
|
+
return parts.join("/");
|
|
2876
|
+
}
|
|
2877
|
+
}
|
|
2878
|
+
// pkgs/@akanjs/language/frameworkMapDoc.ts
|
|
2879
|
+
var startMarker = "<!-- akan:framework-map:start -->";
|
|
2880
|
+
var endMarker = "<!-- akan:framework-map:end -->";
|
|
2881
|
+
var citedPattern = /`([A-Z][a-z][A-Za-z0-9]*)(?:[.#][\w$#]+)?(?:\(\))?`/g;
|
|
2882
|
+
|
|
2883
|
+
class FrameworkMapDoc {
|
|
2884
|
+
static regenerate = "bun run akan sync-architecture";
|
|
2885
|
+
static render(map, docDir) {
|
|
2886
|
+
const nameOf = new Map(map.facets.map((facet) => [facet.id, facet.name]));
|
|
2887
|
+
const facets = [...map.facets].sort((a, b) => a.layer - b.layer || (a.id < b.id ? -1 : 1));
|
|
2888
|
+
const list = (names) => names.length ? names.join(", ") : "\u2014";
|
|
2889
|
+
const importsOf = (facet, kind) => map.imports.filter(({ from, value, type, dynamic }) => from === facet.id && (kind === "value" ? value + dynamic > 0 : type > 0 && value + dynamic === 0)).map(({ to, value }) => `\`${nameOf.get(to) ?? to}\`${kind === "value" && value === 0 ? " (lazy)" : ""}`);
|
|
2890
|
+
const lines = [
|
|
2891
|
+
startMarker,
|
|
2892
|
+
`<!-- Generated from the framework source by \`${FrameworkMapDoc.regenerate}\`. Edit outside the markers. -->`,
|
|
2893
|
+
"<!-- Outside the markers, every backticked PascalCase name must be a framework class or channel. -->",
|
|
2894
|
+
"",
|
|
2895
|
+
"## Facet layers",
|
|
2896
|
+
"",
|
|
2897
|
+
"A facet loads values only from facets in lower layers. A type-only import loads nothing, so it may point anywhere.",
|
|
2898
|
+
"",
|
|
2899
|
+
"| Layer | Facet | Loads values from | Imports only types from |",
|
|
2900
|
+
"| --- | --- | --- | --- |",
|
|
2901
|
+
...facets.map((facet) => `| ${facet.layer} | \`${facet.name}\` | ${list(importsOf(facet, "value"))} | ${list(importsOf(facet, "type"))} |`),
|
|
2902
|
+
...map.cycles.length ? [
|
|
2903
|
+
"",
|
|
2904
|
+
`Facets that load values from each other: ${map.cycles.map((cycle) => cycle.map((id) => `\`${nameOf.get(id) ?? id}\``).join(" \u21C4 ")).join("; ")}.`
|
|
2905
|
+
] : [],
|
|
2906
|
+
"",
|
|
2907
|
+
"## Process messages",
|
|
2908
|
+
"",
|
|
2909
|
+
"Each channel is a union of messages told apart by `type`. A sender writes the message's `type` literal; a handler",
|
|
2910
|
+
"branches on it or relays it. Both are named by the class or top-level function they sit in. A dash means no site",
|
|
2911
|
+
"was recognised.",
|
|
2912
|
+
...map.channels.flatMap((channel) => {
|
|
2913
|
+
const declared = FrameworkMapDoc.#relative(docDir, channel.at.file);
|
|
2914
|
+
return [
|
|
2915
|
+
"",
|
|
2916
|
+
`### \`${channel.name}\`: ${channel.label}`,
|
|
2917
|
+
"",
|
|
2918
|
+
`Declared in [${declared}](${declared}).`,
|
|
2919
|
+
"",
|
|
2920
|
+
"| Message | Sent by | Handled by |",
|
|
2921
|
+
"| --- | --- | --- |",
|
|
2922
|
+
...channel.messages.map(({ type, senders, handlers }) => `| \`${type}\` | ${FrameworkMapDoc.#owners(senders)} | ${FrameworkMapDoc.#owners(handlers)} |`)
|
|
2923
|
+
];
|
|
2924
|
+
}),
|
|
2925
|
+
...map.missing.length ? ["", `Channels not found: ${map.missing.join("; ")}.`] : [],
|
|
2926
|
+
endMarker
|
|
2927
|
+
];
|
|
2928
|
+
return lines.join(`
|
|
2929
|
+
`);
|
|
2930
|
+
}
|
|
2931
|
+
static generatedOf(markdown) {
|
|
2932
|
+
const start = markdown.indexOf(startMarker);
|
|
2933
|
+
const end = markdown.indexOf(endMarker, start);
|
|
2934
|
+
return start < 0 || end < 0 ? null : markdown.slice(start, end + endMarker.length);
|
|
2935
|
+
}
|
|
2936
|
+
static splice(markdown, generated) {
|
|
2937
|
+
const current = FrameworkMapDoc.generatedOf(markdown);
|
|
2938
|
+
if (current !== null)
|
|
2939
|
+
return markdown.replace(current, () => generated);
|
|
2940
|
+
return `${markdown.trimEnd()}
|
|
2941
|
+
|
|
2942
|
+
${generated}
|
|
2943
|
+
`;
|
|
2944
|
+
}
|
|
2945
|
+
static citedClasses(markdown) {
|
|
2946
|
+
const current = FrameworkMapDoc.generatedOf(markdown);
|
|
2947
|
+
const handKept = current === null ? markdown : markdown.replace(current, "");
|
|
2948
|
+
return [...new Set([...handKept.matchAll(citedPattern)].map(([, name]) => name))].sort();
|
|
2949
|
+
}
|
|
2950
|
+
static unknownClasses(markdown, map) {
|
|
2951
|
+
const known = new Set([
|
|
2952
|
+
...map.facets.flatMap(({ classes }) => classes.map(({ name }) => name)),
|
|
2953
|
+
...map.channels.map(({ name }) => name)
|
|
2954
|
+
]);
|
|
2955
|
+
return FrameworkMapDoc.citedClasses(markdown).filter((name) => !known.has(name));
|
|
2956
|
+
}
|
|
2957
|
+
static #owners(sites) {
|
|
2958
|
+
return sites.length ? sites.map(({ owner }) => `\`${owner}\``).join(", ") : "\u2014";
|
|
2959
|
+
}
|
|
2960
|
+
static #relative(fromDir, file) {
|
|
2961
|
+
const from = fromDir.split("/").filter(Boolean);
|
|
2962
|
+
const to = file.split("/").filter(Boolean);
|
|
2963
|
+
let shared = 0;
|
|
2964
|
+
while (shared < from.length && shared < to.length - 1 && from[shared] === to[shared])
|
|
2965
|
+
shared += 1;
|
|
2966
|
+
return [...from.slice(shared).map(() => ".."), ...to.slice(shared)].join("/");
|
|
2967
|
+
}
|
|
2968
|
+
}
|
|
2969
|
+
export { AkanGraphDiff, createFactReader, createAst, AkanIndex, FrameworkMapBuilder, FrameworkMapDoc };
|