@akanjs/devkit 3.3.0-rc.37 → 3.3.0-rc.38
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 +55 -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 +14 -7
- package/frontendBuild/clientEntryDiscovery.test.ts +193 -0
- package/frontendBuild/clientEntryDiscovery.ts +41 -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 +129 -0
- package/frontendBuild/routeHandoffs.test.ts +10 -0
- package/frontendBuild/routeHandoffs.ts +3 -1
- 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 +6 -1
- package/transforms/exportReach.test.ts +162 -0
- package/transforms/exportReach.ts +304 -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/workspace/scanInfo.ts +24 -0
|
@@ -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
|
+
}
|
|
@@ -43,7 +43,12 @@ export const createBarrelImportsPlugin = async (
|
|
|
43
43
|
resolvePackage,
|
|
44
44
|
...(route ? { packageRoot: app.workspace.workspaceRoot } : {}),
|
|
45
45
|
});
|
|
46
|
-
const objects = route
|
|
46
|
+
const objects = route
|
|
47
|
+
? new ObjectMemberImports({
|
|
48
|
+
resolve: ObjectMemberImports.resolverOf(resolvePackage),
|
|
49
|
+
barrels: { names: barrels, analyzer },
|
|
50
|
+
})
|
|
51
|
+
: null;
|
|
47
52
|
|
|
48
53
|
return {
|
|
49
54
|
name: "barrel-imports",
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { ExportReach } from "./exportReach";
|
|
3
|
+
|
|
4
|
+
const read = (source: string, file = "View.tsx") => ExportReach.read(source, file) as ExportReach;
|
|
5
|
+
const importsOf = (source: string, name: string) => {
|
|
6
|
+
const reached = read(source).of(name);
|
|
7
|
+
return reached.known ? reached.imports : null;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const view = `
|
|
11
|
+
import { Remove, Edit } from "./Memo.Util";
|
|
12
|
+
import { Chart } from "./Chart";
|
|
13
|
+
import type { Memo } from "./memo.constant";
|
|
14
|
+
import * as Unit from "./Memo.Unit";
|
|
15
|
+
import { format } from "./format";
|
|
16
|
+
|
|
17
|
+
const Title = ({ memo }: { memo: Memo }) => <h1>{format(memo.title)}</h1>;
|
|
18
|
+
|
|
19
|
+
export const General = ({ memo }: { memo: Memo }) => (
|
|
20
|
+
<div>
|
|
21
|
+
<Title memo={memo} />
|
|
22
|
+
<Remove id={memo.id} />
|
|
23
|
+
</div>
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
export function Stats({ memo }: { memo: Memo }) {
|
|
27
|
+
return <Chart value={memo.count} />;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const Card = ({ memo }: { memo: Memo }) => <Unit.Card memo={memo} />;
|
|
31
|
+
export const All = () => Object.values(Unit);
|
|
32
|
+
export interface Props { memo: Memo }
|
|
33
|
+
`;
|
|
34
|
+
|
|
35
|
+
describe("ExportReach", () => {
|
|
36
|
+
test("reaches, from one export, the imports its declaration reads and those of the declarations it reads", () => {
|
|
37
|
+
expect(importsOf(view, "General")).toEqual([
|
|
38
|
+
{ specifier: "./Memo.Util", names: ["Remove"] },
|
|
39
|
+
{ specifier: "./format", names: ["format"] },
|
|
40
|
+
]);
|
|
41
|
+
expect(importsOf(view, "Stats")).toEqual([{ specifier: "./Chart", names: ["Chart"] }]);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("asks a namespace import for the members read from it, and for the whole module once it is handed on", () => {
|
|
45
|
+
expect(importsOf(view, "Card")).toEqual([{ specifier: "./Memo.Unit", names: ["Card"] }]);
|
|
46
|
+
expect(importsOf(view, "All")).toEqual([{ specifier: "./Memo.Unit", names: null }]);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("reaches nothing for a type, and does not follow a name read only in a type", () => {
|
|
50
|
+
expect(importsOf(view, "Props")).toEqual([]);
|
|
51
|
+
expect(
|
|
52
|
+
read(view)
|
|
53
|
+
.whole()
|
|
54
|
+
.map(({ specifier }) => specifier),
|
|
55
|
+
).not.toContain("./memo.constant");
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("answers a name the file does not hold as unknown, with the `export *` targets that may hold it", () => {
|
|
59
|
+
expect(read(view).of("Missing")).toEqual({ known: false, stars: [] });
|
|
60
|
+
expect(read('export * from "./a";\nexport * from "./b";\nexport const X = 1;\n').of("Y")).toEqual({
|
|
61
|
+
known: false,
|
|
62
|
+
stars: ["./a", "./b"],
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test("follows an export list, a re-export, a namespace re-export and a default export", () => {
|
|
67
|
+
const source = `
|
|
68
|
+
import Base from "./Base";
|
|
69
|
+
import { Row } from "./Row";
|
|
70
|
+
const List = () => <Row />;
|
|
71
|
+
export { List as Rows };
|
|
72
|
+
export { Cell as GridCell, default as Grid } from "./Grid";
|
|
73
|
+
export * as Icons from "./icons";
|
|
74
|
+
export default function Page() { return <Base />; }
|
|
75
|
+
`;
|
|
76
|
+
expect(importsOf(source, "Rows")).toEqual([{ specifier: "./Row", names: ["Row"] }]);
|
|
77
|
+
expect(importsOf(source, "GridCell")).toEqual([{ specifier: "./Grid", names: ["Cell"] }]);
|
|
78
|
+
expect(importsOf(source, "Grid")).toEqual([{ specifier: "./Grid", names: ["default"] }]);
|
|
79
|
+
expect(importsOf(source, "Icons")).toEqual([{ specifier: "./icons", names: null }]);
|
|
80
|
+
expect(importsOf(source, "default")).toEqual([{ specifier: "./Base", names: ["default"] }]);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test("reads a member put on a declaration with the declaration, not with every importer", () => {
|
|
84
|
+
const source = `
|
|
85
|
+
import { Header } from "./Header";
|
|
86
|
+
import { Footer } from "./Footer";
|
|
87
|
+
import { Plain } from "./Plain";
|
|
88
|
+
export const Card = () => null;
|
|
89
|
+
Card.Header = Header;
|
|
90
|
+
Object.assign(Card, { Footer });
|
|
91
|
+
export const Other = () => <Plain />;
|
|
92
|
+
`;
|
|
93
|
+
expect(importsOf(source, "Card")).toEqual([
|
|
94
|
+
{ specifier: "./Header", names: ["Header"] },
|
|
95
|
+
{ specifier: "./Footer", names: ["Footer"] },
|
|
96
|
+
]);
|
|
97
|
+
expect(importsOf(source, "Other")).toEqual([{ specifier: "./Plain", names: ["Plain"] }]);
|
|
98
|
+
expect(read(source).always()).toEqual([]);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test("reaches, for every importer, a bare import and what a statement that is no declaration reads", () => {
|
|
102
|
+
const source = `
|
|
103
|
+
import "./polyfill";
|
|
104
|
+
import { register } from "./registry";
|
|
105
|
+
import { Panel } from "./Panel";
|
|
106
|
+
import { Plain } from "./Plain";
|
|
107
|
+
register("panel", Panel);
|
|
108
|
+
export const Other = () => <Plain />;
|
|
109
|
+
`;
|
|
110
|
+
expect(read(source).always()).toEqual([
|
|
111
|
+
{ specifier: "./polyfill", names: null },
|
|
112
|
+
{ specifier: "./registry", names: ["register"] },
|
|
113
|
+
{ specifier: "./Panel", names: ["Panel"] },
|
|
114
|
+
]);
|
|
115
|
+
expect(importsOf(source, "Other")).toEqual([{ specifier: "./Plain", names: ["Plain"] }]);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test("reaches a module loaded on demand as a whole, from the export whose declaration loads it", () => {
|
|
119
|
+
const source = `
|
|
120
|
+
import { lazy } from "react";
|
|
121
|
+
const Editor = lazy(() => import("./Editor"));
|
|
122
|
+
export const Rich = () => <Editor />;
|
|
123
|
+
export const Plain = () => null;
|
|
124
|
+
`;
|
|
125
|
+
expect(importsOf(source, "Rich")).toEqual([
|
|
126
|
+
{ specifier: "./Editor", names: null },
|
|
127
|
+
{ specifier: "react", names: ["lazy"] },
|
|
128
|
+
]);
|
|
129
|
+
expect(importsOf(source, "Plain")).toEqual([]);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test("counts a property name as no read of an import of that name, and a shadowed name as one", () => {
|
|
133
|
+
const source = `
|
|
134
|
+
import { Card } from "./Card";
|
|
135
|
+
import { Row } from "./Row";
|
|
136
|
+
export const A = (props: { Card: string }) => [props.Card, { Card: 1 }];
|
|
137
|
+
export const B = (Row: string) => Row;
|
|
138
|
+
`;
|
|
139
|
+
expect(importsOf(source, "A")).toEqual([]);
|
|
140
|
+
expect(importsOf(source, "B")).toEqual([{ specifier: "./Row", names: ["Row"] }]);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test("the whole module reaches every import a value of it reads, re-exports and `export *` included", () => {
|
|
144
|
+
const source = `
|
|
145
|
+
import { A } from "./a";
|
|
146
|
+
import type { T } from "./t";
|
|
147
|
+
export { B } from "./b";
|
|
148
|
+
export * from "./c";
|
|
149
|
+
export const X = () => <A />;
|
|
150
|
+
`;
|
|
151
|
+
expect(read(source).whole()).toEqual([
|
|
152
|
+
{ specifier: "./a", names: ["A"] },
|
|
153
|
+
{ specifier: "./b", names: ["B"] },
|
|
154
|
+
{ specifier: "./c", names: null },
|
|
155
|
+
]);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test("does not read a CommonJS module, whose exports are assigned at run time", () => {
|
|
159
|
+
expect(ExportReach.read('const a = require("./a");\nmodule.exports = { a };\n', "legacy.js")).toBeNull();
|
|
160
|
+
expect(ExportReach.read("export const a = 1;\n", "modern.js")).not.toBeNull();
|
|
161
|
+
});
|
|
162
|
+
});
|