paperlint 20.0.1 → 20.1.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/.claude-plugin/plugin.json +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +5 -1
- package/dist/cli.js.map +1 -1
- package/dist/domain/paper-folder.d.ts.map +1 -1
- package/dist/domain/paper-folder.js +4 -0
- package/dist/domain/paper-folder.js.map +1 -1
- package/dist/domain/paper-layout.d.ts +18 -6
- package/dist/domain/paper-layout.d.ts.map +1 -1
- package/dist/domain/paper-layout.js +44 -38
- package/dist/domain/paper-layout.js.map +1 -1
- package/dist/folder-layout-hook.js +1 -1
- package/dist/hook-counts.d.ts +30 -0
- package/dist/hook-counts.d.ts.map +1 -0
- package/dist/hook-counts.js +35 -0
- package/dist/hook-counts.js.map +1 -0
- package/dist/hooks-settings.d.ts +8 -10
- package/dist/hooks-settings.d.ts.map +1 -1
- package/dist/hooks-settings.js +31 -35
- package/dist/hooks-settings.js.map +1 -1
- package/dist/lint-edited-block.d.ts +8 -0
- package/dist/lint-edited-block.d.ts.map +1 -0
- package/dist/lint-edited-block.js +30 -0
- package/dist/lint-edited-block.js.map +1 -0
- package/dist/lint-edited-hook.d.ts +5 -6
- package/dist/lint-edited-hook.d.ts.map +1 -1
- package/dist/lint-edited-hook.js +47 -25
- package/dist/lint-edited-hook.js.map +1 -1
- package/dist/lint-edited-shell.d.ts +15 -0
- package/dist/lint-edited-shell.d.ts.map +1 -0
- package/dist/lint-edited-shell.js +49 -0
- package/dist/lint-edited-shell.js.map +1 -0
- package/docs/e2e.md +1 -1
- package/docs/install.md +11 -11
- package/docs/layout.md +19 -4
- package/docs/rules/paper/allowed-files-only.md +31 -28
- package/package.json +1 -1
- package/plugin/hooks/hooks.json +11 -0
- package/src/cli.harness.mjs +1 -1
- package/src/cli.test.ts +1 -1
- package/src/cli.ts +5 -1
- package/src/doctor.test.ts +23 -6
- package/src/domain/paper-folder.ts +3 -0
- package/src/domain/paper-layout.test.ts +89 -49
- package/src/domain/paper-layout.ts +61 -42
- package/src/folder-layout-hook.test.ts +1 -1
- package/src/folder-layout-hook.ts +1 -1
- package/src/folder-layout-rule.test.ts +1 -1
- package/src/hook-counts.test.ts +67 -0
- package/src/hook-counts.ts +64 -0
- package/src/hooks-settings.harness.ts +15 -18
- package/src/hooks-settings.ts +43 -49
- package/src/init-plugin.test.ts +8 -2
- package/src/layout-docs.test.ts +47 -0
- package/src/lint-edited-bash.test.ts +324 -0
- package/src/lint-edited-block.ts +44 -0
- package/src/lint-edited-hook.test.ts +10 -11
- package/src/lint-edited-hook.ts +72 -35
- package/src/lint-edited-shell.ts +72 -0
|
@@ -31,7 +31,14 @@
|
|
|
31
31
|
*/
|
|
32
32
|
import { basename, extname } from "node:path";
|
|
33
33
|
import { leaf, renderTree, type TreeNode } from "./layout-tree.ts";
|
|
34
|
-
import {
|
|
34
|
+
import {
|
|
35
|
+
DATA_EXTENSIONS,
|
|
36
|
+
NOTE_EXTENSIONS,
|
|
37
|
+
REPRO_DIR,
|
|
38
|
+
REPRO_NODES,
|
|
39
|
+
SCRIPT_EXTENSIONS,
|
|
40
|
+
TOOL_FILES,
|
|
41
|
+
} from "./repro-layout.ts";
|
|
35
42
|
|
|
36
43
|
/** One entry at the paper folder's root: its name, and whether it is a folder. */
|
|
37
44
|
export interface LayoutEntry {
|
|
@@ -51,7 +58,7 @@ export interface Stray {
|
|
|
51
58
|
/** The entry's name; a folder's ends in a slash. */
|
|
52
59
|
readonly name: string;
|
|
53
60
|
readonly kind: "file" | "dir";
|
|
54
|
-
/** Where it goes: one destination
|
|
61
|
+
/** Where it goes: one destination, by its name, else by its kind, else `extra/`. */
|
|
55
62
|
readonly cure: string;
|
|
56
63
|
}
|
|
57
64
|
|
|
@@ -185,12 +192,10 @@ export const BUILD_BYPRODUCTS: readonly string[] = BYPRODUCT_EXTENSIONS.map(
|
|
|
185
192
|
export const OS_FILES: readonly string[] = [".DS_Store", "Thumbs.db"];
|
|
186
193
|
|
|
187
194
|
/** The live state of a paper — its stages, its plan, its scorecard — is one file. */
|
|
188
|
-
const STATUS_SECTIONS =
|
|
189
|
-
'a section of PIPELINE-STATUS.md — the paper\'s one live-state file holds its stages, its plan ("Plan / now") and its scorecard; move what is still live there and delete the file';
|
|
195
|
+
const STATUS_SECTIONS = "a section of PIPELINE-STATUS.md";
|
|
190
196
|
|
|
191
|
-
/** The shared corpus of a venue's accepted papers. */
|
|
192
|
-
const VENUE_CORPUS =
|
|
193
|
-
"<papers-root>/venues/<venue>/, outside the paper folder: one corpus per venue, shared by every paper that targets it";
|
|
197
|
+
/** The shared corpus of a venue's accepted papers, outside every paper folder. */
|
|
198
|
+
const VENUE_CORPUS = "<papers-root>/venues/<venue>/";
|
|
194
199
|
|
|
195
200
|
/**
|
|
196
201
|
* Names met in real paper folders, each with its one destination. Matched ignoring case, a file's
|
|
@@ -203,34 +208,43 @@ export const KNOWN_CURES: readonly {
|
|
|
203
208
|
}[] = [
|
|
204
209
|
{ name: "PLAN.md", kind: "file", cure: STATUS_SECTIONS },
|
|
205
210
|
{ name: "plans", kind: "dir", cure: STATUS_SECTIONS },
|
|
206
|
-
{ name: "DECISION-LOG.md", kind: "file", cure: "research/
|
|
207
|
-
{
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
},
|
|
212
|
-
{
|
|
213
|
-
name: "data",
|
|
214
|
-
kind: "dir",
|
|
215
|
-
cure: "repro/data/ (the data behind the numbers)",
|
|
216
|
-
},
|
|
217
|
-
{
|
|
218
|
-
name: "artifact",
|
|
219
|
-
kind: "dir",
|
|
220
|
-
cure: "repro/ — its scripts in repro/tools/, its data in repro/data/; the anonymized copy uploaded for review is about submitting and goes in submission/",
|
|
221
|
-
},
|
|
222
|
-
{
|
|
223
|
-
name: "archive",
|
|
224
|
-
kind: "dir",
|
|
225
|
-
cure: "versions/ for frozen PDFs, submission/ for an earlier attempt's forms, mails and checklists, extra/ for the rest",
|
|
226
|
-
},
|
|
211
|
+
{ name: "DECISION-LOG.md", kind: "file", cure: "research/" },
|
|
212
|
+
{ name: "EMAILS.md", kind: "file", cure: "submission/" },
|
|
213
|
+
{ name: "data", kind: "dir", cure: "repro/data/" },
|
|
214
|
+
{ name: "artifact", kind: "dir", cure: "repro/" },
|
|
215
|
+
{ name: "archive", kind: "dir", cure: `${ESCAPE_HATCH}/` },
|
|
227
216
|
{ name: "venue-corpus", kind: "dir", cure: VENUE_CORPUS },
|
|
228
217
|
{ name: "venues", kind: "dir", cure: VENUE_CORPUS },
|
|
229
218
|
];
|
|
230
219
|
|
|
231
|
-
/**
|
|
232
|
-
export const
|
|
233
|
-
"
|
|
220
|
+
/** The extensions of the images a figure is drawn from. */
|
|
221
|
+
export const IMAGE_EXTENSIONS: readonly string[] = [
|
|
222
|
+
".png",
|
|
223
|
+
".jpg",
|
|
224
|
+
".jpeg",
|
|
225
|
+
".gif",
|
|
226
|
+
".svg",
|
|
227
|
+
".eps",
|
|
228
|
+
];
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* For a file nobody listed: the folder its kind names, by its exact name or its extension (matched
|
|
232
|
+
* ignoring case). The same kinds `repro/allowed-files-only` sorts by. No `.pdf`: a root PDF may be
|
|
233
|
+
* a figure, a draft or a review, and `versions/` holds only cycle folders (`versions/layout`).
|
|
234
|
+
*/
|
|
235
|
+
export const CURE_BY_FILE: readonly {
|
|
236
|
+
readonly names: readonly string[];
|
|
237
|
+
readonly extensions: readonly string[];
|
|
238
|
+
readonly cure: string;
|
|
239
|
+
}[] = [
|
|
240
|
+
{ names: TOOL_FILES, extensions: SCRIPT_EXTENSIONS, cure: "repro/tools/" },
|
|
241
|
+
{ names: [], extensions: DATA_EXTENSIONS, cure: "repro/data/" },
|
|
242
|
+
{ names: [], extensions: IMAGE_EXTENSIONS, cure: "figures/" },
|
|
243
|
+
{ names: [], extensions: NOTE_EXTENSIONS, cure: "research/" },
|
|
244
|
+
];
|
|
245
|
+
|
|
246
|
+
/** For anything else: the folder that is never judged. */
|
|
247
|
+
export const CURE_BY_DEFAULT = `${ESCAPE_HATCH}/`;
|
|
234
248
|
|
|
235
249
|
const isAllowedFile = (name: string, declared: Declared): boolean =>
|
|
236
250
|
ROOT_FILES.includes(name) ||
|
|
@@ -244,12 +258,20 @@ const isAllowed = (e: LayoutEntry, declared: Declared): boolean =>
|
|
|
244
258
|
? ROOT_DIRS.includes(e.name) || declared.roots.includes(e.name)
|
|
245
259
|
: isAllowedFile(e.name, declared);
|
|
246
260
|
|
|
247
|
-
/** Where
|
|
261
|
+
/** Where `e` belongs: its listed destination, else its kind's folder, else `extra/`. */
|
|
248
262
|
function cureFor(e: LayoutEntry): string {
|
|
249
263
|
const known = KNOWN_CURES.find(
|
|
250
264
|
(c) => c.kind === e.kind && c.name.toLowerCase() === e.name.toLowerCase(),
|
|
251
265
|
);
|
|
252
|
-
|
|
266
|
+
if (known !== undefined) return known.cure;
|
|
267
|
+
const ext = extname(e.name).toLowerCase();
|
|
268
|
+
const byKind =
|
|
269
|
+
e.kind === "file"
|
|
270
|
+
? CURE_BY_FILE.find(
|
|
271
|
+
(c) => c.names.includes(e.name) || c.extensions.includes(ext),
|
|
272
|
+
)
|
|
273
|
+
: undefined;
|
|
274
|
+
return byKind?.cure ?? CURE_BY_DEFAULT;
|
|
253
275
|
}
|
|
254
276
|
|
|
255
277
|
/** The root entries that are not part of the layout, in the order given. Pure. */
|
|
@@ -289,7 +311,7 @@ export const PAPER_NODES: readonly TreeNode[] = [
|
|
|
289
311
|
})),
|
|
290
312
|
];
|
|
291
313
|
|
|
292
|
-
/** The drawing of the paper folder's layout
|
|
314
|
+
/** The drawing of the paper folder's layout, which the rule's page embeds. */
|
|
293
315
|
export const PAPER_LAYOUT_TREE: string = renderTree("<paper>/", PAPER_NODES);
|
|
294
316
|
|
|
295
317
|
/** The folder beside the paper folders that holds one corpus of accepted papers per venue. */
|
|
@@ -326,14 +348,11 @@ export const PAPERS_LAYOUT_TREE: string = renderTree(
|
|
|
326
348
|
|
|
327
349
|
/**
|
|
328
350
|
* The one text a stray entry is reported with, by the rule `paper/allowed-files-only` and by the hook that
|
|
329
|
-
* refuses to create such an entry
|
|
330
|
-
*
|
|
351
|
+
* refuses to create such an entry: one line, the entry and its one destination. The layout, and how
|
|
352
|
+
* each destination is picked, are on the rule's page, which the finding's rule id names.
|
|
331
353
|
*/
|
|
332
354
|
export function strayMessage(s: Stray): string {
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
PAPER_LAYOUT_TREE,
|
|
337
|
-
`Rule paper/allowed-files-only: https://github.com/zernie/paperlint/blob/main/docs/rules/paper/allowed-files-only.md`,
|
|
338
|
-
].join("\n");
|
|
355
|
+
const fallback =
|
|
356
|
+
s.cure === CURE_BY_DEFAULT ? "the folder for its kind" : CURE_BY_DEFAULT;
|
|
357
|
+
return `\`${s.name}\` does not belong at a paper folder's root: move it to ${s.cure}, or ${fallback}.`;
|
|
339
358
|
}
|
|
@@ -75,7 +75,7 @@ describe("paper-folder-layout hook — denies a new stray entry", () => {
|
|
|
75
75
|
},
|
|
76
76
|
});
|
|
77
77
|
expect(out?.hookSpecificOutput.permissionDecisionReason).toMatch(
|
|
78
|
-
/^`PLAN\.md`
|
|
78
|
+
/^`PLAN\.md` does not belong at a paper folder's root: move it to a section of PIPELINE-STATUS\.md[^\n]*$/u,
|
|
79
79
|
);
|
|
80
80
|
});
|
|
81
81
|
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* created, not at the next lint. PreToolUse on `Edit|Write|MultiEdit` (`plugin/hooks/hooks.json`):
|
|
4
4
|
* when the file about to be written does not exist yet and would add an entry the layout does not
|
|
5
5
|
* allow — at a paper folder's root, or at the top of its `repro/` — the call is denied, and the
|
|
6
|
-
* reason is the rule's own message: where the entry goes
|
|
6
|
+
* reason is the rule's own message: where the entry goes (for `repro/`, also its layout drawn). The agent writes it
|
|
7
7
|
* where it belongs instead, in the same turn.
|
|
8
8
|
*
|
|
9
9
|
* ── WHAT IT NEVER BLOCKS ─────────────────────────────────────────────────────────
|
|
@@ -75,7 +75,7 @@ describe("paper/allowed-files-only — reports", () => {
|
|
|
75
75
|
),
|
|
76
76
|
).toEqual([
|
|
77
77
|
expect.stringMatching(
|
|
78
|
-
/^`archive\/`
|
|
78
|
+
/^`archive\/` does not belong at a paper folder's root: move it to extra\//,
|
|
79
79
|
),
|
|
80
80
|
]);
|
|
81
81
|
});
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The doctor's hook arithmetic (`src/hook-counts.ts`), both halves: a hook is short when fewer commands
|
|
3
|
+
* run it than the wiring ships (none, or some: `lint-edited` after an Edit only, as an older `init`
|
|
4
|
+
* wired it), over-wired when more do, and neither when the set is whole or entirely unwired.
|
|
5
|
+
*/
|
|
6
|
+
import { describe, expect, it } from "vitest";
|
|
7
|
+
import {
|
|
8
|
+
overWired,
|
|
9
|
+
partlyMissing,
|
|
10
|
+
shortLabels,
|
|
11
|
+
type HookCount,
|
|
12
|
+
} from "./hook-counts.ts";
|
|
13
|
+
|
|
14
|
+
/** `lint-edited` hangs off an Edit and a Bash command; the others once each. */
|
|
15
|
+
const SHIPPED = { "submission-confirm": 1, "lint-edited": 2 };
|
|
16
|
+
/** `places`: the distinct event and matcher pairs it runs under; one per command unless given. */
|
|
17
|
+
const count = (
|
|
18
|
+
name: string,
|
|
19
|
+
ours: number,
|
|
20
|
+
other = 0,
|
|
21
|
+
places = ours + other,
|
|
22
|
+
): HookCount => ({ name, ours, other, places });
|
|
23
|
+
|
|
24
|
+
describe("hook counts — against the shipped wiring", () => {
|
|
25
|
+
it("🔴 wired by an older init, lint-edited once of its two commands: short, and named with its count", () => {
|
|
26
|
+
const counts = [count("submission-confirm", 1), count("lint-edited", 1)];
|
|
27
|
+
expect(partlyMissing(counts, SHIPPED)).toEqual(["lint-edited"]);
|
|
28
|
+
expect(shortLabels(counts, SHIPPED)).toEqual([
|
|
29
|
+
"lint-edited (in 1 of its 2 places)",
|
|
30
|
+
]);
|
|
31
|
+
expect(overWired(counts, SHIPPED)).toEqual([]);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("a hook nothing runs is named bare; a name the wiring does not ship counts as once", () => {
|
|
35
|
+
const counts = [count("submission-confirm", 1), count("unknown", 0)];
|
|
36
|
+
expect(shortLabels(counts, SHIPPED)).toEqual(["unknown"]);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("wired as shipped, under any spelling: nothing short, nothing over", () => {
|
|
40
|
+
const counts = [count("submission-confirm", 0, 1), count("lint-edited", 2)];
|
|
41
|
+
expect(partlyMissing(counts, SHIPPED)).toEqual([]);
|
|
42
|
+
expect(overWired(counts, SHIPPED)).toEqual([]);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("nothing wired at all is not partly wired", () => {
|
|
46
|
+
const counts = [count("submission-confirm", 0), count("lint-edited", 0)];
|
|
47
|
+
expect(partlyMissing(counts, SHIPPED)).toEqual([]);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("🔴 more commands than shipped: over-wired, lint-edited only past its two", () => {
|
|
51
|
+
const counts = [count("submission-confirm", 1, 1), count("lint-edited", 2)];
|
|
52
|
+
expect(overWired(counts, SHIPPED)).toEqual([counts[0]]);
|
|
53
|
+
expect(overWired([count("lint-edited", 3)], SHIPPED)).toHaveLength(1);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("🔴 two commands after an Edit and none after Bash: short of its Bash place, and over-wired after the Edit", () => {
|
|
57
|
+
const counts = [
|
|
58
|
+
count("submission-confirm", 1),
|
|
59
|
+
count("lint-edited", 1, 1, 1),
|
|
60
|
+
];
|
|
61
|
+
expect(partlyMissing(counts, SHIPPED)).toEqual(["lint-edited"]);
|
|
62
|
+
expect(shortLabels(counts, SHIPPED)).toEqual([
|
|
63
|
+
"lint-edited (in 1 of its 2 places)",
|
|
64
|
+
]);
|
|
65
|
+
expect(overWired(counts, SHIPPED)).toEqual([counts[1]]);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The arithmetic of `paperlint doctor`'s hook section (`src/hooks-settings.ts`): in how many places
|
|
3
|
+
* (an event and a matcher) each shipped hook runs, against how many the shipped wiring has. One hook
|
|
4
|
+
* may hang off two places (`lint-edited` after an Edit and after a Bash command), so it is wired whole
|
|
5
|
+
* only when it runs in as many distinct places as it ships: two commands in one place stand in for
|
|
6
|
+
* none of the others. It is wired twice when one place runs it more than once, or when it has more
|
|
7
|
+
* commands than it ships.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** How many commands run one shipped hook, split by spelling, and in how many distinct places. */
|
|
11
|
+
export interface HookCount {
|
|
12
|
+
readonly name: string;
|
|
13
|
+
readonly ours: number;
|
|
14
|
+
readonly other: number;
|
|
15
|
+
/** The distinct event and matcher pairs its commands hang off. */
|
|
16
|
+
readonly places: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** How many commands, each in a place of its own, of the shipped wiring run each hook name. */
|
|
20
|
+
export type Shipped = Readonly<Record<string, number>>;
|
|
21
|
+
|
|
22
|
+
export const total = (c: HookCount): number => c.ours + c.other;
|
|
23
|
+
|
|
24
|
+
/** In how many places the shipped wiring runs `name`; a name it does not ship, once. */
|
|
25
|
+
const times = (shipped: Shipped, name: string): number => shipped[name] ?? 1;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The hooks short of their shipped places in a set that is partly wired; empty when it is whole or
|
|
29
|
+
* when nothing is wired. A hook in some of its places is short too: `lint-edited` wired by an older
|
|
30
|
+
* `init` after an Edit only, however many commands run it there.
|
|
31
|
+
*/
|
|
32
|
+
export function partlyMissing(
|
|
33
|
+
counts: readonly HookCount[],
|
|
34
|
+
shipped: Shipped,
|
|
35
|
+
): readonly string[] {
|
|
36
|
+
if (counts.every((c) => total(c) === 0)) return [];
|
|
37
|
+
return counts
|
|
38
|
+
.filter((c) => c.places < times(shipped, c.name))
|
|
39
|
+
.map((c) => c.name);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The hooks that one place runs more than once, or that have more commands than the wiring ships. */
|
|
43
|
+
export const overWired = (
|
|
44
|
+
counts: readonly HookCount[],
|
|
45
|
+
shipped: Shipped,
|
|
46
|
+
): readonly HookCount[] =>
|
|
47
|
+
counts.filter(
|
|
48
|
+
(c) => total(c) > c.places || total(c) > times(shipped, c.name),
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
/** Each short hook as the doctor names it: `lint-edited (in 1 of its 2 places)`, or the bare name. */
|
|
52
|
+
export const shortLabels = (
|
|
53
|
+
counts: readonly HookCount[],
|
|
54
|
+
shipped: Shipped,
|
|
55
|
+
): readonly string[] => {
|
|
56
|
+
const short = partlyMissing(counts, shipped);
|
|
57
|
+
return counts
|
|
58
|
+
.filter((c) => short.includes(c.name))
|
|
59
|
+
.map((c) =>
|
|
60
|
+
total(c) === 0
|
|
61
|
+
? c.name
|
|
62
|
+
: `${c.name} (in ${String(c.places)} of its ${String(times(shipped, c.name))} places)`,
|
|
63
|
+
);
|
|
64
|
+
};
|
|
@@ -69,21 +69,16 @@ check(
|
|
|
69
69
|
wiring.names.join() === "submission-confirm,paper-folder-layout,lint-edited",
|
|
70
70
|
);
|
|
71
71
|
// ── settings written by hand: shapes Claude Code would ignore are skipped, not crashed on ──
|
|
72
|
+
const MALFORMED = {
|
|
73
|
+
PreToolUse: "npx paperlint hook submission-confirm",
|
|
74
|
+
PostToolUse: [{ matcher: "Edit" }, null, { hooks: "x" }],
|
|
75
|
+
Stop: [{ hooks: [{ type: "command" }, null] }],
|
|
76
|
+
};
|
|
77
|
+
const zero = { ours: 0, other: 0, places: 0 };
|
|
72
78
|
check(
|
|
73
79
|
"an event that is not a list, an entry with no `hooks` list, a handler with no command → no commands",
|
|
74
|
-
JSON.stringify(
|
|
75
|
-
|
|
76
|
-
{
|
|
77
|
-
hooks: {
|
|
78
|
-
PreToolUse: "npx paperlint hook submission-confirm",
|
|
79
|
-
PostToolUse: [{ matcher: "Edit" }, null, { hooks: "x" }],
|
|
80
|
-
Stop: [{ hooks: [{ type: "command" }, null] }],
|
|
81
|
-
},
|
|
82
|
-
},
|
|
83
|
-
wiring.names,
|
|
84
|
-
),
|
|
85
|
-
) ===
|
|
86
|
-
JSON.stringify(wiring.names.map((name) => ({ name, ours: 0, other: 0 }))),
|
|
80
|
+
JSON.stringify(wiredCounts({ hooks: MALFORMED }, wiring.names)) ===
|
|
81
|
+
JSON.stringify(wiring.names.map((name) => ({ name, ...zero }))),
|
|
87
82
|
);
|
|
88
83
|
{
|
|
89
84
|
const file = join(tmpdir(), `paperlint-no-hooks-${String(process.pid)}.json`);
|
|
@@ -196,7 +191,7 @@ try {
|
|
|
196
191
|
"no settings file → it is created with the four hooks",
|
|
197
192
|
r.status === "written" &&
|
|
198
193
|
wiredCounts(s, wiring.names).every(
|
|
199
|
-
(c) => c.ours ===
|
|
194
|
+
(c) => c.ours === wiring.times[c.name] && c.other === 0,
|
|
200
195
|
),
|
|
201
196
|
);
|
|
202
197
|
const before = text(dir);
|
|
@@ -231,8 +226,10 @@ try {
|
|
|
231
226
|
isDeepStrictEqual(s["permissions"], { allow: ["Bash(ls:*)"] }),
|
|
232
227
|
);
|
|
233
228
|
check(
|
|
234
|
-
"and ours are there,
|
|
235
|
-
wiredCounts(s, wiring.names).every(
|
|
229
|
+
"and ours are there, each as many times as the wiring ships it",
|
|
230
|
+
wiredCounts(s, wiring.names).every(
|
|
231
|
+
(c) => c.ours === wiring.times[c.name],
|
|
232
|
+
),
|
|
236
233
|
);
|
|
237
234
|
}
|
|
238
235
|
|
|
@@ -317,8 +314,8 @@ try {
|
|
|
317
314
|
};
|
|
318
315
|
const wiredOnce = merge({}, wiring.compiled, MANAGED_BY);
|
|
319
316
|
check(
|
|
320
|
-
"wired
|
|
321
|
-
/✓ wired — submission-confirm, paper-folder-layout, lint-edited,
|
|
317
|
+
"wired as shipped → ✓ wired, with the fresh-clone consequence",
|
|
318
|
+
/✓ wired — submission-confirm, paper-folder-layout, lint-edited, each as shipped/.test(
|
|
322
319
|
doc(wiredOnce),
|
|
323
320
|
) && /cannot run until `npm install`/.test(doc(wiredOnce)),
|
|
324
321
|
);
|
package/src/hooks-settings.ts
CHANGED
|
@@ -25,6 +25,15 @@
|
|
|
25
25
|
// eslint-disable-next-line boundaries/dependencies -- legacy I/O, moves behind a port in #76
|
|
26
26
|
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
27
27
|
import { basename, dirname, join, posix } from "node:path";
|
|
28
|
+
import { countBy } from "remeda";
|
|
29
|
+
import {
|
|
30
|
+
overWired,
|
|
31
|
+
partlyMissing,
|
|
32
|
+
shortLabels,
|
|
33
|
+
total,
|
|
34
|
+
type HookCount,
|
|
35
|
+
type Shipped,
|
|
36
|
+
} from "./hook-counts.ts";
|
|
28
37
|
import { fileURLToPath } from "node:url";
|
|
29
38
|
import {
|
|
30
39
|
BIN_FILE,
|
|
@@ -131,19 +140,18 @@ export function hookRun(command: string): {
|
|
|
131
140
|
return run && { name: run.name, ours: run.ours };
|
|
132
141
|
}
|
|
133
142
|
|
|
134
|
-
/** Every command in a settings object, with the event it hangs off. */
|
|
135
|
-
export
|
|
136
|
-
settings
|
|
137
|
-
)
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
for (const h of listOf(prop(entry)?.["hooks"])) {
|
|
143
|
+
/** Every command in a settings object, with the event and the matcher it hangs off. */
|
|
144
|
+
export const commandsIn = (settings: Settings) =>
|
|
145
|
+
Object.entries(prop(settings["hooks"]) ?? {}).flatMap(([event, entries]) =>
|
|
146
|
+
listOf(entries).flatMap((entry) => {
|
|
147
|
+
const m = prop(entry)?.["matcher"];
|
|
148
|
+
const matcher = typeof m === "string" ? m : "";
|
|
149
|
+
return listOf(prop(entry)?.["hooks"]).flatMap((h) => {
|
|
142
150
|
const command = prop(h)?.["command"];
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
|
|
151
|
+
return typeof command === "string" ? [{ event, matcher, command }] : [];
|
|
152
|
+
});
|
|
153
|
+
}),
|
|
154
|
+
);
|
|
147
155
|
|
|
148
156
|
/** The settings file is the user's: any level may be missing or of the wrong kind. */
|
|
149
157
|
const listOf = (v: unknown): readonly unknown[] => (Array.isArray(v) ? v : []);
|
|
@@ -175,6 +183,8 @@ export interface Wiring {
|
|
|
175
183
|
readonly compiled: HooksMap;
|
|
176
184
|
/** Derived from the commands, in order. Never empty — an empty set throws. */
|
|
177
185
|
readonly names: readonly string[];
|
|
186
|
+
/** How many commands of the shipped wiring run each name (`src/hook-counts.ts`). */
|
|
187
|
+
readonly times: Shipped;
|
|
178
188
|
}
|
|
179
189
|
|
|
180
190
|
/**
|
|
@@ -195,14 +205,11 @@ export function shippedWiring(file: string = WIRING_FILE): Wiring {
|
|
|
195
205
|
throw new Error(
|
|
196
206
|
`${file} names no paperlint hook — there is nothing to wire`,
|
|
197
207
|
);
|
|
198
|
-
return {
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
readonly name: string;
|
|
204
|
-
readonly ours: number;
|
|
205
|
-
readonly other: number;
|
|
208
|
+
return {
|
|
209
|
+
compiled,
|
|
210
|
+
names: [...new Set(names)],
|
|
211
|
+
times: countBy(names, (n) => n),
|
|
212
|
+
};
|
|
206
213
|
}
|
|
207
214
|
|
|
208
215
|
/** One count per name in `names`, in that order — a name nothing runs has zeros, never a gap. */
|
|
@@ -210,21 +217,18 @@ export function wiredCounts(
|
|
|
210
217
|
settings: Settings,
|
|
211
218
|
names: readonly string[],
|
|
212
219
|
): readonly HookCount[] {
|
|
213
|
-
const runs = commandsIn(settings).flatMap(({ command }) => {
|
|
220
|
+
const runs = commandsIn(settings).flatMap(({ event, matcher, command }) => {
|
|
214
221
|
const run = hookRun(command);
|
|
215
|
-
return run ? [run] : [];
|
|
222
|
+
return run ? [{ ...run, place: `${event} ${matcher}` }] : [];
|
|
223
|
+
});
|
|
224
|
+
return names.map((name) => {
|
|
225
|
+
const mine = runs.filter((r) => r.name === name);
|
|
226
|
+
const ours = mine.filter((r) => r.ours).length;
|
|
227
|
+
const places = new Set(mine.map((r) => r.place)).size;
|
|
228
|
+
return { name, ours, other: mine.length - ours, places };
|
|
216
229
|
});
|
|
217
|
-
const count = (name: string, ours: boolean): number =>
|
|
218
|
-
runs.filter((r) => r.name === name && r.ours === ours).length;
|
|
219
|
-
return names.map((name) => ({
|
|
220
|
-
name,
|
|
221
|
-
ours: count(name, true),
|
|
222
|
-
other: count(name, false),
|
|
223
|
-
}));
|
|
224
230
|
}
|
|
225
231
|
|
|
226
|
-
const total = (c: HookCount): number => c.ours + c.other;
|
|
227
|
-
|
|
228
232
|
export type SettingsRead =
|
|
229
233
|
| {
|
|
230
234
|
readonly status: "absent";
|
|
@@ -396,21 +400,12 @@ export function unwiredHooks(
|
|
|
396
400
|
const none = { missing: [], byHand: false };
|
|
397
401
|
if (read.status === "unparsable") return none;
|
|
398
402
|
const counts = wiredCounts(read.settings, wiring.names);
|
|
399
|
-
const missing = partlyMissing(counts, wiring.
|
|
403
|
+
const missing = partlyMissing(counts, wiring.times);
|
|
400
404
|
return missing.length === 0
|
|
401
405
|
? none
|
|
402
406
|
: { missing, byHand: counts.some((c) => c.other > 0) };
|
|
403
407
|
}
|
|
404
408
|
|
|
405
|
-
/** The hooks missing from a set that is partly wired; empty when it is whole or when nothing is wired. */
|
|
406
|
-
const partlyMissing = (
|
|
407
|
-
counts: readonly HookCount[],
|
|
408
|
-
names: readonly string[],
|
|
409
|
-
): readonly string[] => {
|
|
410
|
-
const missing = counts.filter((c) => total(c) === 0).map((c) => c.name);
|
|
411
|
-
return missing.length === names.length ? [] : missing;
|
|
412
|
-
};
|
|
413
|
-
|
|
414
409
|
/**
|
|
415
410
|
* `paperlint doctor`'s section. `bad` counts the states that are MISWIRED, which fail the run: a
|
|
416
411
|
* hook missing from an otherwise wired set ("partly wired"), and a hook wired twice. "Not wired at
|
|
@@ -431,10 +426,9 @@ export function doctorHooks(
|
|
|
431
426
|
return { lines: out, bad: 0 };
|
|
432
427
|
}
|
|
433
428
|
const counts = wiredCounts(read.settings, wiring.names);
|
|
434
|
-
const
|
|
435
|
-
const twice = counts.
|
|
436
|
-
const partly =
|
|
437
|
-
const remedy = remedyFor(counts);
|
|
429
|
+
const short = partlyMissing(counts, wiring.times);
|
|
430
|
+
const twice = overWired(counts, wiring.times);
|
|
431
|
+
const partly = short.length > 0;
|
|
438
432
|
if (twice.length > 0) {
|
|
439
433
|
out.push(` ⚠ wired TWICE — each of these runs more than once per event:`);
|
|
440
434
|
for (const c of twice) out.push(` ${c.name} ×${String(total(c))}`);
|
|
@@ -442,19 +436,19 @@ export function doctorHooks(
|
|
|
442
436
|
` keep one command per hook; \`npx paperlint init\` writes the ${MANAGED_BY} form`,
|
|
443
437
|
);
|
|
444
438
|
}
|
|
445
|
-
if (
|
|
439
|
+
if (counts.every((c) => total(c) === 0))
|
|
446
440
|
out.push(
|
|
447
441
|
` ⚠ not wired — none of the ${String(wiring.names.length)} hooks is in ${SETTINGS_PATH}. ` +
|
|
448
442
|
`\`npx paperlint init\` wires them`,
|
|
449
443
|
);
|
|
450
444
|
else if (partly)
|
|
451
445
|
out.push(
|
|
452
|
-
` ⚠ partly wired — missing: ${
|
|
453
|
-
` ${
|
|
446
|
+
` ⚠ partly wired — missing: ${shortLabels(counts, wiring.times).join(", ")}`,
|
|
447
|
+
` ${remedyFor(counts)}`,
|
|
454
448
|
);
|
|
455
449
|
else if (twice.length === 0)
|
|
456
450
|
out.push(
|
|
457
|
-
` ✓ wired — ${wiring.names.join(", ")},
|
|
451
|
+
` ✓ wired — ${wiring.names.join(", ")}, each as shipped`,
|
|
458
452
|
` ${FRESH_CLONE_NOTE}`,
|
|
459
453
|
);
|
|
460
454
|
return { lines: out, bad: partly || twice.length > 0 ? 1 : 0 };
|
package/src/init-plugin.test.ts
CHANGED
|
@@ -57,6 +57,12 @@ const settingsOf = (root: string): Settings => {
|
|
|
57
57
|
const read = readSettings(root);
|
|
58
58
|
return read.status === "unparsable" ? {} : read.settings;
|
|
59
59
|
};
|
|
60
|
+
/** How many commands the shipped wiring has: a hook may hang off two events. */
|
|
61
|
+
const shippedCommands = Object.values(shippedWiring().times).reduce(
|
|
62
|
+
(a, b) => a + b,
|
|
63
|
+
0,
|
|
64
|
+
);
|
|
65
|
+
|
|
60
66
|
const oursWired = (root: string): number =>
|
|
61
67
|
wiredCounts(settingsOf(root), shippedWiring().names).reduce(
|
|
62
68
|
(n, c) => n + c.ours,
|
|
@@ -71,7 +77,7 @@ describe("init and the plugin", () => {
|
|
|
71
77
|
expect(r.out).toContain(
|
|
72
78
|
"Claude Code plugin: not turned on — stdin is not a terminal, so nothing was asked. To turn it on: npx paperlint init --plugin",
|
|
73
79
|
);
|
|
74
|
-
expect(oursWired(root)).toBe(
|
|
80
|
+
expect(oursWired(root)).toBe(shippedCommands);
|
|
75
81
|
});
|
|
76
82
|
|
|
77
83
|
it("an existing project, re-run with --plugin: init's hook commands and links go, the project's own settings stay", async () => {
|
|
@@ -80,7 +86,7 @@ describe("init and the plugin", () => {
|
|
|
80
86
|
".claude/settings.json": '{ "permissions": { "allow": ["Bash(ls)"] } }',
|
|
81
87
|
});
|
|
82
88
|
await initRun(root);
|
|
83
|
-
expect(oursWired(root)).toBe(
|
|
89
|
+
expect(oursWired(root)).toBe(shippedCommands);
|
|
84
90
|
const cc = claude();
|
|
85
91
|
const r = await initRun(root, {
|
|
86
92
|
claude: cc,
|
package/src/layout-docs.test.ts
CHANGED
|
@@ -11,6 +11,9 @@ import { dirname, join } from "node:path";
|
|
|
11
11
|
import { fileURLToPath } from "node:url";
|
|
12
12
|
import { describe, expect, it } from "vitest";
|
|
13
13
|
import {
|
|
14
|
+
CURE_BY_DEFAULT,
|
|
15
|
+
CURE_BY_FILE,
|
|
16
|
+
KNOWN_CURES,
|
|
14
17
|
PAPER_LAYOUT_TREE,
|
|
15
18
|
PAPERS_LAYOUT_TREE,
|
|
16
19
|
} from "./domain/paper-layout.ts";
|
|
@@ -50,3 +53,47 @@ describe("the pages draw the layout the code enforces", () => {
|
|
|
50
53
|
expect(embedded("# a page\n")).toBeNull();
|
|
51
54
|
});
|
|
52
55
|
});
|
|
56
|
+
|
|
57
|
+
const tick = (s: string): string => `\`${s}\``;
|
|
58
|
+
|
|
59
|
+
/** Each destination the finding picks, with the backticked names or extensions its table row must hold. */
|
|
60
|
+
const ROWS: readonly (readonly string[])[] = [
|
|
61
|
+
...KNOWN_CURES.map((c) => [
|
|
62
|
+
tick(c.kind === "dir" ? `${c.name}/` : c.name),
|
|
63
|
+
c.cure,
|
|
64
|
+
]),
|
|
65
|
+
...CURE_BY_FILE.map((c) => [
|
|
66
|
+
...c.names.map(tick),
|
|
67
|
+
...c.extensions.map(tick),
|
|
68
|
+
tick(c.cure),
|
|
69
|
+
]),
|
|
70
|
+
[tick(CURE_BY_DEFAULT)],
|
|
71
|
+
];
|
|
72
|
+
|
|
73
|
+
/** The rows no single line of the page holds whole: a row lost, or its destination swapped. */
|
|
74
|
+
const unlisted = (page: string): readonly (readonly string[])[] => {
|
|
75
|
+
const lines = page.split("\n");
|
|
76
|
+
return ROWS.filter(
|
|
77
|
+
(row) => !lines.some((line) => row.every((cell) => line.includes(cell))),
|
|
78
|
+
);
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
describe("the rule's page lists every destination the one-line finding picks", () => {
|
|
82
|
+
const page = readFileSync(
|
|
83
|
+
join(ROOT, "docs/rules/paper/allowed-files-only.md"),
|
|
84
|
+
"utf8",
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
it("paper/allowed-files-only has a line per known entry and per extension group, with its destination", () => {
|
|
88
|
+
expect(unlisted(page)).toEqual([]);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("a page whose two destinations are swapped, or without the list, is caught", () => {
|
|
92
|
+
const swapped = page
|
|
93
|
+
.replace("| `repro/data/` |", "| @@ |")
|
|
94
|
+
.replace("| `repro/tools/` |", "| `repro/data/` |")
|
|
95
|
+
.replace("| @@ |", "| `repro/tools/` |");
|
|
96
|
+
expect(unlisted(swapped).length).toBeGreaterThan(0);
|
|
97
|
+
expect(unlisted("# a page\n")).toEqual(ROWS);
|
|
98
|
+
});
|
|
99
|
+
});
|