@akanjs/cli 3.2.0-rc.0 → 3.2.0-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.build-stamp +1 -1
- package/{CodeAgent-h6g8dg3a.js → CodeAgent-6cpjtj25.js} +3 -3
- package/{CodeTui-n9gacepn.js → CodeTui-zyc7zaq6.js} +1 -1
- package/TunnelShare-zkfkrw8w.js +14 -0
- package/{agent.command-2f657613.js → agent.command-kf1g3nh4.js} +17 -17
- package/akanCode.js +6 -6
- package/{application.command-yb8v177e.js → application.command-vb278qnq.js} +13 -13
- package/{applicationBuildRunner-sctcmkyx.js → applicationBuildRunner-bqbyeg84.js} +6 -6
- package/buildBatch.proc.js +6 -6
- package/{cloud.command-et06ja22.js → cloud.command-ca777ee2.js} +19 -19
- package/cloud.script-agpxzq6j.js +30 -0
- package/{code.command-7m67c7zc.js → code.command-j08txqcd.js} +6 -6
- package/{context.command-569z0rqf.js → context.command-2fa6egp9.js} +19 -19
- package/context.runner-ftv8373t.js +29 -0
- package/{devTui-evr3b35y.js → devTui-w38ch13a.js} +1 -1
- package/{guideline.command-gnv3e8p8.js → guideline.command-w6yqpdec.js} +2 -2
- package/incrementalBuilder.proc.js +7 -7
- package/{index-jdamtr7e.js → index-0fj9ra3q.js} +7 -7
- package/{index-thckcabc.js → index-0mqga3h9.js} +18 -3
- package/{index-abgh0jve.js → index-1cb9apzn.js} +1 -1
- package/{index-ed6ea0qp.js → index-20wvskt8.js} +1 -1
- package/{index-tenw3mah.js → index-23860fej.js} +16 -6
- package/{index-5w9my9qe.js → index-2gees180.js} +8 -8
- package/{index-93ewzmp6.js → index-386canwe.js} +21 -14
- package/{index-fjr4d6yd.js → index-466z5931.js} +5 -5
- package/{index-d4n1wmrz.js → index-65b6ms4c.js} +4 -4
- package/{index-6wpaq392.js → index-6h775rv3.js} +4 -4
- package/{index-aj3ehkcx.js → index-7kmbqp0b.js} +1 -1
- package/{index-1q1cjn8s.js → index-84c2pgak.js} +93 -53
- package/{index-sy481q2x.js → index-8jpc5fqv.js} +1 -1
- package/{index-ryrm5z2c.js → index-8nbx607g.js} +10 -10
- package/{index-3wjbhj2n.js → index-abfbf01s.js} +1 -1
- package/{index-zxsygkda.js → index-annxfpgz.js} +7 -7
- package/{index-8g7tdsk4.js → index-bdmgqzb5.js} +4 -4
- package/{index-vkzfk7qt.js → index-c5ymqb05.js} +40 -30
- package/{index-0fb3cpbt.js → index-cbcwxp5v.js} +5 -5
- package/{index-zpv14wpy.js → index-djc5nxpy.js} +3 -3
- package/{index-s2grpzef.js → index-dk4g0x0d.js} +3 -3
- package/{index-drz9e84d.js → index-e22zedc2.js} +2 -2
- package/{index-w639tvkd.js → index-ebwkh7bt.js} +7 -4
- package/{index-8ts9774g.js → index-htxck0pn.js} +4 -4
- package/{index-9tbgb4d8.js → index-j1hjb477.js} +1 -1
- package/{index-kpdv34rq.js → index-j7p0jgaq.js} +10 -7
- package/{index-ktb3wcqz.js → index-kcpk2d20.js} +8 -8
- package/{index-7y5yghjg.js → index-m5sg5spm.js} +932 -431
- package/{index-rc5fvqj2.js → index-mvwvxgtd.js} +28 -26
- package/{index-e3vmjtph.js → index-pq5rjkch.js} +2 -2
- package/{index-48d5qsj3.js → index-qgazn58x.js} +4 -4
- package/{index-eajbmk10.js → index-v50xjbbe.js} +1 -1
- package/{index-7xdpww43.js → index-vbhx60x5.js} +323 -129
- package/{index-sr5da35e.js → index-wfmpxke3.js} +2 -2
- package/{index-mmym5xmb.js → index-wr9magsr.js} +5 -3
- package/{index-mqntfzv5.js → index-xa0289eg.js} +2 -2
- package/{index-8pb9yvp8.js → index-xq97me1h.js} +49 -12
- package/{index-p6hjwt2d.js → index-y725zb1a.js} +1 -1
- package/{index-hqrkqxx2.js → index-ygrdw6jw.js} +1 -1
- package/index.js +23 -23
- package/{library.command-1gp5qe3r.js → library.command-8d03a0pq.js} +4 -4
- package/{localRegistry.command-v3g21rex.js → localRegistry.command-vyv5k6t0.js} +18 -18
- package/{module.command-4xz9he6h.js → module.command-vy3kg965.js} +7 -7
- package/{package.command-s7qpnn1g.js → package.command-qjh13rs9.js} +7 -7
- package/package.json +2 -2
- package/{page.command-fan34c49.js → page.command-t0nx1jat.js} +3 -3
- package/{pipeline.command-ph4fnn9w.js → pipeline.command-2yjeas7m.js} +2 -2
- package/{primitive.command-3fqh8emf.js → primitive.command-8vm49279.js} +7 -7
- package/{projectTypecheck-vkb40hjc.js → projectTypecheck-s9hhe5sa.js} +1 -1
- package/{quality.command-ecj6exsh.js → quality.command-v76fev72.js} +6 -6
- package/{repair.command-fe9jw719.js → repair.command-fp4m3w03.js} +6 -6
- package/{scalar.command-cnvfhk0n.js → scalar.command-hxe6try8.js} +6 -6
- package/skills/akan-scaffold/SKILL.md +2 -1
- package/{subspace.command-yxtt3v3e.js → subspace.command-q42n1844.js} +8 -8
- package/{tunnel.command-ggj38mqf.js → tunnel.command-22g11n0x.js} +7 -7
- package/{verify.command-w9ttxkdz.js → verify.command-rx43bha1.js} +14 -14
- package/{workflow.command-ss8devj0.js → workflow.command-yg0d8mmq.js} +30 -15
- package/{workspace.command-rw1p0xeq.js → workspace.command-fcfztvn5.js} +34 -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;
|
|
@@ -39,6 +40,40 @@ class AkanGraphDiff {
|
|
|
39
40
|
summary[node.change] += 1;
|
|
40
41
|
return { version: 1, nodes, edges, summary };
|
|
41
42
|
}
|
|
43
|
+
static #renamed(base, head) {
|
|
44
|
+
const baseIds = new Set(base.nodes.map((node) => node.id));
|
|
45
|
+
const headIds = new Set(head.nodes.map((node) => node.id));
|
|
46
|
+
const pairKeyOf = ({ kind, owner, name, at }) => at ? `${kind}
|
|
47
|
+
${owner ?? ""}
|
|
48
|
+
${name}
|
|
49
|
+
${at.file}` : null;
|
|
50
|
+
const shortIdOf = ({ kind, owner, name }) => `${kind}:${owner ?? ""}:${name}`;
|
|
51
|
+
const isFileKeyed = (node) => node.id !== shortIdOf(node) && node.id.startsWith(`${node.kind}:${node.owner ?? ""}:`) && node.id.endsWith(`#${node.name}`);
|
|
52
|
+
const fromBase = new Map(base.nodes.flatMap((node) => {
|
|
53
|
+
const key = headIds.has(node.id) ? null : pairKeyOf(node);
|
|
54
|
+
return key ? [[key, node]] : [];
|
|
55
|
+
}));
|
|
56
|
+
const renames = new Map;
|
|
57
|
+
for (const node of head.nodes) {
|
|
58
|
+
if (baseIds.has(node.id))
|
|
59
|
+
continue;
|
|
60
|
+
const key = pairKeyOf(node);
|
|
61
|
+
const before = key ? fromBase.get(key) : undefined;
|
|
62
|
+
if (!before)
|
|
63
|
+
continue;
|
|
64
|
+
const isPair = isFileKeyed(node) ? before.id === shortIdOf(node) : isFileKeyed(before) && node.id === shortIdOf(before);
|
|
65
|
+
if (isPair)
|
|
66
|
+
renames.set(before.id, node.id);
|
|
67
|
+
}
|
|
68
|
+
if (!renames.size)
|
|
69
|
+
return base;
|
|
70
|
+
const rename = (id) => renames.get(id) ?? id;
|
|
71
|
+
return {
|
|
72
|
+
...base,
|
|
73
|
+
nodes: base.nodes.map((node) => renames.has(node.id) ? { ...node, id: rename(node.id) } : node),
|
|
74
|
+
edges: base.edges.map((edge) => ({ ...edge, from: rename(edge.from), to: rename(edge.to) }))
|
|
75
|
+
};
|
|
76
|
+
}
|
|
42
77
|
static #mark(node, change) {
|
|
43
78
|
return node.fields ? { ...node, change, fields: node.fields.map((field) => ({ ...field, change })) } : { ...node, change };
|
|
44
79
|
}
|
|
@@ -72,6 +107,239 @@ class AkanGraphDiff {
|
|
|
72
107
|
return node.fields ? { ...node, fields: node.fields.map(({ digest: _field, ...field }) => field) } : node;
|
|
73
108
|
}
|
|
74
109
|
}
|
|
110
|
+
// pkgs/@akanjs/language/guardFacts.ts
|
|
111
|
+
var guardLabelOf = (guard, labels) => {
|
|
112
|
+
const [, role = null, local = guard] = guard.match(/^([\w$]+) (.+)$/) ?? [];
|
|
113
|
+
const label = labels[local];
|
|
114
|
+
return label === undefined ? guard : role ? `${role} ${label}` : label;
|
|
115
|
+
};
|
|
116
|
+
var maxLabelArgs = 32;
|
|
117
|
+
var guardRefReaderOf = (ts, ast, unwrap) => {
|
|
118
|
+
const pathOf = (expression) => {
|
|
119
|
+
if (ts.isIdentifier(expression))
|
|
120
|
+
return expression.text;
|
|
121
|
+
if (!ts.isPropertyAccessExpression(expression))
|
|
122
|
+
return null;
|
|
123
|
+
const head = pathOf(expression.expression);
|
|
124
|
+
return head && `${head}.${expression.name.text}`;
|
|
125
|
+
};
|
|
126
|
+
const calleeOf = (call) => pathOf(unwrap(call.expression)) ?? "?";
|
|
127
|
+
const labelOf = (source, call) => {
|
|
128
|
+
const args = call.arguments.map((arg) => arg.getText(source)).join(", ");
|
|
129
|
+
return `${calleeOf(call)}(${args.length <= maxLabelArgs && !args.includes(`
|
|
130
|
+
`) ? args : "\u2026"})`;
|
|
131
|
+
};
|
|
132
|
+
const guardRefOf = (expression, made, member) => {
|
|
133
|
+
const value = unwrap(expression);
|
|
134
|
+
if (!ts.isCallExpression(value))
|
|
135
|
+
return pathOf(value) ?? ast.expressionName(value);
|
|
136
|
+
const prefix = `${member}:${calleeOf(value)}@`;
|
|
137
|
+
const name = `${prefix}${made.filter((other) => other.name.startsWith(prefix)).length + 1}`;
|
|
138
|
+
made.push({ call: value, name });
|
|
139
|
+
return name;
|
|
140
|
+
};
|
|
141
|
+
const guardsIn = (source, object, made, member) => {
|
|
142
|
+
const guards = object?.properties.find((property) => ts.isPropertyAssignment(property) && ast.nodeName(property.name) === "guards")?.initializer;
|
|
143
|
+
if (!guards)
|
|
144
|
+
return [];
|
|
145
|
+
const refOf = (expression) => guardRefOf(expression, made, member);
|
|
146
|
+
if (ts.isArrayLiteralExpression(guards))
|
|
147
|
+
return guards.elements.map(refOf).filter((name) => name !== null);
|
|
148
|
+
if (ts.isObjectLiteralExpression(guards))
|
|
149
|
+
return guards.properties.filter(ts.isPropertyAssignment).flatMap((property) => {
|
|
150
|
+
const value = unwrap(property.initializer);
|
|
151
|
+
const names = ts.isArrayLiteralExpression(value) ? value.elements.map(refOf) : [refOf(value)];
|
|
152
|
+
return names.map((name) => `${ast.nodeName(property.name)} ${name ?? "?"}`);
|
|
153
|
+
});
|
|
154
|
+
const single = refOf(guards);
|
|
155
|
+
return single ? [single] : [];
|
|
156
|
+
};
|
|
157
|
+
return { pathOf, labelOf, guardsIn };
|
|
158
|
+
};
|
|
159
|
+
var guardFactoryReaderOf = (ts, ast, { spanOf, extentOf, unwrap, docOf }, pathOf) => {
|
|
160
|
+
const implementsGuard = (node, guardNames) => !!node.heritageClauses?.some((clause) => clause.token === ts.SyntaxKind.ImplementsKeyword && clause.types.some(({ expression }) => guardNames.has(ast.expressionName(expression) ?? "")));
|
|
161
|
+
const scopeOf = (node) => {
|
|
162
|
+
const scope = node.members.find((member) => ts.isPropertyDeclaration(member) && ast.nodeName(member.name) === "scope" && !!ts.getModifiers(member)?.some(({ kind }) => kind === ts.SyntaxKind.StaticKeyword))?.initializer;
|
|
163
|
+
const value = scope && unwrap(scope);
|
|
164
|
+
return value && ts.isStringLiteralLike(value) ? value.text : null;
|
|
165
|
+
};
|
|
166
|
+
const functionsOf = (source) => {
|
|
167
|
+
const functions = new Map;
|
|
168
|
+
for (const statement of source.statements) {
|
|
169
|
+
if (ts.isFunctionDeclaration(statement) && statement.name && statement.body)
|
|
170
|
+
functions.set(statement.name.text, { statement, name: statement.name, body: statement.body });
|
|
171
|
+
if (!ts.isVariableStatement(statement))
|
|
172
|
+
continue;
|
|
173
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
174
|
+
const initializer = declaration.initializer && unwrap(declaration.initializer);
|
|
175
|
+
if (initializer && ts.isIdentifier(declaration.name) && (ts.isArrowFunction(initializer) || ts.isFunctionExpression(initializer)))
|
|
176
|
+
functions.set(declaration.name.text, { statement, name: declaration.name, body: initializer.body });
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return functions;
|
|
180
|
+
};
|
|
181
|
+
const returnedOf = (body, guardNames) => {
|
|
182
|
+
const locals = new Map;
|
|
183
|
+
const pending = [];
|
|
184
|
+
const visit = (node) => {
|
|
185
|
+
if (ts.isClassDeclaration(node) && node.name)
|
|
186
|
+
locals.set(node.name.text, node);
|
|
187
|
+
if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && node.initializer)
|
|
188
|
+
locals.set(node.name.text, node.initializer);
|
|
189
|
+
if (ts.isReturnStatement(node) && node.expression)
|
|
190
|
+
pending.push(node.expression);
|
|
191
|
+
if (!ts.isFunctionLike(node) && !ts.isClassLike(node))
|
|
192
|
+
ts.forEachChild(node, visit);
|
|
193
|
+
};
|
|
194
|
+
if (ts.isBlock(body))
|
|
195
|
+
ts.forEachChild(body, visit);
|
|
196
|
+
else
|
|
197
|
+
pending.push(body);
|
|
198
|
+
let guard = null;
|
|
199
|
+
const calls = [];
|
|
200
|
+
const seen = new Set;
|
|
201
|
+
const passed = new Set;
|
|
202
|
+
for (let node = pending.pop();node; node = pending.pop()) {
|
|
203
|
+
if (seen.has(node))
|
|
204
|
+
continue;
|
|
205
|
+
seen.add(node);
|
|
206
|
+
const value = ts.isExpression(node) ? unwrap(node) : node;
|
|
207
|
+
if ((ts.isClassExpression(value) || ts.isClassDeclaration(value)) && implementsGuard(value, guardNames))
|
|
208
|
+
guard ??= value;
|
|
209
|
+
else if (ts.isConditionalExpression(value))
|
|
210
|
+
pending.push(value.whenTrue, value.whenFalse);
|
|
211
|
+
else if (ts.isBinaryExpression(value))
|
|
212
|
+
pending.push(value.left, value.right);
|
|
213
|
+
else if (ts.isIdentifier(value)) {
|
|
214
|
+
const local = locals.get(value.text);
|
|
215
|
+
if (local)
|
|
216
|
+
pending.push(local);
|
|
217
|
+
} else if (ts.isCallExpression(value)) {
|
|
218
|
+
const callee = pathOf(unwrap(value.expression));
|
|
219
|
+
if (callee && !passed.has(node) && !calls.includes(callee))
|
|
220
|
+
calls.push(callee);
|
|
221
|
+
for (const arg of value.arguments)
|
|
222
|
+
passed.add(arg);
|
|
223
|
+
pending.push(...value.arguments);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
return { guard, calls };
|
|
227
|
+
};
|
|
228
|
+
const guardFactoriesOf = (source, guardNames, isImported) => {
|
|
229
|
+
const functions = functionsOf(source);
|
|
230
|
+
const returned = new Map([...functions].map(([name, { body }]) => [name, returnedOf(body, guardNames)]));
|
|
231
|
+
const members = new Set;
|
|
232
|
+
const isFactoryCall = (callee) => members.has(callee) || !functions.has(callee) && isImported(callee);
|
|
233
|
+
for (let isGrowing = true;isGrowing; ) {
|
|
234
|
+
isGrowing = false;
|
|
235
|
+
for (const [name, { guard, calls }] of returned) {
|
|
236
|
+
if (members.has(name) || !guard && !calls.some(isFactoryCall))
|
|
237
|
+
continue;
|
|
238
|
+
members.add(name);
|
|
239
|
+
isGrowing = true;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
const factories = new Map;
|
|
243
|
+
for (const name of members) {
|
|
244
|
+
const found = functions.get(name);
|
|
245
|
+
const made = returned.get(name);
|
|
246
|
+
if (!found || !made)
|
|
247
|
+
continue;
|
|
248
|
+
factories.set(name, {
|
|
249
|
+
name,
|
|
250
|
+
span: spanOf(source, found.name),
|
|
251
|
+
extent: extentOf(source, found.statement),
|
|
252
|
+
scope: made.guard ? scopeOf(made.guard) : null,
|
|
253
|
+
doc: docOf(source, found.statement),
|
|
254
|
+
direct: !!made.guard,
|
|
255
|
+
via: made.calls.filter(isFactoryCall)
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
return factories;
|
|
259
|
+
};
|
|
260
|
+
return { implementsGuard, scopeOf, guardFactoriesOf };
|
|
261
|
+
};
|
|
262
|
+
var createGuardFactReader = (ts, ast, base) => {
|
|
263
|
+
const { spanOf, extentOf, unwrap, docOf } = base;
|
|
264
|
+
const { pathOf, labelOf, guardsIn } = guardRefReaderOf(ts, ast, unwrap);
|
|
265
|
+
const { implementsGuard, scopeOf, guardFactoriesOf } = guardFactoryReaderOf(ts, ast, base, pathOf);
|
|
266
|
+
const guardsOf = (source, imports, madeInPlace) => {
|
|
267
|
+
const guardNames = new Set([
|
|
268
|
+
"Guard",
|
|
269
|
+
...imports.filter(({ imported }) => imported === "Guard").map(({ name }) => name)
|
|
270
|
+
]);
|
|
271
|
+
const imported = new Set(imports.map(({ name }) => name));
|
|
272
|
+
const isImported = (callee) => imported.has(callee.split(".")[0] ?? callee);
|
|
273
|
+
const factories = guardFactoriesOf(source, guardNames, isImported);
|
|
274
|
+
const guards = [];
|
|
275
|
+
const calls = [];
|
|
276
|
+
const makeOf = (made, value, isInPlace = false) => {
|
|
277
|
+
if (ts.isClassExpression(value) && implementsGuard(value, guardNames)) {
|
|
278
|
+
guards.push({ ...made, scope: scopeOf(value), factory: null });
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
const callee = ts.isCallExpression(value) ? pathOf(unwrap(value.expression)) : null;
|
|
282
|
+
const factory = callee && factories.get(callee);
|
|
283
|
+
if (factory)
|
|
284
|
+
guards.push({ ...made, scope: factory.scope, factory: factory.name });
|
|
285
|
+
else if (callee && (isImported(callee) || isInPlace))
|
|
286
|
+
calls.push({ ...made, callee, kind: "call", scope: null });
|
|
287
|
+
};
|
|
288
|
+
for (const statement of source.statements) {
|
|
289
|
+
if (ts.isClassDeclaration(statement) && statement.name) {
|
|
290
|
+
const declared = {
|
|
291
|
+
name: statement.name.text,
|
|
292
|
+
span: spanOf(source, statement.name),
|
|
293
|
+
extent: extentOf(source, statement),
|
|
294
|
+
scope: scopeOf(statement),
|
|
295
|
+
doc: docOf(source, statement)
|
|
296
|
+
};
|
|
297
|
+
const base = statement.heritageClauses?.find(({ token }) => token === ts.SyntaxKind.ExtendsKeyword)?.types[0];
|
|
298
|
+
const callee = base && pathOf(base.expression);
|
|
299
|
+
if (implementsGuard(statement, guardNames))
|
|
300
|
+
guards.push({ ...declared, factory: null });
|
|
301
|
+
else if (callee)
|
|
302
|
+
calls.push({ ...declared, callee, kind: "extends" });
|
|
303
|
+
}
|
|
304
|
+
if (!ts.isVariableStatement(statement))
|
|
305
|
+
continue;
|
|
306
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
307
|
+
const initializer = declaration.initializer && unwrap(declaration.initializer);
|
|
308
|
+
if (!initializer || !ts.isIdentifier(declaration.name))
|
|
309
|
+
continue;
|
|
310
|
+
const holder = declaration.name.text;
|
|
311
|
+
makeOf({
|
|
312
|
+
name: holder,
|
|
313
|
+
span: spanOf(source, declaration.name),
|
|
314
|
+
extent: extentOf(source, statement),
|
|
315
|
+
doc: docOf(source, statement)
|
|
316
|
+
}, initializer);
|
|
317
|
+
if (!ts.isObjectLiteralExpression(initializer))
|
|
318
|
+
continue;
|
|
319
|
+
for (const property of initializer.properties) {
|
|
320
|
+
const key = ts.isPropertyAssignment(property) ? ast.nodeName(property.name) : null;
|
|
321
|
+
if (key && ts.isPropertyAssignment(property))
|
|
322
|
+
makeOf({
|
|
323
|
+
name: `${holder}.${key}`,
|
|
324
|
+
span: spanOf(source, property.name),
|
|
325
|
+
extent: extentOf(source, property),
|
|
326
|
+
doc: docOf(source, property)
|
|
327
|
+
}, unwrap(property.initializer));
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
for (const { call, name } of madeInPlace)
|
|
332
|
+
makeOf({ name, span: spanOf(source, call), extent: extentOf(source, call), doc: null }, call, true);
|
|
333
|
+
const guardLabels = Object.fromEntries(madeInPlace.map(({ call, name }) => [name, labelOf(source, call)]));
|
|
334
|
+
return { guards, guardFactories: [...factories.values()], guardCalls: calls, guardLabels };
|
|
335
|
+
};
|
|
336
|
+
return { guardsIn, guardsOf };
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
// pkgs/@akanjs/language/escapeRegExp.ts
|
|
340
|
+
var regExpSpecials = /[\\^$.*+?()[\]{}|]/g;
|
|
341
|
+
var escapeRegExp = (text) => text.replace(regExpSpecials, (special) => `\\${special}`);
|
|
342
|
+
|
|
75
343
|
// pkgs/@akanjs/language/moduleFacts.ts
|
|
76
344
|
var testFilePattern = /\.(?:test|spec)\.tsx?$/;
|
|
77
345
|
var moduleCallees = new Set([
|
|
@@ -140,21 +408,6 @@ var createFactReader = (ts, ast) => {
|
|
|
140
408
|
const value = unwrap(expression);
|
|
141
409
|
return ts.isPropertyAccessExpression(value) && ts.isIdentifier(value.expression) ? `${value.expression.text}.${value.name.text}` : ast.expressionName(value);
|
|
142
410
|
};
|
|
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
411
|
const optionObjectOf = (call) => call.arguments.find((arg) => ts.isObjectLiteralExpression(arg) && arg.properties.some((property) => ast.propertyName(property) === "guards"));
|
|
159
412
|
const chainOf = (initializer) => {
|
|
160
413
|
let root = unwrap(initializer);
|
|
@@ -171,7 +424,7 @@ var createFactReader = (ts, ast) => {
|
|
|
171
424
|
const node = keyName(property);
|
|
172
425
|
return name && node ? [{ name, span: spanOf(source, node), extent: extentOf(source, property) }] : [];
|
|
173
426
|
});
|
|
174
|
-
const sliceKeysOf = (source, builder) => (returnedObject(builder)?.properties ?? []).flatMap((property) => {
|
|
427
|
+
const sliceKeysOf = (source, builder, made, model) => (returnedObject(builder)?.properties ?? []).flatMap((property) => {
|
|
175
428
|
if (!ts.isPropertyAssignment(property))
|
|
176
429
|
return [];
|
|
177
430
|
const name = ast.nodeName(property.name);
|
|
@@ -183,7 +436,7 @@ var createFactReader = (ts, ast) => {
|
|
|
183
436
|
name,
|
|
184
437
|
span: spanOf(source, property.name),
|
|
185
438
|
extent: extentOf(source, property),
|
|
186
|
-
guards: root ? guardsIn(optionObjectOf(root)) : [],
|
|
439
|
+
guards: root ? guardsIn(source, optionObjectOf(root), made, `${model}.${name}`) : [],
|
|
187
440
|
live: methods.includes("live"),
|
|
188
441
|
argCount: methods.filter((method) => argMethods.has(method)).length
|
|
189
442
|
}
|
|
@@ -289,7 +542,7 @@ var createFactReader = (ts, ast) => {
|
|
|
289
542
|
return name && targetName ? [{ name, span: spanOf(source, property.name), target: targetName }] : [];
|
|
290
543
|
});
|
|
291
544
|
};
|
|
292
|
-
const endpointKeysOf = (source, builder) => {
|
|
545
|
+
const endpointKeysOf = (source, builder, made, model) => {
|
|
293
546
|
const verbOf = verbReaderOf(builder?.parameters[0]);
|
|
294
547
|
return (returnedObject(builder)?.properties ?? []).flatMap((property) => {
|
|
295
548
|
if (!ts.isPropertyAssignment(property))
|
|
@@ -304,7 +557,7 @@ var createFactReader = (ts, ast) => {
|
|
|
304
557
|
span: spanOf(source, property.name),
|
|
305
558
|
extent: extentOf(source, property),
|
|
306
559
|
verb: root ? verbOf(root.expression) : null,
|
|
307
|
-
guards: root ? guardsIn(optionObjectOf(root)) : [],
|
|
560
|
+
guards: root ? guardsIn(source, optionObjectOf(root), made, `${model}.${name}`) : [],
|
|
308
561
|
argCount: methods.filter((method) => argMethods.has(method)).length
|
|
309
562
|
}
|
|
310
563
|
];
|
|
@@ -371,22 +624,6 @@ var createFactReader = (ts, ast) => {
|
|
|
371
624
|
})) : []
|
|
372
625
|
];
|
|
373
626
|
};
|
|
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
627
|
const docOf = (source, node) => {
|
|
391
628
|
const { text } = source;
|
|
392
629
|
const ranges = ts.getLeadingCommentRanges(text, node.getFullStart()) ?? [];
|
|
@@ -402,108 +639,20 @@ var createFactReader = (ts, ast) => {
|
|
|
402
639
|
`)).map((line) => line.trim().replace(/^\/\*\*?|\*\/$/g, "").replace(/^(?:\/\/[?*!]?|\*)/, "").replace(/\{@link\s+([^}\s|]+)[^}]*\}/g, "$1").trim()).filter((line) => line && !/^(?:biome-ignore|eslint-|@ts-)/.test(line));
|
|
403
640
|
return lines.length ? lines.join(" ") : null;
|
|
404
641
|
};
|
|
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
|
-
};
|
|
642
|
+
const { guardsIn, guardsOf } = createGuardFactReader(ts, ast, { spanOf, extentOf, unwrap, docOf });
|
|
498
643
|
const lowerFirst = (text) => text.charAt(0).toLowerCase() + text.slice(1);
|
|
499
644
|
const importsIn = (source) => source.statements.filter(ts.isImportDeclaration).flatMap(importsOf);
|
|
500
645
|
const read = (source, owner) => {
|
|
501
646
|
const imports = importsIn(source);
|
|
647
|
+
const madeInPlace = [];
|
|
502
648
|
const facts = {
|
|
503
649
|
fileName: source.fileName,
|
|
504
650
|
owner,
|
|
505
651
|
imports,
|
|
506
|
-
|
|
652
|
+
guards: [],
|
|
653
|
+
guardFactories: [],
|
|
654
|
+
guardCalls: [],
|
|
655
|
+
guardLabels: {},
|
|
507
656
|
constants: [],
|
|
508
657
|
enums: [],
|
|
509
658
|
filters: [],
|
|
@@ -521,64 +670,496 @@ var createFactReader = (ts, ast) => {
|
|
|
521
670
|
const heritage = ast.heritageCallNamed(statement, moduleCallees);
|
|
522
671
|
if (!heritage)
|
|
523
672
|
continue;
|
|
524
|
-
const { callee, call } = heritage;
|
|
525
|
-
const name = { name: statement.name.text, span: spanOf(source, statement.name) };
|
|
526
|
-
const builder = builderOf(call);
|
|
527
|
-
if (callee === "via")
|
|
528
|
-
facts.constants.push(constantOf(source, name, call));
|
|
529
|
-
else if (callee === "enumOf")
|
|
530
|
-
facts.enums.push({ ...name, values: enumValuesOf(call) });
|
|
531
|
-
else if (callee === "from")
|
|
532
|
-
facts.filters.push({ ...name, queries: filterQueriesOf(source, builder) });
|
|
533
|
-
else if (callee === "into")
|
|
534
|
-
facts.models.push({ ...name, model: name.name.endsWith("Model") ? lowerFirst(name.name.slice(0, -5)) : null });
|
|
535
|
-
else if (callee === "serve")
|
|
536
|
-
facts.services.push({
|
|
537
|
-
...name,
|
|
538
|
-
model: moduleRefOf(call.arguments[0], "db"),
|
|
539
|
-
methods: methodsOf(source, statement),
|
|
540
|
-
plugs: plugsOf(source, builder)
|
|
541
|
-
});
|
|
542
|
-
else if (callee === "slice")
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
673
|
+
const { callee, call } = heritage;
|
|
674
|
+
const name = { name: statement.name.text, span: spanOf(source, statement.name) };
|
|
675
|
+
const builder = builderOf(call);
|
|
676
|
+
if (callee === "via")
|
|
677
|
+
facts.constants.push(constantOf(source, name, call));
|
|
678
|
+
else if (callee === "enumOf")
|
|
679
|
+
facts.enums.push({ ...name, values: enumValuesOf(call) });
|
|
680
|
+
else if (callee === "from")
|
|
681
|
+
facts.filters.push({ ...name, queries: filterQueriesOf(source, builder) });
|
|
682
|
+
else if (callee === "into")
|
|
683
|
+
facts.models.push({ ...name, model: name.name.endsWith("Model") ? lowerFirst(name.name.slice(0, -5)) : null });
|
|
684
|
+
else if (callee === "serve")
|
|
685
|
+
facts.services.push({
|
|
686
|
+
...name,
|
|
687
|
+
model: moduleRefOf(call.arguments[0], "db"),
|
|
688
|
+
methods: methodsOf(source, statement),
|
|
689
|
+
plugs: plugsOf(source, builder)
|
|
690
|
+
});
|
|
691
|
+
else if (callee === "slice") {
|
|
692
|
+
const model = moduleRefOf(call.arguments[0], "srv");
|
|
693
|
+
const member = model ?? name.name;
|
|
694
|
+
facts.slices.push({
|
|
695
|
+
...name,
|
|
696
|
+
model,
|
|
697
|
+
guards: guardsIn(source, call.arguments.find(ts.isObjectLiteralExpression), madeInPlace, member),
|
|
698
|
+
keys: sliceKeysOf(source, builder, madeInPlace, member)
|
|
699
|
+
});
|
|
700
|
+
} else if (callee === "internal")
|
|
701
|
+
facts.internals.push({
|
|
702
|
+
...name,
|
|
703
|
+
model: moduleRefOf(call.arguments[0], "srv"),
|
|
704
|
+
keys: internalKeysOf(source, builder)
|
|
705
|
+
});
|
|
706
|
+
else if (callee === "adapt" || callee === "dangerouslyAdapt") {
|
|
707
|
+
const refName = call.arguments[0] && unwrap(call.arguments[0]);
|
|
708
|
+
facts.adaptors.push({
|
|
709
|
+
...name,
|
|
710
|
+
extent: extentOf(source, statement),
|
|
711
|
+
refName: refName && ts.isStringLiteralLike(refName) ? refName.text : null,
|
|
712
|
+
plugs: plugsOf(source, builder),
|
|
713
|
+
doc: docOf(source, statement)
|
|
714
|
+
});
|
|
715
|
+
} else if (callee === "endpoint") {
|
|
716
|
+
const model = moduleRefOf(call.arguments[0], "srv");
|
|
717
|
+
facts.endpoints.push({
|
|
718
|
+
...name,
|
|
719
|
+
model,
|
|
720
|
+
keys: endpointKeysOf(source, builder, madeInPlace, model ?? name.name)
|
|
721
|
+
});
|
|
722
|
+
} else if (callee === "store")
|
|
723
|
+
facts.stores.push({
|
|
724
|
+
...name,
|
|
725
|
+
model: moduleRefOf(call.arguments[0], "sig"),
|
|
726
|
+
stateKeys: objectKeys(source, returnedObject(builder)),
|
|
727
|
+
actions: methodsOf(source, statement)
|
|
728
|
+
});
|
|
729
|
+
}
|
|
730
|
+
return { ...facts, ...guardsOf(source, imports, madeInPlace) };
|
|
731
|
+
};
|
|
732
|
+
return { read, spanOf, extentOf, docOf, importsIn, moduleRefOf, chainOf, unwrap };
|
|
733
|
+
};
|
|
734
|
+
|
|
735
|
+
// pkgs/@akanjs/language/guardResolver.ts
|
|
736
|
+
var frameworkOwner = "akanjs";
|
|
737
|
+
var frameworkPattern = /^akanjs(?:\/|$)/;
|
|
738
|
+
var aliasPattern = /^@(apps|libs)\/([^/]+)/;
|
|
739
|
+
var maxMapDepth = 4;
|
|
740
|
+
var starExportPattern = /export\s*\*\s*from\s*["']([^"']+)["']/g;
|
|
741
|
+
var declaringPatternOf = (name, isAnyBinding) => {
|
|
742
|
+
const escaped = escapeRegExp(name);
|
|
743
|
+
const braced = "\\{(?:[^}]*?[\\s,])?";
|
|
744
|
+
const leads = [
|
|
745
|
+
"function\\s*\\*?\\s*",
|
|
746
|
+
"(?:const|let|var)\\s+",
|
|
747
|
+
`export\\s*${braced}`,
|
|
748
|
+
...isAnyBinding ? ["class\\s+", `(?:const|let|var)\\s*${braced}`] : []
|
|
749
|
+
];
|
|
750
|
+
return new RegExp(`(?:${leads.join("|")})${escaped}(?![\\w$])|,\\s*${escaped}\\s*(?::[^=;,]+)?=(?![=>])`);
|
|
751
|
+
};
|
|
752
|
+
|
|
753
|
+
class GuardResolver {
|
|
754
|
+
#source;
|
|
755
|
+
#routes;
|
|
756
|
+
#guards = new Map;
|
|
757
|
+
#madeInPlace = new Map;
|
|
758
|
+
#mapEntries = new Map;
|
|
759
|
+
#reexports = new Map;
|
|
760
|
+
#declarations = new Map;
|
|
761
|
+
#starExports = new Map;
|
|
762
|
+
#ownerFiles = null;
|
|
763
|
+
#modules = null;
|
|
764
|
+
constructor(source, routes) {
|
|
765
|
+
this.#source = source;
|
|
766
|
+
this.#routes = routes;
|
|
767
|
+
}
|
|
768
|
+
find(local, site) {
|
|
769
|
+
return this.#findGuard(local, site, 0);
|
|
770
|
+
}
|
|
771
|
+
madeInPlace(fileName, name) {
|
|
772
|
+
return this.#madeInPlace.get(`${fileName}
|
|
773
|
+
${name}`);
|
|
774
|
+
}
|
|
775
|
+
declare(facts) {
|
|
776
|
+
const factories = new Map;
|
|
777
|
+
const declaredFactories = [];
|
|
778
|
+
for (const file of facts)
|
|
779
|
+
for (const fact of file.guardFactories) {
|
|
780
|
+
const factory = {
|
|
781
|
+
fact,
|
|
782
|
+
owner: file.owner,
|
|
783
|
+
site: file,
|
|
784
|
+
made: [],
|
|
785
|
+
isReal: fact.direct,
|
|
786
|
+
scope: fact.scope,
|
|
787
|
+
targets: []
|
|
788
|
+
};
|
|
789
|
+
factories.set(fact.name, [...factories.get(fact.name) ?? [], factory]);
|
|
790
|
+
declaredFactories.push(factory);
|
|
791
|
+
}
|
|
792
|
+
for (const factory of declaredFactories)
|
|
793
|
+
factory.targets = factory.fact.via.map((callee) => this.#factoryAt(factories, callee, factory.site)).filter((target) => target !== undefined);
|
|
794
|
+
for (let isGrowing = true;isGrowing; ) {
|
|
795
|
+
isGrowing = false;
|
|
796
|
+
for (const factory of declaredFactories) {
|
|
797
|
+
const target = factory.targets.find((other) => other.isReal);
|
|
798
|
+
if (factory.isReal || !target)
|
|
799
|
+
continue;
|
|
800
|
+
factory.isReal = true;
|
|
801
|
+
factory.scope ??= target.scope;
|
|
802
|
+
isGrowing = true;
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
const partsOf = (factory, seen = new Set) => {
|
|
806
|
+
if (seen.has(factory))
|
|
807
|
+
return [];
|
|
808
|
+
seen.add(factory);
|
|
809
|
+
return [
|
|
810
|
+
{ fileName: factory.site.fileName, extent: factory.fact.extent },
|
|
811
|
+
...factory.targets.filter((target) => target.isReal).flatMap((target) => partsOf(target, seen))
|
|
812
|
+
];
|
|
813
|
+
};
|
|
814
|
+
const declare = (file, made, scope, parts) => {
|
|
815
|
+
const { name, span, extent, doc } = made;
|
|
816
|
+
const label = file.guardLabels[name];
|
|
817
|
+
const own = { fileName: file.fileName, extent };
|
|
818
|
+
const guard = { name, owner: file.owner, span, scope, doc, parts: [own, ...parts], file: null, label };
|
|
819
|
+
if (name.includes("@"))
|
|
820
|
+
this.#madeInPlace.set(`${file.fileName}
|
|
821
|
+
${name}`, guard);
|
|
822
|
+
else
|
|
823
|
+
this.#guards.set(name, [...this.#guards.get(name) ?? [], guard]);
|
|
824
|
+
return guard;
|
|
825
|
+
};
|
|
826
|
+
const extending = [];
|
|
827
|
+
for (const file of facts) {
|
|
828
|
+
for (const made of file.guards) {
|
|
829
|
+
const factory = made.factory ? factories.get(made.factory)?.find((other) => other.site.fileName === file.fileName) : undefined;
|
|
830
|
+
if (!made.factory)
|
|
831
|
+
declare(file, made, made.scope, []);
|
|
832
|
+
else if (factory?.isReal)
|
|
833
|
+
factory.made.push(declare(file, made, factory.scope, partsOf(factory)));
|
|
834
|
+
}
|
|
835
|
+
for (const made of file.guardCalls) {
|
|
836
|
+
if (made.kind === "extends") {
|
|
837
|
+
extending.push([file, made]);
|
|
838
|
+
continue;
|
|
839
|
+
}
|
|
840
|
+
const factory = this.#factoryAt(factories, made.callee, file);
|
|
841
|
+
if (factory?.isReal)
|
|
842
|
+
factory.made.push(declare(file, made, factory.scope, partsOf(factory)));
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
for (let isGrowing = true;isGrowing; ) {
|
|
846
|
+
isGrowing = false;
|
|
847
|
+
for (const [idx, entry] of extending.entries()) {
|
|
848
|
+
const [file, made] = entry;
|
|
849
|
+
const found = this.#findGuard(made.callee, file, 0, true);
|
|
850
|
+
if (!("guard" in found))
|
|
851
|
+
continue;
|
|
852
|
+
declare(file, made, made.scope ?? found.guard.scope, found.guard.parts);
|
|
853
|
+
extending.splice(idx, 1);
|
|
854
|
+
isGrowing = true;
|
|
855
|
+
break;
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
for (const { fact, made, isReal } of declaredFactories)
|
|
859
|
+
if (isReal && made.every((guard) => !guard.doc))
|
|
860
|
+
for (const guard of made)
|
|
861
|
+
guard.doc = fact.doc;
|
|
862
|
+
for (const list of this.#guards.values())
|
|
863
|
+
for (const guard of list)
|
|
864
|
+
if (list.some((other) => other !== guard && other.owner === guard.owner))
|
|
865
|
+
guard.file = this.inOwner(guard.span.fileName, guard.owner);
|
|
866
|
+
for (const guard of this.#madeInPlace.values())
|
|
867
|
+
guard.file = this.inOwner(guard.span.fileName, guard.owner);
|
|
868
|
+
return [...[...this.#guards.values()].flat(), ...this.#madeInPlace.values()];
|
|
869
|
+
}
|
|
870
|
+
#factoryAt(factories, callee, site, depth = 0) {
|
|
871
|
+
const [head = callee, member] = callee.split(".");
|
|
872
|
+
const imported = site.imports.find((item) => item.name === head);
|
|
873
|
+
if (!imported)
|
|
874
|
+
return member ? undefined : factories.get(callee)?.find((other) => other.site.fileName === site.fileName);
|
|
875
|
+
if (member && imported.imported !== "*")
|
|
876
|
+
return;
|
|
877
|
+
const from = this.ownerOfSpecifier(site.fileName, imported.from);
|
|
878
|
+
if (from === undefined || from === frameworkOwner)
|
|
879
|
+
return;
|
|
880
|
+
const name = member ?? (imported.imported === "default" || imported.imported === "*" ? head : imported.imported);
|
|
881
|
+
const candidates = factories.get(name) ?? [];
|
|
882
|
+
const near = this.pathOfSpecifier(site.fileName, imported.from);
|
|
883
|
+
const own = this.#nearest(candidates.filter((other) => other.owner === from), ({ site }) => site.fileName, near);
|
|
884
|
+
if (own)
|
|
885
|
+
return own;
|
|
886
|
+
const reexport = from && depth < maxMapDepth ? this.reexportOf(from, name, near) : null;
|
|
887
|
+
if (reexport)
|
|
888
|
+
return this.#factoryAt(factories, reexport.local, reexport.site, depth + 1);
|
|
889
|
+
return from && near ? this.#throughStar(candidates, from, name, near) : undefined;
|
|
890
|
+
}
|
|
891
|
+
#findGuard(local, site, depth, isBase = false) {
|
|
892
|
+
const segments = local.split(".");
|
|
893
|
+
const head = segments[0] ?? local;
|
|
894
|
+
const imported = site.imports.find((item) => item.name === head);
|
|
895
|
+
const from = imported ? this.ownerOfSpecifier(site.fileName, imported.from) : undefined;
|
|
896
|
+
if (imported && from === undefined)
|
|
897
|
+
return { external: true };
|
|
898
|
+
const near = imported ? this.pathOfSpecifier(site.fileName, imported.from) : this.#source.relative(site.fileName).replace(/\.tsx?$/, "");
|
|
899
|
+
const named = imported && imported.imported !== "default" && imported.imported !== "*" ? imported.imported : null;
|
|
900
|
+
const path = imported?.imported === "*" ? segments.slice(1) : [named ?? head, ...segments.slice(1)];
|
|
901
|
+
const holder = imported ? from ?? null : site.owner;
|
|
902
|
+
const [objectName, member] = path;
|
|
903
|
+
if (path.length === 2 && objectName && member && holder !== frameworkOwner && depth < maxMapDepth) {
|
|
904
|
+
const made = this.#nearest((this.#guards.get(`${objectName}.${member}`) ?? []).filter((guard) => guard.owner === holder), ({ span }) => span.fileName, near);
|
|
905
|
+
if (made)
|
|
906
|
+
return { guard: made };
|
|
907
|
+
for (const entry of this.#mapEntriesOf(objectName, member, imported ? null : site.fileName, holder)) {
|
|
908
|
+
const found = this.#findGuard(entry.name, entry.site, depth + 1, isBase);
|
|
909
|
+
if ("guard" in found)
|
|
910
|
+
return found;
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
const name = path[path.length - 1] ?? head;
|
|
914
|
+
if (from === frameworkOwner)
|
|
915
|
+
return { name, from };
|
|
916
|
+
const declared = this.#guards.get(name) ?? [];
|
|
917
|
+
const own = this.#nearest(declared.filter((other) => other.owner === (imported ? from : site.owner)), ({ span }) => span.fileName, near);
|
|
918
|
+
if (own)
|
|
919
|
+
return { guard: own };
|
|
920
|
+
if (!imported) {
|
|
921
|
+
const visible = isBase ? undefined : declared.find((other) => this.visible(other.owner, site.owner));
|
|
922
|
+
return visible ? { guard: visible } : { name, from };
|
|
923
|
+
}
|
|
924
|
+
const reexport = from && depth < maxMapDepth ? this.reexportOf(from, name, near) : null;
|
|
925
|
+
if (reexport)
|
|
926
|
+
return this.#findGuard(reexport.local, reexport.site, depth + 1, isBase);
|
|
927
|
+
const reached = from && near ? this.#throughStar(declared, from, name, near) : undefined;
|
|
928
|
+
return reached ? { guard: reached } : { name, from };
|
|
929
|
+
}
|
|
930
|
+
reexportOf(owner, name, near) {
|
|
931
|
+
const key = `${owner}
|
|
932
|
+
${name}
|
|
933
|
+
${near ?? ""}`;
|
|
934
|
+
const cached = this.#reexports.get(key);
|
|
935
|
+
if (cached !== undefined)
|
|
936
|
+
return cached;
|
|
937
|
+
let found = null;
|
|
938
|
+
const exporting = new RegExp(`export\\s*\\{(?:[^}]*?[\\s,])?${escapeRegExp(name)}(?![\\w$])`);
|
|
939
|
+
for (const fileName of this.#filesOf(owner, name, near)) {
|
|
940
|
+
const text = this.#source.readText(fileName);
|
|
941
|
+
if (text === undefined || !exporting.test(text))
|
|
942
|
+
continue;
|
|
943
|
+
const reexport = this.#reexportIn(this.#source.parse(fileName, text), name);
|
|
944
|
+
if (!reexport)
|
|
945
|
+
continue;
|
|
946
|
+
const site = { fileName, owner, imports: [] };
|
|
947
|
+
found = reexport.from ? {
|
|
948
|
+
local: reexport.name,
|
|
949
|
+
site: { ...site, imports: [{ ...reexport, imported: reexport.name, from: reexport.from }] }
|
|
950
|
+
} : { local: reexport.name, site };
|
|
951
|
+
break;
|
|
952
|
+
}
|
|
953
|
+
this.#reexports.set(key, found);
|
|
954
|
+
return found;
|
|
955
|
+
}
|
|
956
|
+
#reexportIn(source, name) {
|
|
957
|
+
const { ts, reader } = this.#source;
|
|
958
|
+
for (const statement of source.statements) {
|
|
959
|
+
if (!ts.isExportDeclaration(statement) || statement.isTypeOnly)
|
|
960
|
+
continue;
|
|
961
|
+
const { exportClause, moduleSpecifier } = statement;
|
|
962
|
+
if (!exportClause || !ts.isNamedExports(exportClause))
|
|
963
|
+
continue;
|
|
964
|
+
const element = exportClause.elements.find((item) => !item.isTypeOnly && item.name.text === name);
|
|
965
|
+
if (!element)
|
|
966
|
+
continue;
|
|
967
|
+
const original = (element.propertyName ?? element.name).text;
|
|
968
|
+
if (moduleSpecifier && ts.isStringLiteral(moduleSpecifier))
|
|
969
|
+
return { name: original, from: moduleSpecifier.text };
|
|
970
|
+
const imported = reader.importsIn(source).find((item) => item.name === original);
|
|
971
|
+
if (!imported)
|
|
972
|
+
return { name: original, from: null };
|
|
973
|
+
return imported.imported === "default" || imported.imported === "*" ? null : { name: imported.imported, from: imported.from };
|
|
974
|
+
}
|
|
975
|
+
return null;
|
|
976
|
+
}
|
|
977
|
+
#filesOf(owner, name, near) {
|
|
978
|
+
const isNear = (fileName) => {
|
|
979
|
+
const relative = this.#source.relative(fileName);
|
|
980
|
+
return !!near && (relative.startsWith(`${near}/`) || relative.replace(/\.tsx?$/, "") === near);
|
|
981
|
+
};
|
|
982
|
+
const candidates = this.filesIn(owner).filter(([, words]) => words.includes(`
|
|
983
|
+
${name}
|
|
984
|
+
`)).map(([fileName]) => fileName);
|
|
985
|
+
return [...candidates.filter(isNear), ...candidates.filter((other) => !isNear(other))];
|
|
986
|
+
}
|
|
987
|
+
#mapEntriesOf(objectName, member, fileName, owner) {
|
|
988
|
+
const key = `${objectName}
|
|
989
|
+
${member}
|
|
990
|
+
${fileName ?? ""}
|
|
991
|
+
${owner ?? ""}`;
|
|
992
|
+
const cached = this.#mapEntries.get(key);
|
|
993
|
+
if (cached)
|
|
994
|
+
return cached;
|
|
995
|
+
const { ts, reader } = this.#source;
|
|
996
|
+
const entries = [];
|
|
997
|
+
const fileNames = [
|
|
998
|
+
...fileName ? [fileName] : [],
|
|
999
|
+
...(owner === null ? [] : this.filesIn(owner)).filter(([other, words]) => other !== fileName && words.includes(`
|
|
1000
|
+
${objectName}
|
|
1001
|
+
`) && words.includes(`
|
|
1002
|
+
${member}
|
|
1003
|
+
`)).map(([other]) => other)
|
|
1004
|
+
];
|
|
1005
|
+
for (const holder of fileNames) {
|
|
1006
|
+
const text = this.#source.readText(holder);
|
|
1007
|
+
if (text === undefined)
|
|
1008
|
+
continue;
|
|
1009
|
+
const source = this.#source.parse(holder, text);
|
|
1010
|
+
const site = { fileName: holder, owner: this.#source.ownerOf(holder), imports: reader.importsIn(source) };
|
|
1011
|
+
for (const statement of source.statements) {
|
|
1012
|
+
if (!ts.isVariableStatement(statement))
|
|
1013
|
+
continue;
|
|
1014
|
+
const map = statement.declarationList.declarations.find((declaration) => ts.isIdentifier(declaration.name) && declaration.name.text === objectName)?.initializer;
|
|
1015
|
+
const object = map && reader.unwrap(map);
|
|
1016
|
+
if (!object || !ts.isObjectLiteralExpression(object))
|
|
1017
|
+
continue;
|
|
1018
|
+
for (const property of object.properties) {
|
|
1019
|
+
if (ts.isShorthandPropertyAssignment(property) && property.name.text === member)
|
|
1020
|
+
entries.push({ name: member, site });
|
|
1021
|
+
if (!ts.isPropertyAssignment(property) || !(ts.isIdentifier(property.name) || ts.isStringLiteral(property.name)) || property.name.text !== member)
|
|
1022
|
+
continue;
|
|
1023
|
+
const value = reader.unwrap(property.initializer);
|
|
1024
|
+
if (ts.isIdentifier(value))
|
|
1025
|
+
entries.push({ name: value.text, site });
|
|
1026
|
+
else if (ts.isPropertyAccessExpression(value) && ts.isIdentifier(value.expression))
|
|
1027
|
+
entries.push({ name: `${value.expression.text}.${value.name.text}`, site });
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
this.#mapEntries.set(key, entries);
|
|
1032
|
+
return entries;
|
|
1033
|
+
}
|
|
1034
|
+
declarationOf(owner, name, near, depth = 0) {
|
|
1035
|
+
const key = `${owner}
|
|
1036
|
+
${name}
|
|
1037
|
+
${near ?? ""}`;
|
|
1038
|
+
const cached = this.#declarations.get(key);
|
|
1039
|
+
if (cached !== undefined)
|
|
1040
|
+
return cached;
|
|
1041
|
+
this.#declarations.set(key, null);
|
|
1042
|
+
const declaring = declaringPatternOf(name, false);
|
|
1043
|
+
let found = null;
|
|
1044
|
+
for (const fileName of this.#filesOf(owner, name, near)) {
|
|
1045
|
+
const text = this.#source.readText(fileName);
|
|
1046
|
+
if (text === undefined || !declaring.test(text))
|
|
1047
|
+
continue;
|
|
1048
|
+
const source = this.#source.parse(fileName, text);
|
|
1049
|
+
const declared = this.#routes.declarationIn(source, name);
|
|
1050
|
+
if (declared) {
|
|
1051
|
+
found = { source, ...declared, name };
|
|
1052
|
+
break;
|
|
1053
|
+
}
|
|
1054
|
+
const reexport = this.#reexportIn(source, name);
|
|
1055
|
+
if (!reexport)
|
|
1056
|
+
continue;
|
|
1057
|
+
if (!reexport.from) {
|
|
1058
|
+
const local = this.#routes.declarationIn(source, reexport.name);
|
|
1059
|
+
found = local ? { source, ...local, name: reexport.name } : null;
|
|
1060
|
+
break;
|
|
1061
|
+
}
|
|
1062
|
+
const next = this.ownerOfSpecifier(fileName, reexport.from);
|
|
1063
|
+
if (next && next !== frameworkOwner && depth < maxMapDepth)
|
|
1064
|
+
found = this.declarationOf(next, reexport.name, this.pathOfSpecifier(fileName, reexport.from), depth + 1);
|
|
1065
|
+
break;
|
|
577
1066
|
}
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
1067
|
+
this.#declarations.set(key, found);
|
|
1068
|
+
return found;
|
|
1069
|
+
}
|
|
1070
|
+
#throughStar(declared, owner, name, near) {
|
|
1071
|
+
if (!declared.length)
|
|
1072
|
+
return;
|
|
1073
|
+
const { owners, isOpaque } = this.#starExportsOf(owner, near);
|
|
1074
|
+
const reached = declared.filter((other) => other.owner !== null && owners.includes(other.owner));
|
|
1075
|
+
return reached.length === 1 && !isOpaque && !this.declares(owner, name) ? reached[0] : undefined;
|
|
1076
|
+
}
|
|
1077
|
+
#starExportsOf(owner, near) {
|
|
1078
|
+
const cached = this.#starExports.get(near);
|
|
1079
|
+
if (cached)
|
|
1080
|
+
return cached;
|
|
1081
|
+
const found = { owners: [], isOpaque: false };
|
|
1082
|
+
this.#starExports.set(near, found);
|
|
1083
|
+
const paths = [near];
|
|
1084
|
+
for (let idx = 0;idx < paths.length && !found.isOpaque; idx++)
|
|
1085
|
+
for (const fileName of this.#modulesAt(paths[idx] ?? "")) {
|
|
1086
|
+
for (const [, specifier = ""] of this.#source.readText(fileName)?.matchAll(starExportPattern) ?? []) {
|
|
1087
|
+
const target = this.ownerOfSpecifier(fileName, specifier);
|
|
1088
|
+
const path = this.pathOfSpecifier(fileName, specifier);
|
|
1089
|
+
if (target === undefined || target === frameworkOwner || path === null)
|
|
1090
|
+
found.isOpaque = true;
|
|
1091
|
+
else if (!paths.includes(path))
|
|
1092
|
+
paths.push(path);
|
|
1093
|
+
if (target && target !== frameworkOwner && target !== owner && !found.owners.includes(target))
|
|
1094
|
+
found.owners.push(target);
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
return found;
|
|
1098
|
+
}
|
|
1099
|
+
#modulesAt(path) {
|
|
1100
|
+
if (!this.#modules) {
|
|
1101
|
+
this.#modules = new Map;
|
|
1102
|
+
for (const [fileName] of this.#source.files) {
|
|
1103
|
+
const module = this.#source.relative(fileName).replace(/\.tsx?$/, "");
|
|
1104
|
+
for (const key of module.endsWith("/index") ? [module, module.slice(0, -6)] : [module])
|
|
1105
|
+
this.#modules.set(key, [...this.#modules.get(key) ?? [], fileName]);
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
return this.#modules.get(path) ?? [];
|
|
1109
|
+
}
|
|
1110
|
+
declares(owner, name) {
|
|
1111
|
+
const declaring = declaringPatternOf(name, true);
|
|
1112
|
+
return this.#filesOf(owner, name, null).some((fileName) => declaring.test(this.#source.readText(fileName) ?? ""));
|
|
1113
|
+
}
|
|
1114
|
+
filesIn(owner) {
|
|
1115
|
+
if (!this.#ownerFiles) {
|
|
1116
|
+
this.#ownerFiles = new Map;
|
|
1117
|
+
for (const entry of this.#source.files) {
|
|
1118
|
+
const fileOwner = testFilePattern.test(entry[0]) ? null : this.#source.ownerOf(entry[0]);
|
|
1119
|
+
const files = fileOwner === null ? null : this.#ownerFiles.get(fileOwner);
|
|
1120
|
+
if (files)
|
|
1121
|
+
files.push(entry);
|
|
1122
|
+
else if (fileOwner !== null)
|
|
1123
|
+
this.#ownerFiles.set(fileOwner, [entry]);
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
return this.#ownerFiles.get(owner) ?? [];
|
|
1127
|
+
}
|
|
1128
|
+
#nearest(items, fileOf, near) {
|
|
1129
|
+
const isNear = (fileName) => {
|
|
1130
|
+
const relative = this.#source.relative(fileName);
|
|
1131
|
+
return !!near && (relative.startsWith(`${near}/`) || relative.replace(/\.tsx?$/, "") === near);
|
|
1132
|
+
};
|
|
1133
|
+
return items.find((item) => isNear(fileOf(item))) ?? items[0];
|
|
1134
|
+
}
|
|
1135
|
+
inOwner(fileName, owner) {
|
|
1136
|
+
const relative = this.#source.relative(fileName);
|
|
1137
|
+
return owner && relative.startsWith(`${owner}/`) ? relative.slice(owner.length + 1) : relative;
|
|
1138
|
+
}
|
|
1139
|
+
ownerOfSpecifier(fileName, specifier) {
|
|
1140
|
+
if (frameworkPattern.test(specifier))
|
|
1141
|
+
return frameworkOwner;
|
|
1142
|
+
const path = this.pathOfSpecifier(fileName, specifier);
|
|
1143
|
+
return path === null ? undefined : ownerOf(path) ?? undefined;
|
|
1144
|
+
}
|
|
1145
|
+
pathOfSpecifier(fileName, specifier) {
|
|
1146
|
+
const alias = specifier.match(aliasPattern);
|
|
1147
|
+
if (alias)
|
|
1148
|
+
return `${alias[1]}/${alias[2]}${specifier.slice(alias[0].length)}`;
|
|
1149
|
+
if (!specifier.startsWith("."))
|
|
1150
|
+
return null;
|
|
1151
|
+
const segments = this.#source.relative(fileName).split("/").slice(0, -1);
|
|
1152
|
+
for (const segment of specifier.split("/"))
|
|
1153
|
+
if (segment === "..")
|
|
1154
|
+
segments.pop();
|
|
1155
|
+
else if (segment !== ".")
|
|
1156
|
+
segments.push(segment);
|
|
1157
|
+
return segments.join("/");
|
|
1158
|
+
}
|
|
1159
|
+
visible(target, from) {
|
|
1160
|
+
return !from || !target || target === from || target.startsWith("libs/");
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
582
1163
|
|
|
583
1164
|
// pkgs/@akanjs/language/routeFacts.ts
|
|
584
1165
|
import { normalizeRoutePattern, tryParseRouteModuleKey } from "akanjs/common/routeConvention.ts";
|
|
@@ -634,24 +1215,23 @@ rootLayout
|
|
|
634
1215
|
}
|
|
635
1216
|
declarationIn(source, name) {
|
|
636
1217
|
const ts = this.#ts;
|
|
1218
|
+
const overloads = source.statements.filter((statement) => ts.isFunctionDeclaration(statement) && statement.name?.text === name);
|
|
1219
|
+
const [first] = overloads;
|
|
1220
|
+
const last = overloads.at(-1);
|
|
1221
|
+
if (first?.name && last)
|
|
1222
|
+
return { statement: first, declared: first.name, extent: { start: first.getStart(source), end: last.getEnd() } };
|
|
637
1223
|
for (const statement of source.statements) {
|
|
638
|
-
if (ts.isFunctionDeclaration(statement) && statement.name?.text === name)
|
|
639
|
-
return { statement, declared: statement.name, initializer: null };
|
|
640
1224
|
if (!ts.isVariableStatement(statement))
|
|
641
1225
|
continue;
|
|
642
1226
|
const declaration = statement.declarationList.declarations.find((item) => ts.isIdentifier(item.name) && item.name.text === name);
|
|
643
1227
|
if (declaration)
|
|
644
|
-
return { statement, declared: declaration.name,
|
|
1228
|
+
return { statement, declared: declaration.name, extent: this.#reader.extentOf(source, statement) };
|
|
645
1229
|
}
|
|
646
1230
|
return null;
|
|
647
1231
|
}
|
|
648
1232
|
#guardOf(source, exported) {
|
|
649
1233
|
const ts = this.#ts;
|
|
650
1234
|
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
1235
|
while (current && ts.isCallExpression(current) && ts.isPropertyAccessExpression(current.expression)) {
|
|
656
1236
|
const { name, expression } = current.expression;
|
|
657
1237
|
const [stage] = current.arguments;
|
|
@@ -663,9 +1243,20 @@ rootLayout
|
|
|
663
1243
|
}
|
|
664
1244
|
#callsIn(source, stage) {
|
|
665
1245
|
const ts = this.#ts;
|
|
666
|
-
const
|
|
1246
|
+
const imports = this.#reader.importsIn(source).filter(({ from }) => RouteFactReader.#workspacePattern.test(from));
|
|
1247
|
+
const imported = new Set(imports.filter(({ imported }) => imported !== "*").map(({ name }) => name));
|
|
1248
|
+
const namespaces = new Set(imports.filter(({ imported }) => imported === "*").map(({ name }) => name));
|
|
667
1249
|
const calls = new Map;
|
|
668
|
-
const callOf = (
|
|
1250
|
+
const callOf = (callee, shadowed) => {
|
|
1251
|
+
if (ts.isPropertyAccessExpression(callee) && ts.isIdentifier(callee.expression) && namespaces.has(callee.expression.text) && !shadowed.has(callee.expression.text)) {
|
|
1252
|
+
const name = `${callee.expression.text}.${callee.name.text}`;
|
|
1253
|
+
if (!calls.has(name))
|
|
1254
|
+
calls.set(name, { name, local: null });
|
|
1255
|
+
return true;
|
|
1256
|
+
}
|
|
1257
|
+
if (!ts.isIdentifier(callee) || shadowed.has(callee.text))
|
|
1258
|
+
return false;
|
|
1259
|
+
const name = callee.text;
|
|
669
1260
|
if (calls.has(name))
|
|
670
1261
|
return true;
|
|
671
1262
|
const declared = this.declarationIn(source, name);
|
|
@@ -675,23 +1266,71 @@ rootLayout
|
|
|
675
1266
|
name,
|
|
676
1267
|
local: declared && {
|
|
677
1268
|
span: this.#reader.spanOf(source, declared.declared),
|
|
678
|
-
extent:
|
|
1269
|
+
extent: declared.extent,
|
|
679
1270
|
doc: this.#reader.docOf(source, declared.statement)
|
|
680
1271
|
}
|
|
681
1272
|
});
|
|
682
1273
|
return true;
|
|
683
1274
|
};
|
|
684
|
-
const visit = (node) => {
|
|
685
|
-
|
|
1275
|
+
const visit = (node, outer) => {
|
|
1276
|
+
const shadowed = this.#scopeOf(node, outer);
|
|
1277
|
+
if (ts.isCallExpression(node) && callOf(node.expression, shadowed))
|
|
686
1278
|
return;
|
|
687
|
-
ts.forEachChild(node, visit);
|
|
1279
|
+
ts.forEachChild(node, (child) => visit(child, shadowed));
|
|
688
1280
|
};
|
|
689
1281
|
if (ts.isIdentifier(stage))
|
|
690
|
-
callOf(stage
|
|
1282
|
+
callOf(stage, new Set);
|
|
691
1283
|
else
|
|
692
|
-
visit(stage);
|
|
1284
|
+
visit(stage, new Set);
|
|
693
1285
|
return [...calls.values()];
|
|
694
1286
|
}
|
|
1287
|
+
#scopeOf(node, outer) {
|
|
1288
|
+
const ts = this.#ts;
|
|
1289
|
+
const names = [];
|
|
1290
|
+
const bind = (name) => {
|
|
1291
|
+
if (ts.isIdentifier(name))
|
|
1292
|
+
names.push(name.text);
|
|
1293
|
+
else
|
|
1294
|
+
for (const element of name.elements)
|
|
1295
|
+
if (!ts.isOmittedExpression(element))
|
|
1296
|
+
bind(element.name);
|
|
1297
|
+
};
|
|
1298
|
+
const declare = (statement) => {
|
|
1299
|
+
if (ts.isVariableStatement(statement))
|
|
1300
|
+
for (const declaration of statement.declarationList.declarations)
|
|
1301
|
+
bind(declaration.name);
|
|
1302
|
+
else if ((ts.isFunctionDeclaration(statement) || ts.isClassDeclaration(statement)) && statement.name)
|
|
1303
|
+
names.push(statement.name.text);
|
|
1304
|
+
};
|
|
1305
|
+
const hoist = (child) => {
|
|
1306
|
+
if (ts.isFunctionLike(child) || ts.isClassLike(child))
|
|
1307
|
+
return;
|
|
1308
|
+
if (ts.isVariableDeclarationList(child) && !(child.flags & ts.NodeFlags.BlockScoped))
|
|
1309
|
+
for (const declaration of child.declarations)
|
|
1310
|
+
bind(declaration.name);
|
|
1311
|
+
ts.forEachChild(child, hoist);
|
|
1312
|
+
};
|
|
1313
|
+
if (ts.isFunctionLike(node)) {
|
|
1314
|
+
for (const parameter of node.parameters)
|
|
1315
|
+
bind(parameter.name);
|
|
1316
|
+
if (ts.isFunctionExpression(node) && node.name)
|
|
1317
|
+
names.push(node.name.text);
|
|
1318
|
+
if ("body" in node && node.body)
|
|
1319
|
+
ts.forEachChild(node.body, hoist);
|
|
1320
|
+
} else if (ts.isClassExpression(node) && node.name)
|
|
1321
|
+
names.push(node.name.text);
|
|
1322
|
+
else if (ts.isBlock(node))
|
|
1323
|
+
node.statements.forEach(declare);
|
|
1324
|
+
else if (ts.isCaseBlock(node))
|
|
1325
|
+
for (const clause of node.clauses)
|
|
1326
|
+
clause.statements.forEach(declare);
|
|
1327
|
+
else if (ts.isCatchClause(node) && node.variableDeclaration)
|
|
1328
|
+
bind(node.variableDeclaration.name);
|
|
1329
|
+
else if ((ts.isForStatement(node) || ts.isForInStatement(node) || ts.isForOfStatement(node)) && node.initializer && ts.isVariableDeclarationList(node.initializer))
|
|
1330
|
+
for (const declaration of node.initializer.declarations)
|
|
1331
|
+
bind(declaration.name);
|
|
1332
|
+
return names.length ? new Set([...outer, ...names]) : outer;
|
|
1333
|
+
}
|
|
695
1334
|
}
|
|
696
1335
|
|
|
697
1336
|
// pkgs/@akanjs/language/akanGraph.ts
|
|
@@ -707,10 +1346,6 @@ var builtinGuards = {
|
|
|
707
1346
|
Public: { scope: "account", doc: "Passes every caller, signed in or not." },
|
|
708
1347
|
None: { scope: "account", doc: "Refuses every caller." }
|
|
709
1348
|
};
|
|
710
|
-
var frameworkOwner = "akanjs";
|
|
711
|
-
var frameworkPattern = /^akanjs(?:\/|$)/;
|
|
712
|
-
var aliasPattern = /^@(apps|libs)\/([^/]+)/;
|
|
713
|
-
var maxMapDepth = 4;
|
|
714
1349
|
|
|
715
1350
|
class GraphBuilder {
|
|
716
1351
|
#source;
|
|
@@ -718,16 +1353,15 @@ class GraphBuilder {
|
|
|
718
1353
|
#edges = new Map;
|
|
719
1354
|
#contexts = new Map;
|
|
720
1355
|
#members = new Map;
|
|
721
|
-
#guards = new Map;
|
|
722
|
-
#mapEntries = new Map;
|
|
723
1356
|
#routeFiles = new Map;
|
|
724
|
-
#declarations = new Map;
|
|
725
1357
|
#routes;
|
|
1358
|
+
#resolver;
|
|
726
1359
|
#adaptors = new Map;
|
|
727
1360
|
#plugs = new Map;
|
|
728
1361
|
constructor(source) {
|
|
729
1362
|
this.#source = source;
|
|
730
1363
|
this.#routes = new RouteFactReader(source.ts, source.reader, source.parse);
|
|
1364
|
+
this.#resolver = new GuardResolver(source, this.#routes);
|
|
731
1365
|
}
|
|
732
1366
|
build() {
|
|
733
1367
|
const { facts } = this.#source;
|
|
@@ -749,7 +1383,8 @@ class GraphBuilder {
|
|
|
749
1383
|
for (const fact of file.adaptors)
|
|
750
1384
|
for (const plug of fact.plugs)
|
|
751
1385
|
this.#addEdge(this.#key("adaptor", file.owner, fact.name), this.#adaptorIdOf(plug.target, file), "plugs", plug.name, this.#location(plug.span));
|
|
752
|
-
this.#
|
|
1386
|
+
for (const guard of this.#resolver.declare(facts))
|
|
1387
|
+
this.#guardNodeOf(guard);
|
|
753
1388
|
for (const file of facts) {
|
|
754
1389
|
const { owner } = file;
|
|
755
1390
|
const scalar = this.#source.relative(file.fileName).match(scalarPattern)?.[1];
|
|
@@ -793,31 +1428,7 @@ ${plug.name}`, this.#adaptorIdOf(plug.target, file));
|
|
|
793
1428
|
for (const query of filter.queries)
|
|
794
1429
|
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
1430
|
}
|
|
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
|
-
}
|
|
1431
|
+
this.#addGuarded(file);
|
|
821
1432
|
for (const internal of file.internals)
|
|
822
1433
|
for (const key of internal.keys)
|
|
823
1434
|
if (internal.model && key.type !== "resolveField")
|
|
@@ -833,7 +1444,7 @@ ${plug.name}`, this.#adaptorIdOf(plug.target, file));
|
|
|
833
1444
|
types.set(node.label, [...types.get(node.label) ?? [], node]);
|
|
834
1445
|
for (const node of typed)
|
|
835
1446
|
for (const field of node.fields ?? []) {
|
|
836
|
-
const candidates = (field.type ? types.get(field.type) ?? [] : []).filter((type) => this.#visible(type.owner, node.owner));
|
|
1447
|
+
const candidates = (field.type ? types.get(field.type) ?? [] : []).filter((type) => this.#resolver.visible(type.owner, node.owner));
|
|
837
1448
|
field.ref = (candidates.find((type) => type.owner === node.owner) ?? candidates[0])?.id ?? null;
|
|
838
1449
|
if (field.ref)
|
|
839
1450
|
this.#addEdge(node.id, field.ref, "field", `${field.name}${field.array ? "[]" : ""}`, field.at);
|
|
@@ -915,61 +1526,52 @@ ${label ?? ""}`;
|
|
|
915
1526
|
else
|
|
916
1527
|
this.#edges.set(key, { from, to, kind, label, count: 1, at });
|
|
917
1528
|
}
|
|
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);
|
|
1529
|
+
#addGuarded(file) {
|
|
1530
|
+
const { owner } = file;
|
|
1531
|
+
for (const slice of file.slices) {
|
|
1532
|
+
const refName = slice.model;
|
|
1533
|
+
if (!refName)
|
|
1534
|
+
continue;
|
|
1535
|
+
const fallback = this.#addNode(this.#key("slice", owner, refName), "slice", owner, refName, "", slice.span, slice.guards.map((guard) => guardLabelOf(guard, file.guardLabels)));
|
|
1536
|
+
for (const guard of slice.guards)
|
|
1537
|
+
this.#guard(fallback, guard, file);
|
|
1538
|
+
for (const key of slice.keys) {
|
|
1539
|
+
const detail = [
|
|
1540
|
+
...key.guards.map((guard) => guardLabelOf(guard, file.guardLabels)),
|
|
1541
|
+
...key.live ? ["live"] : []
|
|
1542
|
+
];
|
|
1543
|
+
const node = this.#addMember("slice", owner, refName, key, `${refName}.${key.name}`, file.fileName, detail);
|
|
1544
|
+
for (const guard of key.guards)
|
|
1545
|
+
this.#guard(node, guard, file);
|
|
950
1546
|
}
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
const
|
|
958
|
-
|
|
959
|
-
|
|
1547
|
+
}
|
|
1548
|
+
for (const endpoint of file.endpoints) {
|
|
1549
|
+
const refName = endpoint.model;
|
|
1550
|
+
if (!refName)
|
|
1551
|
+
continue;
|
|
1552
|
+
for (const key of endpoint.keys) {
|
|
1553
|
+
const detail = [
|
|
1554
|
+
...key.verb ? [key.verb] : [],
|
|
1555
|
+
...key.guards.map((guard) => guardLabelOf(guard, file.guardLabels))
|
|
1556
|
+
];
|
|
1557
|
+
const node = this.#addMember("endpoint", owner, refName, key, `${refName}.${key.name}`, file.fileName, detail);
|
|
1558
|
+
for (const guard of key.guards)
|
|
1559
|
+
this.#guard(node, guard, file);
|
|
960
1560
|
}
|
|
961
1561
|
}
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
1562
|
+
}
|
|
1563
|
+
#guard(node, guard, file) {
|
|
1564
|
+
const [, role = null, local = guard] = guard.match(/^([\w$]+) (.+)$/) ?? [];
|
|
1565
|
+
if (!local || local === "?")
|
|
1566
|
+
return;
|
|
1567
|
+
const id = local.includes("@") ? this.#madeInPlaceIdOf(local, file) : this.#guardIdOf(local, file);
|
|
1568
|
+
if (id)
|
|
1569
|
+
this.#addEdge(node.id, id, "guard", role, node.at);
|
|
970
1570
|
}
|
|
971
1571
|
#guardIdOf(local, site) {
|
|
972
|
-
const found = this.#
|
|
1572
|
+
const found = this.#resolver.find(local, site);
|
|
1573
|
+
if ("external" in found)
|
|
1574
|
+
return null;
|
|
973
1575
|
if ("guard" in found)
|
|
974
1576
|
return this.#guardNodeOf(found.guard);
|
|
975
1577
|
const { name, from } = found;
|
|
@@ -982,99 +1584,18 @@ ${label ?? ""}`;
|
|
|
982
1584
|
node.doc = builtin.doc;
|
|
983
1585
|
return node.id;
|
|
984
1586
|
}
|
|
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;
|
|
1587
|
+
#madeInPlaceIdOf(name, file) {
|
|
1588
|
+
const made = this.#resolver.madeInPlace(file.fileName, name);
|
|
1589
|
+
if (made)
|
|
1590
|
+
return this.#guardNodeOf(made);
|
|
1591
|
+
const head = name.slice(name.indexOf(":") + 1, name.indexOf("@")).split(".")[0] ?? name;
|
|
1592
|
+
const imported = file.imports.find((item) => item.name === head);
|
|
1593
|
+
const from = imported ? this.#resolver.ownerOfSpecifier(file.fileName, imported.from) : file.owner;
|
|
1594
|
+
if (from === undefined || from === frameworkOwner)
|
|
1595
|
+
return null;
|
|
1596
|
+
const scope = `${this.#resolver.inOwner(file.fileName, file.owner)}#${name}`;
|
|
1597
|
+
const label = guardLabelOf(name, file.guardLabels);
|
|
1598
|
+
return this.#addNode(this.#key("guard", file.owner, scope), "guard", file.owner, null, name, null, [], label).id;
|
|
1078
1599
|
}
|
|
1079
1600
|
#addRoutes() {
|
|
1080
1601
|
const routes = [];
|
|
@@ -1127,22 +1648,23 @@ ${dir}`, node);
|
|
|
1127
1648
|
}
|
|
1128
1649
|
#routeGuardIdOf({ name: local, local: declared }, fact, text) {
|
|
1129
1650
|
if (declared) {
|
|
1130
|
-
const
|
|
1131
|
-
const id = this.#key("guard", fact.owner, `${file}#${local}`);
|
|
1651
|
+
const id = this.#key("guard", fact.owner, `${this.#resolver.inOwner(fact.fileName, fact.owner)}#${local}`);
|
|
1132
1652
|
return this.#routeGuardNodeOf(id, fact.owner, local, declared.span, declared.doc, text, declared.extent);
|
|
1133
1653
|
}
|
|
1134
|
-
const
|
|
1135
|
-
|
|
1654
|
+
const [head = local, member] = local.split(".");
|
|
1655
|
+
const imported = fact.imports.find((item) => item.name === head);
|
|
1656
|
+
if (!imported || member && imported.imported !== "*")
|
|
1136
1657
|
return null;
|
|
1137
|
-
const name = imported.imported !== "default" && imported.imported !== "*" ? imported.imported : local;
|
|
1138
|
-
const from = this.#ownerOfSpecifier(fact.fileName, imported.from);
|
|
1658
|
+
const name = member ?? (imported.imported !== "default" && imported.imported !== "*" ? imported.imported : local);
|
|
1659
|
+
const from = this.#resolver.ownerOfSpecifier(fact.fileName, imported.from);
|
|
1139
1660
|
if (!from || from === frameworkOwner)
|
|
1140
1661
|
return null;
|
|
1141
|
-
const found = this.#declarationOf(from, name, this.#pathOfSpecifier(fact.fileName, imported.from));
|
|
1662
|
+
const found = this.#resolver.declarationOf(from, name, this.#resolver.pathOfSpecifier(fact.fileName, imported.from));
|
|
1142
1663
|
if (!found)
|
|
1143
1664
|
return null;
|
|
1144
1665
|
const { reader } = this.#source;
|
|
1145
|
-
|
|
1666
|
+
const owner = this.#source.ownerOf(found.source.fileName) ?? from;
|
|
1667
|
+
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
1668
|
}
|
|
1147
1669
|
#routeGuardNodeOf(id, owner, name, span, doc, text, extent) {
|
|
1148
1670
|
const isNew = !this.#nodes.has(id);
|
|
@@ -1155,10 +1677,10 @@ ${dir}`, node);
|
|
|
1155
1677
|
#adaptorIdOf(local, file) {
|
|
1156
1678
|
const [head, member] = local.split(".");
|
|
1157
1679
|
const imported = file.imports.find((item) => item.name === head);
|
|
1158
|
-
const from = imported ? this.#ownerOfSpecifier(file.fileName, imported.from) : undefined;
|
|
1680
|
+
const from = imported ? this.#resolver.ownerOfSpecifier(file.fileName, imported.from) : undefined;
|
|
1159
1681
|
const name = member ?? (imported && imported.imported !== "default" && imported.imported !== "*" ? imported.imported : head);
|
|
1160
1682
|
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)));
|
|
1683
|
+
const found = from === undefined ? declared.find((adaptor) => adaptor.owner === file.owner) ?? declared.find((adaptor) => this.#resolver.visible(adaptor.owner, file.owner)) : declared.find((adaptor) => adaptor.owner === from) ?? (from === frameworkOwner ? undefined : declared.find((adaptor) => this.#resolver.visible(adaptor.owner, from)));
|
|
1162
1684
|
if (found)
|
|
1163
1685
|
return this.#adaptorNodeOf(found);
|
|
1164
1686
|
const owner = from === frameworkOwner ? null : imported ? from ?? null : file.owner;
|
|
@@ -1174,38 +1696,22 @@ ${dir}`, node);
|
|
|
1174
1696
|
this.#digest(node, this.#source.readText(fileName), fact.extent);
|
|
1175
1697
|
return node.id;
|
|
1176
1698
|
}
|
|
1177
|
-
#guardNodeOf({ name, owner, span, scope, doc, parts }) {
|
|
1178
|
-
const
|
|
1699
|
+
#guardNodeOf({ name, owner, span, scope, doc, parts, file, label }) {
|
|
1700
|
+
const id = this.#key("guard", owner, file ? `${file}#${name}` : name);
|
|
1701
|
+
const node = this.#addNode(id, "guard", owner, null, name, span, scope ? [scope] : [], label ?? name);
|
|
1179
1702
|
if (doc && !node.doc)
|
|
1180
1703
|
node.doc = doc;
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1704
|
+
if (node.digest)
|
|
1705
|
+
return node.id;
|
|
1706
|
+
const digest = this.#hashOf(...parts.map(({ fileName, extent }) => [this.#source.readText(fileName), extent]));
|
|
1707
|
+
if (!digest)
|
|
1708
|
+
return node.id;
|
|
1709
|
+
node.digest = digest;
|
|
1710
|
+
const sources = [...new Set(parts.map(({ fileName }) => this.#source.relative(fileName)))].filter((source) => source !== node.at?.file);
|
|
1711
|
+
if (sources.length)
|
|
1712
|
+
node.sources = sources;
|
|
1187
1713
|
return node.id;
|
|
1188
1714
|
}
|
|
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
1715
|
#moduleDirOf(fileName) {
|
|
1210
1716
|
const dir = this.#source.relative(fileName).match(moduleDirPattern)?.[1] ?? null;
|
|
1211
1717
|
return dir?.startsWith("_") && !dir.startsWith("__") ? dir.slice(1) : dir;
|
|
@@ -1241,20 +1747,13 @@ ${dir}`, node);
|
|
|
1241
1747
|
visit(constant);
|
|
1242
1748
|
return [...fields.values()];
|
|
1243
1749
|
}
|
|
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
1750
|
#targetNodeOf({ family, owner, refName, sourceKey }) {
|
|
1252
1751
|
const isDefault = /^[A-Z]/.test(sourceKey);
|
|
1253
1752
|
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
1753
|
return id && this.#nodes.has(id) ? id : null;
|
|
1255
1754
|
}
|
|
1256
1755
|
#membersNamed(kind, name, owner, refName) {
|
|
1257
|
-
const nodes = (this.#members.get(`${kind}:${name}`) ?? []).filter((node) => (!refName || node.module === refName) && this.#visible(node.owner, owner));
|
|
1756
|
+
const nodes = (this.#members.get(`${kind}:${name}`) ?? []).filter((node) => (!refName || node.module === refName) && this.#resolver.visible(node.owner, owner));
|
|
1258
1757
|
const own = nodes.filter((node) => node.owner === owner);
|
|
1259
1758
|
return (own.length ? own : nodes).map((node) => node.id);
|
|
1260
1759
|
}
|
|
@@ -1454,6 +1953,10 @@ import {
|
|
|
1454
1953
|
sliceNames,
|
|
1455
1954
|
stateSetterName
|
|
1456
1955
|
} from "akanjs/common/akanNames.ts";
|
|
1956
|
+
var guardDetailOf = (facts, guards) => {
|
|
1957
|
+
const labels = Object.assign({}, ...facts.map((file) => file.guardLabels));
|
|
1958
|
+
return guards.length ? [`guards: ${guards.map((guard) => guardLabelOf(guard, labels)).join(", ")}`] : [];
|
|
1959
|
+
};
|
|
1457
1960
|
var capitalize2 = (text) => text.charAt(0).toUpperCase() + text.slice(1);
|
|
1458
1961
|
|
|
1459
1962
|
class NameTable {
|
|
@@ -1553,7 +2056,7 @@ class NameTable {
|
|
|
1553
2056
|
role: "sig",
|
|
1554
2057
|
gate: "sig",
|
|
1555
2058
|
label: sliceClass.name,
|
|
1556
|
-
detail:
|
|
2059
|
+
detail: guardDetailOf(facts, sliceClass.guards)
|
|
1557
2060
|
});
|
|
1558
2061
|
if (modelDoc)
|
|
1559
2062
|
this.#add({
|
|
@@ -1618,7 +2121,7 @@ class NameTable {
|
|
|
1618
2121
|
sourceKey: slice.name,
|
|
1619
2122
|
span: slice.span,
|
|
1620
2123
|
label: `slice ${refName} (default)`,
|
|
1621
|
-
detail:
|
|
2124
|
+
detail: guardDetailOf(facts, slice.guards)
|
|
1622
2125
|
};
|
|
1623
2126
|
this.#addSliceNames(defaultTarget, refName, "", modelNameSet);
|
|
1624
2127
|
for (const key of slice.keys)
|
|
@@ -1628,7 +2131,7 @@ class NameTable {
|
|
|
1628
2131
|
sourceKey: key.name,
|
|
1629
2132
|
span: key.span,
|
|
1630
2133
|
label: `slice ${key.name}`,
|
|
1631
|
-
detail: [...
|
|
2134
|
+
detail: [...guardDetailOf(facts, key.guards), ...key.live ? ["live"] : []]
|
|
1632
2135
|
}, refName, key.name, modelNameSet, slice.name);
|
|
1633
2136
|
}
|
|
1634
2137
|
if (filterClass) {
|
|
@@ -1685,7 +2188,7 @@ class NameTable {
|
|
|
1685
2188
|
role: key.verb === "pubsub" ? "subscribe" : "listen",
|
|
1686
2189
|
gate: null,
|
|
1687
2190
|
label: `${key.verb} ${key.name}`,
|
|
1688
|
-
detail:
|
|
2191
|
+
detail: guardDetailOf(facts, key.guards)
|
|
1689
2192
|
});
|
|
1690
2193
|
}
|
|
1691
2194
|
}
|
|
@@ -1743,14 +2246,12 @@ var dictionaryPattern = /\.dictionary\.ts$/;
|
|
|
1743
2246
|
var srvkitPattern = /\/srvkit\//;
|
|
1744
2247
|
var wordPattern = /[A-Za-z_$][\w$]*/g;
|
|
1745
2248
|
var identifierPattern = /^[A-Za-z_$][\w$]*$/;
|
|
1746
|
-
var regExpSpecials = /[\\^$.*+?()[\]{}|]/g;
|
|
1747
2249
|
var stateReceivers = new Set(["use", "sel", "get", "do"]);
|
|
1748
2250
|
var lensFamilies = new Set(["slice", "query", "formSetter", "storeState", "endpoint"]);
|
|
1749
2251
|
var modellessFamilies = new Set(["query", "storeState", "endpoint"]);
|
|
1750
2252
|
var gateWords = { srv: "srv", sig: "sig", db: "db", slice: "slice" };
|
|
1751
2253
|
var parseCacheSize = 64;
|
|
1752
2254
|
var pathOf = (fileName) => fileName.replace(/\\/g, "/");
|
|
1753
|
-
var escapeRegExp = (text) => text.replace(regExpSpecials, (special) => `\\${special}`);
|
|
1754
2255
|
var receiverGap = String.raw`(?:\s|\)|\/\*[\s\S]*?\*\/)*`;
|
|
1755
2256
|
|
|
1756
2257
|
class AkanIndex {
|