@ref0/workspace 0.10.0 → 0.11.0
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 +143 -1
- package/dist/bin.mjs +19 -7
- package/dist/lint/generic/folder-imports.mjs +84 -0
- package/dist/lint/generic/folder-layers.d.mts +40 -0
- package/dist/lint/generic/folder-layers.mjs +82 -0
- package/dist/lint/generic/imports.mjs +34 -0
- package/dist/lint/generic/package-graph.d.mts +36 -0
- package/dist/lint/generic/package-graph.mjs +111 -0
- package/dist/lint/generic/package-imports.mjs +85 -0
- package/dist/lint/generic/plugin.d.mts +4 -3
- package/dist/lint/generic/plugin.mjs +8 -3
- package/dist/lint/generic/rule-options.mjs +17 -0
- package/dist/lint/generic/workspace-packages.d.mts +29 -0
- package/dist/lint/generic/workspace-packages.mjs +78 -0
- package/dist/lint/graph.d.mts +4 -0
- package/dist/lint/graph.mjs +3 -0
- package/dist/review-record/command.mjs +107 -0
- package/dist/review-record/index.d.mts +3 -0
- package/dist/review-record/index.mjs +3 -0
- package/dist/review-record/review-record.d.mts +33 -0
- package/dist/review-record/review-record.mjs +1253 -0
- package/dist/review-record/settings.d.mts +39 -0
- package/dist/review-record/settings.mjs +100 -0
- package/dist/symbols/symbols.mjs +1 -1
- package/package.json +9 -1
package/README.md
CHANGED
|
@@ -42,7 +42,8 @@ changes, beside its code, in the package too.
|
|
|
42
42
|
|
|
43
43
|
`ref0-generic`'s rules: `seeded-properties`, `index-barrel-only` and `export-type-side-effects`,
|
|
44
44
|
which `base.lint` turns on, and `effect-encoding`, `log-messages`, `env-at-entrypoints`,
|
|
45
|
-
`entry-reexports` and `
|
|
45
|
+
`entry-reexports`, `no-comments`, `package-imports` and `folder-imports`, which a repo turns on for
|
|
46
|
+
its own files. `entry-reexports` takes the repo's root,
|
|
46
47
|
the folders of the packages whose entry files it checks, and the export condition that names an
|
|
47
48
|
entry point's source:
|
|
48
49
|
|
|
@@ -72,6 +73,37 @@ that leaves out what it doesn't own, such as vendored code:
|
|
|
72
73
|
|
|
73
74
|
and add `"comments": "ref0-workspace comment-budget"` to the root `package.json`'s scripts.
|
|
74
75
|
|
|
76
|
+
`package-imports` holds each package and app to a declared graph: what it imports of each other
|
|
77
|
+
package, by entry point, and how, as `values`, `types` (only `import type`) or `tests`. Nothing
|
|
78
|
+
imports an app, a relative path never leaves a package, and a package never imports itself by name
|
|
79
|
+
outside its tests. It takes the root, globs of the folders that hold packages and apps, the graph,
|
|
80
|
+
and `nodeFiles`, the files of a package that run only in Node and take values where it takes types.
|
|
81
|
+
`folder-imports` holds each layered source folder: a file imports its own place and places below
|
|
82
|
+
it, never beside or above. A source may name `exceptions`, by file and module, which fail once no
|
|
83
|
+
import needs them; `features`, places nothing outside them imports; and `entries`, when its top
|
|
84
|
+
files are entry points, `reexports` or `code`. `@ref0/workspace/lint/graph` exports the types to
|
|
85
|
+
declare both with, and `graphProblems`, which a repo's test expects to return none:
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
import type { FolderLayers, PackageGraph } from '@ref0/workspace/lint/graph';
|
|
89
|
+
|
|
90
|
+
export const packageGraph = {
|
|
91
|
+
'@acme/ui': {},
|
|
92
|
+
'@acme/site': { '@acme/ui': { values: ['.', 'cast'], tests: ['testing'] } },
|
|
93
|
+
} as const satisfies PackageGraph<'@acme/ui' | '@acme/site', '@acme/ui'>;
|
|
94
|
+
|
|
95
|
+
export const folderLayers = {
|
|
96
|
+
'packages/ui/src': { layers: [['tokens'], ['components'], ['blocks']], entries: 'reexports' },
|
|
97
|
+
} as const satisfies FolderLayers;
|
|
98
|
+
|
|
99
|
+
// In the config's overrides:
|
|
100
|
+
'ref0-generic/package-imports': [
|
|
101
|
+
'error',
|
|
102
|
+
{ root, packages: ['packages/*'], apps: ['apps/*'], graph: packageGraph },
|
|
103
|
+
],
|
|
104
|
+
'ref0-generic/folder-imports': ['error', { root, sources: folderLayers }],
|
|
105
|
+
```
|
|
106
|
+
|
|
75
107
|
## Checks
|
|
76
108
|
|
|
77
109
|
`ref0-workspace <check>`, from anywhere in the repo's checkout:
|
|
@@ -91,6 +123,9 @@ and add `"comments": "ref0-workspace comment-budget"` to the root `package.json`
|
|
|
91
123
|
grows, so a review sees it.
|
|
92
124
|
- `symbols find <words...>` and `symbols check <base> [<head>]` or `symbols check --all`: what the
|
|
93
125
|
repo already has, and a change that copies it, as [Symbols](#symbols) says.
|
|
126
|
+
- `review-record [--count | --tier | --check <comment> [<body>] | --benched]`: whether a pull
|
|
127
|
+
request's review is on record for its merge risk, in CI, and a branch's floor before it opens, as
|
|
128
|
+
[The review gate](#the-review-gate) says.
|
|
94
129
|
- `types`: the types of every project the root `tsconfig.json` reaches, and Effect's rules, with
|
|
95
130
|
the repo's own `effect-tsgo tsrs`, each error once, in the project that includes its file. Each
|
|
96
131
|
project's result is kept for every checkout on the machine, in `REF0_WORKSPACE_TYPES_CACHE` or
|
|
@@ -161,6 +196,113 @@ that never installed the repo's hooks. A job that force-pushes a
|
|
|
161
196
|
`types` and `type-coverage` run the repo's own `@effect/tsgo`, `tsrs` and `typescript`, its
|
|
162
197
|
optional peers, resolved from its root, at the versions its catalog pins.
|
|
163
198
|
|
|
199
|
+
## The review gate
|
|
200
|
+
|
|
201
|
+
`ref0-workspace review-record` is the step of CI's result job that fails a pull request whose
|
|
202
|
+
review isn't on record for its merge risk, as the `review` skill's `references/merge-risk.md` and
|
|
203
|
+
the repo's `.agents/repo/review.md` say. Its body names a tier on a `Merge risk:` line, at or above
|
|
204
|
+
its floor, the highest tier its changed lines, the always-review list and its test code give it,
|
|
205
|
+
and the gate checks the session comment or `Break:` lines that tier needs.
|
|
206
|
+
|
|
207
|
+
- Bare, in CI: judges pull request `PR` of `GITHUB_REPOSITORY`, from `BASE` to `HEAD`, reading
|
|
208
|
+
GitHub with `gh` and `GH_TOKEN`, and the history from the Git checkout it runs in, which holds
|
|
209
|
+
both commits whole, blobs aside. It prints each reason it fails as an Actions `::error::`.
|
|
210
|
+
- `--count`: the branch's net lines of production source since its merge base with `origin/main`,
|
|
211
|
+
and on stderr whether that's a large change and its floor.
|
|
212
|
+
- `--tier`: the branch's floor, and what sets it, as a `Merge risk:` line.
|
|
213
|
+
- `--check <comment-file> [<body-file>]`: CI's checks on a session comment and a body, before you
|
|
214
|
+
post them.
|
|
215
|
+
- `--benched`: ci-bench's cases, one a line, that pull request `PR`'s files tie to, from the bench
|
|
216
|
+
list in the checkout it runs in.
|
|
217
|
+
|
|
218
|
+
Each reads the repo's settings, `.agents/repo/review.json`, and its always-review list,
|
|
219
|
+
`.agents/repo/review-paths.txt`, at the base: CI's from GitHub at `BASE`, and the branch
|
|
220
|
+
subcommands' at `origin/main`'s tip. A pull request's own copies count only once it merges.
|
|
221
|
+
Every key of `review.json` may be left out, for the value in brackets, which ref-0/git's gate
|
|
222
|
+
began with, and the file holds no other:
|
|
223
|
+
|
|
224
|
+
| Key | What it holds |
|
|
225
|
+
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
226
|
+
| `reviewTypes` | The review types a session may name, in lowercase, as the `review` skill's `references/types.md` lists them (its 14) |
|
|
227
|
+
| `consideredTypes` | The types each session names, as run or as not run with why, on a change a review covers: each one of `reviewTypes` (stress test, system design, red team, storage, program design, formal verification, fuzzing and fidelity) |
|
|
228
|
+
| `lowTierLines` | Changed lines, additions and deletions since the merge base, up to which a pull request may be low (150) |
|
|
229
|
+
| `highTierLines` | Changed lines up to which it may be medium, at least `lowTierLines` (500) |
|
|
230
|
+
| `productionSource` | Production source, as globs, whose net lines count toward a large change, less test code and fixtures (`packages/*/src/*.ts`, `packages/*/src/*.tsx`, `apps/*/src/*.ts`, `apps/*/src/*.tsx`) |
|
|
231
|
+
| `testCode` | Test code, as globs in `review-paths.txt`'s form (`*.test.ts`, `*.test-d.ts`, `*/testing/*`) |
|
|
232
|
+
| `testHelpers` | Shared test helpers, test code too whether `testCode` names them or not, whose change puts a pull request at medium (`*/testing/*`) |
|
|
233
|
+
| `benchedPaths` | The bench list's path, whose `<case> <glob>` lines tie source to the bench cases that time it (none: no bench) |
|
|
234
|
+
| `benchRuns` | Files under a `testing/` folder that a bench case runs all the same (none) |
|
|
235
|
+
|
|
236
|
+
```json
|
|
237
|
+
{ "benchedPaths": "bench/benched-paths.txt", "lowTierLines": 200 }
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
A file that doesn't read, a key it doesn't know, or a considered type the repo doesn't accept
|
|
241
|
+
stops the gate, which fails closed.
|
|
242
|
+
|
|
243
|
+
CI runs the command at the version the base's `pnpm-workspace.yaml` catalog names, from an npm
|
|
244
|
+
install of that version with no peers, which loads no peer, in a blobless clone of the base and
|
|
245
|
+
the head, so a pull request can't change the gate that judges it:
|
|
246
|
+
|
|
247
|
+
```yaml
|
|
248
|
+
- name: Fetch review-record history
|
|
249
|
+
id: fetch
|
|
250
|
+
if: always() && github.event_name == 'pull_request'
|
|
251
|
+
timeout-minutes: 2
|
|
252
|
+
env:
|
|
253
|
+
BASE: ${{ github.event.pull_request.base.sha }}
|
|
254
|
+
GH_TOKEN: ${{ github.token }}
|
|
255
|
+
GIT_TERMINAL_PROMPT: '0'
|
|
256
|
+
run: |
|
|
257
|
+
set -eu
|
|
258
|
+
rm -rf review-record
|
|
259
|
+
git init -q review-record
|
|
260
|
+
git -C review-record remote add origin "${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git"
|
|
261
|
+
git -C review-record \
|
|
262
|
+
-c credential.helper= \
|
|
263
|
+
-c 'credential.helper=!gh auth git-credential' \
|
|
264
|
+
-c credential.interactive=never \
|
|
265
|
+
fetch -q --no-tags --filter=blob:none origin "$GITHUB_SHA" "$BASE"
|
|
266
|
+
gate=$(git -C review-record ls-tree --name-only "$BASE" -- .agents/repo/review.json)
|
|
267
|
+
if [ -z "$gate" ]; then
|
|
268
|
+
echo "::notice::The base, $BASE, has no .agents/repo/review.json, so no review gate judges this pull request."
|
|
269
|
+
echo 'gate=none' >> "$GITHUB_OUTPUT"
|
|
270
|
+
exit 0
|
|
271
|
+
fi
|
|
272
|
+
git -C review-record \
|
|
273
|
+
-c credential.helper= \
|
|
274
|
+
-c 'credential.helper=!gh auth git-credential' \
|
|
275
|
+
-c credential.interactive=never \
|
|
276
|
+
checkout -q "$BASE" -- pnpm-workspace.yaml
|
|
277
|
+
version=$(sed -n "s/^ '@ref0\/workspace': \([0-9][0-9A-Za-z.+-]*\)$/\1/p" review-record/pnpm-workspace.yaml)
|
|
278
|
+
case "$version" in
|
|
279
|
+
'' | *[!0-9A-Za-z.+-]*) echo "::error::The base's catalog names no one @ref0/workspace version." >&2; exit 1 ;;
|
|
280
|
+
esac
|
|
281
|
+
npm install --prefix "$RUNNER_TEMP/review-record" --no-save --no-package-lock --omit=peer \
|
|
282
|
+
--omit=optional --ignore-scripts --no-audit --no-fund "@ref0/workspace@$version"
|
|
283
|
+
echo 'gate=base' >> "$GITHUB_OUTPUT"
|
|
284
|
+
- name: The review is on record
|
|
285
|
+
if: always() && github.event_name == 'pull_request' && steps.fetch.outcome == 'success' && steps.fetch.outputs.gate == 'base'
|
|
286
|
+
working-directory: review-record
|
|
287
|
+
env:
|
|
288
|
+
BASE: ${{ github.event.pull_request.base.sha }}
|
|
289
|
+
GH_TOKEN: ${{ github.token }}
|
|
290
|
+
HEAD: ${{ github.event.pull_request.head.sha }}
|
|
291
|
+
PR: ${{ github.event.pull_request.number }}
|
|
292
|
+
run: node "$RUNNER_TEMP/review-record/node_modules/@ref0/workspace/dist/bin.mjs" review-record
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
The job's token reads the pull request, its comments and the repo's contents. The fetch's
|
|
296
|
+
credential helper reads `GH_TOKEN` from the environment, so the token is never in `.git/config` or
|
|
297
|
+
on a command line, and a blob the gate reads later is fetched the same way, failing closed when the
|
|
298
|
+
fetch does. A base with no `review.json` holds no gate yet, and the job says so. A repo's
|
|
299
|
+
`review-paths.txt` lists `review.json`, `pnpm-workspace.yaml` and `.github/workflows/ci.yml` at
|
|
300
|
+
high, since each changes the gate the next pull request meets.
|
|
301
|
+
|
|
302
|
+
`@ref0/workspace/review-record` exports what a repo's own code reuses of the gate: `testsOnly` and
|
|
303
|
+
`benchedCases`, which a bench's seeds and its list's tests read, and `settingsFrom` and
|
|
304
|
+
`settingsPath`, to read the settings they take.
|
|
305
|
+
|
|
164
306
|
## Symbols
|
|
165
307
|
|
|
166
308
|
`symbols` keeps a repo from collecting copies of its own code. `find` shows what the repo already
|
package/dist/bin.mjs
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { changelog } from "./changelog.mjs";
|
|
3
|
-
import { commentBudget } from "./comment-budget.mjs";
|
|
4
3
|
import { commitMessage } from "./commit-message.mjs";
|
|
5
4
|
import { docsBudget } from "./docs-budget.mjs";
|
|
6
5
|
import { defaultCondition, packageExports } from "./package-exports.mjs";
|
|
7
6
|
import { preCommit } from "./pre-commit.mjs";
|
|
8
7
|
import { prePush } from "./pre-push.mjs";
|
|
9
|
-
import {
|
|
10
|
-
import { symbolsFind } from "./symbols/find.mjs";
|
|
8
|
+
import { reviewRecord } from "./review-record/command.mjs";
|
|
11
9
|
import { typeCoverage } from "./type-coverage.mjs";
|
|
12
10
|
import { types } from "./types/types.mjs";
|
|
13
11
|
import { execFileSync } from "node:child_process";
|
|
@@ -38,6 +36,12 @@ import { parseArgs } from "node:util";
|
|
|
38
36
|
* ref0-workspace pre-push <remote> <url> Git's pre-push hook: no force-push or delete
|
|
39
37
|
* of main, deploy/* or release/*, then the
|
|
40
38
|
* repo's checks
|
|
39
|
+
* ref0-workspace review-record [--count | --tier | --check <comment> [<body>] | --benched]
|
|
40
|
+
* a pull request's review is on record for its
|
|
41
|
+
* merge risk
|
|
42
|
+
*
|
|
43
|
+
* A check that needs a peer, Vite+ or a type checker, imports its module when it runs, so
|
|
44
|
+
* `review-record`, which CI runs from an install with no peers, loads none of them.
|
|
41
45
|
*/
|
|
42
46
|
/** The root of the Git checkout the command runs in, or where it runs outside one. */
|
|
43
47
|
const root = () => {
|
|
@@ -81,14 +85,17 @@ const checks = /* @__PURE__ */ new Map([
|
|
|
81
85
|
["comment-budget", {
|
|
82
86
|
args: "[--write | --grant <path>...]",
|
|
83
87
|
about: "each file's banned comments, within its budget for ref0-generic/no-comments; --write lowers each budget to what its file holds, and --grant sets the budgets of the files under the paths to what each holds",
|
|
84
|
-
run: (args) =>
|
|
88
|
+
run: async (args) => {
|
|
89
|
+
const { commentBudget } = await import("./comment-budget.mjs");
|
|
90
|
+
return commentBudget(args, root());
|
|
91
|
+
}
|
|
85
92
|
}],
|
|
86
93
|
["symbols", {
|
|
87
94
|
args: "find <words...> | check <base> [<head>] | check --all",
|
|
88
95
|
about: "what the repo has, before you write it again, and no change copying it",
|
|
89
|
-
run: ([command, ...rest]) => {
|
|
90
|
-
if (command === "find") return symbolsFind(rest, cwd());
|
|
91
|
-
if (command === "check") return symbolsCheck(rest, cwd());
|
|
96
|
+
run: async ([command, ...rest]) => {
|
|
97
|
+
if (command === "find") return (await import("./symbols/find.mjs")).symbolsFind(rest, cwd());
|
|
98
|
+
if (command === "check") return (await import("./symbols/check.mjs")).symbolsCheck(rest, cwd());
|
|
92
99
|
return usageError();
|
|
93
100
|
}
|
|
94
101
|
}],
|
|
@@ -140,6 +147,11 @@ const checks = /* @__PURE__ */ new Map([
|
|
|
140
147
|
if (remote === void 0 || url === void 0 || extra.length > 0) return usageError();
|
|
141
148
|
return prePush(root(), url, readFileSync(0, "utf8"), env);
|
|
142
149
|
}
|
|
150
|
+
}],
|
|
151
|
+
["review-record", {
|
|
152
|
+
args: "[--count | --tier | --check <comment> [<body>] | --benched]",
|
|
153
|
+
about: "CI: a pull request's review is on record for its merge risk; --count, --tier and --check ask it of this branch, and --benched lists ci-bench's cases",
|
|
154
|
+
run: (args) => reviewRecord(args, root(), env)
|
|
143
155
|
}]
|
|
144
156
|
]);
|
|
145
157
|
function usage() {
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { folderImportsOf, layerProblem, moduleNamed } from "./folder-layers.mjs";
|
|
2
|
+
import { importsVisitor } from "./imports.mjs";
|
|
3
|
+
import { testFile } from "./package-graph.mjs";
|
|
4
|
+
import { decodedOnce } from "./rule-options.mjs";
|
|
5
|
+
import { fromRoot } from "./workspace-packages.mjs";
|
|
6
|
+
import { resolve } from "node:path";
|
|
7
|
+
import { defineRule } from "vite-plus/lint/plugins";
|
|
8
|
+
// src/lint/generic/folder-imports.ts
|
|
9
|
+
const optionsOf = decodedOnce(folderImportsOf);
|
|
10
|
+
/** Whether a statement of an entry file re-exports: `export { … } from`, or `export *`. */
|
|
11
|
+
const reexports = (statement) => statement.type === "ExportNamedDeclaration" && statement.source !== null || statement.type === "ExportAllDeclaration";
|
|
12
|
+
const folderImportsRule = defineRule({
|
|
13
|
+
meta: {
|
|
14
|
+
messages: {
|
|
15
|
+
above: "{{from}} sits below {{to}} in the folder layers, so it imports nothing of it. Move what it needs down, to where both can import it.",
|
|
16
|
+
beside: "{{from}} and {{to}} sit in one layer of the folder layers, so neither imports the other. Move what it needs down, or join the two from a place above both.",
|
|
17
|
+
feature: "{{to}} is a feature a host can leave out, so {{from}} imports nothing of it. Read it through a seam {{from}} declares.",
|
|
18
|
+
entry: "{{to}} is an entry point, above every place of the folder layers, so {{from}} imports nothing of it. Import the module it re-exports.",
|
|
19
|
+
reexportOnly: "This file is an entry point at the top of its source, so it only re-exports: move this into a place of the folder layers, and re-export it here.",
|
|
20
|
+
unplaced: "{{place}} is in no layer of the folder layers. Add it to the layer it belongs in.",
|
|
21
|
+
stale: "The folder layers' exceptions let this file import {{module}}, and no import here needs that. Delete the exception."
|
|
22
|
+
},
|
|
23
|
+
schema: [{
|
|
24
|
+
type: "object",
|
|
25
|
+
properties: {
|
|
26
|
+
root: { type: "string" },
|
|
27
|
+
sources: { type: "object" }
|
|
28
|
+
},
|
|
29
|
+
required: ["root", "sources"],
|
|
30
|
+
additionalProperties: false
|
|
31
|
+
}]
|
|
32
|
+
},
|
|
33
|
+
create(context) {
|
|
34
|
+
const { root, sources } = optionsOf(context.options);
|
|
35
|
+
const path = fromRoot(root, resolve(context.filename));
|
|
36
|
+
const holding = sources.filter(({ folder }) => path.startsWith(`${folder}/`));
|
|
37
|
+
if (holding.length === 0 || testFile.test(path)) return {};
|
|
38
|
+
const used = /* @__PURE__ */ new Set();
|
|
39
|
+
const imports = importsVisitor((node, imported) => {
|
|
40
|
+
if (!imported.startsWith(".")) return;
|
|
41
|
+
const target = fromRoot(root, resolve(context.filename, "..", imported.replace(/\?.*$/, "")));
|
|
42
|
+
for (const source of holding) {
|
|
43
|
+
if (!target.startsWith(`${source.folder}/`)) continue;
|
|
44
|
+
const file = path.slice(source.folder.length + 1);
|
|
45
|
+
const module = moduleNamed(target.slice(source.folder.length + 1));
|
|
46
|
+
const problem = layerProblem(source, file, module);
|
|
47
|
+
if (problem === void 0) continue;
|
|
48
|
+
if (problem.messageId !== "unplaced" && source.exceptions?.[file]?.includes(module)) {
|
|
49
|
+
used.add(`${source.folder}\0${module}`);
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
context.report({
|
|
53
|
+
node,
|
|
54
|
+
...problem
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
const checkProgram = (program) => {
|
|
59
|
+
for (const source of holding) {
|
|
60
|
+
const file = path.slice(source.folder.length + 1);
|
|
61
|
+
if (source.entries !== "reexports" || moduleNamed(file).includes("/")) continue;
|
|
62
|
+
for (const statement of program.body) if (!reexports(statement)) context.report({
|
|
63
|
+
node: statement,
|
|
64
|
+
messageId: "reexportOnly"
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
return {
|
|
69
|
+
...imports,
|
|
70
|
+
Program: checkProgram,
|
|
71
|
+
"Program:exit"(program) {
|
|
72
|
+
for (const source of holding) {
|
|
73
|
+
const file = path.slice(source.folder.length + 1);
|
|
74
|
+
for (const module of source.exceptions?.[file] ?? []) if (!used.has(`${source.folder}\0${module}`)) context.report({
|
|
75
|
+
node: program,
|
|
76
|
+
messageId: "stale",
|
|
77
|
+
data: { module }
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
export { folderImportsRule };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How one source folder's places stack. A place is a folder of the source, a folder inside one,
|
|
3
|
+
* such as `protocol/bytes`, or a file at its top, named without its extension. A file imports any
|
|
4
|
+
* module of its own place, and of any place in a layer below its own: never one beside it, or
|
|
5
|
+
* above.
|
|
6
|
+
*/
|
|
7
|
+
export interface SourceLayers {
|
|
8
|
+
/** The source's places, layer by layer, from the bottom. */
|
|
9
|
+
readonly layers: ReadonlyArray<ReadonlyArray<string>>;
|
|
10
|
+
/**
|
|
11
|
+
* Imports allowed against the layers and the features, by the file that makes each, from the
|
|
12
|
+
* source, to the modules it imports, without their extensions. An entry no import needs fails,
|
|
13
|
+
* so the list only shrinks.
|
|
14
|
+
*/
|
|
15
|
+
readonly exceptions?: {
|
|
16
|
+
readonly [file: string]: ReadonlyArray<string>;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Places a host can leave out. No other place imports one, wherever it sits, but a file in a
|
|
20
|
+
* `testing/` folder, which builds whole hosts.
|
|
21
|
+
*/
|
|
22
|
+
readonly features?: ReadonlyArray<string>;
|
|
23
|
+
/**
|
|
24
|
+
* When the files at the top of the source are its entry points, above every place: each imports
|
|
25
|
+
* anything, and nothing imports one. `reexports` holds each to re-exports, as a package's are;
|
|
26
|
+
* `code` lets each hold code, as an app's server does. Left out, the top files are places.
|
|
27
|
+
*/
|
|
28
|
+
readonly entries?: 'reexports' | 'code';
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Each layered source folder, from the root, such as `packages/cli/src`, to its layers. A source
|
|
32
|
+
* may sit in a place of another, such as `packages/cli/src/harness`, and a file is held to every
|
|
33
|
+
* source it's in. `Source` lets a repo hold the keys to its own, as `satisfies FolderLayers<…>`.
|
|
34
|
+
*/
|
|
35
|
+
export type FolderLayers<Source extends string = string> = { readonly [Name in Source]: SourceLayers; };
|
|
36
|
+
/** What `ref0-generic/folder-imports` takes: the repo's root, as an absolute path, and its sources. */
|
|
37
|
+
export interface FolderImportsOptions {
|
|
38
|
+
readonly root: string;
|
|
39
|
+
readonly sources: FolderLayers;
|
|
40
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
// src/lint/generic/folder-layers.ts
|
|
3
|
+
const decodeFolderImportsOptions = Schema.decodeUnknownSync(Schema.Tuple([Schema.Struct({
|
|
4
|
+
root: Schema.String,
|
|
5
|
+
sources: Schema.Record(Schema.String, Schema.Struct({
|
|
6
|
+
layers: Schema.Array(Schema.Array(Schema.String)),
|
|
7
|
+
exceptions: Schema.optionalKey(Schema.Record(Schema.String, Schema.Array(Schema.String))),
|
|
8
|
+
features: Schema.optionalKey(Schema.Array(Schema.String)),
|
|
9
|
+
entries: Schema.optionalKey(Schema.Literals(["reexports", "code"]))
|
|
10
|
+
}))
|
|
11
|
+
})]));
|
|
12
|
+
/** The rule's options as decoded: the root, and each source with its places' ranks. */
|
|
13
|
+
const folderImportsOf = (options) => {
|
|
14
|
+
const [{ root, sources }] = decodeFolderImportsOptions(options);
|
|
15
|
+
return {
|
|
16
|
+
root,
|
|
17
|
+
sources: Object.entries(sources).map(([folder, source]) => ({
|
|
18
|
+
...source,
|
|
19
|
+
folder,
|
|
20
|
+
ranks: new Map(source.layers.flatMap((layer, rank) => layer.map((place) => [place, rank])))
|
|
21
|
+
}))
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
/** The module a path names, without its extension, such as `protocol/bytes/sha1`. */
|
|
25
|
+
const moduleNamed = (path) => path.replace(/\.[cm]?[jt]sx?$/, "");
|
|
26
|
+
/** Whether a module sits at the top of its source. */
|
|
27
|
+
const atTop = (module) => !module.includes("/");
|
|
28
|
+
/**
|
|
29
|
+
* The place that holds `module`: the deepest place it's in, such as `protocol/bytes` for
|
|
30
|
+
* `protocol/bytes/sha1`. A module in no place gives the first folder on its path that holds no
|
|
31
|
+
* place, such as `drafts` for `drafts/idea`, or the module itself.
|
|
32
|
+
*/
|
|
33
|
+
const placeOf = (source, module) => {
|
|
34
|
+
const places = [...source.ranks.keys()];
|
|
35
|
+
const holding = places.filter((place) => module === place || module.startsWith(`${place}/`)).toSorted((a, b) => b.length - a.length)[0];
|
|
36
|
+
if (holding !== void 0) return holding;
|
|
37
|
+
const folders = module.split("/").slice(0, -1);
|
|
38
|
+
return folders.map((_, depth) => folders.slice(0, depth + 1).join("/")).find((folder) => !places.some((place) => place.startsWith(`${folder}/`))) ?? module;
|
|
39
|
+
};
|
|
40
|
+
const testingFile = /(^|\/)testing\//;
|
|
41
|
+
/**
|
|
42
|
+
* What's wrong with `file` importing `module`, both from the source's folder, if anything: a place
|
|
43
|
+
* in no layer, an entry point imported, a feature imported from outside it, or a place beside its
|
|
44
|
+
* own or above it. The source's exceptions aren't read here.
|
|
45
|
+
*/
|
|
46
|
+
const layerProblem = (source, file, module) => {
|
|
47
|
+
const fromModule = moduleNamed(file);
|
|
48
|
+
if (source.entries !== void 0 && atTop(fromModule)) return void 0;
|
|
49
|
+
const from = placeOf(source, fromModule);
|
|
50
|
+
if (source.entries !== void 0 && atTop(module)) return {
|
|
51
|
+
messageId: "entry",
|
|
52
|
+
data: {
|
|
53
|
+
from,
|
|
54
|
+
to: module
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
const to = placeOf(source, module);
|
|
58
|
+
if (from === to) return void 0;
|
|
59
|
+
const fromRank = source.ranks.get(from);
|
|
60
|
+
const toRank = source.ranks.get(to);
|
|
61
|
+
if (fromRank === void 0 || toRank === void 0) return {
|
|
62
|
+
messageId: "unplaced",
|
|
63
|
+
data: { place: fromRank === void 0 ? from : to }
|
|
64
|
+
};
|
|
65
|
+
const features = source.features ?? [];
|
|
66
|
+
if (features.includes(to) && !features.includes(from) && !testingFile.test(file)) return {
|
|
67
|
+
messageId: "feature",
|
|
68
|
+
data: {
|
|
69
|
+
from,
|
|
70
|
+
to
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
if (toRank < fromRank) return void 0;
|
|
74
|
+
return {
|
|
75
|
+
messageId: toRank === fromRank ? "beside" : "above",
|
|
76
|
+
data: {
|
|
77
|
+
from,
|
|
78
|
+
to
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
export { folderImportsOf, layerProblem, moduleNamed, placeOf };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
// src/lint/generic/imports.ts
|
|
3
|
+
const isString = Schema.is(Schema.String);
|
|
4
|
+
/** The module a dynamic import names, when it's written out. */
|
|
5
|
+
const sourceOf = (node) => {
|
|
6
|
+
if (node.type === "Literal") return isString(node.value) ? node.value : void 0;
|
|
7
|
+
return node.type === "TemplateLiteral" && node.expressions.length === 0 ? node.quasis[0]?.value.cooked ?? void 0 : void 0;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* A visitor that hands `check` each import in a file: its node, the module it names, and whether
|
|
11
|
+
* it takes only types, so it loads nothing. That's an import, a re-export, a dynamic import whose
|
|
12
|
+
* module is written out, and an `import()` type. `import { type A }` isn't types only:
|
|
13
|
+
* `verbatimModuleSyntax` keeps it as `import {} from`, which still loads its module. It visits
|
|
14
|
+
* Oxlint's tree in a rule, and oxc's from `parseSync`, so each node is only what it reads.
|
|
15
|
+
*/
|
|
16
|
+
const importsVisitor = (check) => ({
|
|
17
|
+
ImportDeclaration(node) {
|
|
18
|
+
check(node, node.source.value, node.importKind === "type");
|
|
19
|
+
},
|
|
20
|
+
ExportNamedDeclaration(node) {
|
|
21
|
+
if (node.source !== null) check(node, node.source.value, node.exportKind === "type");
|
|
22
|
+
},
|
|
23
|
+
ExportAllDeclaration(node) {
|
|
24
|
+
check(node, node.source.value, node.exportKind === "type");
|
|
25
|
+
},
|
|
26
|
+
ImportExpression(node) {
|
|
27
|
+
const source = sourceOf(node.source);
|
|
28
|
+
if (source !== void 0) check(node, source, false);
|
|
29
|
+
},
|
|
30
|
+
TSImportType(node) {
|
|
31
|
+
check(node, node.source.value, true);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
export { importsVisitor };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Workspace } from "./workspace-packages.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* How a package may import an entry point of another: `values`, anything; `types`, only with
|
|
4
|
+
* `import type`, which loads nothing; or `tests`, anything, but only in its tests and the modules
|
|
5
|
+
* in a `testing/` folder they share.
|
|
6
|
+
*/
|
|
7
|
+
export type How = 'values' | 'types' | 'tests';
|
|
8
|
+
/**
|
|
9
|
+
* The entry points of a package another imports, by how it may import each. An entry point is a
|
|
10
|
+
* key of the package's `exports` without its `./`, such as `cast`, or `.` for its root. A
|
|
11
|
+
* pattern's is its folder: `testing/vectors` for `./testing/vectors/*.json`.
|
|
12
|
+
*/
|
|
13
|
+
export type Edge = { readonly [Kind in How]?: ReadonlyArray<string>; };
|
|
14
|
+
/**
|
|
15
|
+
* What each package and app may import of the others, by name, then by entry point. Every package
|
|
16
|
+
* and app is named, with `{}` when it imports none of the others. `From` and `To` let a repo hold
|
|
17
|
+
* its graph to its own names, as `satisfies PackageGraph<Library | App, Library>`.
|
|
18
|
+
*/
|
|
19
|
+
export type PackageGraph<From extends string = string, To extends string = string> = { readonly [Name in From]: { readonly [Name in To]?: Edge; }; };
|
|
20
|
+
/**
|
|
21
|
+
* What `ref0-generic/package-imports` takes: where the packages are, the graph, and `nodeFiles`,
|
|
22
|
+
* the files of a package that run only in Node, by its name, from its folder, which take values
|
|
23
|
+
* where it takes only types.
|
|
24
|
+
*/
|
|
25
|
+
export interface PackageGraphOptions extends Workspace {
|
|
26
|
+
readonly graph: PackageGraph;
|
|
27
|
+
readonly nodeFiles?: {
|
|
28
|
+
readonly [name: string]: ReadonlyArray<string>;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* What's wrong with the graph itself: a package it leaves out, a name no package has, an edge to
|
|
33
|
+
* an app, an edge to a package its importer doesn't depend on, an entry point the target doesn't
|
|
34
|
+
* export, and a Node-only file that isn't there. A repo's test expects none.
|
|
35
|
+
*/
|
|
36
|
+
export declare const graphProblems: (options: PackageGraphOptions) => ReadonlyArray<string>;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { ownerOf, workspacePackages } from "./workspace-packages.mjs";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { join, posix } from "node:path";
|
|
4
|
+
import { Schema } from "effect";
|
|
5
|
+
// src/lint/generic/package-graph.ts
|
|
6
|
+
const hows = [
|
|
7
|
+
"values",
|
|
8
|
+
"types",
|
|
9
|
+
"tests"
|
|
10
|
+
];
|
|
11
|
+
const Edge = Schema.Struct({
|
|
12
|
+
values: Schema.optionalKey(Schema.Array(Schema.String)),
|
|
13
|
+
types: Schema.optionalKey(Schema.Array(Schema.String)),
|
|
14
|
+
tests: Schema.optionalKey(Schema.Array(Schema.String))
|
|
15
|
+
});
|
|
16
|
+
const PackageGraphOptions = Schema.Struct({
|
|
17
|
+
root: Schema.String,
|
|
18
|
+
packages: Schema.Array(Schema.String),
|
|
19
|
+
apps: Schema.optionalKey(Schema.Array(Schema.String)),
|
|
20
|
+
graph: Schema.Record(Schema.String, Schema.Record(Schema.String, Edge)),
|
|
21
|
+
nodeFiles: Schema.optionalKey(Schema.Record(Schema.String, Schema.Array(Schema.String)))
|
|
22
|
+
});
|
|
23
|
+
const decodePackageGraphOptions = Schema.decodeUnknownSync(Schema.Tuple([PackageGraphOptions]));
|
|
24
|
+
/** A test: what a test runner runs. */
|
|
25
|
+
const testFile = /\.(test|spec)(-d)?\.[cm]?[jt]sx?$/;
|
|
26
|
+
/** Code only tests run: a test, or a module in a `testing/` folder that tests share. */
|
|
27
|
+
const testCode = (path) => testFile.test(path) || /\/testing\/[^/]+$/.test(path);
|
|
28
|
+
/** The workspace package an import names, such as `@acme/ui` for `@acme/ui/cast`. */
|
|
29
|
+
const packageNamed = (source, packages) => {
|
|
30
|
+
const parts = source.split("/");
|
|
31
|
+
return packages.get(source.startsWith("@") ? parts.slice(0, 2).join("/") : parts[0] ?? "");
|
|
32
|
+
};
|
|
33
|
+
/** Whether `subpath`, such as `./testing/vectors/keys.json`, matches an `exports` pattern. */
|
|
34
|
+
const matches = (pattern, subpath) => {
|
|
35
|
+
const [before = "", after = ""] = pattern.split("*");
|
|
36
|
+
return subpath.startsWith(before) && subpath.endsWith(after);
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* The entry point of `to` an import names, as the graph writes it: `cast` for `@acme/ui/cast`, `.`
|
|
40
|
+
* for `@acme/ui`, `base.css` for `@acme/ui/base.css?raw`, and a pattern's folder,
|
|
41
|
+
* `testing/vectors` for `@acme/core/testing/vectors/keys.json`.
|
|
42
|
+
*/
|
|
43
|
+
const entryNamed = (to, source) => {
|
|
44
|
+
const subpath = `.${source.replace(/\?.*$/, "").slice(to.name.length)}`;
|
|
45
|
+
const pattern = to.entries.find((key) => key.includes("*") && matches(key, subpath));
|
|
46
|
+
const entry = pattern === void 0 ? subpath : pattern.slice(0, pattern.indexOf("/*"));
|
|
47
|
+
return entry === "." ? entry : entry.slice(2);
|
|
48
|
+
};
|
|
49
|
+
/** Whether `to` exports `entry`, as the graph writes it. */
|
|
50
|
+
const exported = (to, entry) => to.entries.some((key) => key === (entry === "." ? "." : `./${entry}`) || key.startsWith(`./${entry}/*`));
|
|
51
|
+
/** An entry point as an import names it, such as `@acme/ui/cast`, or `@acme/ui` for `.`. */
|
|
52
|
+
const shown = (to, entry) => entry === "." ? to : `${to}/${entry}`;
|
|
53
|
+
/** The entry points an edge lists, each in backticks, in order. */
|
|
54
|
+
const listed = (edge) => hows.flatMap((how) => edge[how] ?? []).toSorted().map((entry) => `\`${entry}\``).join(", ");
|
|
55
|
+
/** Why `path`, in `from`, may not import `to`'s `entry` as it does, if it may not. */
|
|
56
|
+
const edgeProblem = ({ graph, nodeFiles }, from, to, entry, path, typesOnly) => {
|
|
57
|
+
if (to.app) return "app";
|
|
58
|
+
const edge = graph[from.name]?.[to.name];
|
|
59
|
+
if (edge === void 0) return "edge";
|
|
60
|
+
if (entry === void 0) return "relative";
|
|
61
|
+
const how = hows.find((kind) => edge[kind]?.includes(entry) === true);
|
|
62
|
+
if (how === void 0) return "entry";
|
|
63
|
+
if (how === "tests" && !testCode(path)) return "tests";
|
|
64
|
+
const runsInNode = testFile.test(path) || (nodeFiles?.[from.name] ?? []).some((file) => `${from.directory}/${file}` === path);
|
|
65
|
+
return how === "types" && !typesOnly && !runsInNode ? "types" : void 0;
|
|
66
|
+
};
|
|
67
|
+
/** What the file at `path` imports by `source`: a package, or a relative path's target from the root. */
|
|
68
|
+
const importedBy = (packages, path, source) => {
|
|
69
|
+
if (!source.startsWith(".")) return {
|
|
70
|
+
to: packageNamed(source, packages),
|
|
71
|
+
target: void 0
|
|
72
|
+
};
|
|
73
|
+
const target = posix.normalize(posix.join(posix.dirname(path), source.replace(/\?.*$/, "")));
|
|
74
|
+
return {
|
|
75
|
+
to: ownerOf(target, packages),
|
|
76
|
+
target
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* What's wrong with the graph itself: a package it leaves out, a name no package has, an edge to
|
|
81
|
+
* an app, an edge to a package its importer doesn't depend on, an entry point the target doesn't
|
|
82
|
+
* export, and a Node-only file that isn't there. A repo's test expects none.
|
|
83
|
+
*/
|
|
84
|
+
const graphProblems = (options) => {
|
|
85
|
+
const { root, graph, nodeFiles = {} } = options;
|
|
86
|
+
const packages = workspacePackages(options);
|
|
87
|
+
const missing = [...packages.keys()].flatMap((name) => graph[name] === void 0 ? [`${name} isn't in the graph: add it, with {} if it imports none of the others.`] : []);
|
|
88
|
+
const edges = Object.entries(graph).flatMap(([from, targets]) => {
|
|
89
|
+
const source = packages.get(from);
|
|
90
|
+
if (source === void 0) return [`${from} is no workspace package: remove it.`];
|
|
91
|
+
return Object.entries(targets).flatMap(([to, edge = {}]) => {
|
|
92
|
+
const target = packages.get(to);
|
|
93
|
+
if (target === void 0) return [`${from}'s edge names ${to}, which is no workspace package.`];
|
|
94
|
+
if (target.app) return [`${from}'s edge names ${to}, an app, which nothing imports.`];
|
|
95
|
+
const undeclared = source.dependencies.has(to) ? [] : [`${from} imports ${to} but its package.json doesn't depend on it.`];
|
|
96
|
+
const unexported = hows.flatMap((how) => (edge[how] ?? []).flatMap((entry) => exported(target, entry) ? [] : [`${from}'s edge lists ${shown(to, entry)}, which ${to} doesn't export.`]));
|
|
97
|
+
return [...undeclared, ...unexported];
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
const nodeOnly = Object.entries(nodeFiles).flatMap(([name, files]) => {
|
|
101
|
+
const owner = packages.get(name);
|
|
102
|
+
if (owner === void 0) return [`nodeFiles names ${name}, which is no workspace package.`];
|
|
103
|
+
return files.flatMap((file) => existsSync(join(root, owner.directory, file)) ? [] : [`nodeFiles lists ${owner.directory}/${file}, which isn't there.`]);
|
|
104
|
+
});
|
|
105
|
+
return [
|
|
106
|
+
...missing,
|
|
107
|
+
...edges,
|
|
108
|
+
...nodeOnly
|
|
109
|
+
];
|
|
110
|
+
};
|
|
111
|
+
export { decodePackageGraphOptions, edgeProblem, entryNamed, graphProblems, importedBy, listed, packageNamed, shown, testFile };
|