paperlint 20.0.0 → 20.1.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.
Files changed (73) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/dist/cli.d.ts +7 -4
  3. package/dist/cli.d.ts.map +1 -1
  4. package/dist/cli.js +36 -19
  5. package/dist/cli.js.map +1 -1
  6. package/dist/domain/paper-folder.d.ts.map +1 -1
  7. package/dist/domain/paper-folder.js +4 -0
  8. package/dist/domain/paper-folder.js.map +1 -1
  9. package/dist/domain/paper-stage.d.ts +20 -0
  10. package/dist/domain/paper-stage.d.ts.map +1 -0
  11. package/dist/domain/paper-stage.js +9 -0
  12. package/dist/domain/paper-stage.js.map +1 -0
  13. package/dist/eslint-rules/CLAUDE.md.spec.d.ts.map +1 -1
  14. package/dist/eslint-rules/CLAUDE.md.spec.js +1 -0
  15. package/dist/eslint-rules/CLAUDE.md.spec.js.map +1 -1
  16. package/dist/hook-counts.d.ts +30 -0
  17. package/dist/hook-counts.d.ts.map +1 -0
  18. package/dist/hook-counts.js +35 -0
  19. package/dist/hook-counts.js.map +1 -0
  20. package/dist/hooks-settings.d.ts +8 -10
  21. package/dist/hooks-settings.d.ts.map +1 -1
  22. package/dist/hooks-settings.js +31 -35
  23. package/dist/hooks-settings.js.map +1 -1
  24. package/dist/lint-edited-block.d.ts +8 -0
  25. package/dist/lint-edited-block.d.ts.map +1 -0
  26. package/dist/lint-edited-block.js +30 -0
  27. package/dist/lint-edited-block.js.map +1 -0
  28. package/dist/lint-edited-hook.d.ts +5 -6
  29. package/dist/lint-edited-hook.d.ts.map +1 -1
  30. package/dist/lint-edited-hook.js +47 -25
  31. package/dist/lint-edited-hook.js.map +1 -1
  32. package/dist/lint-edited-shell.d.ts +15 -0
  33. package/dist/lint-edited-shell.d.ts.map +1 -0
  34. package/dist/lint-edited-shell.js +49 -0
  35. package/dist/lint-edited-shell.js.map +1 -0
  36. package/dist/paper-files.d.ts +2 -1
  37. package/dist/paper-files.d.ts.map +1 -1
  38. package/dist/paper-files.js +1 -0
  39. package/dist/paper-files.js.map +1 -1
  40. package/dist/rules-config.d.ts +6 -0
  41. package/dist/rules-config.d.ts.map +1 -1
  42. package/dist/rules-config.js.map +1 -1
  43. package/dist/settled-paper.d.ts +6 -0
  44. package/dist/settled-paper.d.ts.map +1 -0
  45. package/dist/settled-paper.js +69 -0
  46. package/dist/settled-paper.js.map +1 -0
  47. package/docs/configuration.md +17 -3
  48. package/docs/e2e.md +1 -1
  49. package/docs/layout.md +19 -4
  50. package/docs/rules/paper/edition-over.md +4 -3
  51. package/eslint-rules/CLAUDE.md +5 -1
  52. package/eslint-rules/CLAUDE.md.spec.ts +3 -0
  53. package/package.json +1 -1
  54. package/plugin/hooks/hooks.json +11 -0
  55. package/src/cli.ts +44 -22
  56. package/src/doctor.test.ts +23 -6
  57. package/src/domain/paper-folder.ts +3 -0
  58. package/src/domain/paper-stage.ts +25 -0
  59. package/src/hook-counts.test.ts +67 -0
  60. package/src/hook-counts.ts +64 -0
  61. package/src/hooks-settings.harness.ts +15 -18
  62. package/src/hooks-settings.ts +43 -49
  63. package/src/init-plugin.test.ts +8 -2
  64. package/src/lint-edited-bash.test.ts +324 -0
  65. package/src/lint-edited-block.ts +44 -0
  66. package/src/lint-edited-hook.test.ts +10 -11
  67. package/src/lint-edited-hook.ts +72 -35
  68. package/src/lint-edited-shell.ts +72 -0
  69. package/src/paper-files.ts +8 -1
  70. package/src/paper-settings-commands.test.ts +13 -16
  71. package/src/rules-config.ts +8 -0
  72. package/src/settled-paper.test.ts +214 -0
  73. package/src/settled-paper.ts +78 -0
@@ -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
- wiredCounts(
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 === 1 && c.other === 0,
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, once each",
235
- wiredCounts(s, wiring.names).every((c) => c.ours === 1),
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 once each → ✓ wired, with the fresh-clone consequence",
321
- /✓ wired — submission-confirm, paper-folder-layout, lint-edited, once each/.test(
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
  );
@@ -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 function commandsIn(
136
- settings: Settings,
137
- ): { readonly event: string; readonly command: string }[] {
138
- const out: { event: string; command: string }[] = [];
139
- for (const [event, entries] of Object.entries(prop(settings["hooks"]) ?? {}))
140
- for (const entry of listOf(entries))
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
- if (typeof command === "string") out.push({ event, command });
144
- }
145
- return out;
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 { compiled, names: [...new Set(names)] };
199
- }
200
-
201
- /** How many commands run one shipped hook, split by spelling. */
202
- export interface HookCount {
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.names);
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 missing = counts.filter((c) => total(c) === 0).map((c) => c.name);
435
- const twice = counts.filter((c) => total(c) > 1);
436
- const partly = partlyMissing(counts, wiring.names).length > 0;
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 (missing.length === wiring.names.length)
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: ${missing.join(", ")}`,
453
- ` ${remedy}`,
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(", ")}, once each`,
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 };
@@ -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(shippedWiring().names.length);
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(shippedWiring().names.length);
89
+ expect(oursWired(root)).toBe(shippedCommands);
84
90
  const cc = claude();
85
91
  const r = await initRun(root, {
86
92
  claude: cc,