@tinkered/cli 0.29.3 → 0.30.1
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/README.md +39 -0
- package/dist/cli.mjs +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/{main-BO5BHvEC.mjs → main-DmBjD1BM.mjs} +188 -144
- package/dist/vite.d.mts +10 -0
- package/dist/vite.mjs +37 -0
- package/package.json +8 -3
package/README.md
CHANGED
|
@@ -228,6 +228,11 @@ Intent finds them (ADR 0143).
|
|
|
228
228
|
State lives in a Core cell.
|
|
229
229
|
- `tinker/gap` — a gap marker sits on an open
|
|
230
230
|
row: see below.
|
|
231
|
+
- `tinker/barrel` — a file in `src/` or `cmds/`
|
|
232
|
+
exports only what it declares. A re-export is
|
|
233
|
+
a finding: `export … from "…"`, of values or
|
|
234
|
+
types, or `export { name }` of an imported
|
|
235
|
+
name (ADR 0160).
|
|
231
236
|
|
|
232
237
|
A page file is a file in `src/` with no `.server`
|
|
233
238
|
in its name.
|
|
@@ -369,6 +374,40 @@ const answer = await fetch("/api/auth");
|
|
|
369
374
|
Each run prints the counts:
|
|
370
375
|
`gaps: G1 3/3, G3 5/5`.
|
|
371
376
|
|
|
377
|
+
## The page guard
|
|
378
|
+
|
|
379
|
+
A page build can take server code by accident,
|
|
380
|
+
through an import or a re-export. `pageGuard()`
|
|
381
|
+
stops it (ADR 0160).
|
|
382
|
+
|
|
383
|
+
```ts
|
|
384
|
+
import { pageGuard } from "@tinkered/cli/vite";
|
|
385
|
+
|
|
386
|
+
export default defineConfig({
|
|
387
|
+
plugins: [pageGuard()],
|
|
388
|
+
});
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
It runs only where the page is built: in dev, in
|
|
392
|
+
the build, and in browser tests. It refuses an
|
|
393
|
+
import of:
|
|
394
|
+
|
|
395
|
+
- a file with a `.server.` part in its name;
|
|
396
|
+
- a `node:` or `bun:` name;
|
|
397
|
+
- `@tinkered/entry`, or one of its entries;
|
|
398
|
+
- `@tinkered/bun`, but not `@tinkered/bun/client`.
|
|
399
|
+
|
|
400
|
+
The build error names the import and its file:
|
|
401
|
+
|
|
402
|
+
```text
|
|
403
|
+
page imports server code "./db.server"
|
|
404
|
+
from src/todo/barrel.ts
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
An `import type` is gone before the build reads
|
|
408
|
+
imports. So a page may take types from a server
|
|
409
|
+
file.
|
|
410
|
+
|
|
372
411
|
## Lint rules
|
|
373
412
|
|
|
374
413
|
`@tinkered/cli/lint` is an Oxlint plugin.
|
package/dist/cli.mjs
CHANGED
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
//#region src/rules.d.ts
|
|
2
2
|
/** Tinker's own rules: `check` finds them in the app's files. */
|
|
3
|
-
declare const own: readonly ["tinker/plain", "tinker/one-wire", "tinker/react-state", "tinker/gap"];
|
|
3
|
+
declare const own: readonly ["tinker/plain", "tinker/one-wire", "tinker/react-state", "tinker/gap", "tinker/barrel"];
|
|
4
4
|
//#endregion
|
|
5
5
|
//#region src/check.d.ts
|
|
6
6
|
declare namespace Check {
|
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { n as check, r as isError, t as main } from "./main-
|
|
1
|
+
import { n as check, r as isError, t as main } from "./main-DmBjD1BM.mjs";
|
|
2
2
|
export { check, isError, main };
|
|
@@ -4,6 +4,45 @@ import ts from "typescript-api";
|
|
|
4
4
|
import { IntentCoreError, listIntentSkills, loadIntentSkill } from "@tanstack/intent/core";
|
|
5
5
|
import { spawnSync } from "node:child_process";
|
|
6
6
|
import { tmpdir } from "node:os";
|
|
7
|
+
//#region src/barrel.ts
|
|
8
|
+
/** The names one import binds: its default, its namespace, and each named one. */
|
|
9
|
+
function boundBy({ name, namedBindings }) {
|
|
10
|
+
const names = name ? [name.text] : [];
|
|
11
|
+
if (!namedBindings) return names;
|
|
12
|
+
if (ts.isNamespaceImport(namedBindings)) return [...names, namedBindings.name.text];
|
|
13
|
+
return [...names, ...namedBindings.elements.map((element) => element.name.text)];
|
|
14
|
+
}
|
|
15
|
+
function importedNames(source) {
|
|
16
|
+
return new Set(source.statements.flatMap((statement) => ts.isImportDeclaration(statement) && statement.importClause ? boundBy(statement.importClause) : []));
|
|
17
|
+
}
|
|
18
|
+
/** `export { a, b as c }` with no `from`: it passes on an import when one local name is one. */
|
|
19
|
+
function passesOn(imported, statement) {
|
|
20
|
+
const list = statement.exportClause;
|
|
21
|
+
if (!list || !ts.isNamedExports(list)) return false;
|
|
22
|
+
return list.elements.some(({ propertyName, name }) => imported.has((propertyName ?? name).text));
|
|
23
|
+
}
|
|
24
|
+
/** A statement that exports what another module declares. */
|
|
25
|
+
function isBarrel(imported, statement) {
|
|
26
|
+
if (!ts.isExportDeclaration(statement)) return false;
|
|
27
|
+
return Boolean(statement.moduleSpecifier) || passesOn(imported, statement);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* `tinker/barrel` (ADR 0160): app code has no barrel. A barrel hides which file an import
|
|
31
|
+
* reaches, so a page can take server code by accident. A finding is `export … from "…"` of
|
|
32
|
+
* values or of types, and `export { name }` of an imported name. A file that exports what it
|
|
33
|
+
* declares passes, as `export const plus = add` does. Only top-level statements can export.
|
|
34
|
+
*/
|
|
35
|
+
function findBarrel({ path, text }) {
|
|
36
|
+
const source = ts.createSourceFile(path, text, ts.ScriptTarget.Latest, true);
|
|
37
|
+
const imported = importedNames(source);
|
|
38
|
+
return source.statements.filter((statement) => isBarrel(imported, statement)).map((statement) => ({
|
|
39
|
+
path,
|
|
40
|
+
line: source.getLineAndCharacterOfPosition(statement.getStart()).line + 1,
|
|
41
|
+
rule: "tinker/barrel",
|
|
42
|
+
what: `\`${statement.getText(source).replace(/\s+/g, " ").replace(/;$/, "")}\`: a re-export`
|
|
43
|
+
}));
|
|
44
|
+
}
|
|
45
|
+
//#endregion
|
|
7
46
|
//#region src/errors.ts
|
|
8
47
|
/** The only place the command throws a registry error. */
|
|
9
48
|
function raise(kind, payload) {
|
|
@@ -17,6 +56,148 @@ function isError(value, kind) {
|
|
|
17
56
|
return value instanceof Error && value.kind === kind;
|
|
18
57
|
}
|
|
19
58
|
//#endregion
|
|
59
|
+
//#region src/exits.ts
|
|
60
|
+
/** The exit of a return with no `kind` of its own. */
|
|
61
|
+
const plain = "value";
|
|
62
|
+
/** A hook's `refuse(payload)`, by the name Core gives its type. */
|
|
63
|
+
const refusal = "Refused";
|
|
64
|
+
/**
|
|
65
|
+
* The app's own code. The base under `tinker/` is Tinker's, and moves into a package that has
|
|
66
|
+
* its own tests (ADR 0159).
|
|
67
|
+
*/
|
|
68
|
+
const appCode = /^(?:src|cmds)\//;
|
|
69
|
+
/** A test file declares no exit: its operations are fixtures. */
|
|
70
|
+
const testFile = /\.test\.[mc]?tsx?$/;
|
|
71
|
+
function kindsOf(checker, type) {
|
|
72
|
+
const kind = type.getProperty("kind");
|
|
73
|
+
if (!kind) return [];
|
|
74
|
+
const of = checker.getTypeOfSymbol(kind);
|
|
75
|
+
const parts = of.isUnion() ? of.types : [of];
|
|
76
|
+
return parts.every((part) => part.isStringLiteral()) ? parts.map((part) => part.value) : [];
|
|
77
|
+
}
|
|
78
|
+
/** Each exit a return type holds: a union gives one per member, and a promise gives its value's. */
|
|
79
|
+
function exitsOf(checker, returned) {
|
|
80
|
+
const type = checker.getAwaitedType(returned) ?? returned;
|
|
81
|
+
if (type.flags & ts.TypeFlags.Never) return [];
|
|
82
|
+
const exits = /* @__PURE__ */ new Set();
|
|
83
|
+
for (const part of type.isUnion() ? type.types : [type]) {
|
|
84
|
+
if (checker.typeToString(part) === refusal) {
|
|
85
|
+
exits.add("refused");
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const kinds = kindsOf(checker, part);
|
|
89
|
+
for (const kind of kinds.length ? kinds : [plain]) exits.add(kind);
|
|
90
|
+
}
|
|
91
|
+
return [...exits].sort();
|
|
92
|
+
}
|
|
93
|
+
/** The names a file imports from Core. */
|
|
94
|
+
function coreImports(source) {
|
|
95
|
+
return source.statements.flatMap((statement) => {
|
|
96
|
+
if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier)) return [];
|
|
97
|
+
const names = statement.importClause?.namedBindings;
|
|
98
|
+
return statement.moduleSpecifier.text === "@tinkered/core" && names && ts.isNamedImports(names) ? [...names.elements] : [];
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
/** The name `operation` has in this file, when it comes from Core. */
|
|
102
|
+
function coreOperation(source) {
|
|
103
|
+
return coreImports(source).find((element) => (element.propertyName ?? element.name).text === "operation")?.name.text;
|
|
104
|
+
}
|
|
105
|
+
function member(options, name) {
|
|
106
|
+
return options.properties.find((property) => property.name && ts.isIdentifier(property.name) && property.name.text === name);
|
|
107
|
+
}
|
|
108
|
+
/** The return type of an operation's `run`, written as a property or as a method. */
|
|
109
|
+
function returnOf(checker, run) {
|
|
110
|
+
if (ts.isMethodDeclaration(run)) return checker.getSignatureFromDeclaration(run)?.getReturnType();
|
|
111
|
+
if (!ts.isPropertyAssignment(run)) return void 0;
|
|
112
|
+
return checker.getTypeAtLocation(run.initializer).getCallSignatures()[0]?.getReturnType();
|
|
113
|
+
}
|
|
114
|
+
/** The options of a call of `operation`, when the call has them as a literal. */
|
|
115
|
+
function optionsOf(node, operation) {
|
|
116
|
+
if (!ts.isCallExpression(node) || !ts.isIdentifier(node.expression)) return void 0;
|
|
117
|
+
const [options] = node.arguments;
|
|
118
|
+
if (node.expression.text !== operation || !options) return void 0;
|
|
119
|
+
return ts.isObjectLiteralExpression(options) ? options : void 0;
|
|
120
|
+
}
|
|
121
|
+
/** An operation's label, when it is a literal: a tool cannot name a label made at run time. */
|
|
122
|
+
function labelOf(options) {
|
|
123
|
+
const label = member(options, "label");
|
|
124
|
+
if (!label || !ts.isPropertyAssignment(label)) return void 0;
|
|
125
|
+
return ts.isStringLiteralLike(label.initializer) ? label.initializer.text : void 0;
|
|
126
|
+
}
|
|
127
|
+
function exitsIn(checker, source, path) {
|
|
128
|
+
const operation = coreOperation(source);
|
|
129
|
+
const found = [];
|
|
130
|
+
if (!operation) return found;
|
|
131
|
+
const visit = (node) => {
|
|
132
|
+
ts.forEachChild(node, visit);
|
|
133
|
+
const options = optionsOf(node, operation);
|
|
134
|
+
const label = options && labelOf(options);
|
|
135
|
+
const run = options && member(options, "run");
|
|
136
|
+
if (!label || !run) return;
|
|
137
|
+
const returned = returnOf(checker, run);
|
|
138
|
+
const line = source.getLineAndCharacterOfPosition(node.getStart()).line + 1;
|
|
139
|
+
for (const exit of returned ? exitsOf(checker, returned) : []) found.push({
|
|
140
|
+
path,
|
|
141
|
+
line,
|
|
142
|
+
label,
|
|
143
|
+
exit
|
|
144
|
+
});
|
|
145
|
+
};
|
|
146
|
+
visit(source);
|
|
147
|
+
return found;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Each exit the app's operations declare, read from the types of the app's own `tsconfig.json`.
|
|
151
|
+
* An app with no `tsconfig.json` declares none.
|
|
152
|
+
*/
|
|
153
|
+
function declaredExits(root) {
|
|
154
|
+
const config = join(root, "tsconfig.json");
|
|
155
|
+
if (!existsSync(config)) return [];
|
|
156
|
+
const parsed = ts.getParsedCommandLineOfConfigFile(config, {}, {
|
|
157
|
+
...ts.sys,
|
|
158
|
+
onUnRecoverableConfigFileDiagnostic: () => void 0
|
|
159
|
+
});
|
|
160
|
+
if (!parsed) return [];
|
|
161
|
+
const program = ts.createProgram(parsed.fileNames, parsed.options);
|
|
162
|
+
const checker = program.getTypeChecker();
|
|
163
|
+
return program.getSourceFiles().flatMap((source) => {
|
|
164
|
+
const path = relative(root, source.fileName).replaceAll("\\", "/");
|
|
165
|
+
if (source.isDeclarationFile || !appCode.test(path) || testFile.test(path)) return [];
|
|
166
|
+
return exitsIn(checker, source, path);
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
/** Where a page test leaves its store's text: one `.json` file a test file, under the app. */
|
|
170
|
+
const pageExits = ".tinker/exits";
|
|
171
|
+
/** The exits of one page file: the keys of the store's JSON object. */
|
|
172
|
+
function keysIn(path) {
|
|
173
|
+
const counts = JSON.parse(readFileSync(path, "utf8"));
|
|
174
|
+
return typeof counts === "object" && counts !== null ? Object.keys(counts) : [];
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* The exits a test run reached. The `exits` capsule wrote `file`: one `label<tab>exit` a line.
|
|
178
|
+
* A page test wrote its store's JSON to a file in `pageExits`.
|
|
179
|
+
*/
|
|
180
|
+
function reachedIn(root, file) {
|
|
181
|
+
const folder = join(root, pageExits);
|
|
182
|
+
const pages = existsSync(folder) ? readdirSync(folder) : [];
|
|
183
|
+
const lines = existsSync(file) ? readFileSync(file, "utf8").split("\n") : [];
|
|
184
|
+
return /* @__PURE__ */ new Set([...lines, ...pages.flatMap((name) => keysIn(join(folder, name)))]);
|
|
185
|
+
}
|
|
186
|
+
const said = {
|
|
187
|
+
refused: "refuses",
|
|
188
|
+
[plain]: "returns"
|
|
189
|
+
};
|
|
190
|
+
/** A finding for each declared exit that no test reached. */
|
|
191
|
+
function unreached(root, file) {
|
|
192
|
+
const reached = reachedIn(root, file);
|
|
193
|
+
return declaredExits(root).filter(({ label, exit }) => !reached.has(`${label}\t${exit}`)).map(({ path, line, label, exit }) => ({
|
|
194
|
+
path,
|
|
195
|
+
line,
|
|
196
|
+
rule: "tinker/exit",
|
|
197
|
+
what: `\`${label}\` ${said[exit] ?? `answers \`${exit}\``}, and no test reaches it`
|
|
198
|
+
}));
|
|
199
|
+
}
|
|
200
|
+
//#endregion
|
|
20
201
|
//#region src/gaps.ts
|
|
21
202
|
/** A marker allows the finding on the next line: `// tinker-gap: G3`, alone on its line. */
|
|
22
203
|
const markerLine = /^\s*\/\/ tinker-gap: (G\d+)\s*$/;
|
|
@@ -1133,7 +1314,11 @@ function readApp(root) {
|
|
|
1133
1314
|
const config = configOf(root);
|
|
1134
1315
|
const app = sourcesOf(root, appFiles, appFile);
|
|
1135
1316
|
const page = pageSources(root, config.page ?? {});
|
|
1136
|
-
const found = [
|
|
1317
|
+
const found = [
|
|
1318
|
+
...findPlain(app, config.plain?.entries ?? {}),
|
|
1319
|
+
...page.flatMap(findPage),
|
|
1320
|
+
...app.filter(({ path }) => appCode.test(path)).flatMap(findBarrel)
|
|
1321
|
+
];
|
|
1137
1322
|
return {
|
|
1138
1323
|
sources: [...new Map([...app, ...page].map((source) => [source.path, source])).values()],
|
|
1139
1324
|
found,
|
|
@@ -1167,7 +1352,8 @@ const own = [
|
|
|
1167
1352
|
"tinker/plain",
|
|
1168
1353
|
"tinker/one-wire",
|
|
1169
1354
|
"tinker/react-state",
|
|
1170
|
-
"tinker/gap"
|
|
1355
|
+
"tinker/gap",
|
|
1356
|
+
"tinker/barrel"
|
|
1171
1357
|
];
|
|
1172
1358
|
/** A lint rule that is a gate, by its lint code: its Tinker rule id. It belongs to `types`. */
|
|
1173
1359
|
const gates = {
|
|
@@ -1269,148 +1455,6 @@ function loadSkill(root, use) {
|
|
|
1269
1455
|
}).content;
|
|
1270
1456
|
}
|
|
1271
1457
|
//#endregion
|
|
1272
|
-
//#region src/exits.ts
|
|
1273
|
-
/** The exit of a return with no `kind` of its own. */
|
|
1274
|
-
const plain = "value";
|
|
1275
|
-
/** A hook's `refuse(payload)`, by the name Core gives its type. */
|
|
1276
|
-
const refusal = "Refused";
|
|
1277
|
-
/**
|
|
1278
|
-
* The app's own code. The base under `tinker/` is Tinker's, and moves into a package that has
|
|
1279
|
-
* its own tests (ADR 0159).
|
|
1280
|
-
*/
|
|
1281
|
-
const appCode = /^(?:src|cmds)\//;
|
|
1282
|
-
/** A test file declares no exit: its operations are fixtures. */
|
|
1283
|
-
const testFile = /\.test\.[mc]?tsx?$/;
|
|
1284
|
-
function kindsOf(checker, type) {
|
|
1285
|
-
const kind = type.getProperty("kind");
|
|
1286
|
-
if (!kind) return [];
|
|
1287
|
-
const of = checker.getTypeOfSymbol(kind);
|
|
1288
|
-
const parts = of.isUnion() ? of.types : [of];
|
|
1289
|
-
return parts.every((part) => part.isStringLiteral()) ? parts.map((part) => part.value) : [];
|
|
1290
|
-
}
|
|
1291
|
-
/** Each exit a return type holds: a union gives one per member, and a promise gives its value's. */
|
|
1292
|
-
function exitsOf(checker, returned) {
|
|
1293
|
-
const type = checker.getAwaitedType(returned) ?? returned;
|
|
1294
|
-
if (type.flags & ts.TypeFlags.Never) return [];
|
|
1295
|
-
const exits = /* @__PURE__ */ new Set();
|
|
1296
|
-
for (const part of type.isUnion() ? type.types : [type]) {
|
|
1297
|
-
if (checker.typeToString(part) === refusal) {
|
|
1298
|
-
exits.add("refused");
|
|
1299
|
-
continue;
|
|
1300
|
-
}
|
|
1301
|
-
const kinds = kindsOf(checker, part);
|
|
1302
|
-
for (const kind of kinds.length ? kinds : [plain]) exits.add(kind);
|
|
1303
|
-
}
|
|
1304
|
-
return [...exits].sort();
|
|
1305
|
-
}
|
|
1306
|
-
/** The names a file imports from Core. */
|
|
1307
|
-
function coreImports(source) {
|
|
1308
|
-
return source.statements.flatMap((statement) => {
|
|
1309
|
-
if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier)) return [];
|
|
1310
|
-
const names = statement.importClause?.namedBindings;
|
|
1311
|
-
return statement.moduleSpecifier.text === "@tinkered/core" && names && ts.isNamedImports(names) ? [...names.elements] : [];
|
|
1312
|
-
});
|
|
1313
|
-
}
|
|
1314
|
-
/** The name `operation` has in this file, when it comes from Core. */
|
|
1315
|
-
function coreOperation(source) {
|
|
1316
|
-
return coreImports(source).find((element) => (element.propertyName ?? element.name).text === "operation")?.name.text;
|
|
1317
|
-
}
|
|
1318
|
-
function member(options, name) {
|
|
1319
|
-
return options.properties.find((property) => property.name && ts.isIdentifier(property.name) && property.name.text === name);
|
|
1320
|
-
}
|
|
1321
|
-
/** The return type of an operation's `run`, written as a property or as a method. */
|
|
1322
|
-
function returnOf(checker, run) {
|
|
1323
|
-
if (ts.isMethodDeclaration(run)) return checker.getSignatureFromDeclaration(run)?.getReturnType();
|
|
1324
|
-
if (!ts.isPropertyAssignment(run)) return void 0;
|
|
1325
|
-
return checker.getTypeAtLocation(run.initializer).getCallSignatures()[0]?.getReturnType();
|
|
1326
|
-
}
|
|
1327
|
-
/** The options of a call of `operation`, when the call has them as a literal. */
|
|
1328
|
-
function optionsOf(node, operation) {
|
|
1329
|
-
if (!ts.isCallExpression(node) || !ts.isIdentifier(node.expression)) return void 0;
|
|
1330
|
-
const [options] = node.arguments;
|
|
1331
|
-
if (node.expression.text !== operation || !options) return void 0;
|
|
1332
|
-
return ts.isObjectLiteralExpression(options) ? options : void 0;
|
|
1333
|
-
}
|
|
1334
|
-
/** An operation's label, when it is a literal: a tool cannot name a label made at run time. */
|
|
1335
|
-
function labelOf(options) {
|
|
1336
|
-
const label = member(options, "label");
|
|
1337
|
-
if (!label || !ts.isPropertyAssignment(label)) return void 0;
|
|
1338
|
-
return ts.isStringLiteralLike(label.initializer) ? label.initializer.text : void 0;
|
|
1339
|
-
}
|
|
1340
|
-
function exitsIn(checker, source, path) {
|
|
1341
|
-
const operation = coreOperation(source);
|
|
1342
|
-
const found = [];
|
|
1343
|
-
if (!operation) return found;
|
|
1344
|
-
const visit = (node) => {
|
|
1345
|
-
ts.forEachChild(node, visit);
|
|
1346
|
-
const options = optionsOf(node, operation);
|
|
1347
|
-
const label = options && labelOf(options);
|
|
1348
|
-
const run = options && member(options, "run");
|
|
1349
|
-
if (!label || !run) return;
|
|
1350
|
-
const returned = returnOf(checker, run);
|
|
1351
|
-
const line = source.getLineAndCharacterOfPosition(node.getStart()).line + 1;
|
|
1352
|
-
for (const exit of returned ? exitsOf(checker, returned) : []) found.push({
|
|
1353
|
-
path,
|
|
1354
|
-
line,
|
|
1355
|
-
label,
|
|
1356
|
-
exit
|
|
1357
|
-
});
|
|
1358
|
-
};
|
|
1359
|
-
visit(source);
|
|
1360
|
-
return found;
|
|
1361
|
-
}
|
|
1362
|
-
/**
|
|
1363
|
-
* Each exit the app's operations declare, read from the types of the app's own `tsconfig.json`.
|
|
1364
|
-
* An app with no `tsconfig.json` declares none.
|
|
1365
|
-
*/
|
|
1366
|
-
function declaredExits(root) {
|
|
1367
|
-
const config = join(root, "tsconfig.json");
|
|
1368
|
-
if (!existsSync(config)) return [];
|
|
1369
|
-
const parsed = ts.getParsedCommandLineOfConfigFile(config, {}, {
|
|
1370
|
-
...ts.sys,
|
|
1371
|
-
onUnRecoverableConfigFileDiagnostic: () => void 0
|
|
1372
|
-
});
|
|
1373
|
-
if (!parsed) return [];
|
|
1374
|
-
const program = ts.createProgram(parsed.fileNames, parsed.options);
|
|
1375
|
-
const checker = program.getTypeChecker();
|
|
1376
|
-
return program.getSourceFiles().flatMap((source) => {
|
|
1377
|
-
const path = relative(root, source.fileName).replaceAll("\\", "/");
|
|
1378
|
-
if (source.isDeclarationFile || !appCode.test(path) || testFile.test(path)) return [];
|
|
1379
|
-
return exitsIn(checker, source, path);
|
|
1380
|
-
});
|
|
1381
|
-
}
|
|
1382
|
-
/** Where a page test leaves its store's text: one `.json` file a test file, under the app. */
|
|
1383
|
-
const pageExits = ".tinker/exits";
|
|
1384
|
-
/** The exits of one page file: the keys of the store's JSON object. */
|
|
1385
|
-
function keysIn(path) {
|
|
1386
|
-
const counts = JSON.parse(readFileSync(path, "utf8"));
|
|
1387
|
-
return typeof counts === "object" && counts !== null ? Object.keys(counts) : [];
|
|
1388
|
-
}
|
|
1389
|
-
/**
|
|
1390
|
-
* The exits a test run reached. The `exits` capsule wrote `file`: one `label<tab>exit` a line.
|
|
1391
|
-
* A page test wrote its store's JSON to a file in `pageExits`.
|
|
1392
|
-
*/
|
|
1393
|
-
function reachedIn(root, file) {
|
|
1394
|
-
const folder = join(root, pageExits);
|
|
1395
|
-
const pages = existsSync(folder) ? readdirSync(folder) : [];
|
|
1396
|
-
const lines = existsSync(file) ? readFileSync(file, "utf8").split("\n") : [];
|
|
1397
|
-
return /* @__PURE__ */ new Set([...lines, ...pages.flatMap((name) => keysIn(join(folder, name)))]);
|
|
1398
|
-
}
|
|
1399
|
-
const said = {
|
|
1400
|
-
refused: "refuses",
|
|
1401
|
-
[plain]: "returns"
|
|
1402
|
-
};
|
|
1403
|
-
/** A finding for each declared exit that no test reached. */
|
|
1404
|
-
function unreached(root, file) {
|
|
1405
|
-
const reached = reachedIn(root, file);
|
|
1406
|
-
return declaredExits(root).filter(({ label, exit }) => !reached.has(`${label}\t${exit}`)).map(({ path, line, label, exit }) => ({
|
|
1407
|
-
path,
|
|
1408
|
-
line,
|
|
1409
|
-
rule: "tinker/exit",
|
|
1410
|
-
what: `\`${label}\` ${said[exit] ?? `answers \`${exit}\``}, and no test reaches it`
|
|
1411
|
-
}));
|
|
1412
|
-
}
|
|
1413
|
-
//#endregion
|
|
1414
1458
|
//#region src/tools.ts
|
|
1415
1459
|
const tscLine = /^(?:(.+)\((\d+),\d+\): )?error (TS\d+): (.*)$/;
|
|
1416
1460
|
const viteConfig = "vite.config.{ts,mts,js,mjs}";
|
package/dist/vite.d.mts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Plugin } from "vite-plus";
|
|
2
|
+
//#region src/vite.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* The page guard (ADR 0160): the page build stops at an import of server code, however deep
|
|
5
|
+
* the re-export that reaches it. It runs only where the page is built, the client environment,
|
|
6
|
+
* so a server build is not touched. `import type` is erased before any import resolves, so a
|
|
7
|
+
* page may still take types from a server file.
|
|
8
|
+
*/
|
|
9
|
+
export declare function pageGuard(): Plugin;
|
|
10
|
+
//#endregion
|
package/dist/vite.mjs
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { relative } from "node:path";
|
|
2
|
+
//#region src/vite.ts
|
|
3
|
+
/**
|
|
4
|
+
* Server code, by the name an import gives it or the file it resolves to (ADR 0160 decision 3).
|
|
5
|
+
* `@tinkered/bun/client` is the one entry of `@tinkered/bun` a page may import.
|
|
6
|
+
*/
|
|
7
|
+
const serverCode = [
|
|
8
|
+
/\.server\./,
|
|
9
|
+
/^(?:bun|node):/,
|
|
10
|
+
/^@tinkered\/entry(?:\/|$)/,
|
|
11
|
+
/^@tinkered\/bun(?!\/client$)(?:\/|$)/
|
|
12
|
+
];
|
|
13
|
+
/**
|
|
14
|
+
* The page guard (ADR 0160): the page build stops at an import of server code, however deep
|
|
15
|
+
* the re-export that reaches it. It runs only where the page is built, the client environment,
|
|
16
|
+
* so a server build is not touched. `import type` is erased before any import resolves, so a
|
|
17
|
+
* page may still take types from a server file.
|
|
18
|
+
*/
|
|
19
|
+
function pageGuard() {
|
|
20
|
+
return {
|
|
21
|
+
name: "tinker:page-guard",
|
|
22
|
+
enforce: "pre",
|
|
23
|
+
applyToEnvironment: (environment) => environment.config.consumer === "client",
|
|
24
|
+
async resolveId(source, importer, options) {
|
|
25
|
+
if (!importer) return null;
|
|
26
|
+
const resolved = await this.resolve(source, importer, options);
|
|
27
|
+
const isServer = (code) => code.test(source) || resolved !== null && code.test(resolved.id);
|
|
28
|
+
if (serverCode.some(isServer)) {
|
|
29
|
+
const from = relative(this.environment.config.root, importer).replaceAll("\\", "/");
|
|
30
|
+
this.error(`page imports server code "${source}" from ${from}`);
|
|
31
|
+
}
|
|
32
|
+
return resolved;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
//#endregion
|
|
37
|
+
export { pageGuard };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tinkered/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.30.1",
|
|
4
4
|
"description": "The tinker command: one check that corrects the writer (types, then rules, each finding with its fix from the skill), and the skills.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
".": "./dist/index.mjs",
|
|
23
23
|
"./config": "./dist/config.mjs",
|
|
24
24
|
"./test": "./dist/test.mjs",
|
|
25
|
+
"./vite": "./dist/vite.mjs",
|
|
25
26
|
"./lint": "./lint/index.mjs",
|
|
26
27
|
"./tsconfig.json": "./base/tsconfig.json",
|
|
27
28
|
"./package.json": "./package.json"
|
|
@@ -36,18 +37,22 @@
|
|
|
36
37
|
"devDependencies": {
|
|
37
38
|
"@stryker-mutator/core": "^10.0.0",
|
|
38
39
|
"@stryker-mutator/vitest-runner": "^10.0.0",
|
|
39
|
-
"@tinkered/core": "^0.
|
|
40
|
+
"@tinkered/core": "^0.30.1",
|
|
40
41
|
"@types/node": "^26.1.1",
|
|
41
42
|
"vite-plus": "1.0.0",
|
|
42
43
|
"vitest": "5.0.1",
|
|
43
44
|
"zod": "^4.0.0"
|
|
44
45
|
},
|
|
45
46
|
"peerDependencies": {
|
|
46
|
-
"@tinkered/core": "^0.
|
|
47
|
+
"@tinkered/core": "^0.30.1",
|
|
48
|
+
"vite-plus": "^1.0.0"
|
|
47
49
|
},
|
|
48
50
|
"peerDependenciesMeta": {
|
|
49
51
|
"@tinkered/core": {
|
|
50
52
|
"optional": true
|
|
53
|
+
},
|
|
54
|
+
"vite-plus": {
|
|
55
|
+
"optional": true
|
|
51
56
|
}
|
|
52
57
|
},
|
|
53
58
|
"engines": {
|