@akanjs/cli 3.2.0-rc.0 → 3.2.0-rc.10
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 +2 -1
- package/{CodeAgent-h6g8dg3a.js → CodeAgent-fk66n37w.js} +3 -3
- package/{CodeTui-n9gacepn.js → CodeTui-4rf1mqmv.js} +1 -1
- package/README.ko.md +26 -1
- package/README.md +26 -1
- package/TunnelShare-44er4e29.js +15 -0
- package/{agent.command-2f657613.js → agent.command-7g9czkkd.js} +20 -19
- package/akanCode.js +6 -6
- package/{application.command-yb8v177e.js → application.command-00spg55s.js} +31 -24
- package/{applicationBuildRunner-sctcmkyx.js → applicationBuildRunner-f4yf0qyc.js} +9 -7
- package/buildBatch.proc.js +7 -6
- package/{cloud.command-et06ja22.js → cloud.command-ab704p0g.js} +24 -25
- package/cloud.script-4p4y0s7e.js +31 -0
- package/{code.command-7m67c7zc.js → code.command-m6c9xk56.js} +7 -6
- package/{context.command-569z0rqf.js → context.command-hgcgxtdk.js} +20 -19
- package/context.runner-w071gmc3.js +30 -0
- package/{devTui-evr3b35y.js → devTui-h77zrjv5.js} +1 -1
- package/{guideline.command-gnv3e8p8.js → guideline.command-6yz6d4hf.js} +3 -2
- package/guidelines/agentRule/agentRule.instruction.md +4 -3
- package/guidelines/conventions/core.md +1 -1
- package/guidelines/conventions/rules/app.md +3 -0
- package/guidelines/conventions/rules/page.md +5 -0
- package/guidelines/errorCodes/errorCodes.instruction.md +140 -0
- package/guidelines/fieldRule/fieldRule.instruction.md +2 -1
- package/guidelines/mcpRule/mcpRule.instruction.md +28 -0
- package/guidelines/pageRule/pageRule.instruction.md +5 -0
- package/guidelines/runtimeRule/runtimeRule.instruction.md +5 -1
- package/guidelines/transportRule/transportRule.instruction.md +5 -0
- package/incrementalBuilder.proc.js +8 -7
- package/{index-vkzfk7qt.js → index-097bjwvm.js} +40 -30
- package/{index-93ewzmp6.js → index-23d8gw08.js} +165 -102
- package/{index-p6hjwt2d.js → index-41mfhgq0.js} +72 -26
- package/{index-ktb3wcqz.js → index-4k9nv4m3.js} +71 -81
- package/{index-ryrm5z2c.js → index-5jwx4bf2.js} +69 -33
- package/{index-5w9my9qe.js → index-6eptmk8z.js} +30 -20
- package/{index-d4n1wmrz.js → index-6fnrhpn1.js} +8 -4
- package/{index-kpdv34rq.js → index-6qtxs86x.js} +10 -7
- package/{index-ed6ea0qp.js → index-6z57w95f.js} +1 -1
- package/{index-drz9e84d.js → index-7ky4brfy.js} +2 -2
- package/{index-8g7tdsk4.js → index-849qvd7t.js} +58 -29
- package/{index-1q1cjn8s.js → index-9xh4zvw3.js} +94 -53
- package/{index-48d5qsj3.js → index-adj7qmz9.js} +23 -15
- package/{index-fjr4d6yd.js → index-apcr1qgk.js} +458 -304
- package/{index-8ts9774g.js → index-aybfcx58.js} +9 -4
- package/{index-3wjbhj2n.js → index-b3pr4ee6.js} +1 -1
- package/{index-tenw3mah.js → index-b4qq3rq0.js} +32 -7
- package/{index-thckcabc.js → index-bper31wr.js} +24 -6
- package/{index-jdamtr7e.js → index-bxkx0x03.js} +17 -10
- package/{index-0fb3cpbt.js → index-c3r23w24.js} +9 -7
- package/{index-8pb9yvp8.js → index-dfsa009c.js} +148 -31
- package/{index-zpv14wpy.js → index-dsg50er9.js} +3 -3
- package/{index-hqrkqxx2.js → index-e5f6czck.js} +46 -7
- package/{index-sy481q2x.js → index-frvjq1rd.js} +1 -1
- package/{index-abgh0jve.js → index-grt0e6kk.js} +1 -1
- package/{index-e3vmjtph.js → index-hf9nvwam.js} +7 -2
- package/{index-zxsygkda.js → index-hhagjhkh.js} +7 -7
- package/index-j4yvgfhr.js +70 -0
- package/{index-s2grpzef.js → index-k2r3hmbz.js} +6 -4
- package/{index-7y5yghjg.js → index-nd39rssn.js} +1162 -435
- package/{index-6wpaq392.js → index-p4htjyvk.js} +44 -5
- package/{index-aj3ehkcx.js → index-p54s83sz.js} +1 -1
- package/{index-7xdpww43.js → index-rdx8cyv2.js} +342 -134
- package/{index-sr5da35e.js → index-sq9d0jhj.js} +21 -18
- package/{index-eajbmk10.js → index-t4d0ckde.js} +2 -2
- package/{index-mqntfzv5.js → index-t8h09yjs.js} +7 -2
- package/index-va90m8nd.js +25 -0
- package/{index-rc5fvqj2.js → index-xfv2p7b5.js} +95 -58
- package/{index-w639tvkd.js → index-xgqz3kak.js} +29 -8
- package/{index-9tbgb4d8.js → index-xy70tcv4.js} +1 -1
- package/{index-mmym5xmb.js → index-z43wddce.js} +5 -3
- package/index.js +24 -23
- package/{library.command-1gp5qe3r.js → library.command-kc1zahcg.js} +5 -4
- package/{localRegistry.command-v3g21rex.js → localRegistry.command-bz2df40m.js} +19 -18
- package/{module.command-4xz9he6h.js → module.command-cchwzz9r.js} +12 -7
- package/{package.command-s7qpnn1g.js → package.command-qcjajpv8.js} +8 -7
- package/package.json +2 -2
- package/{page.command-fan34c49.js → page.command-1t86dx8a.js} +4 -3
- package/{pipeline.command-ph4fnn9w.js → pipeline.command-2xx9z3d0.js} +3 -2
- package/{primitive.command-3fqh8emf.js → primitive.command-mc0x8txr.js} +8 -7
- package/{projectTypecheck-vkb40hjc.js → projectTypecheck-tz497yx7.js} +2 -1
- package/{quality.command-ecj6exsh.js → quality.command-11rayf5c.js} +7 -6
- package/{repair.command-fe9jw719.js → repair.command-8w41vy78.js} +7 -6
- package/{scalar.command-cnvfhk0n.js → scalar.command-rvwh98ny.js} +7 -6
- package/skills/akan-debug/SKILL.md +7 -3
- package/skills/akan-err/SKILL.md +7 -0
- package/skills/akan-mcp/SKILL.md +7 -1
- package/skills/akan-scaffold/SKILL.md +3 -2
- package/skills/akan-transport/SKILL.md +2 -0
- package/skills/akan-validate/SKILL.md +1 -0
- package/{subspace.command-yxtt3v3e.js → subspace.command-0970n7kd.js} +9 -8
- package/{tunnel.command-ggj38mqf.js → tunnel.command-bcy0rtmh.js} +8 -7
- package/typecheck.proc.js +5 -2
- package/{verify.command-w9ttxkdz.js → verify.command-ynaztxr2.js} +18 -16
- package/{workflow.command-ss8devj0.js → workflow.command-yp9ajthb.js} +45 -16
- package/{workspace.command-rw1p0xeq.js → workspace.command-e4wfxm86.js} +39 -34
- package/TunnelShare-k3ja4w9x.js +0 -14
- package/cloud.script-s6rbrwer.js +0 -30
- package/context.runner-ms8v1rxy.js +0 -29
|
@@ -9,7 +9,8 @@ var isEdited = (before, after) => before.digest !== undefined && after.digest !=
|
|
|
9
9
|
var byId = (a, b) => a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
|
|
10
10
|
|
|
11
11
|
class AkanGraphDiff {
|
|
12
|
-
static between(
|
|
12
|
+
static between(given, head) {
|
|
13
|
+
const base = AkanGraphDiff.#renamed(given, head);
|
|
13
14
|
const baseNodes = new Map(base.nodes.map((node) => [node.id, node]));
|
|
14
15
|
const headNodes = new Map(head.nodes.map((node) => [node.id, node]));
|
|
15
16
|
const marked = new Map;
|
|
@@ -28,6 +29,9 @@ class AkanGraphDiff {
|
|
|
28
29
|
...head.edges.flatMap((edge) => !baseKeys.has(edgeKeyOf(edge)) ? [{ ...edge, change: "added" }] : marked.has(edge.from) || marked.has(edge.to) ? [edge] : []),
|
|
29
30
|
...base.edges.filter((edge) => !headKeys.has(edgeKeyOf(edge))).map((edge) => ({ ...edge, change: "removed" }))
|
|
30
31
|
];
|
|
32
|
+
const shown = new Set([...marked.keys(), ...edges.flatMap((edge) => [edge.from, edge.to])]);
|
|
33
|
+
const listed = new Set(edges.map(edgeKeyOf));
|
|
34
|
+
edges.push(...head.edges.filter((edge) => edge.kind === "binds" && shown.has(edge.from) && !listed.has(edgeKeyOf(edge))));
|
|
31
35
|
const context = edges.flatMap((edge) => [edge.from, edge.to]).filter((id) => !marked.has(id)).flatMap((id) => {
|
|
32
36
|
const node = headNodes.get(id) ?? baseNodes.get(id);
|
|
33
37
|
return node ? [node] : [];
|
|
@@ -39,6 +43,40 @@ class AkanGraphDiff {
|
|
|
39
43
|
summary[node.change] += 1;
|
|
40
44
|
return { version: 1, nodes, edges, summary };
|
|
41
45
|
}
|
|
46
|
+
static #renamed(base, head) {
|
|
47
|
+
const baseIds = new Set(base.nodes.map((node) => node.id));
|
|
48
|
+
const headIds = new Set(head.nodes.map((node) => node.id));
|
|
49
|
+
const pairKeyOf = ({ kind, owner, name, at }) => at ? `${kind}
|
|
50
|
+
${owner ?? ""}
|
|
51
|
+
${name}
|
|
52
|
+
${at.file}` : null;
|
|
53
|
+
const shortIdOf = ({ kind, owner, name }) => `${kind}:${owner ?? ""}:${name}`;
|
|
54
|
+
const isFileKeyed = (node) => node.id !== shortIdOf(node) && node.id.startsWith(`${node.kind}:${node.owner ?? ""}:`) && node.id.endsWith(`#${node.name}`);
|
|
55
|
+
const fromBase = new Map(base.nodes.flatMap((node) => {
|
|
56
|
+
const key = headIds.has(node.id) ? null : pairKeyOf(node);
|
|
57
|
+
return key ? [[key, node]] : [];
|
|
58
|
+
}));
|
|
59
|
+
const renames = new Map;
|
|
60
|
+
for (const node of head.nodes) {
|
|
61
|
+
if (baseIds.has(node.id))
|
|
62
|
+
continue;
|
|
63
|
+
const key = pairKeyOf(node);
|
|
64
|
+
const before = key ? fromBase.get(key) : undefined;
|
|
65
|
+
if (!before)
|
|
66
|
+
continue;
|
|
67
|
+
const isPair = isFileKeyed(node) ? before.id === shortIdOf(node) : isFileKeyed(before) && node.id === shortIdOf(before);
|
|
68
|
+
if (isPair)
|
|
69
|
+
renames.set(before.id, node.id);
|
|
70
|
+
}
|
|
71
|
+
if (!renames.size)
|
|
72
|
+
return base;
|
|
73
|
+
const rename = (id) => renames.get(id) ?? id;
|
|
74
|
+
return {
|
|
75
|
+
...base,
|
|
76
|
+
nodes: base.nodes.map((node) => renames.has(node.id) ? { ...node, id: rename(node.id) } : node),
|
|
77
|
+
edges: base.edges.map((edge) => ({ ...edge, from: rename(edge.from), to: rename(edge.to) }))
|
|
78
|
+
};
|
|
79
|
+
}
|
|
42
80
|
static #mark(node, change) {
|
|
43
81
|
return node.fields ? { ...node, change, fields: node.fields.map((field) => ({ ...field, change })) } : { ...node, change };
|
|
44
82
|
}
|
|
@@ -72,6 +110,261 @@ class AkanGraphDiff {
|
|
|
72
110
|
return node.fields ? { ...node, fields: node.fields.map(({ digest: _field, ...field }) => field) } : node;
|
|
73
111
|
}
|
|
74
112
|
}
|
|
113
|
+
// pkgs/@akanjs/language/escapeRegExp.ts
|
|
114
|
+
var regExpSpecials = /[\\^$.*+?()[\]{}|]/g;
|
|
115
|
+
var escapeRegExp = (text) => text.replace(regExpSpecials, (special) => `\\${special}`);
|
|
116
|
+
|
|
117
|
+
// pkgs/@akanjs/language/guardFacts.ts
|
|
118
|
+
var guardLabelOf = (guard, labels) => {
|
|
119
|
+
const [, role = null, local = guard] = guard.match(/^([\w$]+) (.+)$/) ?? [];
|
|
120
|
+
const label = labels[local];
|
|
121
|
+
return label === undefined ? guard : role ? `${role} ${label}` : label;
|
|
122
|
+
};
|
|
123
|
+
var maxLabelArgs = 32;
|
|
124
|
+
var maxLayers = 4;
|
|
125
|
+
var guardRefReaderOf = (ts, ast, unwrap) => {
|
|
126
|
+
const pathOf = (expression) => {
|
|
127
|
+
if (ts.isIdentifier(expression))
|
|
128
|
+
return expression.text;
|
|
129
|
+
if (!ts.isPropertyAccessExpression(expression))
|
|
130
|
+
return null;
|
|
131
|
+
const head = pathOf(expression.expression);
|
|
132
|
+
return head && `${head}.${expression.name.text}`;
|
|
133
|
+
};
|
|
134
|
+
const layersOf = (call) => {
|
|
135
|
+
const inner = unwrap(call.expression);
|
|
136
|
+
if (!ts.isCallExpression(inner))
|
|
137
|
+
return { root: call, layers: 0 };
|
|
138
|
+
const { root, layers } = layersOf(inner);
|
|
139
|
+
return { root, layers: layers + 1 };
|
|
140
|
+
};
|
|
141
|
+
const calleeOf = (call) => pathOf(unwrap(layersOf(call).root.expression)) ?? "?";
|
|
142
|
+
const labelOf = (source, call) => {
|
|
143
|
+
const args = call.arguments.map((arg) => arg.getText(source)).join(", ");
|
|
144
|
+
const inner = unwrap(call.expression);
|
|
145
|
+
const head = ts.isCallExpression(inner) ? labelOf(source, inner) : pathOf(inner) ?? "?";
|
|
146
|
+
return `${head}(${args.length <= maxLabelArgs && !args.includes(`
|
|
147
|
+
`) ? args : "\u2026"})`;
|
|
148
|
+
};
|
|
149
|
+
const guardRefOf = (expression, made, member) => {
|
|
150
|
+
const value = unwrap(expression);
|
|
151
|
+
if (!ts.isCallExpression(value))
|
|
152
|
+
return pathOf(value) ?? ast.expressionName(value);
|
|
153
|
+
const prefix = `${member}:${calleeOf(value)}@`;
|
|
154
|
+
const name = `${prefix}${made.filter((other) => other.name.startsWith(prefix)).length + 1}`;
|
|
155
|
+
made.push({ call: value, name });
|
|
156
|
+
return name;
|
|
157
|
+
};
|
|
158
|
+
const guardsIn = (source, object, made, member) => {
|
|
159
|
+
const guards = object?.properties.find((property) => ts.isPropertyAssignment(property) && ast.nodeName(property.name) === "guards")?.initializer;
|
|
160
|
+
if (!guards)
|
|
161
|
+
return [];
|
|
162
|
+
const refOf = (expression) => guardRefOf(expression, made, member);
|
|
163
|
+
if (ts.isArrayLiteralExpression(guards))
|
|
164
|
+
return guards.elements.map(refOf).filter((name) => name !== null);
|
|
165
|
+
if (ts.isObjectLiteralExpression(guards))
|
|
166
|
+
return guards.properties.filter(ts.isPropertyAssignment).flatMap((property) => {
|
|
167
|
+
const value = unwrap(property.initializer);
|
|
168
|
+
const names = ts.isArrayLiteralExpression(value) ? value.elements.map(refOf) : [refOf(value)];
|
|
169
|
+
return names.map((name) => `${ast.nodeName(property.name)} ${name ?? "?"}`);
|
|
170
|
+
});
|
|
171
|
+
const single = refOf(guards);
|
|
172
|
+
return single ? [single] : [];
|
|
173
|
+
};
|
|
174
|
+
return { pathOf, layersOf, labelOf, guardsIn };
|
|
175
|
+
};
|
|
176
|
+
var guardFactoryReaderOf = (ts, ast, { spanOf, extentOf, unwrap, docOf }, pathOf, layersOf) => {
|
|
177
|
+
const implementsGuard = (node, guardNames) => !!node.heritageClauses?.some((clause) => clause.token === ts.SyntaxKind.ImplementsKeyword && clause.types.some(({ expression }) => guardNames.has(ast.expressionName(expression) ?? "")));
|
|
178
|
+
const scopeOf = (node) => {
|
|
179
|
+
const scope = node.members.find((member) => ts.isPropertyDeclaration(member) && ast.nodeName(member.name) === "scope" && !!ts.getModifiers(member)?.some(({ kind }) => kind === ts.SyntaxKind.StaticKeyword))?.initializer;
|
|
180
|
+
const value = scope && unwrap(scope);
|
|
181
|
+
return value && ts.isStringLiteralLike(value) ? value.text : null;
|
|
182
|
+
};
|
|
183
|
+
const functionsOf = (source) => {
|
|
184
|
+
const functions = new Map;
|
|
185
|
+
for (const statement of source.statements) {
|
|
186
|
+
if (ts.isFunctionDeclaration(statement) && statement.name && statement.body)
|
|
187
|
+
functions.set(statement.name.text, { statement, name: statement.name, body: statement.body });
|
|
188
|
+
if (!ts.isVariableStatement(statement))
|
|
189
|
+
continue;
|
|
190
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
191
|
+
const initializer = declaration.initializer && unwrap(declaration.initializer);
|
|
192
|
+
if (initializer && ts.isIdentifier(declaration.name) && (ts.isArrowFunction(initializer) || ts.isFunctionExpression(initializer)))
|
|
193
|
+
functions.set(declaration.name.text, { statement, name: declaration.name, body: initializer.body });
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return functions;
|
|
197
|
+
};
|
|
198
|
+
const returnedOf = (body, guardNames, depth = 0) => {
|
|
199
|
+
const locals = new Map;
|
|
200
|
+
const pending = [];
|
|
201
|
+
const visit = (node) => {
|
|
202
|
+
if (ts.isClassDeclaration(node) && node.name)
|
|
203
|
+
locals.set(node.name.text, node);
|
|
204
|
+
if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && node.initializer)
|
|
205
|
+
locals.set(node.name.text, node.initializer);
|
|
206
|
+
if (ts.isReturnStatement(node) && node.expression)
|
|
207
|
+
pending.push(node.expression);
|
|
208
|
+
if (!ts.isFunctionLike(node) && !ts.isClassLike(node))
|
|
209
|
+
ts.forEachChild(node, visit);
|
|
210
|
+
};
|
|
211
|
+
if (ts.isBlock(body))
|
|
212
|
+
ts.forEachChild(body, visit);
|
|
213
|
+
else
|
|
214
|
+
pending.push(body);
|
|
215
|
+
let guard = null;
|
|
216
|
+
const calls = [];
|
|
217
|
+
const seen = new Set;
|
|
218
|
+
const passed = new Set;
|
|
219
|
+
const returned = [];
|
|
220
|
+
for (let node = pending.pop();node; node = pending.pop()) {
|
|
221
|
+
if (seen.has(node))
|
|
222
|
+
continue;
|
|
223
|
+
seen.add(node);
|
|
224
|
+
const value = ts.isExpression(node) ? unwrap(node) : node;
|
|
225
|
+
if ((ts.isClassExpression(value) || ts.isClassDeclaration(value)) && implementsGuard(value, guardNames))
|
|
226
|
+
guard ??= value;
|
|
227
|
+
else if ((ts.isArrowFunction(value) || ts.isFunctionExpression(value)) && !passed.has(node) && depth < maxLayers)
|
|
228
|
+
returned.push(value.body);
|
|
229
|
+
else if (ts.isConditionalExpression(value))
|
|
230
|
+
pending.push(value.whenTrue, value.whenFalse);
|
|
231
|
+
else if (ts.isBinaryExpression(value))
|
|
232
|
+
pending.push(value.left, value.right);
|
|
233
|
+
else if (ts.isIdentifier(value)) {
|
|
234
|
+
const local = locals.get(value.text);
|
|
235
|
+
if (local)
|
|
236
|
+
pending.push(local);
|
|
237
|
+
} else if (ts.isCallExpression(value)) {
|
|
238
|
+
const { root, layers } = layersOf(value);
|
|
239
|
+
const callee = pathOf(unwrap(root.expression));
|
|
240
|
+
if (callee && !passed.has(node) && !calls.some((call) => call.callee === callee))
|
|
241
|
+
calls.push({ callee, layers });
|
|
242
|
+
for (let call = value;ts.isCallExpression(call); call = unwrap(call.expression)) {
|
|
243
|
+
for (const arg of call.arguments)
|
|
244
|
+
passed.add(arg);
|
|
245
|
+
pending.push(...call.arguments);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
const [inner] = returned;
|
|
250
|
+
if (guard || calls.length || !inner)
|
|
251
|
+
return { guard, calls, layers: 0 };
|
|
252
|
+
const nested = returnedOf(inner, guardNames, depth + 1);
|
|
253
|
+
return { ...nested, layers: nested.layers + 1 };
|
|
254
|
+
};
|
|
255
|
+
const guardFactoriesOf = (source, guardNames, isImported) => {
|
|
256
|
+
const functions = functionsOf(source);
|
|
257
|
+
const returned = new Map([...functions].map(([name, { body }]) => [name, returnedOf(body, guardNames)]));
|
|
258
|
+
const members = new Set;
|
|
259
|
+
const isFactoryCall = (callee) => members.has(callee) || !functions.has(callee) && isImported(callee);
|
|
260
|
+
for (let isGrowing = true;isGrowing; ) {
|
|
261
|
+
isGrowing = false;
|
|
262
|
+
for (const [name, { guard, calls }] of returned) {
|
|
263
|
+
if (members.has(name) || !guard && !calls.some(({ callee }) => isFactoryCall(callee)))
|
|
264
|
+
continue;
|
|
265
|
+
members.add(name);
|
|
266
|
+
isGrowing = true;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
const factories = new Map;
|
|
270
|
+
for (const name of members) {
|
|
271
|
+
const found = functions.get(name);
|
|
272
|
+
const made = returned.get(name);
|
|
273
|
+
if (!found || !made)
|
|
274
|
+
continue;
|
|
275
|
+
factories.set(name, {
|
|
276
|
+
name,
|
|
277
|
+
span: spanOf(source, found.name),
|
|
278
|
+
extent: extentOf(source, found.statement),
|
|
279
|
+
scope: made.guard ? scopeOf(made.guard) : null,
|
|
280
|
+
doc: docOf(source, found.statement),
|
|
281
|
+
direct: !!made.guard,
|
|
282
|
+
via: made.calls.filter(({ callee }) => isFactoryCall(callee)),
|
|
283
|
+
layers: made.layers
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
return factories;
|
|
287
|
+
};
|
|
288
|
+
return { implementsGuard, scopeOf, guardFactoriesOf };
|
|
289
|
+
};
|
|
290
|
+
var createGuardFactReader = (ts, ast, base) => {
|
|
291
|
+
const { spanOf, extentOf, unwrap, docOf } = base;
|
|
292
|
+
const { pathOf, layersOf, labelOf, guardsIn } = guardRefReaderOf(ts, ast, unwrap);
|
|
293
|
+
const { implementsGuard, scopeOf, guardFactoriesOf } = guardFactoryReaderOf(ts, ast, base, pathOf, layersOf);
|
|
294
|
+
const guardsOf = (source, imports, madeInPlace) => {
|
|
295
|
+
const guardNames = new Set([
|
|
296
|
+
"Guard",
|
|
297
|
+
...imports.filter(({ imported }) => imported === "Guard").map(({ name }) => name)
|
|
298
|
+
]);
|
|
299
|
+
const imported = new Set(imports.map(({ name }) => name));
|
|
300
|
+
const isImported = (callee) => imported.has(callee.split(".")[0] ?? callee);
|
|
301
|
+
const factories = guardFactoriesOf(source, guardNames, isImported);
|
|
302
|
+
const guards = [];
|
|
303
|
+
const calls = [];
|
|
304
|
+
const makeOf = (made, value, isInPlace = false) => {
|
|
305
|
+
if (ts.isClassExpression(value) && implementsGuard(value, guardNames)) {
|
|
306
|
+
guards.push({ ...made, scope: scopeOf(value), factory: null, layers: 0 });
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
const { root, layers } = ts.isCallExpression(value) ? layersOf(value) : { root: null, layers: 0 };
|
|
310
|
+
const callee = root && pathOf(unwrap(root.expression));
|
|
311
|
+
const factory = callee && factories.get(callee);
|
|
312
|
+
if (factory)
|
|
313
|
+
guards.push({ ...made, scope: factory.scope, factory: factory.name, layers });
|
|
314
|
+
else if (callee && (isImported(callee) || isInPlace))
|
|
315
|
+
calls.push({ ...made, callee, kind: "call", scope: null, layers });
|
|
316
|
+
};
|
|
317
|
+
for (const statement of source.statements) {
|
|
318
|
+
if (ts.isClassDeclaration(statement) && statement.name) {
|
|
319
|
+
const declared = {
|
|
320
|
+
name: statement.name.text,
|
|
321
|
+
span: spanOf(source, statement.name),
|
|
322
|
+
extent: extentOf(source, statement),
|
|
323
|
+
scope: scopeOf(statement),
|
|
324
|
+
doc: docOf(source, statement)
|
|
325
|
+
};
|
|
326
|
+
const base = statement.heritageClauses?.find(({ token }) => token === ts.SyntaxKind.ExtendsKeyword)?.types[0];
|
|
327
|
+
const callee = base && pathOf(base.expression);
|
|
328
|
+
if (implementsGuard(statement, guardNames))
|
|
329
|
+
guards.push({ ...declared, factory: null, layers: 0 });
|
|
330
|
+
else if (callee)
|
|
331
|
+
calls.push({ ...declared, callee, kind: "extends", layers: 0 });
|
|
332
|
+
}
|
|
333
|
+
if (!ts.isVariableStatement(statement))
|
|
334
|
+
continue;
|
|
335
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
336
|
+
const initializer = declaration.initializer && unwrap(declaration.initializer);
|
|
337
|
+
if (!initializer || !ts.isIdentifier(declaration.name))
|
|
338
|
+
continue;
|
|
339
|
+
const holder = declaration.name.text;
|
|
340
|
+
makeOf({
|
|
341
|
+
name: holder,
|
|
342
|
+
span: spanOf(source, declaration.name),
|
|
343
|
+
extent: extentOf(source, statement),
|
|
344
|
+
doc: docOf(source, statement)
|
|
345
|
+
}, initializer);
|
|
346
|
+
if (!ts.isObjectLiteralExpression(initializer))
|
|
347
|
+
continue;
|
|
348
|
+
for (const property of initializer.properties) {
|
|
349
|
+
const key = ts.isPropertyAssignment(property) ? ast.nodeName(property.name) : null;
|
|
350
|
+
if (key && ts.isPropertyAssignment(property))
|
|
351
|
+
makeOf({
|
|
352
|
+
name: `${holder}.${key}`,
|
|
353
|
+
span: spanOf(source, property.name),
|
|
354
|
+
extent: extentOf(source, property),
|
|
355
|
+
doc: docOf(source, property)
|
|
356
|
+
}, unwrap(property.initializer));
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
for (const { call, name } of madeInPlace)
|
|
361
|
+
makeOf({ name, span: spanOf(source, call), extent: extentOf(source, call), doc: null }, call, true);
|
|
362
|
+
const guardLabels = Object.fromEntries(madeInPlace.map(({ call, name }) => [name, labelOf(source, call)]));
|
|
363
|
+
return { guards, guardFactories: [...factories.values()], guardCalls: calls, guardLabels };
|
|
364
|
+
};
|
|
365
|
+
return { guardsIn, guardsOf };
|
|
366
|
+
};
|
|
367
|
+
|
|
75
368
|
// pkgs/@akanjs/language/moduleFacts.ts
|
|
76
369
|
var testFilePattern = /\.(?:test|spec)\.tsx?$/;
|
|
77
370
|
var moduleCallees = new Set([
|
|
@@ -140,21 +433,6 @@ var createFactReader = (ts, ast) => {
|
|
|
140
433
|
const value = unwrap(expression);
|
|
141
434
|
return ts.isPropertyAccessExpression(value) && ts.isIdentifier(value.expression) ? `${value.expression.text}.${value.name.text}` : ast.expressionName(value);
|
|
142
435
|
};
|
|
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
436
|
const optionObjectOf = (call) => call.arguments.find((arg) => ts.isObjectLiteralExpression(arg) && arg.properties.some((property) => ast.propertyName(property) === "guards"));
|
|
159
437
|
const chainOf = (initializer) => {
|
|
160
438
|
let root = unwrap(initializer);
|
|
@@ -171,7 +449,7 @@ var createFactReader = (ts, ast) => {
|
|
|
171
449
|
const node = keyName(property);
|
|
172
450
|
return name && node ? [{ name, span: spanOf(source, node), extent: extentOf(source, property) }] : [];
|
|
173
451
|
});
|
|
174
|
-
const sliceKeysOf = (source, builder) => (returnedObject(builder)?.properties ?? []).flatMap((property) => {
|
|
452
|
+
const sliceKeysOf = (source, builder, made, model) => (returnedObject(builder)?.properties ?? []).flatMap((property) => {
|
|
175
453
|
if (!ts.isPropertyAssignment(property))
|
|
176
454
|
return [];
|
|
177
455
|
const name = ast.nodeName(property.name);
|
|
@@ -183,7 +461,7 @@ var createFactReader = (ts, ast) => {
|
|
|
183
461
|
name,
|
|
184
462
|
span: spanOf(source, property.name),
|
|
185
463
|
extent: extentOf(source, property),
|
|
186
|
-
guards: root ? guardsIn(optionObjectOf(root)) : [],
|
|
464
|
+
guards: root ? guardsIn(source, optionObjectOf(root), made, `${model}.${name}`) : [],
|
|
187
465
|
live: methods.includes("live"),
|
|
188
466
|
argCount: methods.filter((method) => argMethods.has(method)).length
|
|
189
467
|
}
|
|
@@ -233,7 +511,7 @@ var createFactReader = (ts, ast) => {
|
|
|
233
511
|
const [size, unit] = intervalUnits.find(([size]) => ms >= size && ms % size === 0) ?? [1, "ms"];
|
|
234
512
|
return `${ms / size}${unit}`;
|
|
235
513
|
};
|
|
236
|
-
const modesOf = (option) => {
|
|
514
|
+
const modesOf = (option, type) => {
|
|
237
515
|
const object = option && unwrap(option);
|
|
238
516
|
if (!object || !ts.isObjectLiteralExpression(object))
|
|
239
517
|
return [];
|
|
@@ -243,9 +521,9 @@ var createFactReader = (ts, ast) => {
|
|
|
243
521
|
if (name === "enabled")
|
|
244
522
|
return value.kind === ts.SyntaxKind.FalseKeyword ? ["disabled"] : [];
|
|
245
523
|
if (name === "once")
|
|
246
|
-
return value.kind === ts.SyntaxKind.TrueKeyword ? ["once"] : [];
|
|
524
|
+
return type === "initialize" && value.kind === ts.SyntaxKind.TrueKeyword ? ["once"] : [];
|
|
247
525
|
if (name === "lock")
|
|
248
|
-
return value.kind === ts.SyntaxKind.FalseKeyword ? ["unlocked"] : [];
|
|
526
|
+
return type === "interval" && value.kind === ts.SyntaxKind.FalseKeyword ? ["unlocked"] : [];
|
|
249
527
|
if (name !== "serverMode" && name !== "operationMode")
|
|
250
528
|
return [];
|
|
251
529
|
const values = ts.isArrayLiteralExpression(value) ? [...value.elements] : [value];
|
|
@@ -263,7 +541,8 @@ var createFactReader = (ts, ast) => {
|
|
|
263
541
|
const { root } = chainOf(property.initializer);
|
|
264
542
|
const type = root ? verbOf(root.expression) : null;
|
|
265
543
|
const [first, second] = root?.arguments ?? [];
|
|
266
|
-
const
|
|
544
|
+
const cron = type === "cron" && first ? unwrap(first) : null;
|
|
545
|
+
const schedule = cron ? ts.isStringLiteralLike(cron) ? cron.text : cron.getText(source) : type === "interval" || type === "timeout" ? durationOf(first, source) : null;
|
|
267
546
|
const option = type === "initialize" || type === "destroy" ? first : second;
|
|
268
547
|
return [
|
|
269
548
|
{
|
|
@@ -272,7 +551,7 @@ var createFactReader = (ts, ast) => {
|
|
|
272
551
|
extent: extentOf(source, property),
|
|
273
552
|
type,
|
|
274
553
|
schedule,
|
|
275
|
-
modes: modesOf(option)
|
|
554
|
+
modes: modesOf(option, type)
|
|
276
555
|
}
|
|
277
556
|
];
|
|
278
557
|
});
|
|
@@ -289,7 +568,20 @@ var createFactReader = (ts, ast) => {
|
|
|
289
568
|
return name && targetName ? [{ name, span: spanOf(source, property.name), target: targetName }] : [];
|
|
290
569
|
});
|
|
291
570
|
};
|
|
292
|
-
const
|
|
571
|
+
const adaptorBindingsIn = (source) => {
|
|
572
|
+
const bindings = [];
|
|
573
|
+
const visit = (node) => {
|
|
574
|
+
if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && node.expression.name.text === "applyAdaptor") {
|
|
575
|
+
const [role, adaptor] = node.arguments.map(guardNameOf);
|
|
576
|
+
if (role && adaptor)
|
|
577
|
+
bindings.push({ role, adaptor, span: spanOf(source, node.expression.name) });
|
|
578
|
+
}
|
|
579
|
+
ts.forEachChild(node, visit);
|
|
580
|
+
};
|
|
581
|
+
visit(source);
|
|
582
|
+
return bindings;
|
|
583
|
+
};
|
|
584
|
+
const endpointKeysOf = (source, builder, made, model) => {
|
|
293
585
|
const verbOf = verbReaderOf(builder?.parameters[0]);
|
|
294
586
|
return (returnedObject(builder)?.properties ?? []).flatMap((property) => {
|
|
295
587
|
if (!ts.isPropertyAssignment(property))
|
|
@@ -304,7 +596,7 @@ var createFactReader = (ts, ast) => {
|
|
|
304
596
|
span: spanOf(source, property.name),
|
|
305
597
|
extent: extentOf(source, property),
|
|
306
598
|
verb: root ? verbOf(root.expression) : null,
|
|
307
|
-
guards: root ? guardsIn(optionObjectOf(root)) : [],
|
|
599
|
+
guards: root ? guardsIn(source, optionObjectOf(root), made, `${model}.${name}`) : [],
|
|
308
600
|
argCount: methods.filter((method) => argMethods.has(method)).length
|
|
309
601
|
}
|
|
310
602
|
];
|
|
@@ -371,22 +663,6 @@ var createFactReader = (ts, ast) => {
|
|
|
371
663
|
})) : []
|
|
372
664
|
];
|
|
373
665
|
};
|
|
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
666
|
const docOf = (source, node) => {
|
|
391
667
|
const { text } = source;
|
|
392
668
|
const ranges = ts.getLeadingCommentRanges(text, node.getFullStart()) ?? [];
|
|
@@ -402,108 +678,20 @@ var createFactReader = (ts, ast) => {
|
|
|
402
678
|
`)).map((line) => line.trim().replace(/^\/\*\*?|\*\/$/g, "").replace(/^(?:\/\/[?*!]?|\*)/, "").replace(/\{@link\s+([^}\s|]+)[^}]*\}/g, "$1").trim()).filter((line) => line && !/^(?:biome-ignore|eslint-|@ts-)/.test(line));
|
|
403
679
|
return lines.length ? lines.join(" ") : null;
|
|
404
680
|
};
|
|
405
|
-
const
|
|
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
|
-
};
|
|
681
|
+
const { guardsIn, guardsOf } = createGuardFactReader(ts, ast, { spanOf, extentOf, unwrap, docOf });
|
|
498
682
|
const lowerFirst = (text) => text.charAt(0).toLowerCase() + text.slice(1);
|
|
499
683
|
const importsIn = (source) => source.statements.filter(ts.isImportDeclaration).flatMap(importsOf);
|
|
500
684
|
const read = (source, owner) => {
|
|
501
685
|
const imports = importsIn(source);
|
|
686
|
+
const madeInPlace = [];
|
|
502
687
|
const facts = {
|
|
503
688
|
fileName: source.fileName,
|
|
504
689
|
owner,
|
|
505
690
|
imports,
|
|
506
|
-
|
|
691
|
+
guards: [],
|
|
692
|
+
guardFactories: [],
|
|
693
|
+
guardCalls: [],
|
|
694
|
+
guardLabels: {},
|
|
507
695
|
constants: [],
|
|
508
696
|
enums: [],
|
|
509
697
|
filters: [],
|
|
@@ -513,6 +701,7 @@ var createFactReader = (ts, ast) => {
|
|
|
513
701
|
endpoints: [],
|
|
514
702
|
internals: [],
|
|
515
703
|
adaptors: [],
|
|
704
|
+
adaptorBindings: source.text.includes("applyAdaptor") ? adaptorBindingsIn(source) : [],
|
|
516
705
|
stores: []
|
|
517
706
|
};
|
|
518
707
|
for (const statement of source.statements) {
|
|
@@ -537,16 +726,19 @@ var createFactReader = (ts, ast) => {
|
|
|
537
726
|
...name,
|
|
538
727
|
model: moduleRefOf(call.arguments[0], "db"),
|
|
539
728
|
methods: methodsOf(source, statement),
|
|
540
|
-
plugs: plugsOf(source, builder)
|
|
729
|
+
plugs: plugsOf(source, builder),
|
|
730
|
+
fields: objectKeys(source, returnedObject(builder)).map(({ name }) => name)
|
|
541
731
|
});
|
|
542
|
-
else if (callee === "slice")
|
|
732
|
+
else if (callee === "slice") {
|
|
733
|
+
const model = moduleRefOf(call.arguments[0], "srv");
|
|
734
|
+
const member = model ?? name.name;
|
|
543
735
|
facts.slices.push({
|
|
544
736
|
...name,
|
|
545
|
-
model
|
|
546
|
-
guards: guardsIn(call.arguments.find(ts.isObjectLiteralExpression)),
|
|
547
|
-
keys: sliceKeysOf(source, builder)
|
|
737
|
+
model,
|
|
738
|
+
guards: guardsIn(source, call.arguments.find(ts.isObjectLiteralExpression), madeInPlace, member),
|
|
739
|
+
keys: sliceKeysOf(source, builder, madeInPlace, member)
|
|
548
740
|
});
|
|
549
|
-
else if (callee === "internal")
|
|
741
|
+
} else if (callee === "internal")
|
|
550
742
|
facts.internals.push({
|
|
551
743
|
...name,
|
|
552
744
|
model: moduleRefOf(call.arguments[0], "srv"),
|
|
@@ -561,13 +753,14 @@ var createFactReader = (ts, ast) => {
|
|
|
561
753
|
plugs: plugsOf(source, builder),
|
|
562
754
|
doc: docOf(source, statement)
|
|
563
755
|
});
|
|
564
|
-
} else if (callee === "endpoint")
|
|
756
|
+
} else if (callee === "endpoint") {
|
|
757
|
+
const model = moduleRefOf(call.arguments[0], "srv");
|
|
565
758
|
facts.endpoints.push({
|
|
566
759
|
...name,
|
|
567
|
-
model
|
|
568
|
-
keys: endpointKeysOf(source, builder)
|
|
760
|
+
model,
|
|
761
|
+
keys: endpointKeysOf(source, builder, madeInPlace, model ?? name.name)
|
|
569
762
|
});
|
|
570
|
-
else if (callee === "store")
|
|
763
|
+
} else if (callee === "store")
|
|
571
764
|
facts.stores.push({
|
|
572
765
|
...name,
|
|
573
766
|
model: moduleRefOf(call.arguments[0], "sig"),
|
|
@@ -575,10 +768,647 @@ var createFactReader = (ts, ast) => {
|
|
|
575
768
|
actions: methodsOf(source, statement)
|
|
576
769
|
});
|
|
577
770
|
}
|
|
578
|
-
return facts;
|
|
579
|
-
};
|
|
580
|
-
return { read, spanOf, extentOf, docOf, importsIn, moduleRefOf, chainOf, unwrap };
|
|
581
|
-
};
|
|
771
|
+
return { ...facts, ...guardsOf(source, imports, madeInPlace) };
|
|
772
|
+
};
|
|
773
|
+
return { read, spanOf, extentOf, docOf, importsIn, moduleRefOf, chainOf, unwrap };
|
|
774
|
+
};
|
|
775
|
+
|
|
776
|
+
// pkgs/@akanjs/language/guardResolver.ts
|
|
777
|
+
var frameworkOwner = "akanjs";
|
|
778
|
+
var frameworkPattern = /^akanjs(?:\/|$)/;
|
|
779
|
+
var aliasPattern = /^@(apps|libs)\/([^/]+)/;
|
|
780
|
+
var maxMapDepth = 4;
|
|
781
|
+
var identifierPattern = /^[\p{ID_Start}$_][\p{ID_Continue}$\u200c\u200d]*$/u;
|
|
782
|
+
var starExportPattern = /export\s*\*\s*from\s*["']([^"']+)["']/g;
|
|
783
|
+
var exportingPatternOf = (name) => new RegExp(`export\\s*\\{(?:[^}]*?[\\s,])?${escapeRegExp(name)}(?![\\w$])`);
|
|
784
|
+
var declaringPatternOf = (name, isAnyBinding) => {
|
|
785
|
+
const escaped = escapeRegExp(name);
|
|
786
|
+
const braced = "\\{(?:[^}]*?[\\s,])?";
|
|
787
|
+
const leads = [
|
|
788
|
+
"function\\s*\\*?\\s*",
|
|
789
|
+
"(?:const|let|var)\\s+",
|
|
790
|
+
`export\\s*${braced}`,
|
|
791
|
+
...isAnyBinding ? ["class\\s+", `(?:const|let|var)\\s*${braced}`] : []
|
|
792
|
+
];
|
|
793
|
+
return new RegExp(`(?:${leads.join("|")})${escaped}(?![\\w$])|,\\s*${escaped}\\s*(?::[^=;,]+)?=(?![=>])`);
|
|
794
|
+
};
|
|
795
|
+
|
|
796
|
+
class GuardResolver {
|
|
797
|
+
#source;
|
|
798
|
+
#routes;
|
|
799
|
+
#guards = new Map;
|
|
800
|
+
#madeInPlace = new Map;
|
|
801
|
+
#mapEntries = new Map;
|
|
802
|
+
#reexports = new Map;
|
|
803
|
+
#declarations = new Map;
|
|
804
|
+
#starExports = new Map;
|
|
805
|
+
#moduleExports = new Map;
|
|
806
|
+
#ownerFiles = null;
|
|
807
|
+
#modules = null;
|
|
808
|
+
constructor(source, routes) {
|
|
809
|
+
this.#source = source;
|
|
810
|
+
this.#routes = routes;
|
|
811
|
+
}
|
|
812
|
+
find(local, site) {
|
|
813
|
+
return this.#findGuard(local, site, 0);
|
|
814
|
+
}
|
|
815
|
+
madeInPlace(fileName, name) {
|
|
816
|
+
return this.#madeInPlace.get(`${fileName}
|
|
817
|
+
${name}`);
|
|
818
|
+
}
|
|
819
|
+
declare(facts) {
|
|
820
|
+
const factories = new Map;
|
|
821
|
+
const declaredFactories = [];
|
|
822
|
+
for (const file of facts)
|
|
823
|
+
for (const fact of file.guardFactories) {
|
|
824
|
+
const factory = {
|
|
825
|
+
fact,
|
|
826
|
+
owner: file.owner,
|
|
827
|
+
site: file,
|
|
828
|
+
made: [],
|
|
829
|
+
isReal: fact.direct,
|
|
830
|
+
scope: fact.scope,
|
|
831
|
+
layers: fact.layers,
|
|
832
|
+
targets: []
|
|
833
|
+
};
|
|
834
|
+
factories.set(fact.name, [...factories.get(fact.name) ?? [], factory]);
|
|
835
|
+
declaredFactories.push(factory);
|
|
836
|
+
}
|
|
837
|
+
for (const factory of declaredFactories)
|
|
838
|
+
factory.targets = factory.fact.via.flatMap(({ callee, layers }) => {
|
|
839
|
+
const target = this.#factoryAt(factories, callee, factory.site);
|
|
840
|
+
return target ? [{ target, layers }] : [];
|
|
841
|
+
});
|
|
842
|
+
for (let isGrowing = true;isGrowing; ) {
|
|
843
|
+
isGrowing = false;
|
|
844
|
+
for (const factory of declaredFactories) {
|
|
845
|
+
const via = factory.targets.find(({ target, layers }) => target.isReal && target.layers >= layers);
|
|
846
|
+
if (factory.isReal || !via)
|
|
847
|
+
continue;
|
|
848
|
+
factory.isReal = true;
|
|
849
|
+
factory.layers = factory.fact.layers + via.target.layers - via.layers;
|
|
850
|
+
factory.scope ??= via.target.scope;
|
|
851
|
+
isGrowing = true;
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
const partsOf = (factory, seen = new Set) => {
|
|
855
|
+
if (seen.has(factory))
|
|
856
|
+
return [];
|
|
857
|
+
seen.add(factory);
|
|
858
|
+
return [
|
|
859
|
+
{ fileName: factory.site.fileName, extent: factory.fact.extent },
|
|
860
|
+
...factory.targets.filter(({ target }) => target.isReal).flatMap(({ target }) => partsOf(target, seen))
|
|
861
|
+
];
|
|
862
|
+
};
|
|
863
|
+
const declare = (file, made, scope, parts) => {
|
|
864
|
+
const { name, span, extent, doc } = made;
|
|
865
|
+
const label = file.guardLabels[name];
|
|
866
|
+
const own = { fileName: file.fileName, extent };
|
|
867
|
+
const guard = { name, owner: file.owner, span, scope, doc, parts: [own, ...parts], file: null, label };
|
|
868
|
+
if (name.includes("@"))
|
|
869
|
+
this.#madeInPlace.set(`${file.fileName}
|
|
870
|
+
${name}`, guard);
|
|
871
|
+
else
|
|
872
|
+
this.#guards.set(name, [...this.#guards.get(name) ?? [], guard]);
|
|
873
|
+
return guard;
|
|
874
|
+
};
|
|
875
|
+
const extending = [];
|
|
876
|
+
for (const file of facts) {
|
|
877
|
+
for (const made of file.guards) {
|
|
878
|
+
const factory = made.factory ? factories.get(made.factory)?.find((other) => other.site.fileName === file.fileName) : undefined;
|
|
879
|
+
if (!made.factory)
|
|
880
|
+
declare(file, made, made.scope, []);
|
|
881
|
+
else if (factory?.isReal && factory.layers === made.layers)
|
|
882
|
+
factory.made.push(declare(file, made, factory.scope, partsOf(factory)));
|
|
883
|
+
}
|
|
884
|
+
for (const made of file.guardCalls) {
|
|
885
|
+
if (made.kind === "extends") {
|
|
886
|
+
extending.push([file, made]);
|
|
887
|
+
continue;
|
|
888
|
+
}
|
|
889
|
+
const factory = this.#factoryAt(factories, made.callee, file);
|
|
890
|
+
if (factory?.isReal && factory.layers === made.layers)
|
|
891
|
+
factory.made.push(declare(file, made, factory.scope, partsOf(factory)));
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
for (let isGrowing = true;isGrowing; ) {
|
|
895
|
+
isGrowing = false;
|
|
896
|
+
for (const [idx, entry] of extending.entries()) {
|
|
897
|
+
const [file, made] = entry;
|
|
898
|
+
const found = this.#findGuard(made.callee, file, 0, true);
|
|
899
|
+
if (!("guard" in found))
|
|
900
|
+
continue;
|
|
901
|
+
declare(file, made, made.scope ?? found.guard.scope, found.guard.parts);
|
|
902
|
+
extending.splice(idx, 1);
|
|
903
|
+
isGrowing = true;
|
|
904
|
+
break;
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
for (const { fact, made, isReal } of declaredFactories)
|
|
908
|
+
if (isReal && made.every((guard) => !guard.doc))
|
|
909
|
+
for (const guard of made)
|
|
910
|
+
guard.doc = fact.doc;
|
|
911
|
+
for (const list of this.#guards.values())
|
|
912
|
+
for (const guard of list)
|
|
913
|
+
if (list.some((other) => other !== guard && other.owner === guard.owner))
|
|
914
|
+
guard.file = this.inOwner(guard.span.fileName, guard.owner);
|
|
915
|
+
for (const guard of this.#madeInPlace.values())
|
|
916
|
+
guard.file = this.inOwner(guard.span.fileName, guard.owner);
|
|
917
|
+
return [...[...this.#guards.values()].flat(), ...this.#madeInPlace.values()];
|
|
918
|
+
}
|
|
919
|
+
#factoryAt(factories, callee, site, depth = 0) {
|
|
920
|
+
const [head = callee, member] = callee.split(".");
|
|
921
|
+
const imported = site.imports.find((item) => item.name === head);
|
|
922
|
+
if (!imported)
|
|
923
|
+
return member ? undefined : factories.get(callee)?.find((other) => other.site.fileName === site.fileName);
|
|
924
|
+
if (member && imported.imported !== "*")
|
|
925
|
+
return;
|
|
926
|
+
const from = this.ownerOfSpecifier(site.fileName, imported.from);
|
|
927
|
+
if (from === undefined || from === frameworkOwner)
|
|
928
|
+
return;
|
|
929
|
+
const name = member ?? (imported.imported === "default" || imported.imported === "*" ? head : imported.imported);
|
|
930
|
+
const candidates = factories.get(name) ?? [];
|
|
931
|
+
const near = this.pathOfSpecifier(site.fileName, imported.from);
|
|
932
|
+
const own = this.#nearest(candidates.filter((other) => other.owner === from), ({ site }) => site.fileName, near);
|
|
933
|
+
if (own)
|
|
934
|
+
return own;
|
|
935
|
+
const reexport = from && depth < maxMapDepth ? this.reexportOf(from, name, near) : null;
|
|
936
|
+
if (reexport)
|
|
937
|
+
return this.#factoryAt(factories, reexport.local, reexport.site, depth + 1);
|
|
938
|
+
return from && near ? this.#throughStar(candidates, from, name, near) : undefined;
|
|
939
|
+
}
|
|
940
|
+
#findGuard(local, site, depth, isBase = false) {
|
|
941
|
+
const segments = local.split(".");
|
|
942
|
+
const head = segments[0] ?? local;
|
|
943
|
+
const imported = site.imports.find((item) => item.name === head);
|
|
944
|
+
const from = imported ? this.ownerOfSpecifier(site.fileName, imported.from) : undefined;
|
|
945
|
+
if (imported && from === undefined)
|
|
946
|
+
return { external: true };
|
|
947
|
+
const near = imported ? this.pathOfSpecifier(site.fileName, imported.from) : this.#source.relative(site.fileName).replace(/\.tsx?$/, "");
|
|
948
|
+
const named = imported && imported.imported !== "default" && imported.imported !== "*" ? imported.imported : null;
|
|
949
|
+
const path = imported?.imported === "*" ? segments.slice(1) : [named ?? head, ...segments.slice(1)];
|
|
950
|
+
const holder = imported ? from ?? null : site.owner;
|
|
951
|
+
const [objectName, member] = path;
|
|
952
|
+
if (path.length === 2 && objectName && member && holder !== frameworkOwner && depth < maxMapDepth) {
|
|
953
|
+
const made = this.#nearest((this.#guards.get(`${objectName}.${member}`) ?? []).filter((guard) => guard.owner === holder), ({ span }) => span.fileName, near);
|
|
954
|
+
if (made)
|
|
955
|
+
return { guard: made };
|
|
956
|
+
for (const entry of this.#mapEntriesOf(objectName, member, imported ? null : site.fileName, holder)) {
|
|
957
|
+
const found = this.#findGuard(entry.name, entry.site, depth + 1, isBase);
|
|
958
|
+
if ("guard" in found)
|
|
959
|
+
return found;
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
const name = path[path.length - 1] ?? head;
|
|
963
|
+
if (from === frameworkOwner)
|
|
964
|
+
return { name, from };
|
|
965
|
+
const declared = this.#guards.get(name) ?? [];
|
|
966
|
+
const own = this.#nearest(declared.filter((other) => other.owner === (imported ? from : site.owner)), ({ span }) => span.fileName, near);
|
|
967
|
+
if (own)
|
|
968
|
+
return { guard: own };
|
|
969
|
+
if (!imported) {
|
|
970
|
+
const visible = isBase ? undefined : declared.find((other) => this.visible(other.owner, site.owner));
|
|
971
|
+
return visible ? { guard: visible } : { name, from };
|
|
972
|
+
}
|
|
973
|
+
const reexport = from && depth < maxMapDepth ? this.reexportOf(from, name, near) : null;
|
|
974
|
+
if (reexport)
|
|
975
|
+
return this.#findGuard(reexport.local, reexport.site, depth + 1, isBase);
|
|
976
|
+
const reached = from && near ? this.#throughStar(declared, from, name, near) : undefined;
|
|
977
|
+
return reached ? { guard: reached } : { name, from };
|
|
978
|
+
}
|
|
979
|
+
reexportOf(owner, name, near) {
|
|
980
|
+
const key = `${owner}
|
|
981
|
+
${name}
|
|
982
|
+
${near ?? ""}`;
|
|
983
|
+
const cached = this.#reexports.get(key);
|
|
984
|
+
if (cached !== undefined)
|
|
985
|
+
return cached;
|
|
986
|
+
let found = null;
|
|
987
|
+
const exporting = exportingPatternOf(name);
|
|
988
|
+
for (const fileName of this.#filesOf(owner, name, near)) {
|
|
989
|
+
const text = this.#source.readText(fileName);
|
|
990
|
+
if (text === undefined || !exporting.test(text))
|
|
991
|
+
continue;
|
|
992
|
+
const reexport = this.#reexportIn(this.#source.parse(fileName, text), name);
|
|
993
|
+
if (!reexport)
|
|
994
|
+
continue;
|
|
995
|
+
const site = { fileName, owner, imports: [] };
|
|
996
|
+
found = reexport.from ? {
|
|
997
|
+
local: reexport.name,
|
|
998
|
+
site: { ...site, imports: [{ ...reexport, imported: reexport.name, from: reexport.from }] }
|
|
999
|
+
} : { local: reexport.name, site };
|
|
1000
|
+
break;
|
|
1001
|
+
}
|
|
1002
|
+
this.#reexports.set(key, found);
|
|
1003
|
+
return found;
|
|
1004
|
+
}
|
|
1005
|
+
#reexportIn(source, name) {
|
|
1006
|
+
const { ts, reader } = this.#source;
|
|
1007
|
+
for (const statement of source.statements) {
|
|
1008
|
+
if (!ts.isExportDeclaration(statement) || statement.isTypeOnly)
|
|
1009
|
+
continue;
|
|
1010
|
+
const { exportClause, moduleSpecifier } = statement;
|
|
1011
|
+
if (!exportClause || !ts.isNamedExports(exportClause))
|
|
1012
|
+
continue;
|
|
1013
|
+
const element = exportClause.elements.find((item) => !item.isTypeOnly && item.name.text === name);
|
|
1014
|
+
if (!element)
|
|
1015
|
+
continue;
|
|
1016
|
+
const original = (element.propertyName ?? element.name).text;
|
|
1017
|
+
if (moduleSpecifier && ts.isStringLiteral(moduleSpecifier))
|
|
1018
|
+
return { name: original, from: moduleSpecifier.text };
|
|
1019
|
+
const imported = reader.importsIn(source).find((item) => item.name === original);
|
|
1020
|
+
if (!imported)
|
|
1021
|
+
return { name: original, from: null };
|
|
1022
|
+
return imported.imported === "default" || imported.imported === "*" ? null : { name: imported.imported, from: imported.from };
|
|
1023
|
+
}
|
|
1024
|
+
return null;
|
|
1025
|
+
}
|
|
1026
|
+
#filesOf(owner, name, near) {
|
|
1027
|
+
const isNear = (fileName) => {
|
|
1028
|
+
const relative = this.#source.relative(fileName);
|
|
1029
|
+
return !!near && (relative.startsWith(`${near}/`) || relative.replace(/\.tsx?$/, "") === near);
|
|
1030
|
+
};
|
|
1031
|
+
if (!identifierPattern.test(name))
|
|
1032
|
+
return [];
|
|
1033
|
+
const candidates = this.#filesIn(owner).filter(([, words]) => words.includes(`
|
|
1034
|
+
${name}
|
|
1035
|
+
`)).map(([fileName]) => fileName);
|
|
1036
|
+
return [...candidates.filter(isNear), ...candidates.filter((other) => !isNear(other))];
|
|
1037
|
+
}
|
|
1038
|
+
#mapEntriesOf(objectName, member, fileName, owner) {
|
|
1039
|
+
const key = `${objectName}
|
|
1040
|
+
${member}
|
|
1041
|
+
${fileName ?? ""}
|
|
1042
|
+
${owner ?? ""}`;
|
|
1043
|
+
const cached = this.#mapEntries.get(key);
|
|
1044
|
+
if (cached)
|
|
1045
|
+
return cached;
|
|
1046
|
+
const { ts, reader } = this.#source;
|
|
1047
|
+
const entries = [];
|
|
1048
|
+
const fileNames = [
|
|
1049
|
+
...fileName ? [fileName] : [],
|
|
1050
|
+
...(owner === null ? [] : this.#filesIn(owner)).filter(([other, words]) => other !== fileName && words.includes(`
|
|
1051
|
+
${objectName}
|
|
1052
|
+
`) && words.includes(`
|
|
1053
|
+
${member}
|
|
1054
|
+
`)).map(([other]) => other)
|
|
1055
|
+
];
|
|
1056
|
+
for (const holder of fileNames) {
|
|
1057
|
+
const text = this.#source.readText(holder);
|
|
1058
|
+
if (text === undefined)
|
|
1059
|
+
continue;
|
|
1060
|
+
const source = this.#source.parse(holder, text);
|
|
1061
|
+
const site = { fileName: holder, owner: this.#source.ownerOf(holder), imports: reader.importsIn(source) };
|
|
1062
|
+
for (const statement of source.statements) {
|
|
1063
|
+
if (!ts.isVariableStatement(statement))
|
|
1064
|
+
continue;
|
|
1065
|
+
const map = statement.declarationList.declarations.find((declaration) => ts.isIdentifier(declaration.name) && declaration.name.text === objectName)?.initializer;
|
|
1066
|
+
const object = map && reader.unwrap(map);
|
|
1067
|
+
if (!object || !ts.isObjectLiteralExpression(object))
|
|
1068
|
+
continue;
|
|
1069
|
+
for (const property of object.properties) {
|
|
1070
|
+
if (ts.isShorthandPropertyAssignment(property) && property.name.text === member)
|
|
1071
|
+
entries.push({ name: member, site });
|
|
1072
|
+
if (!ts.isPropertyAssignment(property) || !(ts.isIdentifier(property.name) || ts.isStringLiteral(property.name)) || property.name.text !== member)
|
|
1073
|
+
continue;
|
|
1074
|
+
const value = reader.unwrap(property.initializer);
|
|
1075
|
+
if (ts.isIdentifier(value))
|
|
1076
|
+
entries.push({ name: value.text, site });
|
|
1077
|
+
else if (ts.isPropertyAccessExpression(value) && ts.isIdentifier(value.expression))
|
|
1078
|
+
entries.push({ name: `${value.expression.text}.${value.name.text}`, site });
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
this.#mapEntries.set(key, entries);
|
|
1083
|
+
return entries;
|
|
1084
|
+
}
|
|
1085
|
+
declarationOf(owner, name, near, depth = 0) {
|
|
1086
|
+
const key = `${owner}
|
|
1087
|
+
${name}
|
|
1088
|
+
${near ?? ""}`;
|
|
1089
|
+
const cached = this.#declarations.get(key);
|
|
1090
|
+
if (cached !== undefined)
|
|
1091
|
+
return cached;
|
|
1092
|
+
this.#declarations.set(key, null);
|
|
1093
|
+
const declaring = declaringPatternOf(name, false);
|
|
1094
|
+
let found = null;
|
|
1095
|
+
for (const fileName of this.#filesOf(owner, name, near)) {
|
|
1096
|
+
const text = this.#source.readText(fileName);
|
|
1097
|
+
if (text === undefined || !declaring.test(text))
|
|
1098
|
+
continue;
|
|
1099
|
+
const source = this.#source.parse(fileName, text);
|
|
1100
|
+
const declared = this.#routes.declarationIn(source, name);
|
|
1101
|
+
if (declared) {
|
|
1102
|
+
found = { source, ...declared, name };
|
|
1103
|
+
break;
|
|
1104
|
+
}
|
|
1105
|
+
const reexport = this.#reexportIn(source, name);
|
|
1106
|
+
if (!reexport)
|
|
1107
|
+
continue;
|
|
1108
|
+
if (!reexport.from) {
|
|
1109
|
+
const local = this.#routes.declarationIn(source, reexport.name);
|
|
1110
|
+
found = local ? { source, ...local, name: reexport.name } : null;
|
|
1111
|
+
break;
|
|
1112
|
+
}
|
|
1113
|
+
const next = this.ownerOfSpecifier(fileName, reexport.from);
|
|
1114
|
+
if (next && next !== frameworkOwner && depth < maxMapDepth)
|
|
1115
|
+
found = this.declarationOf(next, reexport.name, this.pathOfSpecifier(fileName, reexport.from), depth + 1);
|
|
1116
|
+
break;
|
|
1117
|
+
}
|
|
1118
|
+
this.#declarations.set(key, found);
|
|
1119
|
+
return found;
|
|
1120
|
+
}
|
|
1121
|
+
#throughStar(declared, owner, name, near) {
|
|
1122
|
+
if (!declared.length)
|
|
1123
|
+
return;
|
|
1124
|
+
const { owners, isOpaque } = this.starExportsOf(owner, near);
|
|
1125
|
+
const reached = declared.filter((other) => other.owner !== null && owners.includes(other.owner));
|
|
1126
|
+
return reached.length === 1 && !isOpaque && !this.declares(owner, name) ? reached[0] : undefined;
|
|
1127
|
+
}
|
|
1128
|
+
starExportsOf(owner, near) {
|
|
1129
|
+
const key = `${owner}
|
|
1130
|
+
${near}`;
|
|
1131
|
+
const cached = this.#starExports.get(key);
|
|
1132
|
+
if (cached)
|
|
1133
|
+
return cached;
|
|
1134
|
+
const found = { owners: [], packages: [], paths: [near], isOpaque: false };
|
|
1135
|
+
this.#starExports.set(key, found);
|
|
1136
|
+
for (let idx = 0;idx < found.paths.length; idx++)
|
|
1137
|
+
for (const fileName of this.#modulesAt(found.paths[idx] ?? ""))
|
|
1138
|
+
for (const [, specifier = ""] of this.#source.readText(fileName)?.matchAll(starExportPattern) ?? []) {
|
|
1139
|
+
const target = this.ownerOfSpecifier(fileName, specifier);
|
|
1140
|
+
const path = this.pathOfSpecifier(fileName, specifier);
|
|
1141
|
+
if (path === null && !found.packages.includes(specifier))
|
|
1142
|
+
found.packages.push(specifier);
|
|
1143
|
+
if (target === undefined || target === frameworkOwner || path === null)
|
|
1144
|
+
found.isOpaque = true;
|
|
1145
|
+
else if (!found.paths.includes(path))
|
|
1146
|
+
found.paths.push(path);
|
|
1147
|
+
if (target && target !== frameworkOwner && target !== owner && !found.owners.includes(target))
|
|
1148
|
+
found.owners.push(target);
|
|
1149
|
+
}
|
|
1150
|
+
return found;
|
|
1151
|
+
}
|
|
1152
|
+
exportsAt(path, name) {
|
|
1153
|
+
const key = `${path}
|
|
1154
|
+
${name}`;
|
|
1155
|
+
const cached = this.#moduleExports.get(key);
|
|
1156
|
+
if (cached)
|
|
1157
|
+
return cached;
|
|
1158
|
+
const found = { named: null, stars: [] };
|
|
1159
|
+
const exporting = exportingPatternOf(name);
|
|
1160
|
+
for (const fileName of this.#modulesAt(path)) {
|
|
1161
|
+
const text = this.#source.readText(fileName);
|
|
1162
|
+
if (text === undefined)
|
|
1163
|
+
continue;
|
|
1164
|
+
const named = identifierPattern.test(name) && exporting.test(text) ? this.#reexportIn(this.#source.parse(fileName, text), name) : null;
|
|
1165
|
+
if (named) {
|
|
1166
|
+
found.named = { ...named, fileName };
|
|
1167
|
+
found.stars = [];
|
|
1168
|
+
break;
|
|
1169
|
+
}
|
|
1170
|
+
for (const [, specifier = ""] of text.matchAll(starExportPattern)) {
|
|
1171
|
+
const target = this.pathOfSpecifier(fileName, specifier);
|
|
1172
|
+
if (target !== null && !found.stars.includes(target))
|
|
1173
|
+
found.stars.push(target);
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
this.#moduleExports.set(key, found);
|
|
1177
|
+
return found;
|
|
1178
|
+
}
|
|
1179
|
+
#modulesAt(path) {
|
|
1180
|
+
if (!this.#modules) {
|
|
1181
|
+
this.#modules = new Map;
|
|
1182
|
+
for (const [fileName] of this.#source.files) {
|
|
1183
|
+
const module = this.#source.relative(fileName).replace(/\.tsx?$/, "");
|
|
1184
|
+
for (const key of module.endsWith("/index") ? [module, module.slice(0, -6)] : [module])
|
|
1185
|
+
this.#modules.set(key, [...this.#modules.get(key) ?? [], fileName]);
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
return this.#modules.get(path) ?? [];
|
|
1189
|
+
}
|
|
1190
|
+
hasModule(path) {
|
|
1191
|
+
return this.#modulesAt(path).length > 0;
|
|
1192
|
+
}
|
|
1193
|
+
packagesUnder(owner, folder) {
|
|
1194
|
+
const packages = new Set;
|
|
1195
|
+
for (const [fileName, words] of this.#filesIn(owner)) {
|
|
1196
|
+
if (!this.#source.relative(fileName).startsWith(`${folder}/`) || !words.includes(`
|
|
1197
|
+
export
|
|
1198
|
+
`))
|
|
1199
|
+
continue;
|
|
1200
|
+
for (const [, specifier = ""] of this.#source.readText(fileName)?.matchAll(starExportPattern) ?? [])
|
|
1201
|
+
if (this.pathOfSpecifier(fileName, specifier) === null)
|
|
1202
|
+
packages.add(specifier);
|
|
1203
|
+
}
|
|
1204
|
+
return [...packages];
|
|
1205
|
+
}
|
|
1206
|
+
declares(owner, name) {
|
|
1207
|
+
const declaring = declaringPatternOf(name, true);
|
|
1208
|
+
return this.#filesOf(owner, name, null).some((fileName) => declaring.test(this.#source.readText(fileName) ?? ""));
|
|
1209
|
+
}
|
|
1210
|
+
declaresAt(path, name) {
|
|
1211
|
+
const declaring = declaringPatternOf(name, true);
|
|
1212
|
+
return this.#modulesAt(path).some((fileName) => declaring.test(this.#source.readText(fileName) ?? ""));
|
|
1213
|
+
}
|
|
1214
|
+
#filesIn(owner) {
|
|
1215
|
+
if (!this.#ownerFiles) {
|
|
1216
|
+
this.#ownerFiles = new Map;
|
|
1217
|
+
for (const entry of this.#source.files) {
|
|
1218
|
+
const fileOwner = testFilePattern.test(entry[0]) ? null : this.#source.ownerOf(entry[0]);
|
|
1219
|
+
const files = fileOwner === null ? null : this.#ownerFiles.get(fileOwner);
|
|
1220
|
+
if (files)
|
|
1221
|
+
files.push(entry);
|
|
1222
|
+
else if (fileOwner !== null)
|
|
1223
|
+
this.#ownerFiles.set(fileOwner, [entry]);
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
return this.#ownerFiles.get(owner) ?? [];
|
|
1227
|
+
}
|
|
1228
|
+
#nearest(items, fileOf, near) {
|
|
1229
|
+
const isNear = (fileName) => {
|
|
1230
|
+
const relative = this.#source.relative(fileName);
|
|
1231
|
+
return !!near && (relative.startsWith(`${near}/`) || relative.replace(/\.tsx?$/, "") === near);
|
|
1232
|
+
};
|
|
1233
|
+
return items.find((item) => isNear(fileOf(item))) ?? items[0];
|
|
1234
|
+
}
|
|
1235
|
+
inOwner(fileName, owner) {
|
|
1236
|
+
const relative = this.#source.relative(fileName);
|
|
1237
|
+
return owner && relative.startsWith(`${owner}/`) ? relative.slice(owner.length + 1) : relative;
|
|
1238
|
+
}
|
|
1239
|
+
ownerOfSpecifier(fileName, specifier) {
|
|
1240
|
+
if (frameworkPattern.test(specifier))
|
|
1241
|
+
return frameworkOwner;
|
|
1242
|
+
const path = this.pathOfSpecifier(fileName, specifier);
|
|
1243
|
+
return path === null ? undefined : ownerOf(path) ?? undefined;
|
|
1244
|
+
}
|
|
1245
|
+
pathOfSpecifier(fileName, specifier) {
|
|
1246
|
+
const alias = specifier.match(aliasPattern);
|
|
1247
|
+
if (alias)
|
|
1248
|
+
return `${alias[1]}/${alias[2]}${specifier.slice(alias[0].length)}`;
|
|
1249
|
+
if (!specifier.startsWith("."))
|
|
1250
|
+
return null;
|
|
1251
|
+
const segments = this.#source.relative(fileName).split("/").slice(0, -1);
|
|
1252
|
+
for (const segment of specifier.split("/"))
|
|
1253
|
+
if (segment === "..")
|
|
1254
|
+
segments.pop();
|
|
1255
|
+
else if (segment !== ".")
|
|
1256
|
+
segments.push(segment);
|
|
1257
|
+
return segments.join("/");
|
|
1258
|
+
}
|
|
1259
|
+
visible(target, from) {
|
|
1260
|
+
return !from || !target || target === from || target.startsWith("libs/");
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1264
|
+
// pkgs/@akanjs/language/adaptorResolver.ts
|
|
1265
|
+
var maxModules = 64;
|
|
1266
|
+
|
|
1267
|
+
class AdaptorResolver {
|
|
1268
|
+
#source;
|
|
1269
|
+
#resolver;
|
|
1270
|
+
#sink;
|
|
1271
|
+
#adaptors = new Map;
|
|
1272
|
+
#plugs = new Map;
|
|
1273
|
+
#serviceFields = new Set;
|
|
1274
|
+
constructor(source, resolver, sink) {
|
|
1275
|
+
this.#source = source;
|
|
1276
|
+
this.#resolver = resolver;
|
|
1277
|
+
this.#sink = sink;
|
|
1278
|
+
}
|
|
1279
|
+
declare(facts) {
|
|
1280
|
+
const counts = new Map;
|
|
1281
|
+
for (const file of facts)
|
|
1282
|
+
for (const fact of file.adaptors) {
|
|
1283
|
+
const key = this.#sink.key("adaptor", file.owner, fact.name);
|
|
1284
|
+
counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
1285
|
+
}
|
|
1286
|
+
const declared = facts.flatMap((file) => file.adaptors.map((fact) => {
|
|
1287
|
+
const key = this.#sink.key("adaptor", file.owner, fact.name);
|
|
1288
|
+
const id = (counts.get(key) ?? 0) > 1 ? this.#sink.key("adaptor", file.owner, `${this.#resolver.inOwner(file.fileName, file.owner)}#${fact.name}`) : key;
|
|
1289
|
+
const adaptor = { fact, owner: file.owner, fileName: file.fileName, id };
|
|
1290
|
+
this.#adaptors.set(fact.name, [...this.#adaptors.get(fact.name) ?? [], adaptor]);
|
|
1291
|
+
this.#nodeOf(adaptor);
|
|
1292
|
+
return { adaptor, file };
|
|
1293
|
+
}));
|
|
1294
|
+
for (const { adaptor, file } of declared)
|
|
1295
|
+
for (const plug of adaptor.fact.plugs)
|
|
1296
|
+
this.#sink.addEdge(adaptor.id, this.#idOf(plug.target, file), "plugs", plug.name, this.#sink.location(plug.span));
|
|
1297
|
+
for (const file of facts)
|
|
1298
|
+
for (const binding of file.adaptorBindings)
|
|
1299
|
+
this.#sink.addEdge(this.#idOf(binding.role, file), this.#idOf(binding.adaptor, file), "binds", file.owner, this.#sink.location(binding.span));
|
|
1300
|
+
}
|
|
1301
|
+
addService(refName, service, file) {
|
|
1302
|
+
for (const plug of service.plugs) {
|
|
1303
|
+
const key = `${refName}
|
|
1304
|
+
${plug.name}`;
|
|
1305
|
+
this.#plugs.set(key, [
|
|
1306
|
+
...this.#plugs.get(key) ?? [],
|
|
1307
|
+
{ owner: file.owner, target: this.#idOf(plug.target, file) }
|
|
1308
|
+
]);
|
|
1309
|
+
}
|
|
1310
|
+
for (const field of service.fields)
|
|
1311
|
+
this.#serviceFields.add(`${file.owner ?? ""}
|
|
1312
|
+
${refName}
|
|
1313
|
+
${field}`);
|
|
1314
|
+
}
|
|
1315
|
+
pluggedOf(owner, module, field) {
|
|
1316
|
+
const plugs = this.#plugs.get(`${module}
|
|
1317
|
+
${field}`) ?? [];
|
|
1318
|
+
const own = plugs.filter((plug) => plug.owner === owner);
|
|
1319
|
+
if (!own.length && this.#serviceFields.has(`${owner ?? ""}
|
|
1320
|
+
${module}
|
|
1321
|
+
${field}`))
|
|
1322
|
+
return [];
|
|
1323
|
+
const inherited = own.length ? own : plugs.filter((plug) => this.#resolver.visible(plug.owner, owner));
|
|
1324
|
+
return [...new Set(inherited.map(({ target }) => target))];
|
|
1325
|
+
}
|
|
1326
|
+
#idOf(local, file) {
|
|
1327
|
+
const [head, member] = local.split(".");
|
|
1328
|
+
const imported = file.imports.find((item) => item.name === head);
|
|
1329
|
+
const name = member ?? (imported && imported.imported !== "default" && imported.imported !== "*" ? imported.imported : head);
|
|
1330
|
+
const path = imported ? this.#resolver.pathOfSpecifier(file.fileName, imported.from) : null;
|
|
1331
|
+
if (imported && path === null && !frameworkPattern.test(imported.from))
|
|
1332
|
+
return this.#packageAdaptorOf(name, imported.from);
|
|
1333
|
+
const from = imported ? this.#resolver.ownerOfSpecifier(file.fileName, imported.from) : undefined;
|
|
1334
|
+
const declared = this.#adaptors.get(name) ?? [];
|
|
1335
|
+
const holder = from === undefined ? file.owner : from;
|
|
1336
|
+
const own = this.#adaptorIn(name, declared, holder, path, file.fileName);
|
|
1337
|
+
if (own && "from" in own)
|
|
1338
|
+
return this.#packageAdaptorOf(own.name, own.from);
|
|
1339
|
+
const found = own === undefined && from !== frameworkOwner ? declared.find((adaptor) => this.#resolver.visible(adaptor.owner, holder)) : own;
|
|
1340
|
+
if (found)
|
|
1341
|
+
return found.id;
|
|
1342
|
+
const reexported = own === undefined && from && from !== frameworkOwner ? this.#packageShimOf(from, name, path) : null;
|
|
1343
|
+
if (reexported)
|
|
1344
|
+
return this.#packageAdaptorOf(reexported.name, reexported.from);
|
|
1345
|
+
const owner = from === frameworkOwner ? null : imported ? from ?? null : file.owner;
|
|
1346
|
+
const detail = from === frameworkOwner ? [frameworkOwner] : [];
|
|
1347
|
+
return this.#sink.addNode(this.#sink.key("adaptor", owner, name), "adaptor", owner, name, null, detail).id;
|
|
1348
|
+
}
|
|
1349
|
+
#adaptorIn(name, declared, owner, path, fileName) {
|
|
1350
|
+
const own = declared.filter((adaptor) => adaptor.owner === owner);
|
|
1351
|
+
const [first] = own;
|
|
1352
|
+
if (path === null)
|
|
1353
|
+
return own.find((adaptor) => adaptor.fileName === fileName) ?? first;
|
|
1354
|
+
if (own.length === 1)
|
|
1355
|
+
return first;
|
|
1356
|
+
if (!this.#resolver.hasModule(path))
|
|
1357
|
+
return own.find((adaptor) => this.#source.relative(adaptor.fileName).startsWith(`${path}/`)) ?? first;
|
|
1358
|
+
const exported = this.#exportedAt(name, path, new Set).filter((item) => first || ("fact" in item));
|
|
1359
|
+
const [only] = exported;
|
|
1360
|
+
if (only)
|
|
1361
|
+
return exported.length === 1 ? only : null;
|
|
1362
|
+
return first ? null : undefined;
|
|
1363
|
+
}
|
|
1364
|
+
#exportedAt(name, path, seen) {
|
|
1365
|
+
const key = `${path}
|
|
1366
|
+
${name}`;
|
|
1367
|
+
if (seen.has(key) || seen.size >= maxModules)
|
|
1368
|
+
return [];
|
|
1369
|
+
seen.add(key);
|
|
1370
|
+
const here = (this.#adaptors.get(name) ?? []).filter((adaptor) => {
|
|
1371
|
+
const module = this.#source.relative(adaptor.fileName).replace(/\.tsx?$/, "");
|
|
1372
|
+
return module === path || module === `${path}/index`;
|
|
1373
|
+
});
|
|
1374
|
+
if (here.length)
|
|
1375
|
+
return here;
|
|
1376
|
+
const { named, stars } = this.#resolver.exportsAt(path, name);
|
|
1377
|
+
if (named?.from && this.#resolver.pathOfSpecifier(named.fileName, named.from) === null)
|
|
1378
|
+
return [{ name: named.name === "default" ? name : named.name, from: named.from }];
|
|
1379
|
+
if (named) {
|
|
1380
|
+
const target = named.from === null ? path : this.#resolver.pathOfSpecifier(named.fileName, named.from);
|
|
1381
|
+
return target === null ? [] : this.#exportedAt(named.name, target, seen);
|
|
1382
|
+
}
|
|
1383
|
+
return [...new Set(stars.flatMap((star) => this.#exportedAt(name, star, seen)))];
|
|
1384
|
+
}
|
|
1385
|
+
#packageAdaptorOf(name, specifier) {
|
|
1386
|
+
if (frameworkPattern.test(specifier))
|
|
1387
|
+
return this.#sink.addNode(this.#sink.key("adaptor", null, name), "adaptor", null, name, null, [frameworkOwner]).id;
|
|
1388
|
+
const pkg = specifier.split("/").slice(0, specifier.startsWith("@") ? 2 : 1).join("/");
|
|
1389
|
+
return this.#sink.addNode(this.#sink.key("adaptor", pkg, name), "adaptor", null, name, null, [pkg]).id;
|
|
1390
|
+
}
|
|
1391
|
+
#packageShimOf(owner, name, near) {
|
|
1392
|
+
const reexport = this.#resolver.reexportOf(owner, name, near);
|
|
1393
|
+
const [shim] = reexport?.site.imports ?? [];
|
|
1394
|
+
if (reexport && shim && this.#resolver.pathOfSpecifier(reexport.site.fileName, shim.from) === null)
|
|
1395
|
+
return { name: reexport.local === "default" ? name : reexport.local, from: shim.from };
|
|
1396
|
+
if (reexport || !near)
|
|
1397
|
+
return null;
|
|
1398
|
+
const { packages, paths } = this.#resolver.hasModule(near) ? this.#resolver.starExportsOf(owner, near) : { packages: this.#resolver.packagesUnder(owner, near), paths: [] };
|
|
1399
|
+
const [only] = packages;
|
|
1400
|
+
const isDeclared = this.#resolver.declares(owner, name) || paths.some((path) => this.#resolver.declaresAt(path, name));
|
|
1401
|
+
return packages.length === 1 && only && !isDeclared ? { name, from: only } : null;
|
|
1402
|
+
}
|
|
1403
|
+
#nodeOf({ fact, owner, fileName, id }) {
|
|
1404
|
+
const detail = fact.refName ? [fact.refName] : [];
|
|
1405
|
+
const node = this.#sink.addNode(id, "adaptor", owner, fact.name, fact.span, detail);
|
|
1406
|
+
if (fact.doc && !node.doc)
|
|
1407
|
+
node.doc = fact.doc;
|
|
1408
|
+
this.#sink.digest(node, this.#source.readText(fileName), fact.extent);
|
|
1409
|
+
return node.id;
|
|
1410
|
+
}
|
|
1411
|
+
}
|
|
582
1412
|
|
|
583
1413
|
// pkgs/@akanjs/language/routeFacts.ts
|
|
584
1414
|
import { normalizeRoutePattern, tryParseRouteModuleKey } from "akanjs/common/routeConvention.ts";
|
|
@@ -634,24 +1464,23 @@ rootLayout
|
|
|
634
1464
|
}
|
|
635
1465
|
declarationIn(source, name) {
|
|
636
1466
|
const ts = this.#ts;
|
|
1467
|
+
const overloads = source.statements.filter((statement) => ts.isFunctionDeclaration(statement) && statement.name?.text === name);
|
|
1468
|
+
const [first] = overloads;
|
|
1469
|
+
const last = overloads.at(-1);
|
|
1470
|
+
if (first?.name && last)
|
|
1471
|
+
return { statement: first, declared: first.name, extent: { start: first.getStart(source), end: last.getEnd() } };
|
|
637
1472
|
for (const statement of source.statements) {
|
|
638
|
-
if (ts.isFunctionDeclaration(statement) && statement.name?.text === name)
|
|
639
|
-
return { statement, declared: statement.name, initializer: null };
|
|
640
1473
|
if (!ts.isVariableStatement(statement))
|
|
641
1474
|
continue;
|
|
642
1475
|
const declaration = statement.declarationList.declarations.find((item) => ts.isIdentifier(item.name) && item.name.text === name);
|
|
643
1476
|
if (declaration)
|
|
644
|
-
return { statement, declared: declaration.name,
|
|
1477
|
+
return { statement, declared: declaration.name, extent: this.#reader.extentOf(source, statement) };
|
|
645
1478
|
}
|
|
646
1479
|
return null;
|
|
647
1480
|
}
|
|
648
1481
|
#guardOf(source, exported) {
|
|
649
1482
|
const ts = this.#ts;
|
|
650
1483
|
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
1484
|
while (current && ts.isCallExpression(current) && ts.isPropertyAccessExpression(current.expression)) {
|
|
656
1485
|
const { name, expression } = current.expression;
|
|
657
1486
|
const [stage] = current.arguments;
|
|
@@ -663,9 +1492,20 @@ rootLayout
|
|
|
663
1492
|
}
|
|
664
1493
|
#callsIn(source, stage) {
|
|
665
1494
|
const ts = this.#ts;
|
|
666
|
-
const
|
|
1495
|
+
const imports = this.#reader.importsIn(source).filter(({ from }) => RouteFactReader.#workspacePattern.test(from));
|
|
1496
|
+
const imported = new Set(imports.filter(({ imported }) => imported !== "*").map(({ name }) => name));
|
|
1497
|
+
const namespaces = new Set(imports.filter(({ imported }) => imported === "*").map(({ name }) => name));
|
|
667
1498
|
const calls = new Map;
|
|
668
|
-
const callOf = (
|
|
1499
|
+
const callOf = (callee, shadowed) => {
|
|
1500
|
+
if (ts.isPropertyAccessExpression(callee) && ts.isIdentifier(callee.expression) && namespaces.has(callee.expression.text) && !shadowed.has(callee.expression.text)) {
|
|
1501
|
+
const name = `${callee.expression.text}.${callee.name.text}`;
|
|
1502
|
+
if (!calls.has(name))
|
|
1503
|
+
calls.set(name, { name, local: null });
|
|
1504
|
+
return true;
|
|
1505
|
+
}
|
|
1506
|
+
if (!ts.isIdentifier(callee) || shadowed.has(callee.text))
|
|
1507
|
+
return false;
|
|
1508
|
+
const name = callee.text;
|
|
669
1509
|
if (calls.has(name))
|
|
670
1510
|
return true;
|
|
671
1511
|
const declared = this.declarationIn(source, name);
|
|
@@ -675,23 +1515,71 @@ rootLayout
|
|
|
675
1515
|
name,
|
|
676
1516
|
local: declared && {
|
|
677
1517
|
span: this.#reader.spanOf(source, declared.declared),
|
|
678
|
-
extent:
|
|
1518
|
+
extent: declared.extent,
|
|
679
1519
|
doc: this.#reader.docOf(source, declared.statement)
|
|
680
1520
|
}
|
|
681
1521
|
});
|
|
682
1522
|
return true;
|
|
683
1523
|
};
|
|
684
|
-
const visit = (node) => {
|
|
685
|
-
|
|
1524
|
+
const visit = (node, outer) => {
|
|
1525
|
+
const shadowed = this.#scopeOf(node, outer);
|
|
1526
|
+
if (ts.isCallExpression(node) && callOf(node.expression, shadowed))
|
|
686
1527
|
return;
|
|
687
|
-
ts.forEachChild(node, visit);
|
|
1528
|
+
ts.forEachChild(node, (child) => visit(child, shadowed));
|
|
688
1529
|
};
|
|
689
1530
|
if (ts.isIdentifier(stage))
|
|
690
|
-
callOf(stage
|
|
1531
|
+
callOf(stage, new Set);
|
|
691
1532
|
else
|
|
692
|
-
visit(stage);
|
|
1533
|
+
visit(stage, new Set);
|
|
693
1534
|
return [...calls.values()];
|
|
694
1535
|
}
|
|
1536
|
+
#scopeOf(node, outer) {
|
|
1537
|
+
const ts = this.#ts;
|
|
1538
|
+
const names = [];
|
|
1539
|
+
const bind = (name) => {
|
|
1540
|
+
if (ts.isIdentifier(name))
|
|
1541
|
+
names.push(name.text);
|
|
1542
|
+
else
|
|
1543
|
+
for (const element of name.elements)
|
|
1544
|
+
if (!ts.isOmittedExpression(element))
|
|
1545
|
+
bind(element.name);
|
|
1546
|
+
};
|
|
1547
|
+
const declare = (statement) => {
|
|
1548
|
+
if (ts.isVariableStatement(statement))
|
|
1549
|
+
for (const declaration of statement.declarationList.declarations)
|
|
1550
|
+
bind(declaration.name);
|
|
1551
|
+
else if ((ts.isFunctionDeclaration(statement) || ts.isClassDeclaration(statement)) && statement.name)
|
|
1552
|
+
names.push(statement.name.text);
|
|
1553
|
+
};
|
|
1554
|
+
const hoist = (child) => {
|
|
1555
|
+
if (ts.isFunctionLike(child) || ts.isClassLike(child))
|
|
1556
|
+
return;
|
|
1557
|
+
if (ts.isVariableDeclarationList(child) && !(child.flags & ts.NodeFlags.BlockScoped))
|
|
1558
|
+
for (const declaration of child.declarations)
|
|
1559
|
+
bind(declaration.name);
|
|
1560
|
+
ts.forEachChild(child, hoist);
|
|
1561
|
+
};
|
|
1562
|
+
if (ts.isFunctionLike(node)) {
|
|
1563
|
+
for (const parameter of node.parameters)
|
|
1564
|
+
bind(parameter.name);
|
|
1565
|
+
if (ts.isFunctionExpression(node) && node.name)
|
|
1566
|
+
names.push(node.name.text);
|
|
1567
|
+
if ("body" in node && node.body)
|
|
1568
|
+
ts.forEachChild(node.body, hoist);
|
|
1569
|
+
} else if (ts.isClassExpression(node) && node.name)
|
|
1570
|
+
names.push(node.name.text);
|
|
1571
|
+
else if (ts.isBlock(node))
|
|
1572
|
+
node.statements.forEach(declare);
|
|
1573
|
+
else if (ts.isCaseBlock(node))
|
|
1574
|
+
for (const clause of node.clauses)
|
|
1575
|
+
clause.statements.forEach(declare);
|
|
1576
|
+
else if (ts.isCatchClause(node) && node.variableDeclaration)
|
|
1577
|
+
bind(node.variableDeclaration.name);
|
|
1578
|
+
else if ((ts.isForStatement(node) || ts.isForInStatement(node) || ts.isForOfStatement(node)) && node.initializer && ts.isVariableDeclarationList(node.initializer))
|
|
1579
|
+
for (const declaration of node.initializer.declarations)
|
|
1580
|
+
bind(declaration.name);
|
|
1581
|
+
return names.length ? new Set([...outer, ...names]) : outer;
|
|
1582
|
+
}
|
|
695
1583
|
}
|
|
696
1584
|
|
|
697
1585
|
// pkgs/@akanjs/language/akanGraph.ts
|
|
@@ -707,10 +1595,6 @@ var builtinGuards = {
|
|
|
707
1595
|
Public: { scope: "account", doc: "Passes every caller, signed in or not." },
|
|
708
1596
|
None: { scope: "account", doc: "Refuses every caller." }
|
|
709
1597
|
};
|
|
710
|
-
var frameworkOwner = "akanjs";
|
|
711
|
-
var frameworkPattern = /^akanjs(?:\/|$)/;
|
|
712
|
-
var aliasPattern = /^@(apps|libs)\/([^/]+)/;
|
|
713
|
-
var maxMapDepth = 4;
|
|
714
1598
|
|
|
715
1599
|
class GraphBuilder {
|
|
716
1600
|
#source;
|
|
@@ -718,16 +1602,21 @@ class GraphBuilder {
|
|
|
718
1602
|
#edges = new Map;
|
|
719
1603
|
#contexts = new Map;
|
|
720
1604
|
#members = new Map;
|
|
721
|
-
#guards = new Map;
|
|
722
|
-
#mapEntries = new Map;
|
|
723
1605
|
#routeFiles = new Map;
|
|
724
|
-
#declarations = new Map;
|
|
725
1606
|
#routes;
|
|
726
|
-
#
|
|
727
|
-
#
|
|
1607
|
+
#resolver;
|
|
1608
|
+
#adaptors;
|
|
728
1609
|
constructor(source) {
|
|
729
1610
|
this.#source = source;
|
|
730
1611
|
this.#routes = new RouteFactReader(source.ts, source.reader, source.parse);
|
|
1612
|
+
this.#resolver = new GuardResolver(source, this.#routes);
|
|
1613
|
+
this.#adaptors = new AdaptorResolver(source, this.#resolver, {
|
|
1614
|
+
key: (kind, owner, scope) => this.#key(kind, owner, scope),
|
|
1615
|
+
addNode: (id, kind, owner, name, span, detail) => this.#addNode(id, kind, owner, null, name, span, detail),
|
|
1616
|
+
addEdge: (from, to, kind, label, at) => this.#addEdge(from, to, kind, label, at),
|
|
1617
|
+
digest: (node, text, extent) => this.#digest(node, text, extent),
|
|
1618
|
+
location: (span) => this.#location(span)
|
|
1619
|
+
});
|
|
731
1620
|
}
|
|
732
1621
|
build() {
|
|
733
1622
|
const { facts } = this.#source;
|
|
@@ -739,17 +1628,9 @@ class GraphBuilder {
|
|
|
739
1628
|
...[...constantsByOwner].filter(([key]) => key !== owner && key?.startsWith("libs/")).flatMap(([, list]) => list)
|
|
740
1629
|
];
|
|
741
1630
|
const typed = [];
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
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);
|
|
1631
|
+
this.#adaptors.declare(facts);
|
|
1632
|
+
for (const guard of this.#resolver.declare(facts))
|
|
1633
|
+
this.#guardNodeOf(guard);
|
|
753
1634
|
for (const file of facts) {
|
|
754
1635
|
const { owner } = file;
|
|
755
1636
|
const scalar = this.#source.relative(file.fileName).match(scalarPattern)?.[1];
|
|
@@ -780,10 +1661,7 @@ class GraphBuilder {
|
|
|
780
1661
|
}
|
|
781
1662
|
for (const method of service.methods)
|
|
782
1663
|
this.#addMember("method", owner, refName, method, `${service.name}.${method.name}`, file.fileName);
|
|
783
|
-
|
|
784
|
-
this.#plugs.set(`${owner ?? ""}
|
|
785
|
-
${refName}
|
|
786
|
-
${plug.name}`, this.#adaptorIdOf(plug.target, file));
|
|
1664
|
+
this.#adaptors.addService(refName, service, file);
|
|
787
1665
|
}
|
|
788
1666
|
for (const filter of file.filters) {
|
|
789
1667
|
if (!filter.name.endsWith("Filter"))
|
|
@@ -793,31 +1671,7 @@ ${plug.name}`, this.#adaptorIdOf(plug.target, file));
|
|
|
793
1671
|
for (const query of filter.queries)
|
|
794
1672
|
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
1673
|
}
|
|
796
|
-
|
|
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
|
-
}
|
|
1674
|
+
this.#addGuarded(file);
|
|
821
1675
|
for (const internal of file.internals)
|
|
822
1676
|
for (const key of internal.keys)
|
|
823
1677
|
if (internal.model && key.type !== "resolveField")
|
|
@@ -833,7 +1687,7 @@ ${plug.name}`, this.#adaptorIdOf(plug.target, file));
|
|
|
833
1687
|
types.set(node.label, [...types.get(node.label) ?? [], node]);
|
|
834
1688
|
for (const node of typed)
|
|
835
1689
|
for (const field of node.fields ?? []) {
|
|
836
|
-
const candidates = (field.type ? types.get(field.type) ?? [] : []).filter((type) => this.#visible(type.owner, node.owner));
|
|
1690
|
+
const candidates = (field.type ? types.get(field.type) ?? [] : []).filter((type) => this.#resolver.visible(type.owner, node.owner));
|
|
837
1691
|
field.ref = (candidates.find((type) => type.owner === node.owner) ?? candidates[0])?.id ?? null;
|
|
838
1692
|
if (field.ref)
|
|
839
1693
|
this.#addEdge(node.id, field.ref, "field", `${field.name}${field.array ? "[]" : ""}`, field.at);
|
|
@@ -915,61 +1769,52 @@ ${label ?? ""}`;
|
|
|
915
1769
|
else
|
|
916
1770
|
this.#edges.set(key, { from, to, kind, label, count: 1, at });
|
|
917
1771
|
}
|
|
918
|
-
#
|
|
919
|
-
const
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
for (const
|
|
928
|
-
const
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
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);
|
|
1772
|
+
#addGuarded(file) {
|
|
1773
|
+
const { owner } = file;
|
|
1774
|
+
for (const slice of file.slices) {
|
|
1775
|
+
const refName = slice.model;
|
|
1776
|
+
if (!refName)
|
|
1777
|
+
continue;
|
|
1778
|
+
const fallback = this.#addNode(this.#key("slice", owner, refName), "slice", owner, refName, "", slice.span, slice.guards.map((guard) => guardLabelOf(guard, file.guardLabels)));
|
|
1779
|
+
for (const guard of slice.guards)
|
|
1780
|
+
this.#guard(fallback, guard, file);
|
|
1781
|
+
for (const key of slice.keys) {
|
|
1782
|
+
const detail = [
|
|
1783
|
+
...key.guards.map((guard) => guardLabelOf(guard, file.guardLabels)),
|
|
1784
|
+
...key.live ? ["live"] : []
|
|
1785
|
+
];
|
|
1786
|
+
const node = this.#addMember("slice", owner, refName, key, `${refName}.${key.name}`, file.fileName, detail);
|
|
1787
|
+
for (const guard of key.guards)
|
|
1788
|
+
this.#guard(node, guard, file);
|
|
950
1789
|
}
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
const
|
|
958
|
-
|
|
959
|
-
|
|
1790
|
+
}
|
|
1791
|
+
for (const endpoint of file.endpoints) {
|
|
1792
|
+
const refName = endpoint.model;
|
|
1793
|
+
if (!refName)
|
|
1794
|
+
continue;
|
|
1795
|
+
for (const key of endpoint.keys) {
|
|
1796
|
+
const detail = [
|
|
1797
|
+
...key.verb ? [key.verb] : [],
|
|
1798
|
+
...key.guards.map((guard) => guardLabelOf(guard, file.guardLabels))
|
|
1799
|
+
];
|
|
1800
|
+
const node = this.#addMember("endpoint", owner, refName, key, `${refName}.${key.name}`, file.fileName, detail);
|
|
1801
|
+
for (const guard of key.guards)
|
|
1802
|
+
this.#guard(node, guard, file);
|
|
960
1803
|
}
|
|
961
1804
|
}
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
1805
|
+
}
|
|
1806
|
+
#guard(node, guard, file) {
|
|
1807
|
+
const [, role = null, local = guard] = guard.match(/^([\w$]+) (.+)$/) ?? [];
|
|
1808
|
+
if (!local || local === "?")
|
|
1809
|
+
return;
|
|
1810
|
+
const id = local.includes("@") ? this.#madeInPlaceIdOf(local, file) : this.#guardIdOf(local, file);
|
|
1811
|
+
if (id)
|
|
1812
|
+
this.#addEdge(node.id, id, "guard", role, node.at);
|
|
970
1813
|
}
|
|
971
1814
|
#guardIdOf(local, site) {
|
|
972
|
-
const found = this.#
|
|
1815
|
+
const found = this.#resolver.find(local, site);
|
|
1816
|
+
if ("external" in found)
|
|
1817
|
+
return null;
|
|
973
1818
|
if ("guard" in found)
|
|
974
1819
|
return this.#guardNodeOf(found.guard);
|
|
975
1820
|
const { name, from } = found;
|
|
@@ -982,99 +1827,18 @@ ${label ?? ""}`;
|
|
|
982
1827
|
node.doc = builtin.doc;
|
|
983
1828
|
return node.id;
|
|
984
1829
|
}
|
|
985
|
-
#
|
|
986
|
-
const
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
const
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
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;
|
|
1830
|
+
#madeInPlaceIdOf(name, file) {
|
|
1831
|
+
const made = this.#resolver.madeInPlace(file.fileName, name);
|
|
1832
|
+
if (made)
|
|
1833
|
+
return this.#guardNodeOf(made);
|
|
1834
|
+
const head = name.slice(name.indexOf(":") + 1, name.indexOf("@")).split(".")[0] ?? name;
|
|
1835
|
+
const imported = file.imports.find((item) => item.name === head);
|
|
1836
|
+
const from = imported ? this.#resolver.ownerOfSpecifier(file.fileName, imported.from) : file.owner;
|
|
1837
|
+
if (from === undefined || from === frameworkOwner)
|
|
1838
|
+
return null;
|
|
1839
|
+
const scope = `${this.#resolver.inOwner(file.fileName, file.owner)}#${name}`;
|
|
1840
|
+
const label = guardLabelOf(name, file.guardLabels);
|
|
1841
|
+
return this.#addNode(this.#key("guard", file.owner, scope), "guard", file.owner, null, name, null, [], label).id;
|
|
1078
1842
|
}
|
|
1079
1843
|
#addRoutes() {
|
|
1080
1844
|
const routes = [];
|
|
@@ -1127,22 +1891,23 @@ ${dir}`, node);
|
|
|
1127
1891
|
}
|
|
1128
1892
|
#routeGuardIdOf({ name: local, local: declared }, fact, text) {
|
|
1129
1893
|
if (declared) {
|
|
1130
|
-
const
|
|
1131
|
-
const id = this.#key("guard", fact.owner, `${file}#${local}`);
|
|
1894
|
+
const id = this.#key("guard", fact.owner, `${this.#resolver.inOwner(fact.fileName, fact.owner)}#${local}`);
|
|
1132
1895
|
return this.#routeGuardNodeOf(id, fact.owner, local, declared.span, declared.doc, text, declared.extent);
|
|
1133
1896
|
}
|
|
1134
|
-
const
|
|
1135
|
-
|
|
1897
|
+
const [head = local, member] = local.split(".");
|
|
1898
|
+
const imported = fact.imports.find((item) => item.name === head);
|
|
1899
|
+
if (!imported || member && imported.imported !== "*")
|
|
1136
1900
|
return null;
|
|
1137
|
-
const name = imported.imported !== "default" && imported.imported !== "*" ? imported.imported : local;
|
|
1138
|
-
const from = this.#ownerOfSpecifier(fact.fileName, imported.from);
|
|
1901
|
+
const name = member ?? (imported.imported !== "default" && imported.imported !== "*" ? imported.imported : local);
|
|
1902
|
+
const from = this.#resolver.ownerOfSpecifier(fact.fileName, imported.from);
|
|
1139
1903
|
if (!from || from === frameworkOwner)
|
|
1140
1904
|
return null;
|
|
1141
|
-
const found = this.#declarationOf(from, name, this.#pathOfSpecifier(fact.fileName, imported.from));
|
|
1905
|
+
const found = this.#resolver.declarationOf(from, name, this.#resolver.pathOfSpecifier(fact.fileName, imported.from));
|
|
1142
1906
|
if (!found)
|
|
1143
1907
|
return null;
|
|
1144
1908
|
const { reader } = this.#source;
|
|
1145
|
-
|
|
1909
|
+
const owner = this.#source.ownerOf(found.source.fileName) ?? from;
|
|
1910
|
+
return this.#routeGuardNodeOf(this.#key("guard", owner, `${this.#resolver.inOwner(found.source.fileName, owner)}#${found.name}`), owner, found.name, reader.spanOf(found.source, found.declared), reader.docOf(found.source, found.statement), found.source.text, found.extent);
|
|
1146
1911
|
}
|
|
1147
1912
|
#routeGuardNodeOf(id, owner, name, span, doc, text, extent) {
|
|
1148
1913
|
const isNew = !this.#nodes.has(id);
|
|
@@ -1152,60 +1917,22 @@ ${dir}`, node);
|
|
|
1152
1917
|
this.#digest(node, text, extent);
|
|
1153
1918
|
return node.id;
|
|
1154
1919
|
}
|
|
1155
|
-
#
|
|
1156
|
-
const
|
|
1157
|
-
const
|
|
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] : []);
|
|
1920
|
+
#guardNodeOf({ name, owner, span, scope, doc, parts, file, label }) {
|
|
1921
|
+
const id = this.#key("guard", owner, file ? `${file}#${name}` : name);
|
|
1922
|
+
const node = this.#addNode(id, "guard", owner, null, name, span, scope ? [scope] : [], label ?? name);
|
|
1179
1923
|
if (doc && !node.doc)
|
|
1180
1924
|
node.doc = doc;
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1925
|
+
if (node.digest)
|
|
1926
|
+
return node.id;
|
|
1927
|
+
const digest = this.#hashOf(...parts.map(({ fileName, extent }) => [this.#source.readText(fileName), extent]));
|
|
1928
|
+
if (!digest)
|
|
1929
|
+
return node.id;
|
|
1930
|
+
node.digest = digest;
|
|
1931
|
+
const sources = [...new Set(parts.map(({ fileName }) => this.#source.relative(fileName)))].filter((source) => source !== node.at?.file);
|
|
1932
|
+
if (sources.length)
|
|
1933
|
+
node.sources = sources;
|
|
1187
1934
|
return node.id;
|
|
1188
1935
|
}
|
|
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
1936
|
#moduleDirOf(fileName) {
|
|
1210
1937
|
const dir = this.#source.relative(fileName).match(moduleDirPattern)?.[1] ?? null;
|
|
1211
1938
|
return dir?.startsWith("_") && !dir.startsWith("__") ? dir.slice(1) : dir;
|
|
@@ -1241,20 +1968,13 @@ ${dir}`, node);
|
|
|
1241
1968
|
visit(constant);
|
|
1242
1969
|
return [...fields.values()];
|
|
1243
1970
|
}
|
|
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
1971
|
#targetNodeOf({ family, owner, refName, sourceKey }) {
|
|
1252
1972
|
const isDefault = /^[A-Z]/.test(sourceKey);
|
|
1253
1973
|
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
1974
|
return id && this.#nodes.has(id) ? id : null;
|
|
1255
1975
|
}
|
|
1256
1976
|
#membersNamed(kind, name, owner, refName) {
|
|
1257
|
-
const nodes = (this.#members.get(`${kind}:${name}`) ?? []).filter((node) => (!refName || node.module === refName) && this.#visible(node.owner, owner));
|
|
1977
|
+
const nodes = (this.#members.get(`${kind}:${name}`) ?? []).filter((node) => (!refName || node.module === refName) && this.#resolver.visible(node.owner, owner));
|
|
1258
1978
|
const own = nodes.filter((node) => node.owner === owner);
|
|
1259
1979
|
return (own.length ? own : nodes).map((node) => node.id);
|
|
1260
1980
|
}
|
|
@@ -1299,10 +2019,8 @@ st
|
|
|
1299
2019
|
visit(source);
|
|
1300
2020
|
}
|
|
1301
2021
|
#link(fileName, source, identifier, chain, context, owner) {
|
|
1302
|
-
const plugged = chain?.length === 2 && chain[0] === "this" && context.kind === "method" && this.#isServiceThis(identifier) ? this.#
|
|
1303
|
-
|
|
1304
|
-
${chain[1]}`) : undefined;
|
|
1305
|
-
const resolved = plugged ? [plugged] : this.#source.resolve(fileName, identifier).map((target) => this.#targetNodeOf(target)).filter((id) => id !== null);
|
|
2022
|
+
const plugged = chain?.length === 2 && chain[0] === "this" && context.kind === "method" && this.#isServiceThis(identifier) ? this.#adaptors.pluggedOf(context.owner, context.module, chain[1]) : [];
|
|
2023
|
+
const resolved = plugged.length ? plugged : this.#source.resolve(fileName, identifier).map((target) => this.#targetNodeOf(target)).filter((id) => id !== null);
|
|
1306
2024
|
const targets = resolved.length ? resolved : this.#plainTargetsOf(chain, identifier.text, context, owner);
|
|
1307
2025
|
if (!targets.length)
|
|
1308
2026
|
return;
|
|
@@ -1454,6 +2172,10 @@ import {
|
|
|
1454
2172
|
sliceNames,
|
|
1455
2173
|
stateSetterName
|
|
1456
2174
|
} from "akanjs/common/akanNames.ts";
|
|
2175
|
+
var guardDetailOf = (facts, guards) => {
|
|
2176
|
+
const labels = Object.assign({}, ...facts.map((file) => file.guardLabels));
|
|
2177
|
+
return guards.length ? [`guards: ${guards.map((guard) => guardLabelOf(guard, labels)).join(", ")}`] : [];
|
|
2178
|
+
};
|
|
1457
2179
|
var capitalize2 = (text) => text.charAt(0).toUpperCase() + text.slice(1);
|
|
1458
2180
|
|
|
1459
2181
|
class NameTable {
|
|
@@ -1553,7 +2275,7 @@ class NameTable {
|
|
|
1553
2275
|
role: "sig",
|
|
1554
2276
|
gate: "sig",
|
|
1555
2277
|
label: sliceClass.name,
|
|
1556
|
-
detail:
|
|
2278
|
+
detail: guardDetailOf(facts, sliceClass.guards)
|
|
1557
2279
|
});
|
|
1558
2280
|
if (modelDoc)
|
|
1559
2281
|
this.#add({
|
|
@@ -1618,7 +2340,7 @@ class NameTable {
|
|
|
1618
2340
|
sourceKey: slice.name,
|
|
1619
2341
|
span: slice.span,
|
|
1620
2342
|
label: `slice ${refName} (default)`,
|
|
1621
|
-
detail:
|
|
2343
|
+
detail: guardDetailOf(facts, slice.guards)
|
|
1622
2344
|
};
|
|
1623
2345
|
this.#addSliceNames(defaultTarget, refName, "", modelNameSet);
|
|
1624
2346
|
for (const key of slice.keys)
|
|
@@ -1628,7 +2350,7 @@ class NameTable {
|
|
|
1628
2350
|
sourceKey: key.name,
|
|
1629
2351
|
span: key.span,
|
|
1630
2352
|
label: `slice ${key.name}`,
|
|
1631
|
-
detail: [...
|
|
2353
|
+
detail: [...guardDetailOf(facts, key.guards), ...key.live ? ["live"] : []]
|
|
1632
2354
|
}, refName, key.name, modelNameSet, slice.name);
|
|
1633
2355
|
}
|
|
1634
2356
|
if (filterClass) {
|
|
@@ -1685,7 +2407,7 @@ class NameTable {
|
|
|
1685
2407
|
role: key.verb === "pubsub" ? "subscribe" : "listen",
|
|
1686
2408
|
gate: null,
|
|
1687
2409
|
label: `${key.verb} ${key.name}`,
|
|
1688
|
-
detail:
|
|
2410
|
+
detail: guardDetailOf(facts, key.guards)
|
|
1689
2411
|
});
|
|
1690
2412
|
}
|
|
1691
2413
|
}
|
|
@@ -1742,15 +2464,13 @@ var sourcePattern = /\.(constant|document|service|signal|store)\.ts$/;
|
|
|
1742
2464
|
var dictionaryPattern = /\.dictionary\.ts$/;
|
|
1743
2465
|
var srvkitPattern = /\/srvkit\//;
|
|
1744
2466
|
var wordPattern = /[A-Za-z_$][\w$]*/g;
|
|
1745
|
-
var
|
|
1746
|
-
var regExpSpecials = /[\\^$.*+?()[\]{}|]/g;
|
|
2467
|
+
var identifierPattern2 = /^[A-Za-z_$][\w$]*$/;
|
|
1747
2468
|
var stateReceivers = new Set(["use", "sel", "get", "do"]);
|
|
1748
2469
|
var lensFamilies = new Set(["slice", "query", "formSetter", "storeState", "endpoint"]);
|
|
1749
2470
|
var modellessFamilies = new Set(["query", "storeState", "endpoint"]);
|
|
1750
2471
|
var gateWords = { srv: "srv", sig: "sig", db: "db", slice: "slice" };
|
|
1751
2472
|
var parseCacheSize = 64;
|
|
1752
2473
|
var pathOf = (fileName) => fileName.replace(/\\/g, "/");
|
|
1753
|
-
var escapeRegExp = (text) => text.replace(regExpSpecials, (special) => `\\${special}`);
|
|
1754
2474
|
var receiverGap = String.raw`(?:\s|\)|\/\*[\s\S]*?\*\/)*`;
|
|
1755
2475
|
|
|
1756
2476
|
class AkanIndex {
|
|
@@ -1853,7 +2573,7 @@ ${[...new Set(text.match(wordPattern))].join(`
|
|
|
1853
2573
|
}
|
|
1854
2574
|
renameAt(givenName, offset, newKey, readText) {
|
|
1855
2575
|
const sources = this.#sourceEntriesAt(pathOf(givenName), offset).filter((entry) => entry.renamed);
|
|
1856
|
-
if (!sources.length || !
|
|
2576
|
+
if (!sources.length || !identifierPattern2.test(newKey))
|
|
1857
2577
|
return null;
|
|
1858
2578
|
return [
|
|
1859
2579
|
{ span: sources[0].span, newText: newKey },
|
|
@@ -1881,6 +2601,8 @@ canPass
|
|
|
1881
2601
|
adapt
|
|
1882
2602
|
`) || words.includes(`
|
|
1883
2603
|
dangerouslyAdapt
|
|
2604
|
+
`) || words.includes(`
|
|
2605
|
+
applyAdaptor
|
|
1884
2606
|
`);
|
|
1885
2607
|
}
|
|
1886
2608
|
static #sourceKeyOf({ family, sourceKey, label, detail, span }) {
|
|
@@ -2561,6 +3283,11 @@ ${union.name}`)));
|
|
|
2561
3283
|
const field = this.#memberOf(this.#classOf(receiver), receiver.name.text);
|
|
2562
3284
|
return field && ts.isPropertyDeclaration(field) ? this.#namesIn(field.type) : new Set;
|
|
2563
3285
|
}
|
|
3286
|
+
if (ts.isCallExpression(receiver) && ts.isPropertyAccessExpression(receiver.expression) && receiver.expression.expression.kind === ts.SyntaxKind.ThisKeyword) {
|
|
3287
|
+
const field = this.#memberOf(this.#classOf(receiver), receiver.expression.name.text);
|
|
3288
|
+
const type = field && ts.isPropertyDeclaration(field) ? field.type : undefined;
|
|
3289
|
+
return type && ts.isFunctionTypeNode(type) ? this.#namesIn(type.type) : new Set;
|
|
3290
|
+
}
|
|
2564
3291
|
return this.#declaredTypeNames(receiver) ?? new Set;
|
|
2565
3292
|
}
|
|
2566
3293
|
#memberName(name) {
|