paperlint 18.4.1 → 18.6.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 (113) hide show
  1. package/CLAUDE.md +6 -2
  2. package/dist/adapters/hotcrp/portal.io.d.ts.map +1 -1
  3. package/dist/adapters/hotcrp/portal.io.js +1 -0
  4. package/dist/adapters/hotcrp/portal.io.js.map +1 -1
  5. package/dist/adapters/hotcrp/response.d.ts.map +1 -1
  6. package/dist/adapters/hotcrp/response.js +15 -0
  7. package/dist/adapters/hotcrp/response.js.map +1 -1
  8. package/dist/adapters/latex/abstract.d.ts +11 -0
  9. package/dist/adapters/latex/abstract.d.ts.map +1 -1
  10. package/dist/adapters/latex/abstract.js +1 -1
  11. package/dist/adapters/latex/abstract.js.map +1 -1
  12. package/dist/adapters/latex/index.d.ts.map +1 -1
  13. package/dist/adapters/latex/index.js +3 -0
  14. package/dist/adapters/latex/index.js.map +1 -1
  15. package/dist/adapters/latex/macros.d.ts +6 -0
  16. package/dist/adapters/latex/macros.d.ts.map +1 -0
  17. package/dist/adapters/latex/macros.js +27 -0
  18. package/dist/adapters/latex/macros.js.map +1 -0
  19. package/dist/adapters/osf/host.io.d.ts +12 -0
  20. package/dist/adapters/osf/host.io.d.ts.map +1 -0
  21. package/dist/adapters/osf/host.io.js +119 -0
  22. package/dist/adapters/osf/host.io.js.map +1 -0
  23. package/dist/adapters/osf/index.d.ts +10 -0
  24. package/dist/adapters/osf/index.d.ts.map +1 -0
  25. package/dist/adapters/osf/index.js +10 -0
  26. package/dist/adapters/osf/index.js.map +1 -0
  27. package/dist/adapters/osf/response.d.ts +11 -0
  28. package/dist/adapters/osf/response.d.ts.map +1 -0
  29. package/dist/adapters/osf/response.js +87 -0
  30. package/dist/adapters/osf/response.js.map +1 -0
  31. package/dist/artifact-upload-rule.d.ts +66 -0
  32. package/dist/artifact-upload-rule.d.ts.map +1 -0
  33. package/dist/artifact-upload-rule.js +151 -0
  34. package/dist/artifact-upload-rule.js.map +1 -0
  35. package/dist/artifact.d.ts +35 -0
  36. package/dist/artifact.d.ts.map +1 -0
  37. package/dist/artifact.js +251 -0
  38. package/dist/artifact.js.map +1 -0
  39. package/dist/cli.d.ts +7 -0
  40. package/dist/cli.d.ts.map +1 -1
  41. package/dist/cli.js +99 -7
  42. package/dist/cli.js.map +1 -1
  43. package/dist/domain/artifact-upload.d.ts +44 -0
  44. package/dist/domain/artifact-upload.d.ts.map +1 -0
  45. package/dist/domain/artifact-upload.js +80 -0
  46. package/dist/domain/artifact-upload.js.map +1 -0
  47. package/dist/domain/submission.d.ts +4 -0
  48. package/dist/domain/submission.d.ts.map +1 -1
  49. package/dist/domain/submission.js.map +1 -1
  50. package/dist/eslint-rules/CLAUDE.md.spec.d.ts.map +1 -1
  51. package/dist/eslint-rules/CLAUDE.md.spec.js +1 -0
  52. package/dist/eslint-rules/CLAUDE.md.spec.js.map +1 -1
  53. package/dist/ports/artifact-host.d.ts +54 -0
  54. package/dist/ports/artifact-host.d.ts.map +1 -0
  55. package/dist/ports/artifact-host.js +2 -0
  56. package/dist/ports/artifact-host.js.map +1 -0
  57. package/dist/ports/latex.d.ts +9 -0
  58. package/dist/ports/latex.d.ts.map +1 -1
  59. package/dist/submission-confirm-hook.d.ts +11 -1
  60. package/dist/submission-confirm-hook.d.ts.map +1 -1
  61. package/dist/submission-confirm-hook.js +83 -21
  62. package/dist/submission-confirm-hook.js.map +1 -1
  63. package/dist/submission.d.ts +19 -2
  64. package/dist/submission.d.ts.map +1 -1
  65. package/dist/submission.js +121 -17
  66. package/dist/submission.js.map +1 -1
  67. package/dist/types.d.ts +6 -0
  68. package/dist/types.d.ts.map +1 -1
  69. package/docs/artifact-upload.md +124 -0
  70. package/docs/optional-rules.md +1 -0
  71. package/docs/osf-artifact.md +4 -1
  72. package/docs/rules/artifact/upload-current.md +85 -0
  73. package/docs/rules.md +1 -0
  74. package/docs/submission.md +19 -4
  75. package/eslint-rules/CLAUDE.md +5 -1
  76. package/eslint-rules/CLAUDE.md.spec.ts +3 -0
  77. package/package.json +1 -1
  78. package/src/adapters/hotcrp/portal.io.ts +1 -0
  79. package/src/adapters/hotcrp/portal.test.ts +43 -2
  80. package/src/adapters/hotcrp/response.ts +21 -0
  81. package/src/adapters/latex/abstract.ts +3 -1
  82. package/src/adapters/latex/index.ts +3 -0
  83. package/src/adapters/latex/macros.test.ts +31 -0
  84. package/src/adapters/latex/macros.ts +43 -0
  85. package/src/adapters/osf/cassette/README.md +20 -0
  86. package/src/adapters/osf/cassette/get-file.json +108 -0
  87. package/src/adapters/osf/cassette/get-listing.json +111 -0
  88. package/src/adapters/osf/cassette/get-missing.json +1 -0
  89. package/src/adapters/osf/cassette/put-new.json +31 -0
  90. package/src/adapters/osf/host.io.ts +156 -0
  91. package/src/adapters/osf/host.test.ts +274 -0
  92. package/src/adapters/osf/index.ts +14 -0
  93. package/src/adapters/osf/response.ts +105 -0
  94. package/src/artifact-upload-rule.test.ts +213 -0
  95. package/src/artifact-upload-rule.ts +213 -0
  96. package/src/artifact.test.ts +504 -0
  97. package/src/artifact.ts +402 -0
  98. package/src/cli.test.ts +149 -1
  99. package/src/cli.ts +121 -8
  100. package/src/domain/artifact-upload.test.ts +80 -0
  101. package/src/domain/artifact-upload.ts +111 -0
  102. package/src/domain/submission.ts +4 -0
  103. package/src/ports/artifact-host.ts +65 -0
  104. package/src/ports/latex.ts +10 -0
  105. package/src/rule-cases.test.ts +44 -0
  106. package/src/submission-confirm-hook.test.ts +113 -1
  107. package/src/submission-confirm-hook.ts +120 -27
  108. package/src/submission-fields-run.test.ts +1 -0
  109. package/src/submission-prior-reviews.test.ts +9 -1
  110. package/src/submission-show.test.ts +81 -0
  111. package/src/submission-update.test.ts +164 -6
  112. package/src/submission.ts +208 -24
  113. package/src/types.ts +6 -0
