paperlint 18.3.2 → 18.4.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.
Files changed (71) hide show
  1. package/dist/adapters/memory/index.d.ts +6 -0
  2. package/dist/adapters/memory/index.d.ts.map +1 -1
  3. package/dist/adapters/memory/index.js +11 -0
  4. package/dist/adapters/memory/index.js.map +1 -1
  5. package/dist/adapters/node/index.d.ts +1 -0
  6. package/dist/adapters/node/index.d.ts.map +1 -1
  7. package/dist/adapters/node/index.js +1 -0
  8. package/dist/adapters/node/index.js.map +1 -1
  9. package/dist/adapters/node/list-tree.io.d.ts +3 -0
  10. package/dist/adapters/node/list-tree.io.d.ts.map +1 -0
  11. package/dist/adapters/node/list-tree.io.js +23 -0
  12. package/dist/adapters/node/list-tree.io.js.map +1 -0
  13. package/dist/adapters/study-registry/index.d.ts +2 -2
  14. package/dist/artifact-rules.d.ts +26 -0
  15. package/dist/artifact-rules.d.ts.map +1 -0
  16. package/dist/artifact-rules.js +117 -0
  17. package/dist/artifact-rules.js.map +1 -0
  18. package/dist/cli.d.ts +2 -1
  19. package/dist/cli.d.ts.map +1 -1
  20. package/dist/cli.js +49 -20
  21. package/dist/cli.js.map +1 -1
  22. package/dist/domain/artifact.d.ts +36 -0
  23. package/dist/domain/artifact.d.ts.map +1 -0
  24. package/dist/domain/artifact.js +116 -0
  25. package/dist/domain/artifact.js.map +1 -0
  26. package/dist/domain/paper-dirs.d.ts +16 -0
  27. package/dist/domain/paper-dirs.d.ts.map +1 -0
  28. package/dist/domain/paper-dirs.js +42 -0
  29. package/dist/domain/paper-dirs.js.map +1 -0
  30. package/dist/lib/paper-config.d.ts.map +1 -1
  31. package/dist/lib/paper-config.js +2 -0
  32. package/dist/lib/paper-config.js.map +1 -1
  33. package/dist/paper-files.d.ts +25 -42
  34. package/dist/paper-files.d.ts.map +1 -1
  35. package/dist/paper-files.js +101 -9
  36. package/dist/paper-files.js.map +1 -1
  37. package/dist/paper-settings.d.ts +3 -0
  38. package/dist/paper-settings.d.ts.map +1 -1
  39. package/dist/paper-settings.js +19 -16
  40. package/dist/paper-settings.js.map +1 -1
  41. package/dist/ports/list-tree.d.ts +11 -0
  42. package/dist/ports/list-tree.d.ts.map +1 -0
  43. package/dist/ports/list-tree.js +2 -0
  44. package/dist/ports/list-tree.js.map +1 -0
  45. package/docs/configuration.md +2 -1
  46. package/docs/rules/artifact/promised.md +64 -0
  47. package/docs/rules/artifact/required-files.md +65 -0
  48. package/docs/rules.md +2 -0
  49. package/lib/paper-config.ts +3 -0
  50. package/package.json +1 -1
  51. package/src/adapters/memory/index.test.ts +9 -0
  52. package/src/adapters/memory/index.ts +15 -0
  53. package/src/adapters/node/index.ts +1 -0
  54. package/src/adapters/node/list-tree.io.ts +27 -0
  55. package/src/adapters/node/list-tree.test.ts +38 -0
  56. package/src/artifact-rules.test.ts +50 -0
  57. package/src/artifact-rules.ts +188 -0
  58. package/src/cli.test.ts +122 -0
  59. package/src/cli.ts +59 -18
  60. package/src/domain/artifact.test.ts +154 -0
  61. package/src/domain/artifact.ts +171 -0
  62. package/src/domain/paper-dirs.test.ts +91 -0
  63. package/src/domain/paper-dirs.ts +62 -0
  64. package/src/new-paper.test.ts +9 -3
  65. package/src/paper-files.test.ts +88 -0
  66. package/src/paper-files.ts +98 -9
  67. package/src/paper-settings-commands.test.ts +18 -0
  68. package/src/paper-settings.test.ts +19 -0
  69. package/src/paper-settings.ts +21 -16
  70. package/src/ports/list-tree.ts +11 -0
  71. package/src/rule-cases.test.ts +134 -1
package/src/cli.ts CHANGED
@@ -24,7 +24,7 @@
24
24
  * replaced it: silently breaking someone else's workflow is worse than asking them to fix a line.
25
25
  */
26
26
  import { ESLint, type Linter } from "eslint";
