@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
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { ObjectModule } from "./objectModule";
|
|
3
|
+
|
|
4
|
+
export interface ReachedImport {
|
|
5
|
+
specifier: string;
|
|
6
|
+
/** null: the whole module (a namespace handed on, a bare or dynamic import, a name the file re-exports as `*`). */
|
|
7
|
+
names: string[] | null;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export type ExportReached =
|
|
11
|
+
| { known: true; imports: ReachedImport[] }
|
|
12
|
+
/** The file neither declares nor names it: one of its `export *` targets may. */
|
|
13
|
+
| { known: false; stars: string[] };
|
|
14
|
+
|
|
15
|
+
interface ImportedName {
|
|
16
|
+
specifier: string;
|
|
17
|
+
/** `*` for a namespace import, `default` for a default import. */
|
|
18
|
+
imported: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
//? null: the name is read as a value of its own. A set: only these members of it are read (`View.General`).
|
|
22
|
+
type Reads = Map<string, Set<string> | null>;
|
|
23
|
+
|
|
24
|
+
interface Unit {
|
|
25
|
+
reads: Reads;
|
|
26
|
+
dynamic: string[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const CJS_EXPORT_RE = /\b(?:module\.exports|exports\.[A-Za-z_$])/;
|
|
30
|
+
|
|
31
|
+
//* What an importer of one export of a server module can reach: the imports read by that export's declaration and by
|
|
32
|
+
//* the top-level declarations it reads in turn. A type reaches nothing. A statement that is not a declaration runs for
|
|
33
|
+
//* every importer, so what it reads is reached always. Names are matched by text, so a shadowed name counts as read:
|
|
34
|
+
//* the answer can hold more than the export uses, never less.
|
|
35
|
+
export class ExportReach {
|
|
36
|
+
readonly stars: string[] = [];
|
|
37
|
+
readonly #imports = new Map<string, ImportedName>();
|
|
38
|
+
readonly #bare: string[] = [];
|
|
39
|
+
readonly #units = new Map<string, Unit[]>();
|
|
40
|
+
readonly #effects: Unit[] = [];
|
|
41
|
+
readonly #exported = new Map<string, string>();
|
|
42
|
+
readonly #reexports = new Map<string, ImportedName>();
|
|
43
|
+
readonly #types = new Set<string>();
|
|
44
|
+
|
|
45
|
+
//? null for a CommonJS module: what it exports is assigned at run time.
|
|
46
|
+
static read(source: string, file: string): ExportReach | null {
|
|
47
|
+
const sourceFile = ts.createSourceFile(file, source, ts.ScriptTarget.Latest, true, ObjectModule.scriptKindOf(file));
|
|
48
|
+
const esm = sourceFile.statements.some(
|
|
49
|
+
(statement) =>
|
|
50
|
+
ts.isImportDeclaration(statement) ||
|
|
51
|
+
ts.isExportDeclaration(statement) ||
|
|
52
|
+
ts.isExportAssignment(statement) ||
|
|
53
|
+
ExportReach.#has(statement, ts.SyntaxKind.ExportKeyword),
|
|
54
|
+
);
|
|
55
|
+
if (!esm && CJS_EXPORT_RE.test(source)) return null;
|
|
56
|
+
const reach = new ExportReach();
|
|
57
|
+
for (const statement of sourceFile.statements) reach.#collect(statement);
|
|
58
|
+
return reach;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
of(name: string): ExportReached {
|
|
62
|
+
const reexport = this.#reexports.get(name);
|
|
63
|
+
if (reexport) return { known: true, imports: [ExportReach.#whole(reexport)] };
|
|
64
|
+
const local = this.#exported.get(name);
|
|
65
|
+
if (local !== undefined) return { known: true, imports: this.#reached([ExportReach.#reading(local)], []) };
|
|
66
|
+
if (this.#types.has(name)) return { known: true, imports: [] };
|
|
67
|
+
return { known: false, stars: this.stars };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
always(): ReachedImport[] {
|
|
71
|
+
return this.#reached(this.#effects, this.#bare);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
whole(): ReachedImport[] {
|
|
75
|
+
const reexports = [...this.#reexports.values(), ...this.stars.map((specifier) => ({ specifier, imported: "*" }))];
|
|
76
|
+
return ExportReach.#merged([
|
|
77
|
+
...this.#reached([...this.#effects, ...[...this.#units.values()].flat()], this.#bare),
|
|
78
|
+
...reexports.map(ExportReach.#whole),
|
|
79
|
+
]);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
static #whole({ specifier, imported }: ImportedName): ReachedImport {
|
|
83
|
+
return { specifier, names: imported === "*" ? null : [imported] };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
static #reading(name: string): Unit {
|
|
87
|
+
return { reads: new Map([[name, null]]), dynamic: [] };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
#reached(starts: Unit[], bare: string[]): ReachedImport[] {
|
|
91
|
+
const used = new Map<string, Set<string> | null>();
|
|
92
|
+
const dynamic = new Set(bare);
|
|
93
|
+
const seen = new Set<Unit>();
|
|
94
|
+
const queue = [...starts];
|
|
95
|
+
for (const unit of queue) {
|
|
96
|
+
if (seen.has(unit)) continue;
|
|
97
|
+
seen.add(unit);
|
|
98
|
+
for (const specifier of unit.dynamic) dynamic.add(specifier);
|
|
99
|
+
for (const [name, members] of unit.reads) {
|
|
100
|
+
if (this.#imports.has(name)) {
|
|
101
|
+
const known = used.get(name);
|
|
102
|
+
if (members === null || known === null) used.set(name, null);
|
|
103
|
+
else used.set(name, new Set([...(known ?? []), ...members]));
|
|
104
|
+
}
|
|
105
|
+
queue.push(...(this.#units.get(name) ?? []));
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const reached: ReachedImport[] = [...dynamic].map((specifier) => ({ specifier, names: null }));
|
|
109
|
+
for (const [local, members] of used) {
|
|
110
|
+
const { specifier, imported } = this.#imports.get(local) as ImportedName;
|
|
111
|
+
if (imported !== "*") reached.push({ specifier, names: [imported] });
|
|
112
|
+
else reached.push({ specifier, names: members ? [...members] : null });
|
|
113
|
+
}
|
|
114
|
+
return ExportReach.#merged(reached);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
static #merged(reached: ReachedImport[]): ReachedImport[] {
|
|
118
|
+
const bySpecifier = new Map<string, Set<string> | null>();
|
|
119
|
+
for (const { specifier, names } of reached) {
|
|
120
|
+
const known = bySpecifier.get(specifier);
|
|
121
|
+
if (names === null || known === null) bySpecifier.set(specifier, null);
|
|
122
|
+
else bySpecifier.set(specifier, new Set([...(known ?? []), ...names]));
|
|
123
|
+
}
|
|
124
|
+
return [...bySpecifier].map(([specifier, names]) => ({ specifier, names: names ? [...names].sort() : null }));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
#collect(statement: ts.Statement) {
|
|
128
|
+
if (ts.isImportDeclaration(statement)) return this.#collectImport(statement);
|
|
129
|
+
if (ts.isExportDeclaration(statement)) return this.#collectExportList(statement);
|
|
130
|
+
if (ts.isInterfaceDeclaration(statement) || ts.isTypeAliasDeclaration(statement)) {
|
|
131
|
+
if (ExportReach.#has(statement, ts.SyntaxKind.ExportKeyword)) this.#types.add(statement.name.text);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
if (ExportReach.#has(statement, ts.SyntaxKind.DeclareKeyword) || ts.isEmptyStatement(statement)) return;
|
|
135
|
+
if (ts.isImportEqualsDeclaration(statement)) return this.#collectImportEquals(statement);
|
|
136
|
+
if (ts.isExportAssignment(statement)) {
|
|
137
|
+
if (statement.isExportEquals) return this.#effects.push(ExportReach.#unitOf(statement));
|
|
138
|
+
this.#exported.set("default", "default");
|
|
139
|
+
return this.#declare("default", ExportReach.#unitOf(statement.expression));
|
|
140
|
+
}
|
|
141
|
+
if (ts.isVariableStatement(statement)) return this.#collectVariables(statement);
|
|
142
|
+
if (
|
|
143
|
+
ts.isFunctionDeclaration(statement) ||
|
|
144
|
+
ts.isClassDeclaration(statement) ||
|
|
145
|
+
ts.isEnumDeclaration(statement) ||
|
|
146
|
+
ts.isModuleDeclaration(statement)
|
|
147
|
+
) {
|
|
148
|
+
if (ts.isFunctionDeclaration(statement) && !statement.body) return;
|
|
149
|
+
const name = statement.name && !ts.isStringLiteral(statement.name) ? statement.name.getText() : "default";
|
|
150
|
+
this.#declare(name, ExportReach.#unitOf(statement));
|
|
151
|
+
if (ExportReach.#has(statement, ts.SyntaxKind.DefaultKeyword)) this.#exported.set("default", name);
|
|
152
|
+
else if (ExportReach.#has(statement, ts.SyntaxKind.ExportKeyword)) this.#exported.set(name, name);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
const unit = ExportReach.#unitOf(statement);
|
|
156
|
+
const owner = ts.isExpressionStatement(statement) ? this.#ownerOf(statement.expression) : null;
|
|
157
|
+
if (owner) this.#declare(owner, unit);
|
|
158
|
+
else this.#effects.push(unit);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
//? `Card.Header = Header` and `Object.assign(Card, { Header })` change only `Card`, so they are read with it.
|
|
162
|
+
#ownerOf(expression: ts.Expression): string | null {
|
|
163
|
+
let target: ts.Expression | undefined;
|
|
164
|
+
if (ts.isBinaryExpression(expression) && expression.operatorToken.kind === ts.SyntaxKind.EqualsToken) {
|
|
165
|
+
target = expression.left;
|
|
166
|
+
while (ts.isPropertyAccessExpression(target)) target = target.expression;
|
|
167
|
+
if (target === expression.left) return null;
|
|
168
|
+
} else if (
|
|
169
|
+
ts.isCallExpression(expression) &&
|
|
170
|
+
ts.isPropertyAccessExpression(expression.expression) &&
|
|
171
|
+
expression.expression.name.text === "assign" &&
|
|
172
|
+
ts.isIdentifier(expression.expression.expression) &&
|
|
173
|
+
expression.expression.expression.text === "Object"
|
|
174
|
+
)
|
|
175
|
+
[target] = expression.arguments;
|
|
176
|
+
return target && ts.isIdentifier(target) && this.#units.has(target.text) ? target.text : null;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
#declare(name: string, unit: Unit) {
|
|
180
|
+
this.#units.set(name, [...(this.#units.get(name) ?? []), unit]);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
#collectImport(statement: ts.ImportDeclaration) {
|
|
184
|
+
if (!ts.isStringLiteral(statement.moduleSpecifier)) return;
|
|
185
|
+
const specifier = statement.moduleSpecifier.text;
|
|
186
|
+
const clause = statement.importClause;
|
|
187
|
+
if (!clause) return this.#bare.push(specifier);
|
|
188
|
+
if (clause.phaseModifier === ts.SyntaxKind.TypeKeyword) return;
|
|
189
|
+
if (clause.name) this.#imports.set(clause.name.text, { specifier, imported: "default" });
|
|
190
|
+
const bindings = clause.namedBindings;
|
|
191
|
+
if (!bindings) return;
|
|
192
|
+
if (ts.isNamespaceImport(bindings)) return this.#imports.set(bindings.name.text, { specifier, imported: "*" });
|
|
193
|
+
for (const element of bindings.elements)
|
|
194
|
+
if (!element.isTypeOnly)
|
|
195
|
+
this.#imports.set(element.name.text, { specifier, imported: (element.propertyName ?? element.name).text });
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
#collectImportEquals(statement: ts.ImportEqualsDeclaration) {
|
|
199
|
+
if (statement.isTypeOnly) return;
|
|
200
|
+
const reference = statement.moduleReference;
|
|
201
|
+
if (ts.isExternalModuleReference(reference) && ts.isStringLiteral(reference.expression))
|
|
202
|
+
this.#imports.set(statement.name.text, { specifier: reference.expression.text, imported: "*" });
|
|
203
|
+
else this.#declare(statement.name.text, ExportReach.#unitOf(statement));
|
|
204
|
+
if (ExportReach.#has(statement, ts.SyntaxKind.ExportKeyword))
|
|
205
|
+
this.#exported.set(statement.name.text, statement.name.text);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
#collectExportList(statement: ts.ExportDeclaration) {
|
|
209
|
+
const from = statement.moduleSpecifier;
|
|
210
|
+
const specifier = from && ts.isStringLiteral(from) ? from.text : null;
|
|
211
|
+
const clause = statement.exportClause;
|
|
212
|
+
if (!clause) {
|
|
213
|
+
if (specifier && !statement.isTypeOnly) this.stars.push(specifier);
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
if (ts.isNamespaceExport(clause)) {
|
|
217
|
+
if (statement.isTypeOnly) this.#types.add(clause.name.text);
|
|
218
|
+
else if (specifier) this.#reexports.set(clause.name.text, { specifier, imported: "*" });
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
for (const element of clause.elements) {
|
|
222
|
+
const exported = element.name.text;
|
|
223
|
+
const local = (element.propertyName ?? element.name).text;
|
|
224
|
+
if (statement.isTypeOnly || element.isTypeOnly) this.#types.add(exported);
|
|
225
|
+
else if (specifier) this.#reexports.set(exported, { specifier, imported: local });
|
|
226
|
+
else this.#exported.set(exported, local);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
#collectVariables(statement: ts.VariableStatement) {
|
|
231
|
+
const exported = ExportReach.#has(statement, ts.SyntaxKind.ExportKeyword);
|
|
232
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
233
|
+
const unit = ExportReach.#unitOf(declaration);
|
|
234
|
+
for (const name of ExportReach.#boundNames(declaration.name)) {
|
|
235
|
+
this.#declare(name, unit);
|
|
236
|
+
if (exported) this.#exported.set(name, name);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
static #boundNames(name: ts.BindingName): string[] {
|
|
242
|
+
if (ts.isIdentifier(name)) return [name.text];
|
|
243
|
+
return name.elements.flatMap((element) =>
|
|
244
|
+
ts.isOmittedExpression(element) ? [] : ExportReach.#boundNames(element.name),
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
static #unitOf(root: ts.Node): Unit {
|
|
249
|
+
const unit: Unit = { reads: new Map(), dynamic: [] };
|
|
250
|
+
const read = (name: string, member: string | null) => {
|
|
251
|
+
const known = unit.reads.get(name);
|
|
252
|
+
if (member === null || known === null) unit.reads.set(name, null);
|
|
253
|
+
else unit.reads.set(name, (known ?? new Set<string>()).add(member));
|
|
254
|
+
};
|
|
255
|
+
const visit = (node: ts.Node): void => {
|
|
256
|
+
if (ts.isExpressionWithTypeArguments(node)) {
|
|
257
|
+
visit(node.expression);
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
if (ts.isTypeNode(node) || ts.isInterfaceDeclaration(node) || ts.isTypeAliasDeclaration(node)) return;
|
|
261
|
+
if (ts.isIdentifier(node)) {
|
|
262
|
+
read(node.text, null);
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
if (ts.isPropertyAccessExpression(node)) {
|
|
266
|
+
if (ts.isIdentifier(node.expression)) read(node.expression.text, node.name.text);
|
|
267
|
+
else visit(node.expression);
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
if (ts.isCallExpression(node)) {
|
|
271
|
+
const [first] = node.arguments;
|
|
272
|
+
const loads =
|
|
273
|
+
node.expression.kind === ts.SyntaxKind.ImportKeyword ||
|
|
274
|
+
(ts.isIdentifier(node.expression) && node.expression.text === "require");
|
|
275
|
+
if (loads && first && ts.isStringLiteralLike(first)) unit.dynamic.push(first.text);
|
|
276
|
+
}
|
|
277
|
+
if (ts.isPropertyAssignment(node) || ts.isJsxAttribute(node)) {
|
|
278
|
+
if (ts.isComputedPropertyName(node.name)) visit(node.name);
|
|
279
|
+
if (node.initializer) visit(node.initializer);
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
if (ts.isBindingElement(node)) {
|
|
283
|
+
if (node.propertyName && ts.isComputedPropertyName(node.propertyName)) visit(node.propertyName);
|
|
284
|
+
visit(node.name);
|
|
285
|
+
if (node.initializer) visit(node.initializer);
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
if (
|
|
289
|
+
(ts.isMethodDeclaration(node) || ts.isPropertyDeclaration(node) || ts.isAccessor(node)) &&
|
|
290
|
+
!ts.isComputedPropertyName(node.name)
|
|
291
|
+
) {
|
|
292
|
+
node.forEachChild((child) => (child === node.name ? undefined : visit(child)));
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
node.forEachChild(visit);
|
|
296
|
+
};
|
|
297
|
+
visit(root);
|
|
298
|
+
return unit;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
static #has(node: ts.Node, kind: ts.SyntaxKind) {
|
|
302
|
+
return ts.canHaveModifiers(node) && (ts.getModifiers(node) ?? []).some((modifier) => modifier.kind === kind);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
@@ -2,7 +2,10 @@ import { describe, expect, test } from "bun:test";
|
|
|
2
2
|
import { mkdir, realpath, symlink } from "node:fs/promises";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { printedInOwnProcess, tempDirs, writeText as write } from "../test/testHelpers";
|
|
5
|
+
import { BarrelAnalyzer } from "./barrelAnalyzer";
|
|
6
|
+
import { createBarrelImportsPlugin } from "./barrelImportsPlugin";
|
|
5
7
|
import { type ObjectKeepReason, ObjectMemberImports } from "./objectMemberImports";
|
|
8
|
+
import { resolveModuleFile } from "./objectModuleAnalyzer";
|
|
6
9
|
|
|
7
10
|
const makeTempRoot = tempDirs("akan-object-member-imports-");
|
|
8
11
|
|
|
@@ -31,14 +34,15 @@ const writeObjects = async () => {
|
|
|
31
34
|
"ui/Scroll/Render.tsx": "export const Render = () => null;\n",
|
|
32
35
|
"ui/Boundary/index.tsx": '"use client";\nimport { Part } from "./Part";\nexport const Boundary = { Part };\n',
|
|
33
36
|
"ui/Boundary/Part.tsx": "export const Part = () => null;\n",
|
|
34
|
-
"ui/
|
|
35
|
-
'import {
|
|
37
|
+
"ui/Observed/index.tsx": [
|
|
38
|
+
'import { observer } from "./observer";',
|
|
36
39
|
'import { A } from "./A";',
|
|
37
40
|
'import { B } from "./B";',
|
|
38
|
-
"export const
|
|
41
|
+
"export const Observed = { A: observer(A), B };",
|
|
39
42
|
].join("\n"),
|
|
40
|
-
"ui/
|
|
41
|
-
"ui/
|
|
43
|
+
"ui/Observed/A.tsx": "export const A = () => null;\n",
|
|
44
|
+
"ui/Observed/B.tsx": "export const B = () => null;\n",
|
|
45
|
+
"ui/Observed/observer.ts": "export const observer = <T,>(value: T) => value;\n",
|
|
42
46
|
"ui/settings.ts": "export const settings = { columns: 2 };\n",
|
|
43
47
|
"ui/Runner/index.ts": [
|
|
44
48
|
'import { run } from "./run";',
|
|
@@ -148,8 +152,8 @@ describe("ObjectMemberImports", () => {
|
|
|
148
152
|
expect(await keptFor('import { Scroll } from "../ui/Scroll";', "<Scroll><Scroll.Render /></Scroll>")).toEqual([
|
|
149
153
|
{ object: "Scroll", file: fileOf("ui/Scroll/index.tsx"), line: 2, reason: "rendered-or-called" },
|
|
150
154
|
]);
|
|
151
|
-
expect(await keptFor('import {
|
|
152
|
-
{ object: "
|
|
155
|
+
expect(await keptFor('import { Observed } from "../ui/Observed";', "<Observed.B />")).toEqual([
|
|
156
|
+
{ object: "Observed", file: fileOf("ui/Observed/index.tsx"), line: 1, reason: "call" },
|
|
153
157
|
]);
|
|
154
158
|
});
|
|
155
159
|
|
|
@@ -256,6 +260,284 @@ describe("ObjectMemberImports", () => {
|
|
|
256
260
|
});
|
|
257
261
|
});
|
|
258
262
|
|
|
263
|
+
const writeWrappers = async () => {
|
|
264
|
+
const root = await realpath(await makeTempRoot());
|
|
265
|
+
const client = (name: string) => `"use client";\nexport const ${name} = () => null;\n`;
|
|
266
|
+
const plain = (name: string) => `export const ${name} = () => null;\n`;
|
|
267
|
+
const copying = (name: string, from: string[], members = "{}") =>
|
|
268
|
+
[
|
|
269
|
+
...from.map((source) => `import { ${source} } from "./${source}";`),
|
|
270
|
+
`export const ${name} = Object.assign({}, ${[...from, members].join(", ")});`,
|
|
271
|
+
].join("\n");
|
|
272
|
+
const files: Record<string, string> = {
|
|
273
|
+
"ui/index.ts": 'export * from "./Field";\nexport * as Parts from "./Parts";\n',
|
|
274
|
+
"ui/Parts.ts": plain("Text"),
|
|
275
|
+
"ui/Field/index.tsx": [
|
|
276
|
+
'"use client";',
|
|
277
|
+
'import { Label } from "./Label";',
|
|
278
|
+
'import { Text } from "./Text";',
|
|
279
|
+
'import { Zone } from "./Zone";',
|
|
280
|
+
"const FieldSection = () => null;",
|
|
281
|
+
"export const Field = Object.assign(FieldSection, { Label, Text, Doc: { Zone } });",
|
|
282
|
+
].join("\n"),
|
|
283
|
+
"ui/Field/Label.tsx": client("Label"),
|
|
284
|
+
"ui/Field/Text.tsx": client("Text"),
|
|
285
|
+
"ui/Field/TextNext.tsx": client("Text"),
|
|
286
|
+
"ui/Field/Zone.tsx": client("Zone"),
|
|
287
|
+
"ui/FieldNext/index.tsx": 'import { Text } from "./Text";\nexport const Field = { Text };\n',
|
|
288
|
+
"ui/FieldNext/Text.tsx": plain("Text"),
|
|
289
|
+
"wrap/Field.tsx": [
|
|
290
|
+
'"use client";',
|
|
291
|
+
'import { memo } from "react";',
|
|
292
|
+
'import { Field as BaseField } from "../ui/Field";',
|
|
293
|
+
'import { Editor } from "./Editor";',
|
|
294
|
+
'import { Label } from "./Label";',
|
|
295
|
+
'import { Upload } from "./Upload";',
|
|
296
|
+
"const Rich = memo(() => <Editor />);",
|
|
297
|
+
"const Section = () => <BaseField />;",
|
|
298
|
+
"export const Field = Object.assign(Section, { Upload }, BaseField, { Rich, Label });",
|
|
299
|
+
].join("\n"),
|
|
300
|
+
"wrap/Editor.tsx": client("Editor"),
|
|
301
|
+
"wrap/Label.tsx": client("Label"),
|
|
302
|
+
"wrap/Upload.tsx": client("Upload"),
|
|
303
|
+
"barrel/Field.tsx": [
|
|
304
|
+
'import { Field as BaseField } from "@demo/ui";',
|
|
305
|
+
'import { Label } from "../wrap/Label";',
|
|
306
|
+
"export const Field = Object.assign(() => null, BaseField, { Label });",
|
|
307
|
+
].join("\n"),
|
|
308
|
+
"barrel/Parts.ts": [
|
|
309
|
+
'import { Parts as BaseParts } from "@demo/ui";',
|
|
310
|
+
'import { Label } from "../wrap/Label";',
|
|
311
|
+
"export const Parts = Object.assign({}, BaseParts, { Label });",
|
|
312
|
+
].join("\n"),
|
|
313
|
+
"copies/A.ts": plain("A"),
|
|
314
|
+
"copies/B.ts": plain("B"),
|
|
315
|
+
"copies/Shared.ts": plain("Shared"),
|
|
316
|
+
"copies/SharedNext.ts": plain("Shared"),
|
|
317
|
+
"copies/rest.ts": plain("Rest"),
|
|
318
|
+
"copies/First.ts":
|
|
319
|
+
'import { A } from "./A";\nimport { Shared } from "./Shared";\nexport const First = { A, Shared };\n',
|
|
320
|
+
"copies/Second.ts":
|
|
321
|
+
'import { B } from "./B";\nimport { Shared } from "./SharedNext";\nexport const Second = { B, Shared };\n',
|
|
322
|
+
"copies/Both.ts": copying("Both", ["First", "Second"]),
|
|
323
|
+
"copies/Failing.ts": 'import { A } from "./A";\nexport const Failing = { A };\nregister(Failing);\n',
|
|
324
|
+
"copies/OverFailing.ts": `import { B } from "./B";\n${copying("OverFailing", ["Failing"], "{ B }")}`,
|
|
325
|
+
"copies/Open.ts":
|
|
326
|
+
'import { A } from "./A";\nimport * as rest from "./rest";\nexport const Open = { ...rest, A };\n',
|
|
327
|
+
"copies/OverOpen.ts": copying("OverOpen", ["Open"]),
|
|
328
|
+
"copies/Plain.ts": plain("Plain"),
|
|
329
|
+
"copies/OverPlain.ts": `import { B } from "./B";\n${copying("OverPlain", ["Plain"], "{ B }")}`,
|
|
330
|
+
"copies/Left.ts": `import { A } from "./A";\n${copying("Left", ["Right"], "{ A }")}`,
|
|
331
|
+
"copies/Right.ts": `import { B } from "./B";\n${copying("Right", ["Left"], "{ B }")}`,
|
|
332
|
+
"copies/Deep5.ts": 'import { A } from "./A";\nexport const Deep5 = { A };\n',
|
|
333
|
+
...Object.fromEntries(
|
|
334
|
+
[1, 2, 3, 4].map((depth) => [`copies/Deep${depth}.ts`, copying(`Deep${depth}`, [`Deep${depth + 1}`])]),
|
|
335
|
+
),
|
|
336
|
+
"copies/Deep0.ts": `import { B } from "./B";\n${copying("Deep0", ["Deep1"], "{ B }")}`,
|
|
337
|
+
"copies/Values.ts": "export const Values = { columns: 2 };\n",
|
|
338
|
+
"copies/OverValues.ts": copying("OverValues", ["Values"], "{ rows: 3 }"),
|
|
339
|
+
"boundary/Server.ts": 'import { Part } from "./Part";\nexport const Server = { Part };\n',
|
|
340
|
+
"boundary/Client.ts": '"use client";\nimport { Part } from "./Part";\nexport const Client = { Part };\n',
|
|
341
|
+
"boundary/Part.tsx": plain("Part"),
|
|
342
|
+
"boundary/ClientOverServer.ts": `"use client";\n${copying("ClientOverServer", ["Server"])}`,
|
|
343
|
+
"boundary/ServerOverClient.ts": copying("ServerOverClient", ["Client"]),
|
|
344
|
+
};
|
|
345
|
+
for (const [file, content] of Object.entries(files)) await write(path.join(root, file), content);
|
|
346
|
+
const fileOf = (file: string) => path.join(root, file);
|
|
347
|
+
const resolvePackage = async (pkgName: string) => {
|
|
348
|
+
if (pkgName !== "@demo/ui" && !pkgName.startsWith("@demo/ui/")) return null;
|
|
349
|
+
const entryFile = await resolveModuleFile(fileOf("x"), `./ui${pkgName.slice("@demo/ui".length)}`);
|
|
350
|
+
return entryFile ? { pkgName, entryFile, pkgDir: fileOf("ui") } : null;
|
|
351
|
+
};
|
|
352
|
+
const objectsOf = ({ barrels = ["@demo/ui"] }: { barrels?: string[] } = {}) =>
|
|
353
|
+
new ObjectMemberImports({
|
|
354
|
+
resolve: ObjectMemberImports.resolverOf(resolvePackage),
|
|
355
|
+
barrels: { names: barrels, analyzer: new BarrelAnalyzer({ resolvePackage }) },
|
|
356
|
+
});
|
|
357
|
+
const rewrite = (lines: string[], objects = objectsOf()) =>
|
|
358
|
+
objects.rewrite(lines.join("\n"), fileOf("page/_index.tsx"));
|
|
359
|
+
const answerOf = async (
|
|
360
|
+
object: string,
|
|
361
|
+
member: string,
|
|
362
|
+
{ dir = "copies", head = "" } = {},
|
|
363
|
+
): Promise<string | null> => {
|
|
364
|
+
const result = await rewrite([
|
|
365
|
+
`${head}import { ${object} } from "../${dir}/${object}";`,
|
|
366
|
+
`export const value = ${object}.${member};`,
|
|
367
|
+
]);
|
|
368
|
+
const [kept] = result?.kept ?? [];
|
|
369
|
+
const narrowedTo = result?.contents?.match(/ from "([^"]+)";/)?.[1];
|
|
370
|
+
return kept?.reason ?? (narrowedTo ? path.relative(root, narrowedTo) : null);
|
|
371
|
+
};
|
|
372
|
+
return { root, fileOf, objectsOf, rewrite, answerOf };
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
describe("ObjectMemberImports on an object that copies an imported one in", () => {
|
|
376
|
+
test("imports a member of the copied object from that object's part file, and a member the wrapper lists after the copy from the wrapper's", async () => {
|
|
377
|
+
const { root, fileOf, rewrite } = await writeWrappers();
|
|
378
|
+
|
|
379
|
+
const result = await rewrite([
|
|
380
|
+
'import { Field } from "../wrap/Field";',
|
|
381
|
+
"export const Page = () => <Field.Text><Field.Label /><Field.Doc.Zone /></Field.Text>;",
|
|
382
|
+
]);
|
|
383
|
+
|
|
384
|
+
expect(result?.contents?.split("\n")).toEqual([
|
|
385
|
+
`import { Text as Field$Text } from ${JSON.stringify(fileOf("ui/Field/Text.tsx"))};`,
|
|
386
|
+
`import { Label as Field$Label } from ${JSON.stringify(fileOf("wrap/Label.tsx"))};`,
|
|
387
|
+
`import { Zone as Field$Doc$Zone } from ${JSON.stringify(fileOf("ui/Field/Zone.tsx"))};`,
|
|
388
|
+
"export const Page = () => <Field$Text><Field$Label /><Field$Doc$Zone /></Field$Text>;",
|
|
389
|
+
]);
|
|
390
|
+
expect(result?.narrowed).toEqual([
|
|
391
|
+
{ object: "Field", file: fileOf("wrap/Field.tsx"), members: ["Doc.Zone", "Label", "Text"] },
|
|
392
|
+
]);
|
|
393
|
+
expect(result?.read.map((file) => path.relative(root, file)).sort()).toEqual([
|
|
394
|
+
"ui/Field/Text.tsx",
|
|
395
|
+
"ui/Field/Zone.tsx",
|
|
396
|
+
"ui/Field/index.tsx",
|
|
397
|
+
"wrap/Field.tsx",
|
|
398
|
+
"wrap/Label.tsx",
|
|
399
|
+
]);
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
test("keeps the whole wrapper for a member declared in its own file, one it lists before the copy, and one no object holds", async () => {
|
|
403
|
+
const { answerOf } = await writeWrappers();
|
|
404
|
+
|
|
405
|
+
for (const member of ["Rich", "Upload", "Missing", "Doc.Missing"])
|
|
406
|
+
expect(await answerOf("Field", member, { dir: "wrap" })).toBe("unresolved-member");
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
test("reads two copied objects last one first, as `Object.assign` lets the later source win", async () => {
|
|
410
|
+
const { answerOf } = await writeWrappers();
|
|
411
|
+
|
|
412
|
+
expect(await answerOf("Both", "A")).toBe("copies/A.ts");
|
|
413
|
+
expect(await answerOf("Both", "B")).toBe("copies/B.ts");
|
|
414
|
+
expect(await answerOf("Both", "Shared")).toBe("copies/SharedNext.ts");
|
|
415
|
+
expect(await answerOf("Both", "Missing")).toBe("unresolved-member");
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
test("keeps the whole wrapper for a member of a copied object whose module fails the rule, lists no such member, or is no object module", async () => {
|
|
419
|
+
const { answerOf } = await writeWrappers();
|
|
420
|
+
|
|
421
|
+
expect(await answerOf("OverFailing", "A")).toBe("effect-statement");
|
|
422
|
+
expect(await answerOf("OverFailing", "B")).toBe("copies/B.ts");
|
|
423
|
+
expect(await answerOf("OverOpen", "A")).toBe("copies/A.ts");
|
|
424
|
+
expect(await answerOf("OverOpen", "Rest")).toBe("unresolved-member");
|
|
425
|
+
expect(await answerOf("OverPlain", "B")).toBe("copies/B.ts");
|
|
426
|
+
expect(await answerOf("OverPlain", "displayName")).toBe("unresolved-member");
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
test("stops at an object that copies itself in through another, and four copies down", async () => {
|
|
430
|
+
const { answerOf } = await writeWrappers();
|
|
431
|
+
|
|
432
|
+
expect(await answerOf("Left", "A")).toBe("copies/A.ts");
|
|
433
|
+
expect(await answerOf("Left", "B")).toBe("copies/B.ts");
|
|
434
|
+
expect(await answerOf("Left", "Missing")).toBe("unresolved-member");
|
|
435
|
+
expect(await answerOf("Deep1", "A")).toBe("copies/A.ts");
|
|
436
|
+
expect(await answerOf("Deep0", "A")).toBe("unresolved-member");
|
|
437
|
+
expect(await answerOf("Deep0", "B")).toBe("copies/B.ts");
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
test("leaves a wrapper alone when neither it nor an object it copies in points a member at a file", async () => {
|
|
441
|
+
const { rewrite } = await writeWrappers();
|
|
442
|
+
|
|
443
|
+
expect(
|
|
444
|
+
await rewrite(['import { OverValues } from "../copies/OverValues";', "export const n = OverValues.columns;"]),
|
|
445
|
+
).toBeNull();
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
test("finds an object copied in from a barrel at the file the barrel declares it in, as an importer's own barrel import is", async () => {
|
|
449
|
+
const { fileOf, objectsOf, rewrite } = await writeWrappers();
|
|
450
|
+
const lines = ['import { Field } from "../barrel/Field";', "export const Page = () => <Field.Text />;"];
|
|
451
|
+
|
|
452
|
+
const narrowed = await rewrite(lines);
|
|
453
|
+
const unlisted = await rewrite(lines, objectsOf({ barrels: [] }));
|
|
454
|
+
const namespace = await rewrite(['import { Parts } from "../barrel/Parts";', "export const text = Parts.Text;"]);
|
|
455
|
+
|
|
456
|
+
expect(narrowed?.contents).toContain(
|
|
457
|
+
`import { Text as Field$Text } from ${JSON.stringify(fileOf("ui/Field/Text.tsx"))};`,
|
|
458
|
+
);
|
|
459
|
+
expect(narrowed?.barrels).toEqual(["@demo/ui"]);
|
|
460
|
+
expect(unlisted?.kept.map(({ reason }) => reason)).toEqual(["unresolved-member"]);
|
|
461
|
+
expect(unlisted?.barrels).toEqual([]);
|
|
462
|
+
expect(namespace?.kept.map(({ reason }) => reason)).toEqual(["unresolved-member"]);
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
test('narrows past a "use client" module anywhere between a server importer and the member only to a client file', async () => {
|
|
466
|
+
const { answerOf } = await writeWrappers();
|
|
467
|
+
const client = { dir: "boundary", head: '"use client";\n' };
|
|
468
|
+
|
|
469
|
+
expect(await answerOf("Field", "Text", { dir: "wrap" })).toBe("ui/Field/Text.tsx");
|
|
470
|
+
expect(await answerOf("ClientOverServer", "Part", { dir: "boundary" })).toBe("server-boundary");
|
|
471
|
+
expect(await answerOf("ServerOverClient", "Part", { dir: "boundary" })).toBe("server-boundary");
|
|
472
|
+
expect(await answerOf("ClientOverServer", "Part", client)).toBe("boundary/Part.tsx");
|
|
473
|
+
expect(await answerOf("ServerOverClient", "Part", client)).toBe("boundary/Part.tsx");
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
test("counts the copied object's module, its part files and the barrel among a wrapper's files, so a part moved there moves the wrapper's digest and fingerprint", async () => {
|
|
477
|
+
const { root, fileOf, objectsOf } = await writeWrappers();
|
|
478
|
+
const wrapper = fileOf("barrel/Field.tsx");
|
|
479
|
+
const stateOf = async () => {
|
|
480
|
+
const objects = objectsOf();
|
|
481
|
+
return [await objects.digest(wrapper), await objects.fingerprint(wrapper, "Field", ["Text"], true)];
|
|
482
|
+
};
|
|
483
|
+
|
|
484
|
+
const files = await objectsOf().filesOf(wrapper);
|
|
485
|
+
expect(new Set(files.map((file) => path.relative(root, file)))).toEqual(
|
|
486
|
+
new Set([
|
|
487
|
+
"barrel/Field.tsx",
|
|
488
|
+
"ui/Field/Label.tsx",
|
|
489
|
+
"ui/Field/Text.tsx",
|
|
490
|
+
"ui/Field/Zone.tsx",
|
|
491
|
+
"ui/Field/index.tsx",
|
|
492
|
+
"ui/index.ts",
|
|
493
|
+
"wrap/Label.tsx",
|
|
494
|
+
]),
|
|
495
|
+
);
|
|
496
|
+
const before = await stateOf();
|
|
497
|
+
await write(fileOf("ui/Field/Text.tsx"), '"use client";\nexport const Text = () => "saved";\n');
|
|
498
|
+
expect(await stateOf()).toEqual(before);
|
|
499
|
+
|
|
500
|
+
const index = await Bun.file(fileOf("ui/Field/index.tsx")).text();
|
|
501
|
+
await write(fileOf("ui/Field/index.tsx"), index.replace("./Text", "./TextNext"));
|
|
502
|
+
const moved = await stateOf();
|
|
503
|
+
expect(moved[0]).not.toBe(before[0]);
|
|
504
|
+
expect(moved[1]).not.toBe(before[1]);
|
|
505
|
+
|
|
506
|
+
await write(fileOf("ui/index.ts"), 'export * from "./FieldNext";\n');
|
|
507
|
+
const redeclared = await stateOf();
|
|
508
|
+
expect(redeclared[0]).not.toBe(moved[0]);
|
|
509
|
+
expect(redeclared[1]).not.toBe(moved[1]);
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
test("a bundle built with the barrel plugin holds the parts a page names through such a wrapper, and neither the wrapper nor the copied object", async () => {
|
|
513
|
+
const { root, fileOf } = await writeWrappers();
|
|
514
|
+
await write(
|
|
515
|
+
fileOf("page/_index.tsx"),
|
|
516
|
+
'import { Field } from "../barrel/Field";\nexport const parts = [Field.Text, Field.Label];\n',
|
|
517
|
+
);
|
|
518
|
+
const app = {
|
|
519
|
+
name: "demo",
|
|
520
|
+
cwdPath: root,
|
|
521
|
+
workspace: { workspaceRoot: root },
|
|
522
|
+
getConfig: async () => ({ barrelImports: ["@demo/ui"] }),
|
|
523
|
+
getTsConfig: async () => ({
|
|
524
|
+
compilerOptions: { paths: { "@demo/ui": ["./ui/index.ts"], "@demo/ui/*": ["./ui/*"] } },
|
|
525
|
+
}),
|
|
526
|
+
};
|
|
527
|
+
|
|
528
|
+
const built = await Bun.build({
|
|
529
|
+
entrypoints: [fileOf("page/_index.tsx")],
|
|
530
|
+
target: "browser",
|
|
531
|
+
metafile: true,
|
|
532
|
+
throw: true,
|
|
533
|
+
plugins: [await createBarrelImportsPlugin(app as never)],
|
|
534
|
+
});
|
|
535
|
+
|
|
536
|
+
const inputs = Object.keys(built.metafile?.inputs ?? {}).map((input) => path.relative(root, path.resolve(input)));
|
|
537
|
+
expect(inputs.sort()).toEqual(["page/_index.tsx", "ui/Field/Text.tsx", "wrap/Label.tsx"]);
|
|
538
|
+
});
|
|
539
|
+
});
|
|
540
|
+
|
|
259
541
|
describe("ObjectMemberImports on a server-rendered page", () => {
|
|
260
542
|
const repoRoot = path.resolve(import.meta.dir, "../../../..");
|
|
261
543
|
|