@@ -0,0 +1,105 @@
1
+ /**
2
+ * OSF's answers, parsed into what the `ArtifactHost` port returns. Pure. The shapes are OSF's own,
3
+ * checked against recorded answers in `cassette/` (README there).
4
+ *
5
+ * - `GET api.osf.io/v2/files/<id>/` and each entry of `GET api.osf.io/v2/nodes/<guid>/files/osfstorage/`:
6
+ * a file's `id`, `attributes.name`, `attributes.kind` and `attributes.extra.hashes` (`md5`,
7
+ * `sha256`), and `links.html`, its page.
8
+ * - `PUT files.osf.io/v1/…` (WaterButler): `{ "data": <file entity> }`, whose `attributes.path` is
9
+ * `/<id>` (`BaseOsfStorageItemMetadata.path` and the upload handler of
10
+ * CenterForOpenScience/waterbutler, read 2026-11-21).
11
+ */
12
+ import { z } from "zod";
13
+ import { err, ok, type Result } from "../../domain/result.ts";
14
+ import type { HostedFile, HostFailure } from "../../ports/artifact-host.ts";
15
+
16
+ const Hash = z.string().nullable().optional();
17
+
18
+ const FileEntry = z.object({
19
+ id: z.string(),
20
+ attributes: z.object({
21
+ name: z.string(),
22
+ kind: z.string(),
23
+ extra: z.object({
24
+ hashes: z.object({ md5: Hash, sha256: Hash }).optional(),
25
+ }),
26
+ }),
27
+ links: z.object({ html: z.string() }),
28
+ });
29
+ type FileEntry = z.infer<typeof FileEntry>;
30
+
31
+ const OneFile = z.object({ data: FileEntry });
32
+ const Listing = z.object({ data: z.array(FileEntry) });
33
+ const Uploaded = z.object({
34
+ data: z.object({ attributes: z.object({ path: z.string() }) }),
35
+ });
36
+
37
+ /** An answer whose shape is not the one parsed: what was expected, and the status it came with. */
38
+ const malformed = (status: number, what: string): HostFailure => ({
39
+ kind: "malformed",
40
+ detail: `HTTP ${String(status)}: the answer is not ${what}`,
41
+ });
42
+
43
+ /** A status that is no success, as a failure: 401 and 403 refused, 404 not found. */
44
+ export function statusFailure(status: number): HostFailure | null {
45
+ if (status === 401 || status === 403)
46
+ return {
47
+ kind: "refused",
48
+ detail: `HTTP ${String(status)}: OSF refused the token, or the file is not public and no token was given`,
49
+ };
50
+ if (status === 404)
51
+ return {
52
+ kind: "not-found",
53
+ detail: "HTTP 404: OSF has no such file or project",
54
+ };
55
+ return status >= 200 && status < 300
56
+ ? null
57
+ : { kind: "refused", detail: `HTTP ${String(status)}` };
58
+ }
59
+
60
+ const hosted = (f: Readonly<FileEntry>): HostedFile => ({
61
+ id: f.id,
62
+ url: f.links.html,
63
+ sha256: f.attributes.extra.hashes?.sha256 ?? null,
64
+ md5: f.attributes.extra.hashes?.md5 ?? null,
65
+ });
66
+
67
+ /** `GET /v2/files/<id>/` → the file. */
68
+ export function parseFile(
69
+ status: number,
70
+ body: unknown,
71
+ ): Result<HostedFile, HostFailure> {
72
+ const bad = statusFailure(status);
73
+ if (bad !== null) return err(bad);
74
+ const f = OneFile.safeParse(body);
75
+ return f.success ? ok(hosted(f.data.data)) : err(malformed(status, "a file"));
76
+ }
77
+
78
+ /** A folder listing → the file called `name` in it, or null when there is none. */
79
+ export function parseListing(
80
+ status: number,
81
+ body: unknown,
82
+ name: string,
83
+ ): Result<HostedFile | null, HostFailure> {
84
+ const bad = statusFailure(status);
85
+ if (bad !== null) return err(bad);
86
+ const l = Listing.safeParse(body);
87
+ if (!l.success) return err(malformed(status, "a list of files"));
88
+ const f = l.data.data.find(
89
+ (e) => e.attributes.kind === "file" && e.attributes.name === name,
90
+ );
91
+ return ok(f === undefined ? null : hosted(f));
92
+ }
93
+
94
+ /** WaterButler's answer to an upload → the id of the file it holds now. */
95
+ export function parseUploaded(
96
+ status: number,
97
+ body: unknown,
98
+ ): Result<string, HostFailure> {
99
+ const bad = statusFailure(status);
100
+ if (bad !== null) return err(bad);
101
+ const u = Uploaded.safeParse(body);
102
+ return u.success
103
+ ? ok(u.data.data.attributes.path.replace(/^\//u, ""))
104
+ : err(malformed(status, "an uploaded file"));
105
+ }
@@ -0,0 +1,213 @@
1
+ /**
2
+ * `artifact/upload-current` (`src/artifact-upload-rule.ts`) over files in memory: every state it can
3
+ * be in, as the finding it makes. Through real ESLint and the CLI, in `src/rule-cases.test.ts`.
4
+ */
5
+ import { expect, test } from "vitest";
6
+ import { latexReader } from "./adapters/latex/index.ts";
7
+ import { memoryFiles } from "./adapters/memory/index.ts";
8
+ import { sourcesCodec } from "./adapters/sources-record/index.ts";
9
+ import {
10
+ artifactUploadRules,
11
+ uploadFindings,
12
+ uploadOptions,
13
+ } from "./artifact-upload-rule.ts";
14
+ import { uploadRecordText } from "./domain/artifact-upload.ts";
15
+ import { recordText } from "../test/recorded-fixture.ts";
16
+
17
+ const DIR = "/work/papers/p";
18
+ const NUMBERS =
19
+ "\\newcommand{\\nRepos}{64}\n\\newcommand{\\nRuns}{7}\n\\newcommand{\\pct}{41.2\\%}\n\\newcommand{\\nA}{1}\n\\newcommand{\\nB}{2}\n\\newcommand{\\nC}{3}\n\\newcommand{\\unused}{9}\n";
20
+ const PAPER =
21
+ "\\documentclass{article}\n\\input{numbersdef}\n\\begin{document}\n\\nRepos{} repos, \\nRuns{} runs, \\pct.\n\\input{sections/more}\n\\end{document}\n";
22
+ const MORE = "\\nA, \\nB, \\nC.\n";
23
+ const AT_UPLOAD = { nRepos: "63", nRuns: "7", pct: "41.2\\%" };
24
+
25
+ const record = (numbers: Readonly<Record<string, string>>): string =>
26
+ uploadRecordText({
27
+ version: 1,
28
+ host: "osf",
29
+ url: "https://osf.io/abcde/",
30
+ file: "artifact.zip",
31
+ uploadedAt: "2026-11-01T10:00:00Z",
32
+ numbers,
33
+ });
34
+
35
+ /** The paper's files under `DIR`, with the build's record of its body file. */
36
+ const tree = (
37
+ extra: Readonly<Record<string, string>>,
38
+ ): Record<string, string> => {
39
+ const own = {
40
+ "paper.tex": PAPER,
41
+ "numbersdef.tex": NUMBERS,
42
+ "sections/more.tex": MORE,
43
+ };
44
+ const built = recordText(own, {
45
+ inputs: [
46
+ ["paper.tex", "body"],
47
+ ["numbersdef.tex", "preamble"],
48
+ ["sections/more.tex", "body"],
49
+ ],
50
+ });
51
+ return Object.fromEntries(
52
+ Object.entries({ ...own, "_build/sources.json": built, ...extra }).map(
53
+ ([p, t]) => [`${DIR}/${p}`, t],
54
+ ),
55
+ );
56
+ };
57
+
58
+ const findings = (
59
+ files: Readonly<Record<string, string>>,
60
+ options: unknown = { numbers: "numbersdef.tex" },
61
+ ) =>
62
+ uploadFindings(DIR, uploadOptions(options), {
63
+ files: memoryFiles(files),
64
+ codec: sourcesCodec,
65
+ latex: latexReader,
66
+ });
67
+
68
+ const RECORD = "submission/artifact-upload.json";
69
+
70
+ test("🔴 stale: names the new and the changed keys the paper prints, a few of each", () => {
71
+ // Guards: nRepos changed, nA–nC new (from the body file the build record lists), \unused not counted.
72
+ expect(findings(tree({ [RECORD]: record(AT_UPLOAD) }))).toEqual([
73
+ {
74
+ messageId: "stale",
75
+ data: {
76
+ count: 4,
77
+ date: "2026-11-01",
78
+ url: "https://osf.io/abcde/",
79
+ detail: "3 new (nA, nB, nC), 1 changed (nRepos)",
80
+ },
81
+ },
82
+ ]);
83
+ });
84
+
85
+ test("more than three keys of a kind: the rest is counted, not listed", () => {
86
+ // Guards: five new keys show three and "2 more".
87
+ expect(findings(tree({ [RECORD]: record({ pct: "1" }) }))).toEqual([
88
+ {
89
+ messageId: "stale",
90
+ data: {
91
+ count: 6,
92
+ date: "2026-11-01",
93
+ url: "https://osf.io/abcde/",
94
+ detail: "5 new (nRepos, nRuns, nA, 2 more), 1 changed (pct)",
95
+ },
96
+ },
97
+ ]);
98
+ const four = tree({
99
+ "sections/more.tex": "\\nA, \\nB, \\nC, \\unused.\n",
100
+ [RECORD]: record({ pct: "1" }),
101
+ });
102
+ // Without a fresh build record the body file is not read: paper.tex alone counts.
103
+ // Guards: a stale build record narrows the uses to paper.tex.
104
+ expect(findings(four)).toEqual([
105
+ {
106
+ messageId: "stale",
107
+ data: {
108
+ count: 3,
109
+ date: "2026-11-01",
110
+ url: "https://osf.io/abcde/",
111
+ detail: "2 new (nRepos, nRuns), 1 changed (pct)",
112
+ },
113
+ },
114
+ ]);
115
+ });
116
+
117
+ test("a number the paper prints through another macro counts: a change under an alias is found, at any depth", () => {
118
+ const numbers =
119
+ "\\newcommand{\\nRepos}{64}\n\\newcommand{\\repoCount}{\\nRepos}\n\\newcommand{\\shown}{\\repoCount{} repos}\n\\newcommand{\\loopA}{\\loopB}\n\\newcommand{\\loopB}{\\loopA}\n";
120
+ const paper =
121
+ "\\documentclass{article}\n\\begin{document}\n\\shown, \\loopA.\n\\end{document}\n";
122
+ const atUpload = {
123
+ nRepos: "63",
124
+ repoCount: "\\nRepos",
125
+ shown: "\\repoCount{} repos",
126
+ loopA: "\\loopB",
127
+ loopB: "\\loopA",
128
+ };
129
+ const files = {
130
+ [`${DIR}/paper.tex`]: paper,
131
+ [`${DIR}/numbersdef.tex`]: numbers,
132
+ [`${DIR}/${RECORD}`]: record(atUpload),
133
+ };
134
+ // Guards: only \shown is printed; \nRepos, two definitions down, changed; a cycle ends.
135
+ expect(findings(files)).toEqual([
136
+ {
137
+ messageId: "stale",
138
+ data: {
139
+ count: 1,
140
+ date: "2026-11-01",
141
+ url: "https://osf.io/abcde/",
142
+ detail: "1 changed (nRepos)",
143
+ },
144
+ },
145
+ ]);
146
+ });
147
+
148
+ test("silent when the record holds every value the paper prints", () => {
149
+ const now = { ...AT_UPLOAD, nRepos: "64", nA: "1", nB: "2", nC: "3" };
150
+ // Guards: a current record makes no finding, whatever unused macros differ.
151
+ expect(findings(tree({ [RECORD]: record(now) }))).toEqual([]);
152
+ const elsewhere = "extra/upload.json";
153
+ // Guards: the `record` option is where the record is read.
154
+ expect(
155
+ findings(tree({ [elsewhere]: record(now) }), {
156
+ numbers: "numbersdef.tex",
157
+ record: elsewhere,
158
+ }),
159
+ ).toEqual([]);
160
+ });
161
+
162
+ test("turned on without what it needs: one finding, naming what answers it", () => {
163
+ // Guards: no record → the command that writes one.
164
+ expect(findings(tree({}))).toEqual([
165
+ { messageId: "noRecord", data: { path: RECORD } },
166
+ ]);
167
+ // Guards: a record that is not one says why.
168
+ expect(findings(tree({ [RECORD]: "[]" }))).toEqual([
169
+ {
170
+ messageId: "badRecord",
171
+ data: {
172
+ path: RECORD,
173
+ why: "the record: Invalid input: expected object, received array",
174
+ },
175
+ },
176
+ ]);
177
+ // Guards: no numbers file, and one defining nothing, before the record is looked at.
178
+ expect(findings(tree({}), { numbers: "gone.tex" })).toEqual([
179
+ { messageId: "noNumbersFile", data: { path: "gone.tex" } },
180
+ ]);
181
+ expect(findings(tree({ "numbersdef.tex": "% nothing\n" }))).toEqual([
182
+ { messageId: "emptyNumbers", data: { path: "numbersdef.tex" } },
183
+ ]);
184
+ // Guards: turned on with no options at all.
185
+ expect(findings(tree({}), null)).toEqual([
186
+ { messageId: "noOption", data: {} },
187
+ ]);
188
+ });
189
+
190
+ test("reports on PIPELINE-STATUS.md's first line only", () => {
191
+ const rule = artifactUploadRules({
192
+ files: memoryFiles(tree({})),
193
+ codec: sourcesCodec,
194
+ latex: latexReader,
195
+ })["upload-current"];
196
+ const seen = (file: string): readonly string[] => {
197
+ const out: string[] = [];
198
+ rule
199
+ .create({
200
+ filename: `${DIR}/${file}`,
201
+ options: [{ numbers: "numbersdef.tex" }],
202
+ sourceCode: { getLocFromIndex: () => ({ line: 1, column: 0 }) },
203
+ report: (d) => {
204
+ out.push(`${String(d.loc.start.line)} ${d.messageId}`);
205
+ },
206
+ })
207
+ .root?.();
208
+ return out;
209
+ };
210
+ // Guards: one finding on the scorecard; nothing on any other file.
211
+ expect(seen("PIPELINE-STATUS.md")).toEqual(["1 noRecord"]);
212
+ expect(seen("paper.tex")).toEqual([]);
213
+ });
@@ -0,0 +1,213 @@
1
+ /**
2
+ * `artifact/upload-current` — the paper prints numbers the uploaded artifact was not built for.
3
+ *
4
+ * OPT-IN, configured the ESLint way: off unless turned on with its options,
5
+ * `"artifact/upload-current": ["warn", { "numbers": "numbersdef.tex" }]` (`record` names another
6
+ * record file). It reads two local files and never the network: the upload record
7
+ * (`src/domain/artifact-upload.ts`), written by `paperlint artifact record|upload` or by anyone, and
8
+ * the paper's numbers file of `\newcommand{\key}{value}` lines. It compares the values recorded at
9
+ * upload with the values now, counting only the macros the paper's source uses (`paper.tex` and the
10
+ * body files the last build's record lists), so a macro defined and never printed never fires.
11
+ *
12
+ * Reported once per paper, on the first line of `PIPELINE-STATUS.md`, like the other folder rules.
13
+ * Turned on and missing what it needs (its `numbers` option, the numbers file, the record), it says
14
+ * so once, naming what answers it, and judges nothing else.
15
+ */
16
+ import { basename, dirname, join, resolve } from "node:path";
17
+ import { callerPath } from "./caller-path.ts";
18
+ import {
19
+ DEFAULT_RECORD,
20
+ drift,
21
+ parseUploadRecord,
22
+ printedThrough,
23
+ type UploadRecord,
24
+ } from "./domain/artifact-upload.ts";
25
+ import { fieldOf } from "./domain/record.ts";
26
+ import { err, ok, type Result } from "./domain/result.ts";
27
+ import type { FolderRuleContext } from "./folder-venue-rule.ts";
28
+ import type { LatexReader } from "./ports/latex.ts";
29
+ import {
30
+ paperRecord,
31
+ recordedFiles,
32
+ type RecordReadDeps,
33
+ } from "./paper-record.ts";
34
+ import { decoded } from "./tex-paper.ts";
35
+ import { rulePageUrl, type Finding } from "./venue-rules.ts";
36
+
37
+ /** The file the rule reports on: one per paper folder. */
38
+ const STATUS_FILE = "PIPELINE-STATUS.md";
39
+
40
+ export interface UploadRuleDeps extends RecordReadDeps {
41
+ readonly latex: LatexReader;
42
+ }
43
+
44
+ /** The rule's options: the numbers file, and the record, each relative to the paper. */
45
+ export interface UploadOptions {
46
+ readonly numbers: string | null;
47
+ readonly record: string;
48
+ }
49
+
50
+ /** The options as ESLint hands them: a string `numbers` or none, `record` or the default. */
51
+ export const uploadOptions = (raw: unknown): UploadOptions => {
52
+ const numbers = fieldOf(raw, "numbers");
53
+ const record = fieldOf(raw, "record");
54
+ return {
55
+ numbers: typeof numbers === "string" ? numbers : null,
56
+ record: typeof record === "string" ? record : DEFAULT_RECORD,
57
+ };
58
+ };
59
+
60
+ /** What the paper prints now: the numbers file's values, and the macros its source uses. */
61
+ export interface Printed {
62
+ readonly values: ReadonlyMap<string, string>;
63
+ readonly used: ReadonlySet<string>;
64
+ }
65
+
66
+ /** Why there are no numbers to compare: the file is not there, or it defines no macro. */
67
+ export type NumbersProblem = "noNumbersFile" | "emptyNumbers";
68
+
69
+ /** The text of the file at `rel` in the paper folder, or null. */
70
+ const textAt = (
71
+ dir: string,
72
+ rel: string,
73
+ deps: UploadRuleDeps,
74
+ ): string | null =>
75
+ decoded(deps.files.readBytes(callerPath(resolve(dir, rel))));
76
+
77
+ /** The files whose uses count: `paper.tex`, and the body files of a fresh build record. */
78
+ function sourcesOf(dir: string, deps: UploadRuleDeps): readonly string[] {
79
+ const r = paperRecord(dir, deps);
80
+ const body = r.kind === "fresh" ? recordedFiles(dir, r.record, "body") : [];
81
+ return [join(dir, "paper.tex"), ...body];
82
+ }
83
+
84
+ /** The paper's numbers now, from the numbers file `numbers` (relative to the paper folder `dir`). */
85
+ export function printedNumbers(
86
+ dir: string,
87
+ numbers: string,
88
+ deps: UploadRuleDeps,
89
+ ): Result<Printed, NumbersProblem> {
90
+ const text = textAt(dir, numbers, deps);
91
+ if (text === null) return err("noNumbersFile");
92
+ const defined = deps.latex.macroValues(text);
93
+ if (defined.length === 0) return err("emptyNumbers");
94
+ const used = sourcesOf(dir, deps).flatMap((f) => {
95
+ const src = textAt(dir, f, deps);
96
+ return src === null ? [] : deps.latex.macrosUsed(src);
97
+ });
98
+ const values = new Map(defined.map((d) => [d.name, d.value]));
99
+ return ok({
100
+ values,
101
+ used: printedThrough(values, new Set(used), deps.latex.macrosUsed),
102
+ });
103
+ }
104
+
105
+ /** How many keys a message names before it says how many more there are. */
106
+ const SHOWN = 3;
107
+ const someOf = (keys: readonly string[]): string =>
108
+ [
109
+ ...keys.slice(0, SHOWN),
110
+ ...(keys.length > SHOWN ? [`${String(keys.length - SHOWN)} more`] : []),
111
+ ].join(", ");
112
+
113
+ /** The finding of a record against the paper's numbers now; none when they agree. */
114
+ function staleFindings(r: UploadRecord, p: Printed): readonly Finding[] {
115
+ const d = drift(r.numbers, p.values, p.used);
116
+ const detail = [
117
+ ...(d.added.length > 0
118
+ ? [`${String(d.added.length)} new (${someOf(d.added)})`]
119
+ : []),
120
+ ...(d.changed.length > 0
121
+ ? [`${String(d.changed.length)} changed (${someOf(d.changed)})`]
122
+ : []),
123
+ ];
124
+ if (detail.length === 0) return [];
125
+ const count = d.added.length + d.changed.length;
126
+ return [
127
+ {
128
+ messageId: "stale",
129
+ data: {
130
+ count,
131
+ date: r.uploadedAt.slice(0, 10),
132
+ url: r.url,
133
+ detail: detail.join(", "),
134
+ },
135
+ },
136
+ ];
137
+ }
138
+
139
+ /** The rule's findings for the paper folder `dir`: at most one. Reads through `deps`; never throws. */
140
+ export function uploadFindings(
141
+ dir: string,
142
+ o: UploadOptions,
143
+ deps: UploadRuleDeps,
144
+ ): readonly Finding[] {
145
+ if (o.numbers === null) return [{ messageId: "noOption", data: {} }];
146
+ const printed = printedNumbers(dir, o.numbers, deps);
147
+ if (!printed.ok)
148
+ return [{ messageId: printed.error, data: { path: o.numbers } }];
149
+ const text = textAt(dir, o.record, deps);
150
+ if (text === null)
151
+ return [{ messageId: "noRecord", data: { path: o.record } }];
152
+ const record = parseUploadRecord(text);
153
+ return record.ok
154
+ ? staleFindings(record.value, printed.value)
155
+ : [{ messageId: "badRecord", data: { path: o.record, why: record.error } }];
156
+ }
157
+
158
+ const META = {
159
+ type: "problem",
160
+ docs: {
161
+ description:
162
+ "opt-in: the paper prints numbers the uploaded artifact, as its upload record says, was not built for",
163
+ url: rulePageUrl("artifact/upload-current"),
164
+ },
165
+ schema: [
166
+ {
167
+ type: "object",
168
+ properties: { numbers: { type: "string" }, record: { type: "string" } },
169
+ additionalProperties: false,
170
+ },
171
+ ],
172
+ messages: {
173
+ stale:
174
+ "the paper prints {{count}} numbers the upload of {{date}} ({{url}}) was not built for: {{detail}} — rebuild and upload the artifact, then record it again (`paperlint artifact record <zip> --url <url>`)",
175
+ noOption:
176
+ 'artifact/upload-current is on without its numbers file: give it as an option, ["warn", { "numbers": "numbersdef.tex" }], relative to the paper',
177
+ noNumbersFile:
178
+ 'the numbers file {{path}} (the rule\'s "numbers" option, relative to the paper) does not exist, so the upload cannot be compared with what the paper prints',
179
+ emptyNumbers:
180
+ "the numbers file {{path}} defines no macro without arguments (\\newcommand{\\key}{value}), so the upload cannot be compared with what the paper prints",
181
+ noRecord:
182
+ "no upload record {{path}}: after uploading the artifact, write one with `paperlint artifact record <zip> --url <url>` (or by hand, docs/artifact-upload.md)",
183
+ badRecord: "the upload record {{path}} cannot be read: {{why}}",
184
+ },
185
+ } as const;
186
+
187
+ /** The rule, as a rule of the `artifact` plugin. On for no paper until its options turn it on. */
188
+ export function artifactUploadRules(deps: UploadRuleDeps): {
189
+ readonly "upload-current": {
190
+ readonly meta: typeof META;
191
+ create(c: FolderRuleContext & { readonly options: readonly unknown[] }): {
192
+ root?: () => void;
193
+ };
194
+ };
195
+ } {
196
+ return {
197
+ "upload-current": {
198
+ meta: META,
199
+ create(context) {
200
+ if (basename(context.filename) !== STATUS_FILE) return {};
201
+ return {
202
+ root() {
203
+ const at = context.sourceCode.getLocFromIndex(0);
204
+ const o = uploadOptions(context.options[0]);
205
+ uploadFindings(dirname(context.filename), o, deps).forEach((f) => {
206
+ context.report({ loc: { start: at, end: at }, ...f });
207
+ });
208
+ },
209
+ };
210
+ },
211
+ },
212
+ };
213
+ }