27
- import { readFileSync, existsSync, statSync } from "node:fs";
27
+ import { readFileSync, readdirSync, existsSync, statSync } from "node:fs";
28
28
  import { createRequire } from "node:module";
29
29
  import { stripVTControlCharacters } from "node:util";
30
30
  import { spawnSync } from "node:child_process";
@@ -63,6 +63,7 @@ import {
63
63
  nodeAdapters,
64
64
  nodeFiles,
65
65
  nodeListDir,
66
+ nodeListTree,
66
67
  nodeCopyFile,
67
68
  nodeMoveFile,
68
69
  } from "./adapters/node/index.ts";
@@ -118,11 +119,15 @@ import type { PortalKind, SupportedPortal } from "./domain/submission.ts";
118
119
  import type { PortalSite } from "./ports/submission-portal.ts";
119
120
  import type { CheckReferences } from "./ports/check-references.ts";
120
121
  import {
122
+ ANY_PAPER,
121
123
  narrowToOwners,
122
124
  ownedPatterns,
125
+ paperFiles,
126
+ papersScope,
123
127
  ruleOwners,
124
128
  scopeToOwned,
125
129
  } from "./paper-files.ts";
130
+ import type { PaperScope } from "./paper-files.ts";
126
131
  import {
127
132
  paperPreset,
128
133
  settingsProblemLine,
@@ -187,6 +192,7 @@ import paperStages from "#eslint-rules/paper-stages";
187
192
  import preregCoverage from "#eslint-rules/prereg-coverage";
188
193
  import { CYCLE_RULE_LEVELS, cycleRules } from "./cycle-rules.ts";
189
194
  import { EDITION_RULE_LEVELS, editionRules } from "./edition-rule.ts";
195
+ import { ARTIFACT_RULE_LEVELS, artifactRules } from "./artifact-rules.ts";
190
196
  import { changesRules } from "./changes-rule.ts";
191
197
  import { latexDiff, withTexBin } from "./adapters/latexdiff/index.ts";
192
198
  import { runVersions } from "./versions-command.ts";
@@ -212,6 +218,7 @@ import {
212
218
  } from "./folder-layout-rule.ts";
213
219
  import { folderLayoutHook } from "./folder-layout-hook.ts";
214
220
  import { paperFolderOf, writtenPath } from "./domain/paper-folder.ts";
221
+ import { papersReached, type PaperTree } from "./domain/paper-dirs.ts";
215
222
  import {
216
223
  NOTICE_HOOKS,
217
224
  collector,
@@ -571,7 +578,11 @@ const TALK_RULE_DEPS = {
571
578
  export function buildConfig(
572
579
  opts: PaperlintConfig = {},
573
580
  texLanguage: unknown,
581
+ // The papers whose own files the blocks claim (`PaperScope`): any folder for the config's shape,
582
+ // the real papers for `paperlint lint`, so a copy kept inside one is claimed by no block (#582).
583
+ papers: PaperScope = ANY_PAPER,
574
584
  ): ConfigBlock[] {
585
+ const own = (glob: string): readonly string[] => paperFiles(papers, glob);
575
586
  const paperRules = { ...typography.rules };
576
587
  // The reference rules judge `_build/references.json`, and only on `paper.tex`.
577
588
  const texPaperRules = {
@@ -612,7 +623,7 @@ export function buildConfig(
612
623
  },
613
624
  },
614
625
  {
615
- files: ["**/PIPELINE-STATUS.md"],
626
+ files: own("PIPELINE-STATUS.md"),
616
627
  // Every paper folder has one: the rules about the folder itself (its stages, its frozen
617
628
  // sources, its name) are judged here, once per paper.
618
629
  plugins: {
@@ -630,6 +641,10 @@ export function buildConfig(
630
641
  },
631
642
  },
632
643
  repro: { rules: REPRO_RULES },
644
+ // On for every paper, silent for one whose paperlint.json declares no `artifact`.
645
+ artifact: {
646
+ rules: artifactRules({ files: nodeFiles, listTree: nodeListTree }),
647
+ },
633
648
  // Registered, on for no paper: `"study/declare-sample": "error"` in paperlint.json turns it on.
634
649
  study: { rules: preregCoverage.rules },
635
650
  cycle: { rules: CYCLE_STATUS_RULES },
@@ -641,13 +656,14 @@ export function buildConfig(
641
656
  "paper/source": "error",
642
657
  ...FOLDER_VENUE_RULE_LEVELS,
643
658
  ...EDITION_RULE_LEVELS,
659
+ ...ARTIFACT_RULE_LEVELS,
644
660
  ...FOLDER_LAYOUT_RULE_LEVELS,
645
661
  ...REPRO_RULE_LEVELS,
646
662
  ...STATUS_CYCLE_RULE_LEVELS,
647
663
  },
648
664
  },
649
665
  {
650
- files: ["**/reviews/*.md"],
666
+ files: own("reviews/*.md"),
651
667
  plugins: { markdown, review: reviewFrontmatter },
652
668
  ...md,
653
669
  // A review's frontmatter is a record, validated by paperlint's JSON Schema
@@ -657,7 +673,7 @@ export function buildConfig(
657
673
  {
658
674
  // A sibling card says how much of the competing paper was read (`read:`). The cards are
659
675
  // written by this package's analyze-sibling-paper skill; its README is an index, not a card.
660
- files: ["**/siblings/*.md"],
676
+ files: own("siblings/*.md"),
661
677
  ignores: ["**/siblings/README.md"],
662
678
  plugins: { markdown, sibling: siblingFrontmatter },
663
679
  ...md,
@@ -669,7 +685,7 @@ export function buildConfig(
669
685
  // corpus without a single `.tex` would be refusing to work where work is possible.
670
686
  if (texLanguage)
671
687
  cfg.push({
672
- files: ["**/paper.tex"],
688
+ files: own("paper.tex"),
673
689
  plugins: {
674
690
  tex: {
675
691
  languages: { latex: texLanguage },
@@ -753,9 +769,11 @@ export function buildConfig(
753
769
  // PIPELINE-STATUS.md than beside paper.tex, and ESLint throws on a rule its plugin lacks.
754
770
  cfg.push(
755
771
  ...fragments,
756
- ...(opts.rules ?? []).flatMap((b) => narrowToOwners(b, owners)),
772
+ ...(opts.rules ?? []).flatMap((b) => narrowToOwners(b, owners, papers)),
757
773
  );
758
- return cfg;
774
+ // With no paper to claim, an own block's `files` is empty, and ESLint refuses an empty `files`: the
775
+ // block goes, and nothing is linted, which the run then says (#582).
776
+ return cfg.filter((b) => b.files === undefined || b.files.length > 0);
759
777
  }
760
778
 
761
779
  /**
@@ -921,16 +939,34 @@ export async function silentOptionalRules(
921
939
  return [...turnedOn].filter((id) => !reached.has(id));
922
940
  }
923
941
 
924
- /**
925
- * The paper directories `paths` reach: each directory and the papers under it. A FILE named on the
926
- * command line belongs to the paper it sits in.
927
- */
928
- function paperDirs(paths: readonly string[]): readonly string[] {
929
- const dirs = paths.map((p) =>
930
- existsSync(p) && statSync(p).isFile() ? dirname(p) : p,
942
+ /** The disk as the paper-reach rule sees it (`src/domain/paper-dirs.ts`). */
943
+ const PAPER_TREE: PaperTree = {
944
+ ...nodeFiles,
945
+ subfolders: (dir) => {
946
+ try {
947
+ return readdirSync(dir, { withFileTypes: true })
948
+ .filter(
949
+ (e) =>
950
+ e.isDirectory() &&
951
+ !e.name.startsWith(".") &&
952
+ e.name !== "node_modules" &&
953
+ e.name !== ESCAPE_HATCH,
954
+ )
955
+ .map((e) => join(dir, e.name));
956
+ } catch {
957
+ return [];
958
+ }
959
+ },
960
+ };
961
+
962
+ /** The papers `paths` reach: a file counts as the folder it sits in (#582). */
963
+ const paperDirs = (paths: readonly string[]): readonly string[] =>
964
+ papersReached(
965
+ paths.map((p) =>
966
+ resolve(existsSync(p) && statSync(p).isFile() ? dirname(p) : p),
967
+ ),
968
+ PAPER_TREE,
931
969
  );
932
- return [...new Set(dirs.flatMap((p) => [p, ...papersIn(p)]))];
933
- }
934
970
 
935
971
  /**
936
972
  * Every linted paper's rules, as ESLint blocks scoped to that paper, in two groups: what its venue
@@ -2508,11 +2544,16 @@ async function paperEslint(
2508
2544
  ? paperBodies(paperDirs([...paths, ...papersRoots(cfg)]), RECORD_DEPS)
2509
2545
  : [];
2510
2546
 
2547
+ const cwd = lintRoot(cfg.root, paths);
2548
+ // The papers whose own files the config names: the ones `paths` reach and the project's. A copy of
2549
+ // a paper kept inside it (a review snapshot, a build copy) is in no such list, so no block claims
2550
+ // it (#582).
2551
+ const claimed = papersScope(paperDirs([...paths, ...papersRoots(cfg)]), cwd);
2511
2552
  const eslint = new ESLint({
2512
- cwd: lintRoot(cfg.root, paths),
2553
+ cwd,
2513
2554
  overrideConfigFile: true,
2514
2555
  overrideConfig: eslintConfig([
2515
- ...buildConfig(opts, texLanguage),
2556
+ ...buildConfig(opts, texLanguage, claimed),
2516
2557
  ...includeBlocks(bodies),
2517
2558
  ]),
2518
2559
  fix,
@@ -0,0 +1,154 @@
1
+ /** A paper's `artifact` key, parsed, and the two judgements over the artifact's paths. */
2
+ import { describe, expect, it } from "vitest";
3
+ import {
4
+ answers,
5
+ brokenPromises,
6
+ listedPaths,
7
+ missingRequired,
8
+ parsePaperArtifact,
9
+ } from "./artifact.ts";
10
+
11
+ /** A bad shape, and the one line that refuses it. */
12
+ const REFUSED: readonly (readonly [string, unknown, string])[] = [
13
+ [
14
+ "not an object",
15
+ "artifact",
16
+ `"artifact" must be an object with a "dir" or a "files"`,
17
+ ],
18
+ [
19
+ "an unknown key",
20
+ { dir: "a", include: ["x"] },
21
+ `"artifact" has an unknown key "include" — known: dir, files, required, promises`,
22
+ ],
23
+ [
24
+ "neither dir nor files",
25
+ { promises: {} },
26
+ `"artifact" needs exactly one of "dir" (the artifact's folder) or "files" (a committed list of its paths), got neither`,
27
+ ],
28
+ [
29
+ "both dir and files",
30
+ { dir: "a", files: "a.txt" },
31
+ `"artifact" needs exactly one of "dir" (the artifact's folder) or "files" (a committed list of its paths), got both`,
32
+ ],
33
+ [
34
+ "an absolute dir",
35
+ { dir: "/srv/artifact" },
36
+ `"artifact.dir" must be a path relative to the paper, got "/srv/artifact"`,
37
+ ],
38
+ [
39
+ "a required name with a folder",
40
+ { dir: "a", required: ["docs/README"] },
41
+ `"artifact.required" must be a list of top-level names without an extension ("README", "LICENSE"), got ["docs/README"]`,
42
+ ],
43
+ [
44
+ "promises as a list",
45
+ { dir: "a", promises: ["**/*.md"] },
46
+ `"artifact.promises" must be an object of label → glob`,
47
+ ],
48
+ [
49
+ "an empty glob",
50
+ { dir: "a", promises: { prompts: "" } },
51
+ `"artifact.promises" entry "prompts" must be a non-empty glob relative to the artifact's root, got ""`,
52
+ ],
53
+ ];
54
+
55
+ describe("parsePaperArtifact", () => {
56
+ it("is null when the paper declares no artifact", () => {
57
+ expect(parsePaperArtifact(undefined)).toEqual({ ok: true, value: null });
58
+ });
59
+
60
+ it("takes a folder, the required names and the promises", () => {
61
+ expect(
62
+ parsePaperArtifact({
63
+ dir: "../artifact",
64
+ required: ["README", "LICENSE"],
65
+ promises: { prompts: "**/prompts/*.md" },
66
+ }),
67
+ ).toEqual({
68
+ ok: true,
69
+ value: {
70
+ source: { kind: "dir", path: "../artifact" },
71
+ required: ["README", "LICENSE"],
72
+ promises: { prompts: "**/prompts/*.md" },
73
+ },
74
+ });
75
+ });
76
+
77
+ it("takes a committed list instead of a folder; required and promises are optional", () => {
78
+ expect(parsePaperArtifact({ files: "artifact-files.txt" })).toEqual({
79
+ ok: true,
80
+ value: {
81
+ source: { kind: "files", path: "artifact-files.txt" },
82
+ required: [],
83
+ promises: {},
84
+ },
85
+ });
86
+ });
87
+
88
+ it.each(REFUSED)("refuses %s", (_, v, why) => {
89
+ // Guards: each bad shape is refused with the one line naming the key and what it must be.
90
+ expect(parsePaperArtifact(v)).toEqual({ ok: false, error: why });
91
+ });
92
+ });
93
+
94
+ describe("answers — a top-level name answers a required name by its name before any extension, case ignored", () => {
95
+ it.each<[string, boolean]>([
96
+ ["README.md", true],
97
+ ["readme.txt", true],
98
+ ["README", true],
99
+ ["READMEFIRST.md", false],
100
+ ])("%s → %s", (name, yes) => {
101
+ expect(answers(name, "README")).toBe(yes);
102
+ });
103
+ });
104
+
105
+ describe("brokenPromises — a promise matching no path", () => {
106
+ it("names the label and the glob of each promise that matches nothing", () => {
107
+ expect(
108
+ brokenPromises({ prompts: "**/prompts/*.md", runs: "runs/*.jsonl" }, [
109
+ "prompts/a.md",
110
+ "README.md",
111
+ ]),
112
+ ).toEqual([["runs", "runs/*.jsonl"]]);
113
+ });
114
+ });
115
+
116
+ describe("one wrapper folder around every path (GitHub's Download ZIP, Zenodo, OSF) is looked through", () => {
117
+ const REQ = ["README", "LICENSE"];
118
+
119
+ it("🔴 a wrapped artifact holding README and LICENSE: nothing missing", () => {
120
+ expect(
121
+ missingRequired(REQ, ["repo-main/README.md", "repo-main/LICENSE"]),
122
+ ).toEqual([]);
123
+ });
124
+
125
+ it("a wrapped artifact without a README: README missing", () => {
126
+ expect(
127
+ missingRequired(REQ, ["repo-main/LICENSE", "repo-main/docs/README.md"]),
128
+ ).toEqual(["README"]);
129
+ });
130
+
131
+ it("two top folders are not unwrapped", () => {
132
+ expect(missingRequired(REQ, ["a/README.md", "b/LICENSE"])).toEqual(REQ);
133
+ });
134
+
135
+ it("a top-level file beside one folder is not unwrapped", () => {
136
+ expect(
137
+ missingRequired(REQ, ["notes.txt", "a/README.md", "a/LICENSE"]),
138
+ ).toEqual(REQ);
139
+ });
140
+
141
+ it("🔴 a promise's glob matches inside the wrapper", () => {
142
+ expect(
143
+ brokenPromises({ data: "data/*.csv" }, ["artifact/data/x.csv"]),
144
+ ).toEqual([]);
145
+ });
146
+ });
147
+
148
+ describe("listedPaths — a committed list as zipinfo -1 or find writes it", () => {
149
+ it("drops blank lines, folder entries and a leading ./", () => {
150
+ expect(
151
+ listedPaths(".\n./README.md\nprompts/\nprompts/a.md\r\n\n LICENSE \n"),
152
+ ).toEqual(["README.md", "prompts/a.md", "LICENSE"]);
153
+ });
154
+ });
@@ -0,0 +1,171 @@
1
+ /**
2
+ * A paper's reproduction artifact, as the paper declares it in its `paperlint.json`:
3
+ *
4
+ * "artifact": {
5
+ * "dir": "<folder, relative to the paper>" ┐ exactly one: where the artifact is, or
6
+ * "files": "<text file, relative to the paper>", ┘ a committed list of its paths, one per line
7
+ * "required": ["README", "LICENSE"], names its top level must hold
8
+ * "promises": { "<label>": "<glob>" } what the paper says it holds
9
+ * }
10
+ *
11
+ * `files` is for a bundle that is not committed (a gitignored zip): its paths as `zipinfo -1` or
12
+ * `find` writes them, relative to the artifact's root. A required name is matched without its
13
+ * extension and case (`README` is answered by README.md, readme.txt, README), and a promise's glob
14
+ * by `path.matchesGlob`, the same way whichever form lists the paths; one folder wrapped around
15
+ * every path is looked through (`unwrapSingleRoot`). `artifact/required-files` and
16
+ * `artifact/promised` judge them. The keys #205 plans for assembling a bundle (`include`, `exclude`,
17
+ * `scrub`) are left free.
18
+ */
19
+ import { isAbsolute, matchesGlob } from "node:path";
20
+ import { err, ok, type Result } from "./result.ts";
21
+ import { isRecord } from "./record.ts";
22
+
23
+ /** Where the artifact's paths come from: its folder, or a committed list of them. */
24
+ export interface ArtifactSource {
25
+ readonly kind: "dir" | "files";
26
+ readonly path: string;
27
+ }
28
+
29
+ /** What a paper says its artifact is. */
30
+ export interface PaperArtifact {
31
+ /** The folder or the list, its path relative to the paper folder. */
32
+ readonly source: ArtifactSource;
33
+ /** Names the artifact's top level must hold, each matched without its extension; empty when none. */
34
+ readonly required: readonly string[];
35
+ /** A label the author chose → a glob, relative to the artifact's root, that must match a path. */
36
+ readonly promises: Readonly<Record<string, string>>;
37
+ }
38
+
39
+ const KEYS = ["dir", "files", "required", "promises"];
40
+
41
+ /** The text of a name before its first dot, case folded: what a required name is matched by. Pure. */
42
+ const stemOf = (name: string): string =>
43
+ name.split(".", 1).join("").toLowerCase();
44
+
45
+ /** Whether a top-level name answers the required name `want` (any extension, any case). Pure. */
46
+ export const answers = (name: string, want: string): boolean =>
47
+ stemOf(name) === want.toLowerCase();
48
+
49
+ /**
50
+ * `paths` without the one folder every one of them sits in, when they all do and no file stands
51
+ * beside it (`repo-main/…` of GitHub's Download ZIP, Zenodo's and OSF's archives); else as they are.
52
+ * Pure.
53
+ */
54
+ export const unwrapSingleRoot = (
55
+ paths: readonly string[],
56
+ ): readonly string[] => {
57
+ const firsts = [...new Set(paths.map((p) => p.split("/", 1).join("")))];
58
+ // With exactly one first segment, joining the list gives it.
59
+ const root = firsts.join("");
60
+ const wrapped =
61
+ firsts.length === 1 && paths.every((p) => p.startsWith(`${root}/`));
62
+ return wrapped ? paths.map((p) => p.slice(root.length + 1)) : paths;
63
+ };
64
+
65
+ /** The required names `paths` (relative to the artifact's root) do not hold at its top level. Pure. */
66
+ export const missingRequired = (
67
+ required: readonly string[],
68
+ paths: readonly string[],
69
+ ): readonly string[] => {
70
+ const top = unwrapSingleRoot(paths).filter((p) => !p.includes("/"));
71
+ return required.filter((want) => !top.some((name) => answers(name, want)));
72
+ };
73
+
74
+ /** The promises whose glob matches none of `paths`, as [label, glob]. Pure. */
75
+ export const brokenPromises = (
76
+ promises: Readonly<Record<string, string>>,
77
+ paths: readonly string[],
78
+ ): readonly (readonly [string, string])[] => {
79
+ const under = unwrapSingleRoot(paths);
80
+ return Object.entries(promises).filter(
81
+ ([, glob]) => !under.some((p) => matchesGlob(p, glob)),
82
+ );
83
+ };
84
+
85
+ /**
86
+ * A committed list of paths → the files it names, relative to the artifact's root: one per line,
87
+ * blank lines and folder entries (`dir/`, as `zipinfo -1` writes them) dropped, a leading `./` (as
88
+ * `find .` writes it) removed. Pure.
89
+ */
90
+ export const listedPaths = (text: string): readonly string[] =>
91
+ text
92
+ .split(/\r?\n/u)
93
+ .map((l) => l.trim().replace(/^(?:\.\/)+/u, ""))
94
+ .filter((l) => l !== "" && l !== "." && !l.endsWith("/"));
95
+
96
+ /** A path relative to the paper: a non-empty string, not absolute. */
97
+ const relativePath = (v: unknown): v is string =>
98
+ typeof v === "string" && v !== "" && !isAbsolute(v);
99
+
100
+ /** `dir` or `files`, exactly one, each relative to the paper. Pure. */
101
+ function sourceOf(
102
+ v: Readonly<Record<string, unknown>>,
103
+ ): Result<ArtifactSource, string> {
104
+ const given = (["dir", "files"] as const).filter((k) => k in v);
105
+ const [kind] = given;
106
+ if (given.length !== 1 || kind === undefined)
107
+ return err(
108
+ `"artifact" needs exactly one of "dir" (the artifact's folder) or "files" (a committed list of its paths), got ${given.length === 0 ? "neither" : "both"}`,
109
+ );
110
+ const path = v[kind];
111
+ return relativePath(path)
112
+ ? ok({ kind, path })
113
+ : err(
114
+ `"artifact.${kind}" must be a path relative to the paper, got ${JSON.stringify(path)}`,
115
+ );
116
+ }
117
+
118
+ /** `required`: absent is none; otherwise names without a folder in them. Pure. */
119
+ function requiredOf(v: unknown): Result<readonly string[], string> {
120
+ if (v === undefined) return ok([]);
121
+ return Array.isArray(v) &&
122
+ v.every((x) => typeof x === "string" && /^[^/\\.\s]+$/u.test(x))
123
+ ? ok(v.filter((x): x is string => typeof x === "string"))
124
+ : err(
125
+ `"artifact.required" must be a list of top-level names without an extension ("README", "LICENSE"), got ${JSON.stringify(v)}`,
126
+ );
127
+ }
128
+
129
+ /** `promises`: absent is none; otherwise each label and glob a non-empty string. Pure. */
130
+ function promisesOf(
131
+ v: unknown,
132
+ ): Result<Readonly<Record<string, string>>, string> {
133
+ if (v === undefined) return ok({});
134
+ if (!isRecord(v))
135
+ return err(`"artifact.promises" must be an object of label → glob`);
136
+ const entries = Object.entries(v);
137
+ const bad = entries.find(
138
+ ([label, glob]) => label === "" || typeof glob !== "string" || glob === "",
139
+ );
140
+ return bad === undefined
141
+ ? ok(Object.fromEntries(entries.map(([l, g]) => [l, String(g)])))
142
+ : err(
143
+ `"artifact.promises" entry ${JSON.stringify(bad[0])} must be a non-empty glob relative to the artifact's root, got ${JSON.stringify(bad[1])}`,
144
+ );
145
+ }
146
+
147
+ /** A paper's `artifact` → the value, null when absent, or one line saying what is wrong. Pure. */
148
+ export function parsePaperArtifact(
149
+ v: unknown,
150
+ ): Result<PaperArtifact | null, string> {
151
+ if (v === undefined) return ok(null);
152
+ if (!isRecord(v))
153
+ return err(`"artifact" must be an object with a "dir" or a "files"`);
154
+ const unknown = Object.keys(v).find((k) => !KEYS.includes(k));
155
+ if (unknown !== undefined)
156
+ return err(
157
+ `"artifact" has an unknown key "${unknown}" — known: ${KEYS.join(", ")}`,
158
+ );
159
+ const source = sourceOf(v);
160
+ if (!source.ok) return source;
161
+ const required = requiredOf(v["required"]);
162
+ if (!required.ok) return required;
163
+ const promises = promisesOf(v["promises"]);
164
+ return promises.ok
165
+ ? ok({
166
+ source: source.value,
167
+ required: required.value,
168
+ promises: promises.value,
169
+ })
170
+ : promises;
171
+ }
@@ -0,0 +1,91 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { dirname } from "node:path";
3
+ import { papersReached, type PaperTree } from "./paper-dirs.ts";
4
+
5
+ /** A disk holding exactly `files`, its folders the ones those files imply. */
6
+ const treeOf = (files: readonly string[]): PaperTree => {
7
+ const dirs = new Set(
8
+ files.flatMap((f) => {
9
+ const up = (d: string): readonly string[] =>
10
+ d === "/" ? [] : [d, ...up(dirname(d))];
11
+ return up(dirname(f));
12
+ }),
13
+ );
14
+ return {
15
+ isFile: (p) => files.includes(p),
16
+ subfolders: (dir) =>
17
+ [...dirs]
18
+ .filter((d) => dirname(d) === dir && !d.endsWith("/extra"))
19
+ .sort(),
20
+ };
21
+ };
22
+
23
+ /** The project every row reads: three papers, a source-only one, a copy in each place one is kept. */
24
+ const PROJECT = treeOf([
25
+ "/w/paperlint.json",
26
+ "/w/papers/p/paper.tex",
27
+ "/w/papers/p/PIPELINE-STATUS.md",
28
+ "/w/papers/p/reviews/r.md",
29
+ "/w/papers/p/reviews/snapshot/paper.tex",
30
+ "/w/papers/p/reviews/snapshot/PIPELINE-STATUS.md",
31
+ "/w/papers/p/old/paper.tex",
32
+ "/w/papers/p/_build/paper.tex",
33
+ "/w/papers/p/extra/2025/paper.tex",
34
+ "/w/papers/q/PIPELINE-STATUS.md",
35
+ "/w/archive/2025/s/paper.tex",
36
+ "/w/archive/2025/s/reviews/r.md",
37
+ "/w/archive/2025/s/reviews/snapshot/PIPELINE-STATUS.md",
38
+ "/w/archive/notes/todo.md",
39
+ ]);
40
+
41
+ describe("the papers a path reaches (#582)", () => {
42
+ test.each([
43
+ [
44
+ "the project root: every paper below it, none inside one",
45
+ ["/w"],
46
+ ["/w/archive/2025/s", "/w/papers/p", "/w/papers/q"],
47
+ ],
48
+ ["a papers directory", ["/w/papers"], ["/w/papers/p", "/w/papers/q"]],
49
+ [
50
+ "a folder two levels above a paper",
51
+ ["/w/archive"],
52
+ ["/w/archive/2025/s"],
53
+ ],
54
+ ["a paper with both markers", ["/w/papers/p"], ["/w/papers/p"]],
55
+ ["a paper with only its scorecard", ["/w/papers/q"], ["/w/papers/q"]],
56
+ [
57
+ "a paper with only its source",
58
+ ["/w/archive/2025/s"],
59
+ ["/w/archive/2025/s"],
60
+ ],
61
+ [
62
+ "a review card's folder, a snapshot beside it",
63
+ ["/w/papers/p/reviews"],
64
+ ["/w/papers/p"],
65
+ ],
66
+ [
67
+ "the same, in a source-only paper",
68
+ ["/w/archive/2025/s/reviews"],
69
+ ["/w/archive/2025/s"],
70
+ ],
71
+ [
72
+ "a build copy named itself",
73
+ ["/w/papers/p/_build"],
74
+ ["/w/papers/p/_build"],
75
+ ],
76
+ [
77
+ "a snapshot named itself",
78
+ ["/w/papers/p/reviews/snapshot"],
79
+ ["/w/papers/p/reviews/snapshot"],
80
+ ],
81
+ ["a folder with no paper at or below it", ["/w/archive/notes"], []],
82
+ ["a paper kept under extra/", ["/w/papers/p/extra/2025"], ["/w/papers/p"]],
83
+ [
84
+ "two paths reaching one paper",
85
+ ["/w/papers/p", "/w/papers/p/reviews"],
86
+ ["/w/papers/p"],
87
+ ],
88
+ ] as const)("%s", (_, dirs, papers) => {
89
+ expect(papersReached(dirs, PROJECT)).toEqual(papers);
90
+ });
91
+ });
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Which papers a path given to `paperlint lint` reaches (#582). One owner for the question, so the
3
+ * claimed scope, the papers' own settings and the record checks cannot disagree about it.
4
+ *
5
+ * The rule, every case of which is a row in `paper-dirs.test.ts`:
6
+ * - a paper is a folder with `paper.tex` or `PIPELINE-STATUS.md` at its top. `paperlint.json` alone
7
+ * is the project's settings, not a paper;
8
+ * - a path inside a paper (the paper itself, a review card, its `reviews/`) reaches the nearest
9
+ * paper at or above it, looking only up, so a snapshot kept beside the card never stands in for
10
+ * the paper the card belongs to;
11
+ * - any other folder (the project root, a papers directory, an archive) reaches the papers below
12
+ * it, at any depth; the walk stops at each paper, so a copy kept inside one is never a paper;
13
+ * - nothing under a folder named `extra/` is a paper, the same reach as lint's own ignore.
14
+ */
15
+ import { unique } from "remeda";
16
+ import { dirname, join, sep } from "node:path";
17
+ import { STATUS_FILE } from "./paper-folder.ts";
18
+ import { ESCAPE_HATCH } from "./paper-layout.ts";
19
+ import { MAIN_FILE } from "./paper-sources.ts";
20
+ import { absolutePath } from "./paths.ts";
21
+ import type { IsFile } from "./paper-folder.ts";
22
+
23
+ /** The two questions the rule asks of the disk. */
24
+ export interface PaperTree extends IsFile {
25
+ /** The folders a walk enters below `dir`: not hidden, not installed packages, not `extra/`. */
26
+ subfolders(dir: string): readonly string[];
27
+ }
28
+
29
+ /** The files that make a folder a paper. */
30
+ export const PAPER_MARKERS: readonly string[] = [MAIN_FILE, STATUS_FILE];
31
+
32
+ const underExtra = (dir: string): boolean =>
33
+ dir.split(sep).includes(ESCAPE_HATCH);
34
+
35
+ export const isPaper = (dir: string, tree: IsFile): boolean =>
36
+ !underExtra(dir) &&
37
+ PAPER_MARKERS.some((m) => tree.isFile(absolutePath(join(dir, m))));
38
+
39
+ /** The nearest paper at or above `dir`; null when there is none. */
40
+ export const paperOwning = (dir: string, tree: IsFile): string | null => {
41
+ if (isPaper(dir, tree)) return dir;
42
+ const parent = dirname(dir);
43
+ return parent === dir ? null : paperOwning(parent, tree);
44
+ };
45
+
46
+ /** The papers below `dir`, at any depth, not looking inside any paper it finds. */
47
+ export const papersBelow = (dir: string, tree: PaperTree): readonly string[] =>
48
+ tree
49
+ .subfolders(dir)
50
+ .flatMap((d) => (isPaper(d, tree) ? [d] : papersBelow(d, tree)));
51
+
52
+ /** The papers the absolute folders `dirs` reach, each once, in the order they were reached. */
53
+ export const papersReached = (
54
+ dirs: readonly string[],
55
+ tree: PaperTree,
56
+ ): readonly string[] =>
57
+ unique(
58
+ dirs.flatMap((d) => {
59
+ const owner = paperOwning(d, tree);
60
+ return owner === null ? papersBelow(d, tree) : [owner];
61
+ }),
62
+ );