@akanjs/devkit 3.3.0-rc.37 → 3.3.0-rc.39
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 +65 -0
- package/app/applicationBuildRunner.ts +7 -1
- package/app/dictionarySizes.ts +2 -5
- package/app/routeFilesReport.test.ts +59 -0
- package/app/routeFilesReport.ts +46 -0
- package/app/routeJsBudget.ts +18 -4
- package/artifact/storeBootstrap.ts +5 -1
- package/biome.base.json +1 -3
- package/codeAgent/skills/akan-page/SKILL.md +28 -7
- package/executors/clientRuntimeKeys.test.ts +72 -1
- package/executors/sysExecutor.ts +10 -8
- package/frontendBuild/allRoutesBuilder.ts +20 -8
- package/frontendBuild/buildRouteClient.test.ts +65 -0
- package/frontendBuild/clientBuildTypes.ts +11 -0
- package/frontendBuild/clientEntriesBundler.ts +21 -11
- package/frontendBuild/clientEntryDiscovery.test.ts +213 -0
- package/frontendBuild/clientEntryDiscovery.ts +49 -8
- package/frontendBuild/devBarrelImports.test.ts +54 -0
- package/frontendBuild/devBarrelImports.ts +1 -0
- package/frontendBuild/dictionaryAssetBuilder.test.ts +12 -4
- package/frontendBuild/dictionaryAssetBuilder.ts +20 -7
- package/frontendBuild/dictionaryConstants.ts +55 -1
- package/frontendBuild/dictionaryPacking.test.ts +58 -0
- package/frontendBuild/dictionaryPacking.ts +43 -0
- package/frontendBuild/dictionaryRoutes.test.ts +65 -0
- package/frontendBuild/dictionaryRoutes.ts +3 -1
- package/frontendBuild/precompressArtifacts.test.ts +22 -0
- package/frontendBuild/precompressArtifacts.ts +18 -11
- package/frontendBuild/renderedEntries.ts +144 -0
- package/frontendBuild/routeClientBuilder.ts +65 -27
- package/frontendBuild/routeHandoffs.test.ts +10 -0
- package/frontendBuild/routeHandoffs.ts +3 -1
- package/frontendBuild/routeModulePreloads.test.ts +32 -0
- package/frontendBuild/routeModulePreloads.ts +20 -11
- package/frontendBuild/routeRuntimeCheck.test.ts +23 -0
- package/frontendBuild/routeRuntimeCheck.ts +10 -3
- package/frontendBuild/routeRuntimePays.test.ts +7 -1
- package/lint/biomeBase.test.ts +23 -3
- package/package.json +3 -3
- package/quality/dictionaryKeys.test.ts +185 -2
- package/quality/dictionaryKeys.ts +82 -9
- package/quality/objectImportReport.ts +6 -3
- package/quality/translatorBindings.ts +187 -0
- package/transforms/barrelImportsPlugin.ts +56 -35
- package/transforms/exportReach.test.ts +162 -0
- package/transforms/exportReach.ts +308 -0
- package/transforms/moduleParts.test.ts +225 -0
- package/transforms/moduleParts.ts +418 -0
- package/transforms/objectMemberImports.test.ts +289 -7
- package/transforms/objectMemberImports.ts +92 -58
- package/transforms/objectMemberLookup.ts +150 -0
- package/transforms/objectModule.test.ts +72 -2
- package/transforms/objectModule.ts +31 -7
- package/transforms/partImports.test.ts +76 -0
- package/transforms/partImports.ts +110 -0
- package/transforms/partModules.test.ts +187 -0
- package/transforms/partModules.ts +90 -0
- package/transforms/partPurity.ts +124 -0
- package/workspace/scanInfo.ts +24 -0
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import { DictionaryKeys } from "./dictionaryKeys";
|
|
2
|
+
import { DictionaryKeys, type DictionaryStaticKeys } from "./dictionaryKeys";
|
|
3
3
|
|
|
4
|
-
const read = (source: string, fileName = "apps/demo/lib/memo/Memo.Unit.tsx") =>
|
|
4
|
+
const read = (source: string, fileName = "apps/demo/lib/memo/Memo.Unit.tsx", staticKeys?: DictionaryStaticKeys) =>
|
|
5
|
+
DictionaryKeys.read(source, fileName, staticKeys);
|
|
5
6
|
|
|
6
7
|
describe("DictionaryKeys", () => {
|
|
7
8
|
test("follows a literal key, a template whose group is written, and a key chosen from a table the file holds", () => {
|
|
@@ -153,6 +154,188 @@ describe("DictionaryKeys", () => {
|
|
|
153
154
|
expect(understood.unread).toEqual([]);
|
|
154
155
|
});
|
|
155
156
|
|
|
157
|
+
test("does not take a parameter or a local named l that holds a plain value for the translator", () => {
|
|
158
|
+
const uses = [
|
|
159
|
+
"return values.map((l) => l.trim());",
|
|
160
|
+
"return rows.map((l) => l.id);",
|
|
161
|
+
"return values.map((l) => format(l));",
|
|
162
|
+
"return rows.toSorted((l, r) => l.order - r.order);",
|
|
163
|
+
"return values.reduce((sum, l) => sum + count(l), 0);",
|
|
164
|
+
"return rows.map(([l, r]) => join(l, r));",
|
|
165
|
+
"const l = line.length; return pad(l);",
|
|
166
|
+
"const l = line + ' '; return [l, line];",
|
|
167
|
+
"let l = 0; l += line.length; l = l * 2; return pad(l);",
|
|
168
|
+
"for (const l of values) send(l); return null;",
|
|
169
|
+
"for (const l in rows) send(l); return null;",
|
|
170
|
+
"for (let l = 0; l < 3; l++) send(l); return null;",
|
|
171
|
+
"const pad = (l: number) => fill(l); return pad(2);",
|
|
172
|
+
"setRows((l) => l.filter(Boolean)); return null;",
|
|
173
|
+
"const l = rows.at(0); return l.id ?? l[0];",
|
|
174
|
+
"function width(l) { return l.length; } return width(line);",
|
|
175
|
+
];
|
|
176
|
+
for (const use of uses) {
|
|
177
|
+
const keys = read(`export const A = ({ values, rows, line }) => { ${use} };`);
|
|
178
|
+
expect(keys.unread, use).toEqual([]);
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test("reads the translator beside a name that shadows it, and each use by the binding it reaches", () => {
|
|
183
|
+
const keys = read(`
|
|
184
|
+
export const Lines = ({ lines }: { lines: string[] }) => {
|
|
185
|
+
const { l } = usePage();
|
|
186
|
+
const widths = lines.map((l) => l.length);
|
|
187
|
+
return <>{l("memo.title")}{lines.map((l) => <li>{l}</li>)}{widths}</>;
|
|
188
|
+
};
|
|
189
|
+
`);
|
|
190
|
+
expect(keys.unread).toEqual([]);
|
|
191
|
+
expect(keys.names).toContain("memo");
|
|
192
|
+
const handedOn = read(
|
|
193
|
+
[
|
|
194
|
+
`export const Lines = ({ lines }: { lines: string[] }) => {`,
|
|
195
|
+
` const { l: t } = usePage();`,
|
|
196
|
+
` const trimmed = lines.map((t) => t.trim());`,
|
|
197
|
+
` return <>{format(t, trimmed)}</>;`,
|
|
198
|
+
`};`,
|
|
199
|
+
].join("\n"),
|
|
200
|
+
);
|
|
201
|
+
expect(handedOn.unread).toEqual([{ line: 4, why: "t passes the translator on" }]);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
test("still takes a parameter or a local for the translator when it may be one", () => {
|
|
205
|
+
const uses = [
|
|
206
|
+
"const labelOf = (l: Translator, name: string) => l(name);",
|
|
207
|
+
"const headOf = (l: Translator) => dictText(l, 'memo.title');",
|
|
208
|
+
"const headOf = (l) => dictText(l, 'memo.title');",
|
|
209
|
+
"function headOf(l) { return dictText(l, 'memo.title'); }",
|
|
210
|
+
"withPage((l) => l(name));",
|
|
211
|
+
"withPage((l) => dictText(l, 'memo.title'));",
|
|
212
|
+
"translators.map((l) => l(name));",
|
|
213
|
+
"translators.forEach((l) => l._(name));",
|
|
214
|
+
"translators.forEach((l) => l['rich'](name));",
|
|
215
|
+
"translators.forEach((l) => l.call(null, name));",
|
|
216
|
+
"wrappers.reduce((l, wrap) => wrap(l), translator);",
|
|
217
|
+
"const l = pick(); helper(l);",
|
|
218
|
+
"const l = page.l; helper(l);",
|
|
219
|
+
"let l = 0; l = pick(); helper(l);",
|
|
220
|
+
"let l = 0; ({ l } = page); helper(l);",
|
|
221
|
+
"let l = 0; [l] = translators; helper(l);",
|
|
222
|
+
"translators.map((l) => { l ??= pick(); return helper(l); });",
|
|
223
|
+
"const { l } = page; helper(l.id);",
|
|
224
|
+
"for (const l of translators) l(name);",
|
|
225
|
+
"setRows((l) => [...l, name]);",
|
|
226
|
+
];
|
|
227
|
+
for (const use of uses) {
|
|
228
|
+
const keys = read(`export const A = ({ name, page, translators, wrappers }) => { ${use} return null; };`);
|
|
229
|
+
expect(keys.unread.length, use).toBeGreaterThan(0);
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
test("follows a key held in a variable that may be null, undefined or false", () => {
|
|
234
|
+
const note = (declared: string, shown = "noteKey ? <p>{l(noteKey)}</p> : null") =>
|
|
235
|
+
read(`
|
|
236
|
+
export const Note = ({ denied, outside, fallback }: { denied: boolean; outside: boolean; fallback: string }) => {
|
|
237
|
+
const { l } = usePage();
|
|
238
|
+
${declared}
|
|
239
|
+
return ${shown};
|
|
240
|
+
};
|
|
241
|
+
`);
|
|
242
|
+
const held = [
|
|
243
|
+
`const noteKey = denied ? "weather.locationDenied" : outside ? "weather.locationOutside" : null;`,
|
|
244
|
+
`const noteKey = denied ? "weather.locationDenied" : undefined;`,
|
|
245
|
+
`const noteKey = !outside && (denied ? "weather.locationDenied" : false);`,
|
|
246
|
+
];
|
|
247
|
+
for (const declared of held) {
|
|
248
|
+
const keys = note(declared);
|
|
249
|
+
expect(keys.unread, declared).toEqual([]);
|
|
250
|
+
expect(keys.names, declared).toContain("weather");
|
|
251
|
+
}
|
|
252
|
+
const unknown = [
|
|
253
|
+
`const noteKey = denied ? "weather.locationDenied" : outside ? fallback : null;`,
|
|
254
|
+
`let noteKey: string | null = null; if (denied) noteKey = "weather.locationDenied"; else if (outside) noteKey = fallback;`,
|
|
255
|
+
`let noteKey: string | null = "weather.locationDenied"; noteKey = fallback;`,
|
|
256
|
+
];
|
|
257
|
+
for (const declared of unknown) expect(note(declared).unread, declared).toHaveLength(1);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
test("follows a key read from a table the file maps into another table", () => {
|
|
261
|
+
const stages = (
|
|
262
|
+
mapped: string,
|
|
263
|
+
table = `{ key: "project.stage.open", icon: BiBox }, { key: "task.stage.done", icon: BiCheck }`,
|
|
264
|
+
) =>
|
|
265
|
+
read(`
|
|
266
|
+
const stages = [${table}] as const;
|
|
267
|
+
export const Stages = ({ counts, decorate }: { counts: number[]; decorate: (stage: object) => { key: string } }) => {
|
|
268
|
+
const { l } = usePage();
|
|
269
|
+
const items = ${mapped};
|
|
270
|
+
return <>{items.map((item) => <Tab key={item.key} title={l(item.key)} />)}</>;
|
|
271
|
+
};
|
|
272
|
+
`);
|
|
273
|
+
const tables = [
|
|
274
|
+
"stages.map((stage) => ({ ...stage, label: l(stage.key) }))",
|
|
275
|
+
"stages.map((stage) => ({ ...stage, label: l._(stage.key), hint: l.trans({ en: 'a', ko: 'b' }), open: true }))",
|
|
276
|
+
"stages.map((stage) => { return { key: stage.key, count: 0 }; })",
|
|
277
|
+
"stages.filter((stage) => stage.icon).map(function (stage) { return { ...stage }; })",
|
|
278
|
+
"stages.map((stage) => ({ ...stage, label: l(stage.key) })).map((stage) => ({ ...stage, on: false }))",
|
|
279
|
+
];
|
|
280
|
+
for (const mapped of tables) {
|
|
281
|
+
const keys = stages(mapped);
|
|
282
|
+
expect(keys.unread, mapped).toEqual([]);
|
|
283
|
+
expect(keys.names, mapped).toEqual(expect.arrayContaining(["project", "task"]));
|
|
284
|
+
}
|
|
285
|
+
const picked = read(`
|
|
286
|
+
const stages = [{ key: "project.stage.open" }, { key: "task.stage.done" }] as const;
|
|
287
|
+
export const Stages = () => { const { l } = usePage(); return stages.map((stage) => stage.key).map((key) => l(key)); };
|
|
288
|
+
`);
|
|
289
|
+
expect(picked.unread).toEqual([]);
|
|
290
|
+
const unknown = [
|
|
291
|
+
"stages.map((stage) => ({ ...stage, key: rename(stage.key) }))",
|
|
292
|
+
"stages.map((stage, idx) => ({ ...stage, key: counts[idx] }))",
|
|
293
|
+
"stages.map((stage) => { if (stage.icon) return decorate(stage); return { ...stage }; })",
|
|
294
|
+
"stages.map((stage) => ({ ...stage, ...decorate(stage) }))",
|
|
295
|
+
"stages.map(decorate)",
|
|
296
|
+
"counts.map(() => decorate({}))",
|
|
297
|
+
];
|
|
298
|
+
for (const mapped of unknown) expect(stages(mapped).unread, mapped).toHaveLength(1);
|
|
299
|
+
const computed = [`{ key: "project.stage.open" }, { key: keyOf("done") }`, `{ key: "project.stage.open", hint }`];
|
|
300
|
+
for (const table of computed)
|
|
301
|
+
expect(stages("stages.map((stage) => ({ ...stage, label: l(stage.key) }))", table).unread, table).toHaveLength(2);
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
test("follows a key picked from a static table of a constant class, which the build reads for it", () => {
|
|
305
|
+
const tables: Record<string, string[]> = { "RunnerBot.labelKeyOf": ["runnerBot.reviewer", "task.fixer"] };
|
|
306
|
+
const staticKeys: DictionaryStaticKeys = (name, member) => tables[`${name}.${member}`] ?? null;
|
|
307
|
+
const sourceOf = (imports: string, shown: string) =>
|
|
308
|
+
`${imports}\nexport const Bot = ({ role }: { role: string }) => { const { l } = usePage(); return <>{${shown}}</>; };`;
|
|
309
|
+
const bot = (imports: string, shown: string) => read(sourceOf(imports, shown), undefined, staticKeys);
|
|
310
|
+
const fromBarrel = `import * as cnst from "../cnst";`;
|
|
311
|
+
const followed: [string, string][] = [
|
|
312
|
+
[fromBarrel, "l(cnst.RunnerBot.labelKeyOf[role])"],
|
|
313
|
+
[`import { cnst } from "@apps/demo/client";`, "l(cnst.RunnerBot.labelKeyOf[role])"],
|
|
314
|
+
[`import { cnst } from "@apps/demo/client";`, "l(cnst.shared.RunnerBot.labelKeyOf.reviewer)"],
|
|
315
|
+
[`import { RunnerBot as Bots } from "./runnerBot.constant";`, "l(Bots.labelKeyOf[role])"],
|
|
316
|
+
[fromBarrel, "Object.values(cnst.RunnerBot.labelKeyOf).map((key) => l(key))"],
|
|
317
|
+
];
|
|
318
|
+
for (const [imports, shown] of followed) {
|
|
319
|
+
const keys = bot(imports, shown);
|
|
320
|
+
expect(keys.unread, shown).toEqual([]);
|
|
321
|
+
expect(keys.names, shown).toEqual(expect.arrayContaining(["runnerBot", "task"]));
|
|
322
|
+
}
|
|
323
|
+
const unread: [string, string][] = [
|
|
324
|
+
[fromBarrel, "l(cnst.RunnerBot.other[role])"],
|
|
325
|
+
[fromBarrel, "l(cnst.Other.labelKeyOf[role])"],
|
|
326
|
+
[fromBarrel, "l(cnst.RunnerBot[role])"],
|
|
327
|
+
[fromBarrel, "Object.keys(cnst.RunnerBot.labelKeyOf).map((name) => l(name))"],
|
|
328
|
+
[`import * as constants from "../cnst";`, "l(constants.RunnerBot.labelKeyOf[role])"],
|
|
329
|
+
[`const cnst = load();`, "l(cnst.RunnerBot.labelKeyOf[role])"],
|
|
330
|
+
[`import { RunnerBot } from "./bots";`, "l(RunnerBot.labelKeyOf[role])"],
|
|
331
|
+
[`const RunnerBot = load();`, "l(RunnerBot.labelKeyOf[role])"],
|
|
332
|
+
];
|
|
333
|
+
for (const [imports, shown] of unread) expect(bot(imports, shown).unread, `${imports} ${shown}`).toHaveLength(1);
|
|
334
|
+
expect(read(sourceOf(fromBarrel, "l(cnst.RunnerBot.labelKeyOf[role])")).unread).toEqual([
|
|
335
|
+
{ line: 2, why: "cnst.RunnerBot.labelKeyOf[role] is read from data at run time" },
|
|
336
|
+
]);
|
|
337
|
+
});
|
|
338
|
+
|
|
156
339
|
test("marks a framework surface that shows the texts of any module", () => {
|
|
157
340
|
expect(read(`import { Signal } from "akanjs/ui";`).anyModule).toContain("Signal from akanjs/ui");
|
|
158
341
|
expect(read(`import { Field } from "akanjs/ui/Constant";`).anyModule).toContain("Constant from akanjs/ui/Constant");
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import ts from "typescript";
|
|
2
2
|
import { RuntimeLexicalScope } from "./runtimeLexicalScope";
|
|
3
|
+
import { TranslatorBindings } from "./translatorBindings";
|
|
3
4
|
|
|
4
5
|
export interface DictionaryKeyUnread {
|
|
5
6
|
line: number;
|
|
@@ -19,6 +20,8 @@ export interface DictionaryKeyRead {
|
|
|
19
20
|
anyModule: string | null;
|
|
20
21
|
}
|
|
21
22
|
|
|
23
|
+
export type DictionaryStaticKeys = (className: string, member: string) => readonly string[] | null;
|
|
24
|
+
|
|
22
25
|
const leadName = /^([A-Za-z_$][\w$]*)(?:\.|$)/;
|
|
23
26
|
const className = /^[A-Z][A-Za-z0-9]*$/;
|
|
24
27
|
const apiKey = /\.signal\.[^.\s]*\.(?:desc|arg)\b/;
|
|
@@ -33,7 +36,9 @@ const objectReaders = new Set(["keys", "values", "entries"]);
|
|
|
33
36
|
const memberCalls = new Set(["field", "enum", "trans"]);
|
|
34
37
|
const translatorChain = new Set(["_", "rich"]);
|
|
35
38
|
const groupHead = /^[A-Za-z_$][\w$]*\./;
|
|
36
|
-
const maxDepth =
|
|
39
|
+
const maxDepth = 24;
|
|
40
|
+
const constantModule = /\.constant(?:\.ts)?$/;
|
|
41
|
+
const constantsName = "cnst";
|
|
37
42
|
|
|
38
43
|
const strip = (node: ts.Expression): ts.Expression =>
|
|
39
44
|
ts.isParenthesizedExpression(node) ||
|
|
@@ -45,11 +50,13 @@ const strip = (node: ts.Expression): ts.Expression =>
|
|
|
45
50
|
: node;
|
|
46
51
|
|
|
47
52
|
//* Which dictionary groups a client file can show: the keys it hands `l`, `l._`, `l.rich` or `translate` must come from
|
|
48
|
-
//* strings
|
|
53
|
+
//* strings the file writes (a literal, a template with a literal group, a table, a typed union) or a constant's static.
|
|
49
54
|
export class DictionaryKeys {
|
|
50
55
|
readonly #sourceFile: ts.SourceFile;
|
|
51
56
|
readonly #scope = new RuntimeLexicalScope();
|
|
52
57
|
readonly #translators = new Set(["l"]);
|
|
58
|
+
readonly #bindings: TranslatorBindings;
|
|
59
|
+
readonly #staticKeys: DictionaryStaticKeys | undefined;
|
|
53
60
|
readonly #names = new Set<string>();
|
|
54
61
|
readonly #classNames = new Set<string>();
|
|
55
62
|
readonly #propNames = new Set<string>();
|
|
@@ -57,14 +64,16 @@ export class DictionaryKeys {
|
|
|
57
64
|
#api = false;
|
|
58
65
|
#anyModule: string | null = null;
|
|
59
66
|
|
|
60
|
-
private constructor(sourceFile: ts.SourceFile) {
|
|
67
|
+
private constructor(sourceFile: ts.SourceFile, staticKeys: DictionaryStaticKeys | undefined) {
|
|
61
68
|
this.#sourceFile = sourceFile;
|
|
69
|
+
this.#staticKeys = staticKeys;
|
|
70
|
+
this.#bindings = new TranslatorBindings(sourceFile, this.#scope, this.#translators);
|
|
62
71
|
}
|
|
63
72
|
|
|
64
|
-
static read(text: string, fileName: string): DictionaryKeyRead {
|
|
73
|
+
static read(text: string, fileName: string, staticKeys?: DictionaryStaticKeys): DictionaryKeyRead {
|
|
65
74
|
const kind = fileName.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
|
|
66
75
|
const sourceFile = ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true, kind);
|
|
67
|
-
return new DictionaryKeys(sourceFile).#read();
|
|
76
|
+
return new DictionaryKeys(sourceFile, staticKeys).#read();
|
|
68
77
|
}
|
|
69
78
|
|
|
70
79
|
#read(): DictionaryKeyRead {
|
|
@@ -181,7 +190,7 @@ export class DictionaryKeys {
|
|
|
181
190
|
}
|
|
182
191
|
|
|
183
192
|
#isTranslator(node: ts.Expression): boolean {
|
|
184
|
-
if (ts.isIdentifier(node)) return this.#translators.has(node.text);
|
|
193
|
+
if (ts.isIdentifier(node)) return this.#translators.has(node.text) && !this.#bindings.isOther(node);
|
|
185
194
|
if (!ts.isPropertyAccessExpression(node)) return false;
|
|
186
195
|
const name = node.name.text;
|
|
187
196
|
if (name === "l") return true;
|
|
@@ -232,7 +241,7 @@ export class DictionaryKeys {
|
|
|
232
241
|
if (depth > maxDepth) return false;
|
|
233
242
|
const next = (inner: ts.Expression) => this.#follows(inner, depth + 1);
|
|
234
243
|
const node = strip(expression);
|
|
235
|
-
if (ts.isStringLiteralLike(node)) return true;
|
|
244
|
+
if (ts.isStringLiteralLike(node) || DictionaryKeys.#isNoKey(node)) return true;
|
|
236
245
|
if (ts.isTemplateExpression(node)) {
|
|
237
246
|
if (node.head.text.includes(".")) return true;
|
|
238
247
|
const [first] = node.templateSpans;
|
|
@@ -256,6 +265,12 @@ export class DictionaryKeys {
|
|
|
256
265
|
return false;
|
|
257
266
|
}
|
|
258
267
|
|
|
268
|
+
static #isNoKey(node: ts.Expression): boolean {
|
|
269
|
+
const { kind } = node;
|
|
270
|
+
if (kind === ts.SyntaxKind.NullKeyword || kind === ts.SyntaxKind.FalseKeyword) return true;
|
|
271
|
+
return ts.isIdentifier(node) && node.text === "undefined";
|
|
272
|
+
}
|
|
273
|
+
|
|
259
274
|
//? With `names`, the property names are the keys: each one is added to the groups, and a name that is not written fails.
|
|
260
275
|
#isTable(expression: ts.Expression, depth: number, names = false): boolean {
|
|
261
276
|
if (depth > maxDepth) return false;
|
|
@@ -275,18 +290,75 @@ export class DictionaryKeys {
|
|
|
275
290
|
});
|
|
276
291
|
if (ts.isIdentifier(node)) return this.#bindingFollows(node, depth + 1, true, names);
|
|
277
292
|
if (ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node))
|
|
278
|
-
return !names && this.#isTable(node.expression, depth + 1);
|
|
293
|
+
return !names && (this.#isConstantTable(node) || this.#isTable(node.expression, depth + 1));
|
|
279
294
|
if (!ts.isCallExpression(node)) return false;
|
|
280
295
|
const callee = strip(node.expression);
|
|
281
296
|
if (!ts.isPropertyAccessExpression(callee)) return false;
|
|
282
297
|
const method = callee.name.text;
|
|
283
298
|
if (ts.isIdentifier(callee.expression) && callee.expression.text === "Object" && objectReaders.has(method))
|
|
284
299
|
return !!node.arguments[0] && this.#isTable(node.arguments[0], depth + 1, method !== "values");
|
|
300
|
+
if (method === "map") return !names && this.#isMapped(node, depth);
|
|
285
301
|
if (!subsetMethods.has(method)) return false;
|
|
286
302
|
const own = this.#isTable(callee.expression, depth + 1, names);
|
|
287
303
|
return own && (method !== "concat" || node.arguments.every((argument) => this.#isTable(argument, depth + 1)));
|
|
288
304
|
}
|
|
289
305
|
|
|
306
|
+
#isConstantTable(node: ts.PropertyAccessExpression | ts.ElementAccessExpression): boolean {
|
|
307
|
+
if (!this.#staticKeys || !ts.isPropertyAccessExpression(node) || !ts.isIdentifier(node.name)) return false;
|
|
308
|
+
const owner = this.#constantClass(strip(node.expression));
|
|
309
|
+
const keys = owner ? this.#staticKeys(owner, node.name.text) : null;
|
|
310
|
+
for (const key of keys ?? []) this.#addText(key, key);
|
|
311
|
+
return !!keys;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
//? `cnst.Memo`, `cnst.shared.Memo` through a lib's constants, or `Memo` imported from a constant file.
|
|
315
|
+
#constantClass(node: ts.Expression): string | null {
|
|
316
|
+
if (ts.isIdentifier(node)) {
|
|
317
|
+
const imported = this.#scope.resolve(node)?.parent;
|
|
318
|
+
if (!imported || !ts.isImportSpecifier(imported)) return null;
|
|
319
|
+
const from = imported.parent.parent.parent.moduleSpecifier;
|
|
320
|
+
const constant = ts.isStringLiteral(from) && constantModule.test(from.text);
|
|
321
|
+
return constant ? (imported.propertyName ?? imported.name).text : null;
|
|
322
|
+
}
|
|
323
|
+
if (!ts.isPropertyAccessExpression(node) || !className.test(node.name.text)) return null;
|
|
324
|
+
let root = strip(node.expression);
|
|
325
|
+
while (ts.isPropertyAccessExpression(root) && !className.test(root.name.text)) root = strip(root.expression);
|
|
326
|
+
const imported = ts.isIdentifier(root) ? this.#scope.resolve(root)?.parent : undefined;
|
|
327
|
+
if (!imported) return null;
|
|
328
|
+
const source = ts.isNamespaceImport(imported)
|
|
329
|
+
? imported.name
|
|
330
|
+
: ts.isImportSpecifier(imported)
|
|
331
|
+
? (imported.propertyName ?? imported.name)
|
|
332
|
+
: null;
|
|
333
|
+
return source?.text === constantsName ? node.name.text : null;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
#isMapped(call: ts.CallExpression, depth: number): boolean {
|
|
337
|
+
const [callback] = call.arguments;
|
|
338
|
+
if (!callback || !(ts.isArrowFunction(callback) || ts.isFunctionExpression(callback))) return false;
|
|
339
|
+
const returned = ts.isBlock(callback.body) ? DictionaryKeys.#returnsOf(callback.body) : [callback.body];
|
|
340
|
+
return returned.length > 0 && returned.every((value) => !!value && this.#isValue(value, depth + 1));
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
static #returnsOf(block: ts.Block): (ts.Expression | undefined)[] {
|
|
344
|
+
const returned: (ts.Expression | undefined)[] = [];
|
|
345
|
+
const visit = (node: ts.Node) => {
|
|
346
|
+
if (ts.isFunctionLike(node)) return;
|
|
347
|
+
if (ts.isReturnStatement(node)) returned.push(node.expression);
|
|
348
|
+
else ts.forEachChild(node, visit);
|
|
349
|
+
};
|
|
350
|
+
ts.forEachChild(block, visit);
|
|
351
|
+
return returned;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
#isTranslated(node: ts.Expression): boolean {
|
|
355
|
+
if (!ts.isCallExpression(node)) return false;
|
|
356
|
+
const callee = strip(node.expression);
|
|
357
|
+
if (this.#isTranslator(callee)) return true;
|
|
358
|
+
if (!ts.isPropertyAccessExpression(callee) || !memberCalls.has(callee.name.text)) return false;
|
|
359
|
+
return this.#isTranslator(strip(callee.expression));
|
|
360
|
+
}
|
|
361
|
+
|
|
290
362
|
#addPropertyName(property: ts.ObjectLiteralElementLike): boolean {
|
|
291
363
|
if (ts.isSpreadAssignment(property)) return true;
|
|
292
364
|
const { name } = property;
|
|
@@ -314,7 +386,8 @@ export class DictionaryKeys {
|
|
|
314
386
|
ts.isJsxElement(node) ||
|
|
315
387
|
ts.isJsxSelfClosingElement(node) ||
|
|
316
388
|
ts.isJsxFragment(node) ||
|
|
317
|
-
(ts.isIdentifier(node) && (node.text === "undefined" || className.test(node.text)))
|
|
389
|
+
(ts.isIdentifier(node) && (node.text === "undefined" || className.test(node.text))) ||
|
|
390
|
+
this.#isTranslated(node)
|
|
318
391
|
)
|
|
319
392
|
return true;
|
|
320
393
|
return this.#follows(node, depth) || this.#isTable(node, depth);
|
|
@@ -50,10 +50,13 @@ export class ObjectImportReport {
|
|
|
50
50
|
if (cached) return cached;
|
|
51
51
|
const executor = AppExecutor.from(workspace, app);
|
|
52
52
|
const resolvePackage = await createTsconfigPackageResolver(executor);
|
|
53
|
+
const barrels = (await executor.getConfig()).barrelImports;
|
|
54
|
+
const analyzer = new BarrelAnalyzer({ resolvePackage, packageRoot: workspace.workspaceRoot });
|
|
55
|
+
const resolve = ObjectMemberImports.resolverOf(resolvePackage);
|
|
53
56
|
const reader: ScopeReader = {
|
|
54
|
-
barrels
|
|
55
|
-
analyzer
|
|
56
|
-
objects: new ObjectMemberImports({ resolve:
|
|
57
|
+
barrels,
|
|
58
|
+
analyzer,
|
|
59
|
+
objects: new ObjectMemberImports({ resolve, barrels: { names: barrels, analyzer } }),
|
|
57
60
|
};
|
|
58
61
|
readers.set(app, reader);
|
|
59
62
|
return reader;
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { RuntimeLexicalScope } from "./runtimeLexicalScope";
|
|
3
|
+
|
|
4
|
+
type Held = "translator" | "plain" | "unknown";
|
|
5
|
+
type Use = "asTranslator" | "ownMember" | "other";
|
|
6
|
+
|
|
7
|
+
const iterations = new Set([
|
|
8
|
+
"map",
|
|
9
|
+
"forEach",
|
|
10
|
+
"filter",
|
|
11
|
+
"flatMap",
|
|
12
|
+
"find",
|
|
13
|
+
"findLast",
|
|
14
|
+
"findIndex",
|
|
15
|
+
"findLastIndex",
|
|
16
|
+
"some",
|
|
17
|
+
"every",
|
|
18
|
+
"sort",
|
|
19
|
+
"toSorted",
|
|
20
|
+
"reduce",
|
|
21
|
+
"reduceRight",
|
|
22
|
+
]);
|
|
23
|
+
const accumulations = new Set(["reduce", "reduceRight"]);
|
|
24
|
+
const callingMembers = ["call", "apply", "bind"];
|
|
25
|
+
const translatorMembers = new Set(["_", "rich", "trans", "field", "enum", ...callingMembers]);
|
|
26
|
+
const plainTypes = new Set([
|
|
27
|
+
ts.SyntaxKind.StringKeyword,
|
|
28
|
+
ts.SyntaxKind.NumberKeyword,
|
|
29
|
+
ts.SyntaxKind.BooleanKeyword,
|
|
30
|
+
ts.SyntaxKind.BigIntKeyword,
|
|
31
|
+
]);
|
|
32
|
+
//? A translator inside an array or object literal is itself a use the reader reports, so the literal holds none unseen.
|
|
33
|
+
const plainKinds = new Set([
|
|
34
|
+
ts.SyntaxKind.TrueKeyword,
|
|
35
|
+
ts.SyntaxKind.FalseKeyword,
|
|
36
|
+
ts.SyntaxKind.NullKeyword,
|
|
37
|
+
ts.SyntaxKind.NumericLiteral,
|
|
38
|
+
ts.SyntaxKind.BigIntLiteral,
|
|
39
|
+
ts.SyntaxKind.RegularExpressionLiteral,
|
|
40
|
+
ts.SyntaxKind.TemplateExpression,
|
|
41
|
+
ts.SyntaxKind.ArrayLiteralExpression,
|
|
42
|
+
ts.SyntaxKind.ObjectLiteralExpression,
|
|
43
|
+
ts.SyntaxKind.PrefixUnaryExpression,
|
|
44
|
+
ts.SyntaxKind.PostfixUnaryExpression,
|
|
45
|
+
ts.SyntaxKind.TypeOfExpression,
|
|
46
|
+
]);
|
|
47
|
+
const eitherSide = new Set([
|
|
48
|
+
ts.SyntaxKind.BarBarToken,
|
|
49
|
+
ts.SyntaxKind.AmpersandAmpersandToken,
|
|
50
|
+
ts.SyntaxKind.QuestionQuestionToken,
|
|
51
|
+
]);
|
|
52
|
+
const replacing = new Set([
|
|
53
|
+
ts.SyntaxKind.EqualsToken,
|
|
54
|
+
ts.SyntaxKind.BarBarEqualsToken,
|
|
55
|
+
ts.SyntaxKind.AmpersandAmpersandEqualsToken,
|
|
56
|
+
ts.SyntaxKind.QuestionQuestionEqualsToken,
|
|
57
|
+
]);
|
|
58
|
+
|
|
59
|
+
const isWrapper = (node: ts.Node): node is ts.Expression & { expression: ts.Expression } =>
|
|
60
|
+
ts.isParenthesizedExpression(node) ||
|
|
61
|
+
ts.isAsExpression(node) ||
|
|
62
|
+
ts.isSatisfiesExpression(node) ||
|
|
63
|
+
ts.isNonNullExpression(node) ||
|
|
64
|
+
ts.isTypeAssertionExpression(node);
|
|
65
|
+
|
|
66
|
+
const isPattern = (node: ts.Node) =>
|
|
67
|
+
ts.isArrayLiteralExpression(node) ||
|
|
68
|
+
ts.isObjectLiteralExpression(node) ||
|
|
69
|
+
ts.isPropertyAssignment(node) ||
|
|
70
|
+
ts.isShorthandPropertyAssignment(node) ||
|
|
71
|
+
ts.isSpreadElement(node) ||
|
|
72
|
+
ts.isSpreadAssignment(node);
|
|
73
|
+
|
|
74
|
+
const isPlain = (expression: ts.Expression): boolean => {
|
|
75
|
+
if (isWrapper(expression)) return isPlain(expression.expression);
|
|
76
|
+
if (ts.isStringLiteralLike(expression) || plainKinds.has(expression.kind)) return true;
|
|
77
|
+
if (ts.isPropertyAccessExpression(expression)) return expression.name.text === "length";
|
|
78
|
+
if (ts.isConditionalExpression(expression)) return isPlain(expression.whenTrue) && isPlain(expression.whenFalse);
|
|
79
|
+
if (!ts.isBinaryExpression(expression)) return false;
|
|
80
|
+
const operator = expression.operatorToken.kind;
|
|
81
|
+
if (eitherSide.has(operator)) return isPlain(expression.left) && isPlain(expression.right);
|
|
82
|
+
const assigns = operator >= ts.SyntaxKind.FirstAssignment && operator <= ts.SyntaxKind.LastAssignment;
|
|
83
|
+
return !assigns && operator !== ts.SyntaxKind.CommaToken;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
//? A name counts as the translator unless shown otherwise: a missed translator drops texts, a mistaken one loads more.
|
|
87
|
+
export class TranslatorBindings {
|
|
88
|
+
readonly #sourceFile: ts.SourceFile;
|
|
89
|
+
readonly #scope: RuntimeLexicalScope;
|
|
90
|
+
readonly #names: ReadonlySet<string>;
|
|
91
|
+
readonly #others = new Map<ts.Identifier, boolean>();
|
|
92
|
+
#uses: Map<ts.Identifier, Use[]> | null = null;
|
|
93
|
+
|
|
94
|
+
constructor(sourceFile: ts.SourceFile, scope: RuntimeLexicalScope, names: ReadonlySet<string>) {
|
|
95
|
+
this.#sourceFile = sourceFile;
|
|
96
|
+
this.#scope = scope;
|
|
97
|
+
this.#names = names;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
isOther(identifier: ts.Identifier): boolean {
|
|
101
|
+
const declared = this.#scope.resolve(identifier);
|
|
102
|
+
if (!declared) return false;
|
|
103
|
+
const known = this.#others.get(declared);
|
|
104
|
+
if (known !== undefined) return known;
|
|
105
|
+
const other = this.#decide(declared);
|
|
106
|
+
this.#others.set(declared, other);
|
|
107
|
+
return other;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
#decide(declared: ts.Identifier): boolean {
|
|
111
|
+
const held = TranslatorBindings.#heldBy(declared);
|
|
112
|
+
if (held === "translator") return false;
|
|
113
|
+
const uses = this.#usesOf(declared);
|
|
114
|
+
if (uses.every((use) => use === "ownMember")) return true;
|
|
115
|
+
return held === "plain" && !uses.includes("asTranslator");
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
static #heldBy(name: ts.Identifier): Held {
|
|
119
|
+
let declaration: ts.Node = name.parent;
|
|
120
|
+
while (ts.isBindingElement(declaration) && ts.isArrayBindingPattern(declaration.parent)) {
|
|
121
|
+
if (declaration.initializer) return "unknown";
|
|
122
|
+
declaration = declaration.parent.parent;
|
|
123
|
+
}
|
|
124
|
+
if (ts.isParameter(declaration)) return TranslatorBindings.#parameterIsPlain(declaration) ? "plain" : "unknown";
|
|
125
|
+
if (!ts.isVariableDeclaration(declaration)) return "translator";
|
|
126
|
+
if (declaration.type) return plainTypes.has(declaration.type.kind) ? "plain" : "unknown";
|
|
127
|
+
const list = declaration.parent;
|
|
128
|
+
if (!ts.isVariableDeclarationList(list)) return "unknown";
|
|
129
|
+
if (ts.isForOfStatement(list.parent) || ts.isForInStatement(list.parent)) return "plain";
|
|
130
|
+
const { initializer } = declaration;
|
|
131
|
+
return declaration.name === name && initializer && isPlain(initializer) ? "plain" : "unknown";
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
static #parameterIsPlain(parameter: ts.ParameterDeclaration): boolean {
|
|
135
|
+
if (parameter.type) return plainTypes.has(parameter.type.kind);
|
|
136
|
+
if (parameter.initializer || parameter.dotDotDotToken) return false;
|
|
137
|
+
const callback = parameter.parent;
|
|
138
|
+
const call = callback.parent;
|
|
139
|
+
if (!(ts.isArrowFunction(callback) || ts.isFunctionExpression(callback)) || !ts.isCallExpression(call))
|
|
140
|
+
return false;
|
|
141
|
+
if (!call.arguments.includes(callback) || !ts.isPropertyAccessExpression(call.expression)) return false;
|
|
142
|
+
const method = call.expression.name.text;
|
|
143
|
+
return iterations.has(method) && !(accumulations.has(method) && callback.parameters[0] === parameter);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
#usesOf(declared: ts.Identifier): Use[] {
|
|
147
|
+
if (!this.#uses) {
|
|
148
|
+
const uses = new Map<ts.Identifier, Use[]>();
|
|
149
|
+
const visit = (node: ts.Node) => {
|
|
150
|
+
if (ts.isIdentifier(node) && this.#names.has(node.text) && RuntimeLexicalScope.isReference(node)) {
|
|
151
|
+
const binding = this.#scope.resolve(node);
|
|
152
|
+
if (binding) uses.set(binding, [...(uses.get(binding) ?? []), TranslatorBindings.#useOf(node)]);
|
|
153
|
+
}
|
|
154
|
+
ts.forEachChild(node, visit);
|
|
155
|
+
};
|
|
156
|
+
visit(this.#sourceFile);
|
|
157
|
+
this.#uses = uses;
|
|
158
|
+
}
|
|
159
|
+
return this.#uses.get(declared) ?? [];
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
static #useOf(reference: ts.Identifier): Use {
|
|
163
|
+
let outer: ts.Node = reference;
|
|
164
|
+
while (isWrapper(outer.parent)) outer = outer.parent;
|
|
165
|
+
const { parent } = outer;
|
|
166
|
+
if (ts.isCallExpression(parent) && parent.expression === outer) return "asTranslator";
|
|
167
|
+
if (ts.isTaggedTemplateExpression(parent) && parent.tag === outer) return "asTranslator";
|
|
168
|
+
if (ts.isPropertyAccessExpression(parent) && parent.expression === outer)
|
|
169
|
+
return translatorMembers.has(parent.name.text) ? "asTranslator" : "ownMember";
|
|
170
|
+
if (ts.isElementAccessExpression(parent) && parent.expression === outer) {
|
|
171
|
+
const key = parent.argumentExpression;
|
|
172
|
+
if (ts.isNumericLiteral(key)) return "ownMember";
|
|
173
|
+
if (!ts.isStringLiteralLike(key)) return "other";
|
|
174
|
+
return translatorMembers.has(key.text) ? "asTranslator" : "ownMember";
|
|
175
|
+
}
|
|
176
|
+
return TranslatorBindings.#isReplaced(outer) ? "asTranslator" : "other";
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
static #isReplaced(reference: ts.Node): boolean {
|
|
180
|
+
let target = reference;
|
|
181
|
+
while (isPattern(target.parent) || isWrapper(target.parent)) target = target.parent;
|
|
182
|
+
const { parent } = target;
|
|
183
|
+
if (ts.isForOfStatement(parent) || ts.isForInStatement(parent)) return parent.initializer === target;
|
|
184
|
+
if (!ts.isBinaryExpression(parent) || parent.left !== target) return false;
|
|
185
|
+
return replacing.has(parent.operatorToken.kind) && (target !== reference || !isPlain(parent.right));
|
|
186
|
+
}
|
|
187
|
+
}
|