@akanjs/devkit 3.3.0-rc.25 → 3.3.0-rc.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +300 -0
- package/akanApp/akanApp.host.ts +11 -0
- package/akanConfig/akanBin.ts +4 -0
- package/akanConfig/akanConfig.test.ts +11 -0
- package/akanConfig/akanConfig.ts +39 -137
- package/akanConfig/akanWorkers.test.ts +39 -0
- package/akanConfig/akanWorkers.ts +32 -0
- package/akanConfig/desktopServerConfig.ts +96 -0
- package/akanConfig/dockerImage.ts +20 -0
- package/akanConfig/types.ts +1 -0
- package/app/appBin.test.ts +63 -0
- package/app/appBin.ts +50 -0
- package/app/applicationBuildReporter.test.ts +32 -0
- package/app/applicationBuildReporter.ts +4 -0
- package/app/applicationBuildRunner.test.ts +3 -0
- package/app/applicationBuildRunner.ts +40 -4
- package/app/dictionarySizes.ts +94 -0
- package/artifact/implicitRootLayout.test.ts +19 -0
- package/artifact/implicitRootLayout.ts +16 -2
- package/artifact/storeBootstrap.test.ts +28 -0
- package/artifact/storeBootstrap.ts +60 -8
- package/biome.base.json +5 -1
- package/codeAgent/skills/akan-agent/SKILL.md +2 -2
- package/codeAgent/skills/akan-deps/SKILL.md +3 -2
- package/codeAgent/skills/akan-err/SKILL.md +2 -6
- package/codeAgent/skills/akan-mcp/SKILL.md +1 -1
- package/codeAgent/skills/akan-module/SKILL.md +2 -2
- package/codeAgent/skills/akan-query/SKILL.md +13 -20
- package/codeAgent/skills/akan-service/SKILL.md +2 -5
- package/codeAgent/skills/akan-transport/SKILL.md +5 -14
- package/codeAgent/skills/akan-ui/SKILL.md +2 -5
- package/context/akanContext.ts +2 -0
- package/executors/clientRuntimeKeyOrder.fixture.json +682 -0
- package/executors/clientRuntimeKeys.fixture.json +393 -0
- package/executors/clientRuntimeKeys.test.ts +1591 -0
- package/executors/executors.test.ts +2 -0
- package/executors/sysExecutor.ts +66 -3
- package/frontendBuild/allRoutesBuilder.ts +72 -3
- package/frontendBuild/autoImportSync.ts +3 -2
- package/frontendBuild/buildRouteClient.test.ts +513 -5
- package/frontendBuild/clientBuildTypes.ts +15 -0
- package/frontendBuild/clientEntriesBundler.ts +38 -0
- package/frontendBuild/clientEntryDiscovery.test.ts +60 -1
- package/frontendBuild/clientEntryDiscovery.ts +72 -4
- package/frontendBuild/csrDevModuleCompiler.test.ts +86 -0
- package/frontendBuild/csrDevModuleCompiler.ts +44 -8
- package/frontendBuild/csrDevPatcher.ts +33 -7
- package/frontendBuild/csrDevTypes.ts +9 -0
- package/frontendBuild/devBarrelImports.test.ts +70 -0
- package/frontendBuild/devBarrelImports.ts +80 -21
- package/frontendBuild/devRegistryBundler.ts +4 -2
- package/frontendBuild/devRouteRuntime.test.ts +492 -0
- package/frontendBuild/devRouteRuntime.ts +167 -0
- package/frontendBuild/dictionaryAssetBuilder.test.ts +316 -0
- package/frontendBuild/dictionaryAssetBuilder.ts +152 -16
- package/frontendBuild/dictionaryConstants.ts +82 -0
- package/frontendBuild/dictionaryPacking.test.ts +84 -0
- package/frontendBuild/dictionaryPacking.ts +92 -0
- package/frontendBuild/dictionaryRoutes.test.ts +186 -0
- package/frontendBuild/dictionaryRoutes.ts +225 -0
- package/frontendBuild/pagesBundleBuilder.test.ts +84 -0
- package/frontendBuild/pagesBundleBuilder.ts +78 -2
- package/frontendBuild/precompressArtifacts.ts +7 -0
- package/frontendBuild/registryMetadataFingerprint.ts +3 -3
- package/frontendBuild/routeClientBuilder.ts +288 -15
- package/frontendBuild/routeHandoffs.test.ts +145 -0
- package/frontendBuild/routeHandoffs.ts +115 -0
- package/frontendBuild/routeRuntimeCheck.test.ts +257 -0
- package/frontendBuild/routeRuntimeCheck.ts +241 -0
- package/frontendBuild/routeRuntimePays.test.ts +137 -0
- package/frontendBuild/routeRuntimePays.ts +117 -0
- package/frontendBuild/ssrBaseArtifactBuilder.ts +24 -34
- package/frontendBuild/ssrDevBundler.test.ts +150 -1
- package/frontendBuild/ssrDevBundler.ts +66 -11
- package/frontendBuild/vendorFileImports.test.ts +254 -0
- package/frontendBuild/vendorFileImports.ts +163 -0
- package/frontendBuild/vendorNarrowing.ts +28 -0
- package/frontendBuild/vendorSpecifiers.ts +2 -0
- package/incrementalBuilder/incrementalBuilder.proc.test.ts +79 -0
- package/incrementalBuilder/incrementalBuilder.proc.ts +20 -1
- package/mobile/desktopBin.ts +2 -2
- package/mobile/desktopServerStage.ts +1 -0
- package/package.json +4 -3
- package/quality/dictionaryKeys.test.ts +173 -0
- package/quality/dictionaryKeys.ts +415 -0
- package/quality/objectImportReport.test.ts +36 -0
- package/quality/objectImportReport.ts +102 -0
- package/quality/qualityScanner.ts +1 -1
- package/quality/routeRuntimeReport.test.ts +91 -0
- package/quality/routeRuntimeReport.ts +121 -0
- package/quality/runtimeConstantFacts.ts +71 -0
- package/quality/runtimeConstantGraph.ts +123 -0
- package/quality/runtimeConstantReads.ts +116 -0
- package/quality/runtimeImports.test.ts +190 -0
- package/quality/runtimeImports.ts +233 -0
- package/quality/runtimeKeyNames.ts +46 -0
- package/quality/runtimeLexicalScope.ts +132 -0
- package/quality/runtimeMarkerReach.test.ts +84 -0
- package/quality/runtimeMarkerReach.ts +255 -0
- package/quality/runtimeModuleFacts.ts +290 -0
- package/quality/runtimeNameTable.test.ts +172 -0
- package/quality/runtimeNameTable.ts +477 -0
- package/quality/runtimeScopeReport.test.ts +123 -0
- package/quality/runtimeScopeReport.ts +167 -0
- package/quality/runtimeSliceKey.ts +269 -0
- package/quality/runtimeSyntax.ts +133 -0
- package/quality/runtimeTable.fixture.ts +189 -0
- package/quality/runtimeUse.test.ts +737 -0
- package/quality/runtimeUse.ts +696 -0
- package/quality/runtimeUseReadable.test.ts +393 -0
- package/quality/ssrScanner.test.ts +13 -0
- package/quality/ssrScanner.ts +1 -1
- package/test/testHelpers.ts +11 -0
- package/transforms/barrelAnalyzer.ts +24 -2
- package/transforms/barrelImportsPlugin.ts +82 -14
- package/transforms/frameworkObjects.fixture.tsx +58 -0
- package/transforms/frameworkObjects.test.ts +54 -0
- package/transforms/modelReference.test.ts +196 -0
- package/transforms/modelReference.ts +39 -0
- package/transforms/objectImportEdits.ts +150 -0
- package/transforms/objectMemberImports.test.ts +347 -0
- package/transforms/objectMemberImports.ts +537 -0
- package/transforms/objectModule.test.ts +237 -0
- package/transforms/objectModule.ts +458 -0
- package/transforms/objectModuleAnalyzer.ts +104 -0
- package/transforms/reactIconImports.test.ts +231 -0
- package/transforms/reactIconImports.ts +325 -0
- package/transforms/routeConstants.ts +122 -0
- package/transforms/routeRuntime.test.ts +835 -0
- package/transforms/routeRuntime.ts +703 -0
- package/transforms/routeRuntimeEdits.ts +92 -0
- package/transforms/routeRuntimeGraph.ts +66 -0
- package/transforms/routeRuntimePlan.ts +244 -0
- package/transforms/signalDescriptors.test.ts +280 -0
- package/transforms/signalDescriptors.ts +227 -0
- package/transforms/thisFreeExports.ts +87 -0
- package/transforms/transforms.test.ts +177 -0
- package/transforms/tsconfigPackageResolver.test.ts +1 -0
- package/workflow/executor.ts +6 -2
- package/workflow/index.ts +1 -0
- package/workflow/plan.ts +36 -7
- package/workflow/types.ts +1 -1
- package/workflow/uiTarget.ts +50 -0
- package/workspace/scanInfo.ts +1 -1
- package/workspace/workspaceLayout.ts +1 -0
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { RuntimeConstantFacts } from "./runtimeConstantFacts";
|
|
3
|
+
import type { RuntimeFamily } from "./runtimeNameTable";
|
|
4
|
+
|
|
5
|
+
export type RuntimeImportKind =
|
|
6
|
+
| "client"
|
|
7
|
+
| "useClient"
|
|
8
|
+
| "usePage"
|
|
9
|
+
| "st"
|
|
10
|
+
| "sig"
|
|
11
|
+
| "piece"
|
|
12
|
+
| "_st"
|
|
13
|
+
| "_useClient"
|
|
14
|
+
| "framework-store"
|
|
15
|
+
| "framework-client"
|
|
16
|
+
| "framework-fetch"
|
|
17
|
+
| "framework-agent"
|
|
18
|
+
| "framework-deep";
|
|
19
|
+
|
|
20
|
+
export interface RuntimeImportTarget {
|
|
21
|
+
scope: string | null;
|
|
22
|
+
kind: RuntimeImportKind;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type RuntimeHandleKind =
|
|
26
|
+
| "st"
|
|
27
|
+
| "root"
|
|
28
|
+
| "self"
|
|
29
|
+
| "state"
|
|
30
|
+
| "getter"
|
|
31
|
+
| "fetch"
|
|
32
|
+
| "signal"
|
|
33
|
+
| "akanjs/store"
|
|
34
|
+
| "akanjs/client"
|
|
35
|
+
| Exclude<RuntimeFamily, "fetch">;
|
|
36
|
+
|
|
37
|
+
export interface RuntimeHandle {
|
|
38
|
+
kind: RuntimeHandleKind;
|
|
39
|
+
members?: ReadonlySet<string>;
|
|
40
|
+
cast?: boolean;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type RuntimeBinding =
|
|
44
|
+
| { type: "handle"; handle: RuntimeHandle }
|
|
45
|
+
| { type: "rootStore" }
|
|
46
|
+
| { type: "storeFactory" }
|
|
47
|
+
| { type: "whole"; name: string }
|
|
48
|
+
| { type: "cnst"; scope: string };
|
|
49
|
+
|
|
50
|
+
export interface RuntimeConstantTarget {
|
|
51
|
+
scope: string;
|
|
52
|
+
kind: "cnst" | "barrel";
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export type RuntimeImportUse =
|
|
56
|
+
| { binding: RuntimeBinding }
|
|
57
|
+
| { reason: "unknown-name" | "whole-runtime-import"; why: string }
|
|
58
|
+
| null;
|
|
59
|
+
|
|
60
|
+
const runtimePathPattern =
|
|
61
|
+
/^((?:apps|libs)\/[^/]+)\/(client|lib\/useClient|lib\/usePage|lib\/st|lib\/sig|lib\/__client\/.+)$/;
|
|
62
|
+
const frameworkPathPattern = /^akanjs\/(store|client|fetch)(\/.+)?$/;
|
|
63
|
+
const constantPathPattern = /^((?:apps|libs)\/[^/]+)\/lib\/cnst$/;
|
|
64
|
+
const quotedPattern = /(?=(["'])((?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\n\r])*)\1)|(?=`((?:\\[\s\S]|[^\\`])*)`)/g;
|
|
65
|
+
const loaderPattern = /\brequire\b|\bimport\s*(?:\(|\/[/*])/;
|
|
66
|
+
const escapePattern = /\\(u\{[0-9a-fA-F]+\}|u[0-9a-fA-F]{4}|x[0-9a-fA-F]{2}|\r\n|[\s\S])/g;
|
|
67
|
+
const simpleEscapes: Record<string, string> = {
|
|
68
|
+
n: "\n",
|
|
69
|
+
r: "\r",
|
|
70
|
+
t: "\t",
|
|
71
|
+
b: "\b",
|
|
72
|
+
f: "\f",
|
|
73
|
+
v: "\v",
|
|
74
|
+
"0": "\0",
|
|
75
|
+
"\r\n": "",
|
|
76
|
+
"\n": "",
|
|
77
|
+
"\r": "",
|
|
78
|
+
"
": "",
|
|
79
|
+
"
": "",
|
|
80
|
+
};
|
|
81
|
+
const quietNames = new Set(["usePage", "msg", "Err"]);
|
|
82
|
+
const wholePieces = new Set(["_st", "_useClient"]);
|
|
83
|
+
//? Each of these hands out the tab's one root store or fetch client, so a name read through it is not visible here.
|
|
84
|
+
const frameworkWholeNames = new Set([
|
|
85
|
+
"RootStore",
|
|
86
|
+
"StoreRegistry",
|
|
87
|
+
"StoreInstance",
|
|
88
|
+
"base",
|
|
89
|
+
"BaseStore",
|
|
90
|
+
"StoreCatalogue",
|
|
91
|
+
"StoreSurfaceSource",
|
|
92
|
+
"ensureStoreSurface",
|
|
93
|
+
"AgentBridge",
|
|
94
|
+
"AgentContext",
|
|
95
|
+
"FetchClient",
|
|
96
|
+
]);
|
|
97
|
+
//? What `akanjs/store/agent` exports that reads the screen or the router and never the root store or the fetch client.
|
|
98
|
+
const agentScreenNames = new Set([
|
|
99
|
+
"AgentCursor",
|
|
100
|
+
"AgentPrompts",
|
|
101
|
+
"AgentVisual",
|
|
102
|
+
"AgentVisualOption",
|
|
103
|
+
"ScreenFlash",
|
|
104
|
+
"ScreenImage",
|
|
105
|
+
"ScreenImageView",
|
|
106
|
+
"ScreenReadOptions",
|
|
107
|
+
"ScreenReader",
|
|
108
|
+
"ScreenSettle",
|
|
109
|
+
"ScreenSettleOptions",
|
|
110
|
+
"ScreenTarget",
|
|
111
|
+
]);
|
|
112
|
+
|
|
113
|
+
export class RuntimeImports {
|
|
114
|
+
static readonly frameworkWholeNames: ReadonlySet<string> = frameworkWholeNames;
|
|
115
|
+
static readonly agentScreenNames: ReadonlySet<string> = agentScreenNames;
|
|
116
|
+
|
|
117
|
+
static mayUseRuntime(text: string, fileName: string) {
|
|
118
|
+
return (
|
|
119
|
+
loaderPattern.test(text) ||
|
|
120
|
+
(text.includes("sliceName") && text.includes("refName")) ||
|
|
121
|
+
RuntimeImports.#quotes(text, (specifier) => !!RuntimeImports.targetOf(specifier, fileName))
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
static mayUseConstants(text: string, fileName: string) {
|
|
126
|
+
return RuntimeImports.#quotes(
|
|
127
|
+
text,
|
|
128
|
+
(specifier) =>
|
|
129
|
+
!!RuntimeImports.constantTargetOf(specifier, fileName) || !!RuntimeConstantFacts.ownerOf(specifier, fileName),
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
static #quotes(text: string, found: (specifier: string) => boolean) {
|
|
134
|
+
for (const [, , quoted, template] of text.matchAll(quotedPattern)) {
|
|
135
|
+
const raw = quoted ?? template?.replace(/\r\n?/g, "\n") ?? "";
|
|
136
|
+
if (found(RuntimeImports.#decode(raw))) return true;
|
|
137
|
+
}
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
static targetOf(specifier: string, fileName: string): RuntimeImportTarget | null {
|
|
142
|
+
const fileScope = fileName.match(/^((?:apps|libs)\/[^/]+)\//)?.[1] ?? null;
|
|
143
|
+
if (specifier.startsWith("akanjs/")) {
|
|
144
|
+
const framework = RuntimeImports.#stripped(path.posix.normalize(specifier)).match(frameworkPathPattern);
|
|
145
|
+
const [, facet, deep] = framework ?? [];
|
|
146
|
+
if (!facet || (facet === "client" && deep && /^\/native(?:\/|$)/.test(deep))) return null;
|
|
147
|
+
const kind = !deep ? `framework-${facet}` : facet === "store" && deep === "/agent" ? "framework-agent" : null;
|
|
148
|
+
return { scope: fileScope, kind: (kind ?? "framework-deep") as RuntimeImportKind };
|
|
149
|
+
}
|
|
150
|
+
const aliased = specifier.match(/^@(apps|libs)\/([\s\S]+)$/);
|
|
151
|
+
const joined = aliased
|
|
152
|
+
? `${aliased[1]}/${aliased[2]}`
|
|
153
|
+
: specifier.startsWith(".")
|
|
154
|
+
? path.posix.join(path.posix.dirname(fileName), specifier)
|
|
155
|
+
: null;
|
|
156
|
+
const match =
|
|
157
|
+
joined === null ? null : RuntimeImports.#stripped(path.posix.normalize(joined)).match(runtimePathPattern);
|
|
158
|
+
if (!match?.[1] || !match[2]) return null;
|
|
159
|
+
const piece = match[2].match(/^lib\/__client\/(.+)$/)?.[1];
|
|
160
|
+
const kind = piece === undefined ? match[2].replace(/^lib\//, "") : wholePieces.has(piece) ? piece : "piece";
|
|
161
|
+
return { scope: match[1], kind: kind as RuntimeImportKind };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
static constantTargetOf(specifier: string, fileName: string): RuntimeConstantTarget | null {
|
|
165
|
+
const barrel = specifier.match(/^@(libs\/[^/]+)$/)?.[1];
|
|
166
|
+
if (barrel) return { scope: barrel, kind: "barrel" };
|
|
167
|
+
const aliased = specifier.match(/^@((?:apps|libs)\/[\s\S]+)$/)?.[1];
|
|
168
|
+
const joined =
|
|
169
|
+
aliased ?? (specifier.startsWith(".") ? path.posix.join(path.posix.dirname(fileName), specifier) : null);
|
|
170
|
+
const scope = joined && RuntimeImports.#stripped(path.posix.normalize(joined)).match(constantPathPattern)?.[1];
|
|
171
|
+
return scope ? { scope, kind: "cnst" } : null;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
static useOf(target: RuntimeImportTarget, imported: string, typeOnly: boolean): RuntimeImportUse {
|
|
175
|
+
const { kind } = target;
|
|
176
|
+
if (kind.startsWith("framework-")) return typeOnly ? null : RuntimeImports.#frameworkUse(kind, imported);
|
|
177
|
+
const whole = (why: string): RuntimeImportUse => ({ reason: "whole-runtime-import", why });
|
|
178
|
+
if (imported === "RootStore" && (kind === "client" || kind === "st" || kind === "_st"))
|
|
179
|
+
return typeOnly
|
|
180
|
+
? { binding: { type: "rootStore" } }
|
|
181
|
+
: whole("a value import of RootStore loads the store runtime whole");
|
|
182
|
+
if (typeOnly) return null;
|
|
183
|
+
if (kind === "sig" || kind === "_st" || kind === "_useClient")
|
|
184
|
+
return whole(`the ${kind} file holds the runtime of every module`);
|
|
185
|
+
if (kind === "piece") return whole("a runtime piece imported by hand is not read name by name");
|
|
186
|
+
const handle = (handleKind: RuntimeHandleKind): RuntimeImportUse => ({
|
|
187
|
+
binding: { type: "handle", handle: { kind: handleKind } },
|
|
188
|
+
});
|
|
189
|
+
if ((imported === "fetch" || imported === "sig") && kind !== "st") return handle(imported);
|
|
190
|
+
if (imported === "st" && kind !== "useClient") return handle("st");
|
|
191
|
+
if (kind === "st" || (kind === "client" && imported === "store"))
|
|
192
|
+
return whole(`${imported} holds every module's store`);
|
|
193
|
+
if (kind !== "client" ? quietNames.has(imported) : quietNames.has(imported) || imported === "cnst") return null;
|
|
194
|
+
if (kind === "client" && /^[A-Z]/.test(imported)) return null;
|
|
195
|
+
return { reason: "unknown-name", why: `${imported} reaches past the names a module answers` };
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
static #frameworkUse(kind: RuntimeImportKind, imported: string): RuntimeImportUse {
|
|
199
|
+
if (kind === "framework-agent" && agentScreenNames.has(imported)) return null;
|
|
200
|
+
if (kind === "framework-deep" || kind === "framework-agent")
|
|
201
|
+
return { reason: "whole-runtime-import", why: "a framework internal may hand out the whole runtime" };
|
|
202
|
+
if (frameworkWholeNames.has(imported)) return { binding: { type: "whole", name: imported } };
|
|
203
|
+
if (kind === "framework-store" && imported === "st") return { binding: { type: "handle", handle: { kind: "st" } } };
|
|
204
|
+
if (kind === "framework-store" && imported === "store") return { binding: { type: "storeFactory" } };
|
|
205
|
+
if (kind === "framework-client" && (imported === "fetch" || imported === "sig"))
|
|
206
|
+
return { binding: { type: "handle", handle: { kind: imported } } };
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
static namespaceUse(kind: RuntimeImportKind, name: string): RuntimeImportUse {
|
|
211
|
+
if (kind === "framework-store" || kind === "framework-client")
|
|
212
|
+
return {
|
|
213
|
+
binding: { type: "handle", handle: { kind: kind === "framework-store" ? "akanjs/store" : "akanjs/client" } },
|
|
214
|
+
};
|
|
215
|
+
if (kind === "framework-fetch") return { binding: { type: "whole", name } };
|
|
216
|
+
return { reason: "whole-runtime-import", why: "a namespace or default import holds the whole runtime" };
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
static #stripped(resolved: string) {
|
|
220
|
+
return resolved.replace(/\.(?:tsx?|jsx?)$/, "").replace(/\/index$/, "");
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
static #decode(raw: string) {
|
|
224
|
+
return raw.replace(escapePattern, (_, escape: string) => {
|
|
225
|
+
if (escape.startsWith("u{")) {
|
|
226
|
+
const code = Number.parseInt(escape.slice(2, -1), 16);
|
|
227
|
+
return code <= 0x10ffff ? String.fromCodePoint(code) : "";
|
|
228
|
+
}
|
|
229
|
+
if (escape.length > 1 && /^[ux]/.test(escape)) return String.fromCharCode(Number.parseInt(escape.slice(1), 16));
|
|
230
|
+
return simpleEscapes[escape] ?? escape;
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import type { RuntimeLexicalScope } from "./runtimeLexicalScope";
|
|
3
|
+
import { staticName } from "./runtimeSyntax";
|
|
4
|
+
|
|
5
|
+
const maxDepth = 8;
|
|
6
|
+
|
|
7
|
+
export class RuntimeKeyNames {
|
|
8
|
+
readonly #lexical: RuntimeLexicalScope;
|
|
9
|
+
|
|
10
|
+
constructor(lexical: RuntimeLexicalScope) {
|
|
11
|
+
this.#lexical = lexical;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
//? An assertion to literal names wins over the value under it: the author states the key behaves as that name.
|
|
15
|
+
namesOf(expression: ts.Expression, depth = 0): string[] | null {
|
|
16
|
+
let value = expression;
|
|
17
|
+
while (ts.isParenthesizedExpression(value) || ts.isNonNullExpression(value)) value = value.expression;
|
|
18
|
+
if (ts.isStringLiteralLike(value) || ts.isNumericLiteral(value)) return [value.text];
|
|
19
|
+
if (ts.isAsExpression(value) || ts.isTypeAssertionExpression(value) || ts.isSatisfiesExpression(value))
|
|
20
|
+
return RuntimeKeyNames.#literalsOf(value.type) ?? this.namesOf(value.expression, depth);
|
|
21
|
+
const initializer = ts.isIdentifier(value) && depth < maxDepth ? this.constantOf(value) : null;
|
|
22
|
+
return initializer ? this.namesOf(initializer, depth + 1) : null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
ofProperty(name: ts.PropertyName | ts.BindingName): string[] | null {
|
|
26
|
+
if (ts.isComputedPropertyName(name)) return this.namesOf(name.expression);
|
|
27
|
+
const text = staticName(name);
|
|
28
|
+
return text === null ? null : [text];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
constantOf(identifier: ts.Identifier): ts.Expression | null {
|
|
32
|
+
const declaration = this.#lexical.resolve(identifier)?.parent;
|
|
33
|
+
if (!declaration || !ts.isVariableDeclaration(declaration) || !ts.isIdentifier(declaration.name)) return null;
|
|
34
|
+
const list = declaration.parent;
|
|
35
|
+
const constant = ts.isVariableDeclarationList(list) && !!(list.flags & ts.NodeFlags.Const);
|
|
36
|
+
return constant ? (declaration.initializer ?? null) : null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static #literalsOf(type: ts.TypeNode): string[] | null {
|
|
40
|
+
if (ts.isParenthesizedTypeNode(type)) return RuntimeKeyNames.#literalsOf(type.type);
|
|
41
|
+
if (ts.isLiteralTypeNode(type)) return ts.isStringLiteralLike(type.literal) ? [type.literal.text] : null;
|
|
42
|
+
if (!ts.isUnionTypeNode(type)) return null;
|
|
43
|
+
const names = type.types.map((member) => RuntimeKeyNames.#literalsOf(member));
|
|
44
|
+
return names.every((member): member is string[] => member !== null) ? [...new Set(names.flat())] : null;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
export type RuntimeThisOwner =
|
|
4
|
+
| { kind: "class"; node: ts.ClassLikeDeclaration; isStatic: boolean }
|
|
5
|
+
| { kind: "function"; node: ts.SignatureDeclaration };
|
|
6
|
+
|
|
7
|
+
export class RuntimeLexicalScope {
|
|
8
|
+
readonly #declared = new WeakMap<ts.Node, Map<string, ts.Identifier>>();
|
|
9
|
+
|
|
10
|
+
resolve(identifier: ts.Identifier): ts.Identifier | null {
|
|
11
|
+
for (let node: ts.Node | undefined = identifier.parent; node; node = node.parent) {
|
|
12
|
+
if (ts.isFunctionLike(node) && RuntimeLexicalScope.evaluatedOutside(node, identifier)) continue;
|
|
13
|
+
const names = this.#namesOf(node);
|
|
14
|
+
const found = names?.get(identifier.text);
|
|
15
|
+
if (found) return found;
|
|
16
|
+
}
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
static thisOwnerOf(node: ts.Node): RuntimeThisOwner | null {
|
|
21
|
+
for (let at: ts.Node | undefined = node.parent; at; at = at.parent) {
|
|
22
|
+
if (ts.isArrowFunction(at) || ts.isClassLike(at)) continue;
|
|
23
|
+
if (ts.isClassElement(at) && ts.isClassLike(at.parent)) {
|
|
24
|
+
if (RuntimeLexicalScope.evaluatedOutside(at, node)) continue;
|
|
25
|
+
const isStatic =
|
|
26
|
+
ts.isClassStaticBlockDeclaration(at) || !!(ts.getCombinedModifierFlags(at) & ts.ModifierFlags.Static);
|
|
27
|
+
return { kind: "class", node: at.parent, isStatic };
|
|
28
|
+
}
|
|
29
|
+
if (ts.isFunctionLike(at)) return { kind: "function", node: at };
|
|
30
|
+
if (ts.isSourceFile(at) || ts.isModuleBlock(at)) return null;
|
|
31
|
+
}
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
//? A computed member name and a decorator run where the member is defined, outside its own scope and `this`.
|
|
36
|
+
static evaluatedOutside(member: ts.Node, node: ts.Node) {
|
|
37
|
+
const within = (outer: ts.Node) => node.pos >= outer.pos && node.end <= outer.end;
|
|
38
|
+
const name = (member as ts.NamedDeclaration).name;
|
|
39
|
+
if (name && ts.isComputedPropertyName(name) && within(name)) return true;
|
|
40
|
+
const parameters = ts.isFunctionLike(member) ? member.parameters : [];
|
|
41
|
+
return [member, ...parameters].some(
|
|
42
|
+
(decorated) => ts.canHaveDecorators(decorated) && (ts.getDecorators(decorated) ?? []).some(within),
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
static isReference(identifier: ts.Identifier) {
|
|
47
|
+
const parent = identifier.parent;
|
|
48
|
+
if (ts.isPropertyAccessExpression(parent)) return parent.expression === identifier;
|
|
49
|
+
if (ts.isQualifiedName(parent)) return parent.left === identifier;
|
|
50
|
+
if (ts.isBindingElement(parent)) return parent.initializer === identifier;
|
|
51
|
+
if (ts.isPropertyAssignment(parent) || ts.isPropertyDeclaration(parent) || ts.isParameter(parent))
|
|
52
|
+
return parent.initializer === identifier;
|
|
53
|
+
if (ts.isVariableDeclaration(parent) || ts.isEnumMember(parent)) return parent.initializer === identifier;
|
|
54
|
+
if (
|
|
55
|
+
ts.isImportSpecifier(parent) ||
|
|
56
|
+
ts.isImportClause(parent) ||
|
|
57
|
+
ts.isNamespaceImport(parent) ||
|
|
58
|
+
ts.isExportSpecifier(parent) ||
|
|
59
|
+
ts.isJsxAttribute(parent) ||
|
|
60
|
+
ts.isLabeledStatement(parent) ||
|
|
61
|
+
ts.isBreakOrContinueStatement(parent) ||
|
|
62
|
+
ts.isTypeParameterDeclaration(parent)
|
|
63
|
+
)
|
|
64
|
+
return false;
|
|
65
|
+
if (
|
|
66
|
+
(ts.isFunctionLike(parent) ||
|
|
67
|
+
ts.isClassLike(parent) ||
|
|
68
|
+
ts.isInterfaceDeclaration(parent) ||
|
|
69
|
+
ts.isTypeAliasDeclaration(parent) ||
|
|
70
|
+
ts.isEnumDeclaration(parent) ||
|
|
71
|
+
ts.isModuleDeclaration(parent) ||
|
|
72
|
+
ts.isPropertySignature(parent) ||
|
|
73
|
+
ts.isGetAccessor(parent) ||
|
|
74
|
+
ts.isSetAccessor(parent)) &&
|
|
75
|
+
(parent as ts.NamedDeclaration).name === identifier
|
|
76
|
+
)
|
|
77
|
+
return false;
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
#namesOf(node: ts.Node) {
|
|
82
|
+
const cached = this.#declared.get(node);
|
|
83
|
+
if (cached) return cached;
|
|
84
|
+
const names = RuntimeLexicalScope.#collect(node);
|
|
85
|
+
if (names) this.#declared.set(node, names);
|
|
86
|
+
return names;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
static #collect(node: ts.Node): Map<string, ts.Identifier> | null {
|
|
90
|
+
const names = new Map<string, ts.Identifier>();
|
|
91
|
+
const addBinding = (name: ts.BindingName) => {
|
|
92
|
+
if (ts.isIdentifier(name)) names.set(name.text, name);
|
|
93
|
+
else for (const element of name.elements) if (!ts.isOmittedExpression(element)) addBinding(element.name);
|
|
94
|
+
};
|
|
95
|
+
const addStatements = (statements: readonly ts.Statement[]) => {
|
|
96
|
+
for (const statement of statements) {
|
|
97
|
+
if (ts.isVariableStatement(statement))
|
|
98
|
+
for (const declaration of statement.declarationList.declarations) addBinding(declaration.name);
|
|
99
|
+
else if (
|
|
100
|
+
(ts.isFunctionDeclaration(statement) ||
|
|
101
|
+
ts.isClassDeclaration(statement) ||
|
|
102
|
+
ts.isEnumDeclaration(statement)) &&
|
|
103
|
+
statement.name
|
|
104
|
+
)
|
|
105
|
+
names.set(statement.name.text, statement.name);
|
|
106
|
+
else if (ts.isImportDeclaration(statement)) RuntimeLexicalScope.#addImport(statement, names);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
if (ts.isSourceFile(node) || ts.isBlock(node) || ts.isModuleBlock(node)) addStatements(node.statements);
|
|
110
|
+
else if (ts.isCaseBlock(node)) for (const clause of node.clauses) addStatements(clause.statements);
|
|
111
|
+
else if (ts.isFunctionLike(node)) {
|
|
112
|
+
for (const parameter of node.parameters) addBinding(parameter.name);
|
|
113
|
+
if (ts.isFunctionExpression(node) && node.name) names.set(node.name.text, node.name);
|
|
114
|
+
} else if (ts.isClassExpression(node) && node.name) names.set(node.name.text, node.name);
|
|
115
|
+
else if (ts.isForStatement(node) || ts.isForInStatement(node) || ts.isForOfStatement(node)) {
|
|
116
|
+
const initializer = node.initializer;
|
|
117
|
+
if (initializer && ts.isVariableDeclarationList(initializer))
|
|
118
|
+
for (const declaration of initializer.declarations) addBinding(declaration.name);
|
|
119
|
+
} else if (ts.isCatchClause(node) && node.variableDeclaration) addBinding(node.variableDeclaration.name);
|
|
120
|
+
else return null;
|
|
121
|
+
return names;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
static #addImport(statement: ts.ImportDeclaration, names: Map<string, ts.Identifier>) {
|
|
125
|
+
const clause = statement.importClause;
|
|
126
|
+
if (!clause) return;
|
|
127
|
+
if (clause.name) names.set(clause.name.text, clause.name);
|
|
128
|
+
const bindings = clause.namedBindings;
|
|
129
|
+
if (bindings && ts.isNamespaceImport(bindings)) names.set(bindings.name.text, bindings.name);
|
|
130
|
+
else if (bindings) for (const element of bindings.elements) names.set(element.name.text, element.name);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { RuntimeMarkerReach } from "./runtimeMarkerReach";
|
|
3
|
+
|
|
4
|
+
const root = "/ws";
|
|
5
|
+
const framework = {
|
|
6
|
+
"node_modules/akanjs/ui/index.ts":
|
|
7
|
+
'export { Field } from "./Field";\nexport { Button } from "./Button";\nexport type { FieldProps } from "./Field";\n',
|
|
8
|
+
"node_modules/akanjs/ui/Button.tsx": '"use client";\nexport const Button = () => null;\n',
|
|
9
|
+
"node_modules/akanjs/ui/Field/index.tsx":
|
|
10
|
+
'"use client";\nimport { Parent } from "./Relation";\nexport interface FieldProps {}\nexport const Field = { Parent };\n',
|
|
11
|
+
"node_modules/akanjs/ui/Field/Relation.tsx":
|
|
12
|
+
'"use client";\nimport { useRelationOptions } from "./useRelationOptions";\nexport const Parent = () => useRelationOptions();\n',
|
|
13
|
+
"node_modules/akanjs/ui/Field/useRelationOptions.ts":
|
|
14
|
+
'"use client";\nimport "../modelRuntime";\nexport const useRelationOptions = () => null;\n',
|
|
15
|
+
"node_modules/akanjs/ui/modelRuntime.ts": "export {};\n",
|
|
16
|
+
};
|
|
17
|
+
const pinsOf = (files: Record<string, string>) => {
|
|
18
|
+
const all: Record<string, string> = { ...framework, ...files };
|
|
19
|
+
const reach = new RuntimeMarkerReach(root, (absPath) => all[absPath.slice(root.length + 1)] ?? null);
|
|
20
|
+
return reach.pinsOf(Object.keys(files).sort());
|
|
21
|
+
};
|
|
22
|
+
const relationChain = [
|
|
23
|
+
"akanjs/ui/Field/index.tsx",
|
|
24
|
+
"akanjs/ui/Field/Relation.tsx",
|
|
25
|
+
"akanjs/ui/Field/useRelationOptions.ts",
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
describe("the pages a framework component reading models by name makes load the whole runtime", () => {
|
|
29
|
+
test("names the imports from a page, through a lib's re-export, to the framework file importing the marker", () => {
|
|
30
|
+
const pins = pinsOf({
|
|
31
|
+
"apps/demo/page/memo/_index.tsx":
|
|
32
|
+
'import { MemoEdit } from "@apps/demo/lib/memo/Memo.Template";\nexport default () => MemoEdit;\n',
|
|
33
|
+
"apps/demo/page/about.tsx":
|
|
34
|
+
'"use client";\nimport { Button, cnst } from "@libs/kit/ui";\nexport default () => [Button, cnst];\n',
|
|
35
|
+
"apps/demo/lib/memo/Memo.Template.tsx":
|
|
36
|
+
'"use client";\nimport { Field, type FieldProps } from "@libs/kit/ui";\nexport const MemoEdit = () => Field;\n',
|
|
37
|
+
"libs/kit/ui/index.ts": 'export * as cnst from "./cnst";\nexport * from "./shim";\n',
|
|
38
|
+
"libs/kit/ui/cnst.ts": "export const memo = 1;\n",
|
|
39
|
+
"libs/kit/ui/shim.ts": 'export { Button, Field, type FieldProps } from "akanjs/ui";\n',
|
|
40
|
+
});
|
|
41
|
+
expect(pins).toEqual([
|
|
42
|
+
{
|
|
43
|
+
page: "apps/demo/page/memo/_index.tsx",
|
|
44
|
+
chain: ["apps/demo/page/memo/_index.tsx", "apps/demo/lib/memo/Memo.Template.tsx", ...relationChain],
|
|
45
|
+
},
|
|
46
|
+
]);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("counts a layout above the page, and neither a type import nor a chain no client file is on", () => {
|
|
50
|
+
const pins = pinsOf({
|
|
51
|
+
"apps/demo/page/_layout.tsx": 'import { Field } from "akanjs/ui";\nexport default () => Field;\n',
|
|
52
|
+
"apps/demo/page/admin/_layout.tsx": 'import type { FieldProps } from "akanjs/ui";\nexport default () => null;\n',
|
|
53
|
+
"apps/demo/page/admin/user.tsx": 'import { FieldProps } from "akanjs/ui";\nexport default () => null;\n',
|
|
54
|
+
"apps/demo/page/doc.tsx": 'import { marked } from "../ui/marked";\nexport default () => marked;\n',
|
|
55
|
+
"apps/demo/ui/marked.ts": 'import "akanjs/ui/modelRuntime";\nexport const marked = 1;\n',
|
|
56
|
+
"libs/kit/page/plain.tsx": "export default () => null;\n",
|
|
57
|
+
});
|
|
58
|
+
const viaLayout = ["apps/demo/page/_layout.tsx", ...relationChain];
|
|
59
|
+
expect(pins).toEqual([
|
|
60
|
+
{ page: "apps/demo/page/admin/user.tsx", chain: viaLayout },
|
|
61
|
+
{ page: "apps/demo/page/doc.tsx", chain: viaLayout },
|
|
62
|
+
]);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("leaves the pages of an app with a build report to that report, which names the routes really held", () => {
|
|
66
|
+
const pins = ["apps/demo/page/a.tsx", "apps/demonstration/page/a.tsx", "libs/kit/page/a.tsx"].map((page) => ({
|
|
67
|
+
page,
|
|
68
|
+
chain: [page, ...relationChain],
|
|
69
|
+
}));
|
|
70
|
+
expect(RuntimeMarkerReach.unbuilt(pins, ["demo"]).map(({ page }) => page)).toEqual([
|
|
71
|
+
"apps/demonstration/page/a.tsx",
|
|
72
|
+
"libs/kit/page/a.tsx",
|
|
73
|
+
]);
|
|
74
|
+
expect(RuntimeMarkerReach.unbuilt(pins, [])).toEqual(pins);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("prints one note per file importing the framework component, with how many pages it holds", () => {
|
|
78
|
+
const pages = ["a", "b", "c", "d"].map((name) => `apps/demo/page/${name}.tsx`);
|
|
79
|
+
const chain = ["apps/demo/ui/Edit.tsx", ...relationChain];
|
|
80
|
+
expect(RuntimeMarkerReach.format(pages.map((page) => ({ page, chain: [page, ...chain] })))).toEqual([
|
|
81
|
+
"apps/demo/ui/Edit.tsx - note akan.runtime.framework-marker: reaches a framework component that reads models by name (apps/demo/ui/Edit.tsx → akanjs/ui/Field/index.tsx → akanjs/ui/Field/Relation.tsx → akanjs/ui/Field/useRelationOptions.ts). 4 pages load the whole client runtime through it: apps/demo/page/a.tsx, apps/demo/page/b.tsx, apps/demo/page/c.tsx and 1 more.",
|
|
82
|
+
]);
|
|
83
|
+
});
|
|
84
|
+
});
|