paperlint 8.0.0 → 9.0.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 (393) hide show
  1. package/.github/CLAUDE.md +22 -0
  2. package/.github/CLAUDE.md.spec.ts +20 -0
  3. package/CLAUDE.md +59 -531
  4. package/CONTRIBUTING.md +7 -6
  5. package/README.md +4 -3
  6. package/dist/CLAUDE.md.spec.d.ts +3 -0
  7. package/dist/CLAUDE.md.spec.d.ts.map +1 -0
  8. package/dist/CLAUDE.md.spec.js +62 -0
  9. package/dist/CLAUDE.md.spec.js.map +1 -0
  10. package/dist/adapters/bibtex/index.d.ts +3 -0
  11. package/dist/adapters/bibtex/index.d.ts.map +1 -0
  12. package/dist/adapters/bibtex/index.js +103 -0
  13. package/dist/adapters/bibtex/index.js.map +1 -0
  14. package/dist/adapters/latex/filecontents.d.ts +33 -0
  15. package/dist/adapters/latex/filecontents.d.ts.map +1 -0
  16. package/dist/adapters/latex/filecontents.js +65 -0
  17. package/dist/adapters/latex/filecontents.js.map +1 -0
  18. package/dist/adapters/latex/includes.d.ts +3 -3
  19. package/dist/adapters/latex/includes.d.ts.map +1 -1
  20. package/dist/adapters/latex/includes.js +18 -10
  21. package/dist/adapters/latex/includes.js.map +1 -1
  22. package/dist/adapters/latex/index.d.ts.map +1 -1
  23. package/dist/adapters/latex/index.js +2 -0
  24. package/dist/adapters/latex/index.js.map +1 -1
  25. package/dist/adapters/latex/nodes.d.ts +12 -0
  26. package/dist/adapters/latex/nodes.d.ts.map +1 -1
  27. package/dist/adapters/latex/nodes.js +39 -0
  28. package/dist/adapters/latex/nodes.js.map +1 -1
  29. package/dist/adapters/latex/parse.d.ts +7 -1
  30. package/dist/adapters/latex/parse.d.ts.map +1 -1
  31. package/dist/adapters/latex/parse.js +55 -6
  32. package/dist/adapters/latex/parse.js.map +1 -1
  33. package/dist/adapters/memory/index.d.ts +3 -0
  34. package/dist/adapters/memory/index.d.ts.map +1 -1
  35. package/dist/adapters/memory/index.js +9 -0
  36. package/dist/adapters/memory/index.js.map +1 -1
  37. package/dist/adapters/references/index.d.ts.map +1 -1
  38. package/dist/adapters/references/index.js +27 -24
  39. package/dist/adapters/references/index.js.map +1 -1
  40. package/dist/adapters/sources-record/index.d.ts +9 -0
  41. package/dist/adapters/sources-record/index.d.ts.map +1 -0
  42. package/dist/adapters/sources-record/index.js +68 -0
  43. package/dist/adapters/sources-record/index.js.map +1 -0
  44. package/dist/adapters/tex-output/blg.d.ts +9 -0
  45. package/dist/adapters/tex-output/blg.d.ts.map +1 -0
  46. package/dist/adapters/tex-output/blg.js +20 -0
  47. package/dist/adapters/tex-output/blg.js.map +1 -0
  48. package/dist/adapters/tex-output/fls.d.ts +8 -0
  49. package/dist/adapters/tex-output/fls.d.ts.map +1 -0
  50. package/dist/adapters/tex-output/fls.js +16 -0
  51. package/dist/adapters/tex-output/fls.js.map +1 -0
  52. package/dist/adapters/tex-output/index.d.ts +7 -0
  53. package/dist/adapters/tex-output/index.d.ts.map +1 -0
  54. package/dist/adapters/tex-output/index.js +7 -0
  55. package/dist/adapters/tex-output/index.js.map +1 -0
  56. package/dist/bib-rules.d.ts +47 -0
  57. package/dist/bib-rules.d.ts.map +1 -0
  58. package/dist/bib-rules.js +127 -0
  59. package/dist/bib-rules.js.map +1 -0
  60. package/dist/build.d.ts +29 -1
  61. package/dist/build.d.ts.map +1 -1
  62. package/dist/build.js +41 -25
  63. package/dist/build.js.map +1 -1
  64. package/dist/claim-provenance.d.ts +2 -2
  65. package/dist/cli.d.ts +6 -1
  66. package/dist/cli.d.ts.map +1 -1
  67. package/dist/cli.js +30 -26
  68. package/dist/cli.js.map +1 -1
  69. package/dist/domain/paper-source.d.ts +1 -10
  70. package/dist/domain/paper-source.d.ts.map +1 -1
  71. package/dist/domain/paper-source.js +34 -24
  72. package/dist/domain/paper-source.js.map +1 -1
  73. package/dist/domain/paper-sources.d.ts +72 -0
  74. package/dist/domain/paper-sources.d.ts.map +1 -0
  75. package/dist/domain/paper-sources.js +18 -0
  76. package/dist/domain/paper-sources.js.map +1 -0
  77. package/dist/domain/sources-record.d.ts +102 -0
  78. package/dist/domain/sources-record.d.ts.map +1 -0
  79. package/dist/domain/sources-record.js +38 -0
  80. package/dist/domain/sources-record.js.map +1 -0
  81. package/dist/domain/tex-run.d.ts +25 -0
  82. package/dist/domain/tex-run.d.ts.map +1 -0
  83. package/dist/domain/tex-run.js +79 -0
  84. package/dist/domain/tex-run.js.map +1 -0
  85. package/dist/eslint-rules/CLAUDE.md.spec.d.ts +3 -0
  86. package/dist/eslint-rules/CLAUDE.md.spec.d.ts.map +1 -0
  87. package/dist/eslint-rules/CLAUDE.md.spec.js +39 -0
  88. package/dist/eslint-rules/CLAUDE.md.spec.js.map +1 -0
  89. package/dist/eslint-rules/install-path-literals.d.ts +1 -1
  90. package/dist/eslint-rules/install-path-literals.js +4 -4
  91. package/dist/eslint-rules/install-path-literals.js.map +1 -1
  92. package/dist/eslint-rules/paper-context.d.ts.map +1 -1
  93. package/dist/eslint-rules/paper-context.js +4 -3
  94. package/dist/eslint-rules/paper-context.js.map +1 -1
  95. package/dist/eslint-rules/paper-typography.d.ts +0 -9
  96. package/dist/eslint-rules/paper-typography.d.ts.map +1 -1
  97. package/dist/eslint-rules/paper-typography.js +12 -5
  98. package/dist/eslint-rules/paper-typography.js.map +1 -1
  99. package/dist/eslint-rules/pdf-last-page-balance.js +1 -1
  100. package/dist/latex-log.d.ts +10 -0
  101. package/dist/latex-log.d.ts.map +1 -1
  102. package/dist/latex-log.js +10 -0
  103. package/dist/latex-log.js.map +1 -1
  104. package/dist/latex-loop.d.ts +2 -0
  105. package/dist/latex-loop.d.ts.map +1 -1
  106. package/dist/latex-loop.js.map +1 -1
  107. package/dist/lib/agent-cli-version.js +1 -1
  108. package/dist/lib/agent-cli-version.js.map +1 -1
  109. package/dist/package-dirs.d.ts.map +1 -1
  110. package/dist/package-dirs.js +3 -2
  111. package/dist/package-dirs.js.map +1 -1
  112. package/dist/paper-includes.d.ts +4 -7
  113. package/dist/paper-includes.d.ts.map +1 -1
  114. package/dist/paper-includes.js +29 -21
  115. package/dist/paper-includes.js.map +1 -1
  116. package/dist/paper-prose.d.ts +2 -2
  117. package/dist/paper-record.d.ts +25 -0
  118. package/dist/paper-record.d.ts.map +1 -0
  119. package/dist/paper-record.js +39 -0
  120. package/dist/paper-record.js.map +1 -0
  121. package/dist/paper-sources.d.ts +66 -0
  122. package/dist/paper-sources.d.ts.map +1 -0
  123. package/dist/paper-sources.js +92 -0
  124. package/dist/paper-sources.js.map +1 -0
  125. package/dist/ports/bib-reader.d.ts +18 -0
  126. package/dist/ports/bib-reader.d.ts.map +1 -0
  127. package/dist/ports/bib-reader.js +2 -0
  128. package/dist/ports/bib-reader.js.map +1 -0
  129. package/dist/ports/check-references.d.ts +9 -4
  130. package/dist/ports/check-references.d.ts.map +1 -1
  131. package/dist/ports/latex.d.ts +15 -0
  132. package/dist/ports/latex.d.ts.map +1 -1
  133. package/dist/ports/sources-codec.d.ts +11 -0
  134. package/dist/ports/sources-codec.d.ts.map +1 -0
  135. package/dist/ports/sources-codec.js +2 -0
  136. package/dist/ports/sources-codec.js.map +1 -0
  137. package/dist/ports/tex-output.d.ts +17 -0
  138. package/dist/ports/tex-output.d.ts.map +1 -0
  139. package/dist/ports/tex-output.js +2 -0
  140. package/dist/ports/tex-output.js.map +1 -0
  141. package/dist/recorded-bibliography.d.ts +89 -0
  142. package/dist/recorded-bibliography.d.ts.map +1 -0
  143. package/dist/recorded-bibliography.js +136 -0
  144. package/dist/recorded-bibliography.js.map +1 -0
  145. package/dist/reference-rules.d.ts +5 -5
  146. package/dist/reference-rules.d.ts.map +1 -1
  147. package/dist/reference-rules.js +150 -77
  148. package/dist/reference-rules.js.map +1 -1
  149. package/dist/references.d.ts +71 -16
  150. package/dist/references.d.ts.map +1 -1
  151. package/dist/references.js +110 -26
  152. package/dist/references.js.map +1 -1
  153. package/dist/register-bands.d.ts +3 -3
  154. package/dist/register-bands.d.ts.map +1 -1
  155. package/dist/register-bands.js.map +1 -1
  156. package/dist/register.d.ts +2 -2
  157. package/dist/sources-record.d.ts +38 -0
  158. package/dist/sources-record.d.ts.map +1 -0
  159. package/dist/sources-record.js +123 -0
  160. package/dist/sources-record.js.map +1 -0
  161. package/dist/sources-rules.d.ts +44 -0
  162. package/dist/sources-rules.d.ts.map +1 -0
  163. package/dist/sources-rules.js +76 -0
  164. package/dist/sources-rules.js.map +1 -0
  165. package/dist/tex-paper.d.ts +10 -15
  166. package/dist/tex-paper.d.ts.map +1 -1
  167. package/dist/tex-paper.js +24 -40
  168. package/dist/tex-paper.js.map +1 -1
  169. package/dist/tex-requirements.js +1 -1
  170. package/dist/tex-venue-rules.d.ts +3 -3
  171. package/dist/tex-venue-rules.d.ts.map +1 -1
  172. package/dist/tex-venue-rules.js.map +1 -1
  173. package/dist/toolchain.js +1 -1
  174. package/docs/configuration.md +12 -4
  175. package/docs/design/paper-sources.md +197 -0
  176. package/docs/e2e.md +1 -1
  177. package/docs/incidents.md +5 -5
  178. package/docs/package-shape-options.md +1 -1
  179. package/docs/references.md +34 -8
  180. package/docs/rules/bib/reachable-entry.md +64 -0
  181. package/docs/rules/paper/sources-fresh.md +81 -0
  182. package/docs/rules.md +62 -57
  183. package/eslint-rules/CLAUDE.md +44 -0
  184. package/eslint-rules/CLAUDE.md.spec.ts +43 -0
  185. package/eslint-rules/install-path-literals.harness.mjs +1 -1
  186. package/eslint-rules/install-path-literals.ts +4 -4
  187. package/eslint-rules/paper-context.ts +4 -3
  188. package/eslint-rules/paper-typography.test.mjs +12 -72
  189. package/eslint-rules/paper-typography.ts +13 -6
  190. package/eslint-rules/pdf-last-page-balance.ts +1 -1
  191. package/eslint.config.mjs +28 -3
  192. package/fixtures/build-e2e/aidc/paper.tex +1 -1
  193. package/fixtures/build-e2e/anon-ack/paper.tex +1 -1
  194. package/fixtures/build-e2e/anon-author/paper.tex +1 -1
  195. package/fixtures/build-e2e/anon-bib/paper.tex +1 -1
  196. package/fixtures/build-e2e/anon-href/paper.tex +1 -1
  197. package/fixtures/build-e2e/anon-macro/paper.tex +1 -1
  198. package/fixtures/build-e2e/anon-pdfauthor/paper.tex +1 -1
  199. package/fixtures/paper-sources/p1/paper.tex +30 -0
  200. package/fixtures/paper-sources/p1/refs.bib +6 -0
  201. package/fixtures/paper-sources/p1/sections/dead.tex +6 -0
  202. package/fixtures/paper-sources/p1/sections/intro.tex +7 -0
  203. package/fixtures/paper-sources/p1/sections/parked.tex +6 -0
  204. package/fixtures/paper-sources/p1/tex-truth.json +37 -0
  205. package/fixtures/paper-sources/p1/versions/2026-01-01-review.tex +4 -0
  206. package/fixtures/paper-sources/v1-stale/paper.tex +14 -0
  207. package/fixtures/paper-sources/v1-stale/refs.bib +6 -0
  208. package/fixtures/paper-sources/v1-stale/tex-truth.json +32 -0
  209. package/fixtures/paper-sources/v10-ifthenelse/anon.bib +1 -0
  210. package/fixtures/paper-sources/v10-ifthenelse/paper.tex +9 -0
  211. package/fixtures/paper-sources/v10-ifthenelse/real.bib +1 -0
  212. package/fixtures/paper-sources/v10-ifthenelse/tex-truth.json +28 -0
  213. package/fixtures/paper-sources/v11-two-blocks/paper.tex +12 -0
  214. package/fixtures/paper-sources/v11-two-blocks/tex-truth.json +30 -0
  215. package/fixtures/paper-sources/v13-percent-text/paper.tex +6 -0
  216. package/fixtures/paper-sources/v13-percent-text/refs.bib +3 -0
  217. package/fixtures/paper-sources/v13-percent-text/tex-truth.json +30 -0
  218. package/fixtures/paper-sources/v15-after-end/paper.tex +6 -0
  219. package/fixtures/paper-sources/v15-after-end/refs.bib +1 -0
  220. package/fixtures/paper-sources/v15-after-end/tex-truth.json +17 -0
  221. package/fixtures/paper-sources/v16-included-block/bibblock.tex +3 -0
  222. package/fixtures/paper-sources/v16-included-block/paper.tex +7 -0
  223. package/fixtures/paper-sources/v16-included-block/refs.bib +1 -0
  224. package/fixtures/paper-sources/v16-included-block/tex-truth.json +35 -0
  225. package/fixtures/paper-sources/v17-include-order/bibsetup.tex +1 -0
  226. package/fixtures/paper-sources/v17-include-order/first.bib +1 -0
  227. package/fixtures/paper-sources/v17-include-order/later.bib +1 -0
  228. package/fixtures/paper-sources/v17-include-order/paper.tex +7 -0
  229. package/fixtures/paper-sources/v17-include-order/tex-truth.json +40 -0
  230. package/fixtures/paper-sources/v2-overwrite/paper.tex +14 -0
  231. package/fixtures/paper-sources/v2-overwrite/refs.bib +6 -0
  232. package/fixtures/paper-sources/v2-overwrite/tex-truth.json +30 -0
  233. package/fixtures/paper-sources/v22-parked-thebibliography/paper.tex +7 -0
  234. package/fixtures/paper-sources/v22-parked-thebibliography/tex-truth.json +17 -0
  235. package/fixtures/paper-sources/v23-parked-include/old.bib +1 -0
  236. package/fixtures/paper-sources/v23-parked-include/paper.tex +7 -0
  237. package/fixtures/paper-sources/v23-parked-include/parked.tex +1 -0
  238. package/fixtures/paper-sources/v23-parked-include/refs.bib +1 -0
  239. package/fixtures/paper-sources/v23-parked-include/tex-truth.json +28 -0
  240. package/fixtures/paper-sources/v24-quoted-fields/paper.tex +6 -0
  241. package/fixtures/paper-sources/v24-quoted-fields/refs.bib +5 -0
  242. package/fixtures/paper-sources/v24-quoted-fields/tex-truth.json +32 -0
  243. package/fixtures/paper-sources/v25-string-differs/paper.tex +10 -0
  244. package/fixtures/paper-sources/v25-string-differs/refs.bib +2 -0
  245. package/fixtures/paper-sources/v25-string-differs/tex-truth.json +32 -0
  246. package/fixtures/paper-sources/v26-comment-in-names/one.bib +1 -0
  247. package/fixtures/paper-sources/v26-comment-in-names/paper.tex +7 -0
  248. package/fixtures/paper-sources/v26-comment-in-names/tex-truth.json +32 -0
  249. package/fixtures/paper-sources/v26-comment-in-names/two.bib +1 -0
  250. package/fixtures/paper-sources/v27-shared-string/abbreviations.bib +2 -0
  251. package/fixtures/paper-sources/v27-shared-string/paper.tex +6 -0
  252. package/fixtures/paper-sources/v27-shared-string/references.bib +6 -0
  253. package/fixtures/paper-sources/v27-shared-string/tex-truth.json +31 -0
  254. package/fixtures/paper-sources/v28-shared-string-block/abbreviations.bib +2 -0
  255. package/fixtures/paper-sources/v28-shared-string-block/paper.tex +14 -0
  256. package/fixtures/paper-sources/v28-shared-string-block/tex-truth.json +33 -0
  257. package/fixtures/paper-sources/v29-generated-input/paper.tex +9 -0
  258. package/fixtures/paper-sources/v29-generated-input/tex-truth.json +24 -0
  259. package/fixtures/paper-sources/v3-declared/paper.bib +6 -0
  260. package/fixtures/paper-sources/v3-declared/paper.tex +6 -0
  261. package/fixtures/paper-sources/v3-declared/tex-truth.json +28 -0
  262. package/fixtures/paper-sources/v30-name-next-line/paper.tex +14 -0
  263. package/fixtures/paper-sources/v30-name-next-line/tex-truth.json +30 -0
  264. package/fixtures/paper-sources/v4-commented/paper.tex +14 -0
  265. package/fixtures/paper-sources/v4-commented/refs.bib +6 -0
  266. package/fixtures/paper-sources/v4-commented/tex-truth.json +28 -0
  267. package/fixtures/paper-sources/v5-percent-entry/paper.tex +20 -0
  268. package/fixtures/paper-sources/v5-percent-entry/tex-truth.json +37 -0
  269. package/fixtures/paper-sources/v6-unclosed/paper.tex +6 -0
  270. package/fixtures/paper-sources/v6-unclosed/refs.bib +5 -0
  271. package/fixtures/paper-sources/v6-unclosed/tex-truth.json +37 -0
  272. package/fixtures/paper-sources/v8-jobname/paper.tex +9 -0
  273. package/fixtures/paper-sources/v8-jobname/tex-truth.json +30 -0
  274. package/fixtures/paper-sources/v9-two-bibliographies/one.bib +1 -0
  275. package/fixtures/paper-sources/v9-two-bibliographies/paper.tex +7 -0
  276. package/fixtures/paper-sources/v9-two-bibliographies/tex-truth.json +35 -0
  277. package/fixtures/paper-sources/v9-two-bibliographies/two.bib +1 -0
  278. package/fixtures/paper-typography/clean-paper/paper.tex +1 -1
  279. package/fixtures/paper-typography/messy-paper/paper.tex +1 -1
  280. package/hooks/CLAUDE.md +34 -0
  281. package/hooks/CLAUDE.md.spec.ts +30 -0
  282. package/lib/agent-cli-version.ts +1 -1
  283. package/package.json +3 -11
  284. package/plugin/CLAUDE.md +22 -0
  285. package/plugin/CLAUDE.md.spec.ts +22 -0
  286. package/scripts/check.test.ts +23 -1
  287. package/scripts/check.ts +7 -3
  288. package/scripts/coverage-config.test.ts +21 -11
  289. package/scripts/layer-legacy-frozen.test.ts +41 -2
  290. package/scripts/layer-legacy-frozen.ts +31 -2
  291. package/skills/CLAUDE.md +19 -0
  292. package/skills/CLAUDE.md.spec.ts +18 -0
  293. package/skills/grade-paper-writing/SKILL.md +4 -3
  294. package/skills/grade-paper-writing/SKILL.md.spec.ts +3 -2
  295. package/skills/grade-paper-writing/prose-lint.mjs +11 -2
  296. package/skills/grade-paper-writing/prose-lint.test.ts +52 -4
  297. package/skills/paper-pipeline/scripts/extract-ref-facts.harness.mjs +83 -33
  298. package/skills/paper-pipeline/scripts/extract-ref-facts.mjs +67 -115
  299. package/skills/paper-pipeline/scripts/extract-ref-facts.test.mjs +128 -121
  300. package/skills/plan-paper-timeline/plan-paper-timeline.effects.harness.mjs +21 -102
  301. package/skills/verify-citations/SKILL.md +7 -4
  302. package/skills/verify-citations/SKILL.md.spec.ts +6 -3
  303. package/skills/verify-citations/scripts/bib-authors.check.test.mjs +17 -19
  304. package/skills/verify-citations/scripts/bib-authors.cli.test.ts +129 -46
  305. package/skills/verify-citations/scripts/bib-authors.d.mts +22 -2
  306. package/skills/verify-citations/scripts/bib-authors.mjs +57 -111
  307. package/skills/verify-citations/scripts/verify-cites.cli.test.ts +16 -0
  308. package/skills/verify-citations/scripts/verify-cites.d.mts +19 -3
  309. package/skills/verify-citations/scripts/verify-cites.mjs +42 -108
  310. package/skills/verify-citations/scripts/verify-cites.test.mjs +23 -36
  311. package/src/CLAUDE.md +23 -1
  312. package/src/CLAUDE.md.spec.ts +72 -0
  313. package/src/adapters/bibtex/index.test.ts +163 -0
  314. package/src/adapters/bibtex/index.ts +155 -0
  315. package/src/adapters/latex/filecontents.ts +114 -0
  316. package/src/adapters/latex/includes.ts +18 -10
  317. package/src/adapters/latex/index.test.ts +73 -0
  318. package/src/adapters/latex/index.ts +2 -0
  319. package/src/adapters/latex/nodes.ts +64 -0
  320. package/src/adapters/latex/parse.test.ts +38 -0
  321. package/src/adapters/latex/parse.ts +67 -6
  322. package/src/adapters/memory/index.ts +17 -0
  323. package/src/adapters/references/index.test.ts +75 -21
  324. package/src/adapters/references/index.ts +37 -32
  325. package/src/adapters/sources-record/index.test.ts +87 -0
  326. package/src/adapters/sources-record/index.ts +82 -0
  327. package/src/adapters/tex-output/blg.ts +27 -0
  328. package/src/adapters/tex-output/fls.ts +24 -0
  329. package/src/adapters/tex-output/index.test.ts +171 -0
  330. package/src/adapters/tex-output/index.ts +12 -0
  331. package/src/bib-rules.test.ts +356 -0
  332. package/src/bib-rules.ts +228 -0
  333. package/src/bibliography-at.test.ts +286 -0
  334. package/src/build.test.ts +210 -0
  335. package/src/build.ts +101 -36
  336. package/src/claim-provenance.test.ts +33 -7
  337. package/src/claim-provenance.ts +2 -2
  338. package/src/cli.harness.mjs +20 -10
  339. package/src/cli.test.ts +14 -10
  340. package/src/cli.ts +37 -28
  341. package/src/domain/paper-source.test.ts +58 -20
  342. package/src/domain/paper-source.ts +38 -42
  343. package/src/domain/paper-sources.ts +94 -0
  344. package/src/domain/sources-record.test.ts +71 -0
  345. package/src/domain/sources-record.ts +136 -0
  346. package/src/domain/tex-run.test.ts +228 -0
  347. package/src/domain/tex-run.ts +139 -0
  348. package/src/heading-case.test.ts +24 -10
  349. package/src/latex-log.ts +20 -0
  350. package/src/latex-loop.harness.ts +1 -0
  351. package/src/latex-loop.ts +2 -0
  352. package/src/linted-set.test.ts +9 -1
  353. package/src/new-paper.harness.mjs +3 -2
  354. package/src/new-paper.test.ts +9 -5
  355. package/src/package-dirs.ts +3 -2
  356. package/src/paper-includes.test.ts +157 -38
  357. package/src/paper-includes.ts +39 -35
  358. package/src/paper-prose.test.ts +34 -5
  359. package/src/paper-prose.ts +2 -2
  360. package/src/paper-record.test.ts +173 -0
  361. package/src/paper-record.ts +66 -0
  362. package/src/paper-settings-commands.test.ts +5 -2
  363. package/src/paper-sources.ts +151 -0
  364. package/src/ports/bib-reader.ts +27 -0
  365. package/src/ports/check-references.ts +7 -3
  366. package/src/ports/latex.ts +19 -0
  367. package/src/ports/sources-codec.ts +11 -0
  368. package/src/ports/tex-output.ts +17 -0
  369. package/src/recorded-bibliography.ts +297 -0
  370. package/src/reference-rules.ts +217 -104
  371. package/src/references.test.ts +520 -177
  372. package/src/references.ts +185 -37
  373. package/src/register-bands.test.ts +5 -1
  374. package/src/register-bands.ts +3 -5
  375. package/src/register.test.ts +6 -1
  376. package/src/register.ts +2 -2
  377. package/src/rule-cases.test.ts +68 -12
  378. package/src/rule-docs.test.ts +0 -1
  379. package/src/sources-record.test.ts +308 -0
  380. package/src/sources-record.ts +231 -0
  381. package/src/sources-rules.test.ts +152 -0
  382. package/src/sources-rules.ts +117 -0
  383. package/src/tex-paper.test.ts +148 -78
  384. package/src/tex-paper.ts +31 -61
  385. package/src/tex-requirements.ts +1 -1
  386. package/src/tex-venue-rules.test.ts +36 -8
  387. package/src/tex-venue-rules.ts +3 -5
  388. package/src/toolchain.ts +1 -1
  389. package/dist/eslint-rules/bib-reachable-entry.d.ts +0 -22
  390. package/dist/eslint-rules/bib-reachable-entry.d.ts.map +0 -1
  391. package/dist/eslint-rules/bib-reachable-entry.js +0 -75
  392. package/dist/eslint-rules/bib-reachable-entry.js.map +0 -1
  393. package/eslint-rules/bib-reachable-entry.ts +0 -76
@@ -0,0 +1,94 @@
1
+ /**
2
+ * WHAT A DATABASE IS — the values the bibtex reader (`BibReader`, src/ports/bib-reader.ts) returns for
3
+ * a `.bib`, or for the body of a `filecontents` block: its entries, and the bytes bibtex takes from
4
+ * them. Which databases a paper reads is TeX's answer, recorded by the build (`_build/sources.json`,
5
+ * src/domain/sources-record.ts, docs/design/paper-sources.md §1); nothing here decides it.
6
+ */
7
+ import type { AbsolutePath } from "./paths.ts";
8
+ import type { Span } from "./tex-document.ts";
9
+
10
+ /** The main file's name — the file TeX is given. */
11
+ export const MAIN_FILE = "paper.tex";
12
+
13
+ /** A name in a name list (`author`, `editor`), split into its parts as BibTeX defines them. */
14
+ export interface BibName {
15
+ readonly lastName?: string;
16
+ readonly firstName?: string;
17
+ readonly prefix?: string;
18
+ readonly suffix?: string;
19
+ /** A name written as one unit (`{World Health Organization}`). */
20
+ readonly name?: string;
21
+ }
22
+
23
+ /**
24
+ * The `@string`s in force: each name as bibtex folds it (upper case) → its value as bibtex takes it,
25
+ * the `@string`s it used expanded and its LaTeX as written. bibtex reads a paper's databases one after
26
+ * the other with one table of them, so a `@string` an earlier database defines is in force in a later
27
+ * one (measured: `\\bibliography{abbreviations,references}` typesets an entry of `references.bib` with
28
+ * the author and booktitle `abbreviations.bib` defines; in the other order both are undefined).
29
+ */
30
+ export type BibStrings = Readonly<Record<string, string>>;
31
+
32
+ /** One entry of a database. */
33
+ export interface BibEntry {
34
+ /** Lower-cased (`@Article` → `article`). */
35
+ readonly type: string;
36
+ readonly key: string;
37
+ /** From its `@` to its closing brace or parenthesis, in the text of the file that holds it. */
38
+ readonly span: Span;
39
+ /** Its fields, LaTeX read into text (`{\"u}` → `ü`, `\url{x}` → a link); a list field joined by ` and `. */
40
+ readonly fields: Readonly<Record<string, string>>;
41
+ /** Its name lists (`author`, `editor`, …), each name split into its parts. */
42
+ readonly names: Readonly<Record<string, readonly BibName[]>>;
43
+ }
44
+
45
+ /** The bytes of one database, where they sit, and what the reader read from them. */
46
+ export interface BibText {
47
+ /** The file holding the bytes: the `.bib`, or the `.tex` that holds the block. */
48
+ readonly path: AbsolutePath;
49
+ /** That file's whole text; every span here is an offset into it. */
50
+ readonly text: string;
51
+ /** The database inside `text`: all of it for a `.bib`, the body for a block. */
52
+ readonly body: Span;
53
+ readonly entries: readonly BibEntry[];
54
+ /** The same entries as bibtex takes them, in order: what two databases are compared by. */
55
+ readonly written: readonly WrittenEntry[];
56
+ /** The `@preamble` commands, as written, in order: bibtex writes them into the `.bbl`. */
57
+ readonly preamble: readonly string[];
58
+ /** The `@string`s the databases bibtex read before this one left in force: none for one read alone. */
59
+ readonly inherited: BibStrings;
60
+ /** The `@string`s in force after it — `inherited` and its own, a later one winning: what the next database starts with. */
61
+ readonly strings: BibStrings;
62
+ }
63
+
64
+ /**
65
+ * An entry as bibtex takes it from the database: the type, the key, and each field's value with the
66
+ * `@string`s it uses expanded where it uses them (a later redefinition changes nothing) and its LaTeX
67
+ * as written — `{\"o}` and `ö` are different values here, as they are in the `.bbl` bibtex writes.
68
+ */
69
+ export interface WrittenEntry {
70
+ /** Lower-cased, as bibtex reads it. */
71
+ readonly type: string;
72
+ readonly key: string;
73
+ /** Whitespace runs folded to one space, as bibtex folds them. */
74
+ readonly fields: Readonly<Record<string, string>>;
75
+ }
76
+
77
+ /**
78
+ * What bibtex takes from a database, for comparing two: each entry as bibtex reads it, in order, its
79
+ * fields in name order, and the `@preamble` commands. Nothing else in the bytes reaches the `.bbl`:
80
+ * not the text between entries (a `filecontents` header among it), not a `@string` no entry uses.
81
+ */
82
+ const contentOf = (b: BibText): string =>
83
+ JSON.stringify([
84
+ b.written.map((e) => [
85
+ e.type,
86
+ e.key,
87
+ Object.entries(e.fields).sort(([x], [y]) => x.localeCompare(y)),
88
+ ]),
89
+ b.preamble,
90
+ ]);
91
+
92
+ /** Whether two databases hold the same, as TeX's own copy of a block holds the block's. */
93
+ export const sameDatabase = (a: BibText, b: BibText): boolean =>
94
+ contentOf(a) === contentOf(b);
@@ -0,0 +1,71 @@
1
+ /**
2
+ * The record of a build as a value: the one text it is parsed from, what a rule reads off it, and what
3
+ * changed since it was made. The files it is about are `src/sources-record.test.ts`'s.
4
+ */
5
+ import { describe, expect, it } from "vitest";
6
+ import { parseSha256 } from "./sha256.ts";
7
+ import {
8
+ changesSince,
9
+ describeChanges,
10
+ serializeSourcesRecord,
11
+ type SourcesRecord,
12
+ } from "./sources-record.ts";
13
+
14
+ const A = "a".repeat(64);
15
+ const B = "b".repeat(64);
16
+
17
+ const RECORD: SourcesRecord = {
18
+ schema: 1,
19
+ inputs: [
20
+ { path: "paper.tex", role: "body" },
21
+ { path: "macros.tex", role: "preamble" },
22
+ { path: "sections/intro.tex", role: "body" },
23
+ ],
24
+ written: ["refs.bib"],
25
+ bibdata: ["refs"],
26
+ bibtex: {
27
+ ran: true,
28
+ databases: ["refs.bib"],
29
+ keys: ["k1"],
30
+ exit: 2,
31
+ errors: [{ message: "bad", file: "refs.bib", line: 4 }],
32
+ },
33
+ sha256: { "paper.tex": A, "macros.tex": null },
34
+ };
35
+
36
+ describe("serializing a record", () => {
37
+ it("is two-space JSON with a final newline, so a diff of two builds reads", () => {
38
+ expect(serializeSourcesRecord({ ...RECORD, bibtex: { ran: false } })).toBe(
39
+ `${JSON.stringify({ ...RECORD, bibtex: { ran: false } }, null, 2)}\n`,
40
+ );
41
+ });
42
+ });
43
+
44
+ describe("what changed since the record", () => {
45
+ const now = (files: Readonly<Record<string, string>>) => (p: string) =>
46
+ files[p] === undefined ? null : parseSha256(files[p]);
47
+
48
+ it("a record whose files are as hashed has no change, a file absent then and now included", () => {
49
+ expect(changesSince(RECORD, now({ "paper.tex": A }))).toEqual([]);
50
+ });
51
+
52
+ it("an edited file, a deleted one, and a file that appeared where there was none", () => {
53
+ expect(changesSince(RECORD, now({ "macros.tex": B }))).toEqual([
54
+ { path: "paper.tex", change: "deleted" },
55
+ { path: "macros.tex", change: "added" },
56
+ ]);
57
+ expect(changesSince(RECORD, now({ "paper.tex": B }))).toEqual([
58
+ { path: "paper.tex", change: "edited" },
59
+ ]);
60
+ });
61
+
62
+ it("names the changes in a line a finding can carry", () => {
63
+ expect(
64
+ describeChanges([
65
+ { path: "paper.tex", change: "edited" },
66
+ { path: "sections/a.tex", change: "deleted" },
67
+ ]),
68
+ ).toBe("paper.tex edited, sections/a.tex deleted");
69
+ expect(describeChanges([])).toBe("");
70
+ });
71
+ });
@@ -0,0 +1,136 @@
1
+ /**
2
+ * WHAT THE LAST BUILD READ — `<paper>/_build/sources.json`, written by `paperlint build` from TeX's
3
+ * own files (docs/design/paper-sources.md §1) and read, offline, by the rules that need to know which
4
+ * files make up a paper and which bibliography it uses. Nothing else answers either question: no
5
+ * code of paperlint reads TeX source to decide them.
6
+ *
7
+ * { "schema": 1,
8
+ * "inputs": [ { "path": "paper.tex", "role": "body" }, … ], the files of the paper directory TeX
9
+ * read, in first-read order
10
+ * "written": [ "refs.bib" ], the `.bib` and `.tex` files TeX wrote (filecontents)
11
+ * "bibdata": [ "refs" ], what `\bibdata` told bibtex to read
12
+ * "bibtex": { "ran": true, "databases": [ "refs.bib" ], the files bibtex opened, the keys it
13
+ * "keys": [ … ], "exit": 0, "errors": [] }, typeset, how it ended
14
+ * "sha256": { "paper.tex": "…", … } } every input and every database bibtex
15
+ * opened, by path and bytes
16
+ *
17
+ * 🔴 A role is the file's, and the main file is the body. A file is `preamble` when TeX read it only
18
+ * before `\begin{document}` — before it opened `paper.aux` for writing (measured) — and `body` when
19
+ * any read of it came after. The main file, the first one read, is `body` whatever its place in that
20
+ * order: it holds `\begin{document}` itself.
21
+ *
22
+ * The text of the file is checked once, where it enters: `SourcesCodec` (`src/ports/sources-codec.ts`),
23
+ * whose adapter holds the zod schema of this shape. The domain holds the value.
24
+ *
25
+ * 🔴 STALENESS is the record's own question: `changesSince` compares the bytes now with `sha256`, one
26
+ * key per file, so a rule can tell that the paper changed after the build without running TeX. A `.bib`
27
+ * TeX wrote is hashed too: the rules read it from disk, so an edit to it after the build is a change.
28
+ */
29
+ import type { Sha256 } from "./sha256.ts";
30
+
31
+ export const SOURCES_SCHEMA = 1;
32
+ export const SOURCES_DIR = "_build";
33
+ export const SOURCES_FILE = "sources.json";
34
+
35
+ /** The job name every file a build writes carries (`paper.aux`, `paper.fls`): the build names its job so. */
36
+ export const JOBNAME = "paper";
37
+
38
+ /** Before or after `\begin{document}`. */
39
+ export type InputRole = "preamble" | "body";
40
+
41
+ /** One file of the paper directory that TeX read. `path` is relative to it, with forward slashes. */
42
+ export interface RecordedInput {
43
+ readonly path: string;
44
+ readonly role: InputRole;
45
+ }
46
+
47
+ /** One error bibtex reported, as its `.blg` says it. */
48
+ export interface BibtexError {
49
+ readonly message: string;
50
+ /** The file it was reading (`refs.bib`, `paper.aux`). */
51
+ readonly file: string;
52
+ /** Null when bibtex named no line. */
53
+ readonly line: number | null;
54
+ }
55
+
56
+ /** What the build's bibtex did — or that the build ran none (the paper names no database). */
57
+ export type RecordedBibtex =
58
+ | { readonly ran: false }
59
+ | {
60
+ readonly ran: true;
61
+ /** The database files it opened, as it names them (`refs.bib`, `bibs/x.bib`). */
62
+ readonly databases: readonly string[];
63
+ /** The keys it typeset: every `\bibitem` of the `.bbl`. */
64
+ readonly keys: readonly string[];
65
+ readonly exit: number;
66
+ readonly errors: readonly BibtexError[];
67
+ };
68
+
69
+ export interface SourcesRecord {
70
+ readonly schema: typeof SOURCES_SCHEMA;
71
+ readonly inputs: readonly RecordedInput[];
72
+ readonly written: readonly string[];
73
+ readonly bibdata: readonly string[];
74
+ readonly bibtex: RecordedBibtex;
75
+ /** Path → digest of its bytes; null for a file that was not there when the record was made. */
76
+ readonly sha256: Readonly<Record<string, string | null>>;
77
+ }
78
+
79
+ export type ParsedRecord =
80
+ | { readonly ok: true; readonly record: SourcesRecord }
81
+ | { readonly ok: false; readonly why: string };
82
+
83
+ /** The record as the file holds it: two-space JSON and a final newline, so a diff of two builds reads. */
84
+ export const serializeSourcesRecord = (r: SourcesRecord): string =>
85
+ `${JSON.stringify(r, null, 2)}\n`;
86
+
87
+ // ── staleness ───────────────────────────────────────────────────────────────────────────
88
+
89
+ export type ChangeKind = "edited" | "deleted" | "added";
90
+
91
+ /** One file whose bytes are not those the build hashed. */
92
+ export interface Change {
93
+ readonly path: string;
94
+ readonly change: ChangeKind;
95
+ }
96
+
97
+ /**
98
+ * The files whose bytes now differ from the record's: edited, deleted, or present where the record saw
99
+ * none. `now` answers for one path with the digest of its bytes, or null when there is no such file.
100
+ * Empty when the record is current.
101
+ */
102
+ export function changesSince(
103
+ r: SourcesRecord,
104
+ now: (path: string) => Sha256 | null,
105
+ ): readonly Change[] {
106
+ return Object.entries(r.sha256).flatMap(
107
+ ([path, recorded]): readonly Change[] => {
108
+ const current = now(path);
109
+ if (current === recorded) return [];
110
+ if (current === null) return [{ path, change: "deleted" }];
111
+ return [{ path, change: recorded === null ? "added" : "edited" }];
112
+ },
113
+ );
114
+ }
115
+
116
+ /** `paper.tex edited, sections/a.tex deleted` — what a finding names. */
117
+ export const describeChanges = (cs: readonly Change[]): string =>
118
+ cs.map((c) => `${c.path} ${c.change}`).join(", ");
119
+
120
+ /**
121
+ * The name among `names` that `name` stands for: `name` itself, else the one name equal to it ignoring
122
+ * case — null when there is none, or several. TeX opens a file by the name the source gives it; on a
123
+ * file system that ignores case that is the source's spelling, on one that does not TeX retries
124
+ * without case, and the record holds the disk's spelling either way. A name and the record's spelling
125
+ * of it are matched by this, once, wherever one is looked up in the other.
126
+ */
127
+ export function spelledIn(
128
+ names: readonly string[],
129
+ name: string,
130
+ ): string | null {
131
+ if (names.includes(name)) return name;
132
+ const [only, ...more] = names.filter(
133
+ (n) => n.toLowerCase() === name.toLowerCase(),
134
+ );
135
+ return only !== undefined && more.length === 0 ? only : null;
136
+ }
@@ -0,0 +1,228 @@
1
+ /**
2
+ * Which files of the paper directory a build's TeX passes read, and which `.bib` files they wrote —
3
+ * decided from the `.fls` lines of each pass. The inputs are `.fls` files captured whole from real
4
+ * runs of the planted papers (test/fixtures/tex-output/README.md); the few shapes no planted paper
5
+ * has are written out, and say so.
6
+ */
7
+ import { readFileSync } from "node:fs";
8
+ import { dirname, join } from "node:path";
9
+ import { fileURLToPath } from "node:url";
10
+ import { describe, expect, it } from "vitest";
11
+ import { texOutput } from "../adapters/tex-output/index.ts";
12
+ import { filesOfRun } from "./tex-run.ts";
13
+
14
+ const CAPTURES = join(
15
+ dirname(dirname(dirname(fileURLToPath(import.meta.url)))),
16
+ "test",
17
+ "fixtures",
18
+ "tex-output",
19
+ );
20
+ /** A planted paper's passes, as `.fls` text → lines, in the order they ran. */
21
+ const passes = (paper: string) =>
22
+ ["first-pass.fls", "paper.fls"].map((f) =>
23
+ texOutput.fls(readFileSync(join(CAPTURES, paper, f), "utf8")),
24
+ );
25
+ const BIBTEX_RAN = { jobname: "paper", generated: ["paper.bbl"] };
26
+
27
+ describe("the files a build's TeX read", () => {
28
+ it("a paper of one file: the main file, a body", () => {
29
+ expect(filesOfRun(passes("v3-declared"), BIBTEX_RAN)).toEqual({
30
+ inputs: [{ path: "paper.tex", role: "body" }],
31
+ written: [],
32
+ });
33
+ });
34
+
35
+ it("an include read after \\begin{document} is body, a database TeX read before it is preamble — in first-read order", () => {
36
+ expect(filesOfRun(passes("p1"), BIBTEX_RAN)).toEqual({
37
+ inputs: [
38
+ { path: "paper.tex", role: "body" },
39
+ { path: "refs.bib", role: "preamble" },
40
+ { path: "sections/intro.tex", role: "body" },
41
+ ],
42
+ written: [],
43
+ });
44
+ });
45
+
46
+ it("a file read before \\begin{document} and never after is preamble", () => {
47
+ expect(filesOfRun(passes("v16-included-block"), BIBTEX_RAN)).toEqual({
48
+ inputs: [
49
+ { path: "paper.tex", role: "body" },
50
+ { path: "bibblock.tex", role: "preamble" },
51
+ { path: "refs.bib", role: "preamble" },
52
+ ],
53
+ written: [],
54
+ });
55
+ });
56
+ });
57
+
58
+ describe("the .bib files TeX wrote", () => {
59
+ it("a .bib TeX wrote (filecontents) is written, and is no input: its bytes are the block's", () => {
60
+ expect(filesOfRun(passes("v8-jobname"), BIBTEX_RAN)).toEqual({
61
+ inputs: [{ path: "paper.tex", role: "body" }],
62
+ written: ["paper.bib"],
63
+ });
64
+ });
65
+
66
+ it("🔴 a block writes its file on the first pass only (the later passes find it there and read it): the last pass alone would list it as an input and not as written", () => {
67
+ const [first, last] = passes("plain-block");
68
+ expect(last?.lines.filter((l) => l.path.endsWith(".bib"))).toEqual([
69
+ { op: "INPUT", path: "./refs.bib" },
70
+ { op: "INPUT", path: "refs.bib" },
71
+ ]);
72
+ expect(filesOfRun(passes("plain-block"), BIBTEX_RAN)).toEqual({
73
+ inputs: [{ path: "paper.tex", role: "body" }],
74
+ written: ["refs.bib"],
75
+ });
76
+ expect(first?.lines.filter((l) => l.path.endsWith(".bib"))).toEqual([
77
+ { op: "OUTPUT", path: "refs.bib" },
78
+ ]);
79
+ });
80
+ });
81
+
82
+ describe("a .tex TeX wrote and then read — the paper's text, generated", () => {
83
+ it("🔴 a block that writes a .tex the body \\input's: the file is written AND an input, a body file — its text is in the PDF", () => {
84
+ // v29-generated-input: pass 1 writes body.tex and reads it after \begin{document}; the later
85
+ // passes find it there, read it in the preamble (the block's check) and again in the body.
86
+ const [first, last] = passes("v29-generated-input");
87
+ expect(first?.lines.filter((l) => l.path.endsWith("body.tex"))[0]).toEqual({
88
+ op: "OUTPUT",
89
+ path: "body.tex",
90
+ });
91
+ expect(
92
+ last?.lines.some((l) => l.op === "OUTPUT" && l.path === "body.tex"),
93
+ ).toBe(false);
94
+ expect(filesOfRun(passes("v29-generated-input"), BIBTEX_RAN)).toEqual({
95
+ inputs: [
96
+ { path: "paper.tex", role: "body" },
97
+ { path: "body.tex", role: "body" },
98
+ ],
99
+ written: ["body.tex"],
100
+ });
101
+ });
102
+
103
+ it("with [overwrite] the last pass writes it again before reading it: still an input", () => {
104
+ const fls = texOutput.fls(
105
+ [
106
+ "PWD /work/paper",
107
+ "INPUT paper.tex",
108
+ "INPUT ./body.tex",
109
+ "OUTPUT body.tex",
110
+ "OUTPUT paper.aux",
111
+ "INPUT ./body.tex",
112
+ ].join("\n"),
113
+ );
114
+ expect(filesOfRun([fls], BIBTEX_RAN)).toEqual({
115
+ inputs: [
116
+ { path: "paper.tex", role: "body" },
117
+ { path: "body.tex", role: "body" },
118
+ ],
119
+ written: ["body.tex"],
120
+ });
121
+ });
122
+ });
123
+
124
+ describe("what is not the paper's: its TeX tree, a sibling directory, what TeX writes", () => {
125
+ it("the .bbl bibtex wrote is no input of the paper; with no bibtex run it is a file TeX read, and so is one", () => {
126
+ const last = passes("v3-declared");
127
+ expect(
128
+ filesOfRun(last, { jobname: "paper", generated: [] }).inputs,
129
+ ).toEqual([
130
+ { path: "paper.tex", role: "body" },
131
+ { path: "paper.bbl", role: "body" },
132
+ ]);
133
+ });
134
+
135
+ it("files the paper's TeX tree supplies are not the paper's: only the paper directory's files are listed", () => {
136
+ const fls = texOutput.fls(
137
+ [
138
+ "PWD /work/paper",
139
+ "INPUT /usr/share/texlive/texmf-dist/tex/latex/base/article.cls",
140
+ "INPUT paper.tex",
141
+ "INPUT /work/paper/inside.tex",
142
+ "INPUT ../shared/outside.tex",
143
+ "INPUT /elsewhere/other.tex",
144
+ "OUTPUT paper.aux",
145
+ ].join("\n"),
146
+ );
147
+ expect(
148
+ filesOfRun([fls], { jobname: "paper", generated: [] }).inputs,
149
+ ).toEqual([
150
+ { path: "paper.tex", role: "body" },
151
+ { path: "inside.tex", role: "preamble" },
152
+ ]);
153
+ });
154
+ });
155
+
156
+ describe("an .fls with no directory, and a name that merely starts with dots", () => {
157
+ it("an .fls that names no directory lists the files named relative to it and no absolute path", () => {
158
+ const fls = texOutput.fls(
159
+ "INPUT /usr/share/texlive/article.cls\nINPUT paper.tex\nINPUT ..x.tex\n",
160
+ );
161
+ expect(
162
+ filesOfRun([fls], { jobname: "paper", generated: [] }).inputs,
163
+ ).toEqual([
164
+ { path: "paper.tex", role: "body" },
165
+ { path: "..x.tex", role: "preamble" },
166
+ ]);
167
+ });
168
+ });
169
+
170
+ describe("roles on the edges: both places, no body, no passes", () => {
171
+ it("a file read in the preamble and again in the body is body: its first read alone would drop it from the prose", () => {
172
+ const fls = texOutput.fls(
173
+ [
174
+ "PWD /work/paper",
175
+ "INPUT paper.tex",
176
+ "INPUT ./macros.tex",
177
+ "OUTPUT paper.aux",
178
+ "INPUT ./macros.tex",
179
+ "INPUT ./intro.tex",
180
+ ].join("\n"),
181
+ );
182
+ expect(
183
+ filesOfRun([fls], { jobname: "paper", generated: [] }).inputs,
184
+ ).toEqual([
185
+ { path: "paper.tex", role: "body" },
186
+ { path: "macros.tex", role: "body" },
187
+ { path: "intro.tex", role: "body" },
188
+ ]);
189
+ });
190
+
191
+ it("a run that stopped before opening the .aux has no body: everything but the main file is preamble", () => {
192
+ const fls = texOutput.fls(
193
+ "PWD /work/paper\nINPUT paper.tex\nINPUT ./a.tex\nINPUT ./b.tex\n",
194
+ );
195
+ expect(
196
+ filesOfRun([fls], { jobname: "paper", generated: [] }).inputs,
197
+ ).toEqual([
198
+ { path: "paper.tex", role: "body" },
199
+ { path: "a.tex", role: "preamble" },
200
+ { path: "b.tex", role: "preamble" },
201
+ ]);
202
+ });
203
+
204
+ it("a file TeX wrote on any pass is not an input, even one it also reads on a later pass (.toc, .out)", () => {
205
+ const first = texOutput.fls(
206
+ "PWD /w\nINPUT paper.tex\nOUTPUT paper.aux\nOUTPUT paper.toc\n",
207
+ );
208
+ const second = texOutput.fls(
209
+ "PWD /w\nINPUT paper.tex\nINPUT ./paper.toc\nOUTPUT paper.aux\n",
210
+ );
211
+ expect(
212
+ filesOfRun([first, second], { jobname: "paper", generated: [] }).inputs,
213
+ ).toEqual([{ path: "paper.tex", role: "body" }]);
214
+ });
215
+
216
+ it("no passes, or passes that opened nothing of the paper, list nothing", () => {
217
+ expect(filesOfRun([], { jobname: "paper", generated: [] })).toEqual({
218
+ inputs: [],
219
+ written: [],
220
+ });
221
+ expect(
222
+ filesOfRun([texOutput.fls("PWD /w\nINPUT /usr/x.cls\n")], {
223
+ jobname: "paper",
224
+ generated: [],
225
+ }),
226
+ ).toEqual({ inputs: [], written: [] });
227
+ });
228
+ });
@@ -0,0 +1,139 @@
1
+ /**
2
+ * WHICH FILES A BUILD'S TeX PASSES READ — decided from the lines of each pass's `.fls` (pdflatex
3
+ * `-recorder`), not from any TeX source (docs/design/paper-sources.md §1).
4
+ *
5
+ * inputs the files of the paper directory the LAST pass read, in the order it first read them,
6
+ * each a `preamble` or `body` file: before or after TeX opened `<jobname>.aux` for writing,
7
+ * which `\begin{document}` does (measured). The main file, read first, is `body`.
8
+ * written the `.bib` and `.tex` files ANY pass wrote — a `filecontents` block's.
9
+ *
10
+ * 🔴 WHY THE LAST PASS FOR INPUTS AND EVERY PASS FOR WHAT WAS WRITTEN. A block without `[overwrite]`
11
+ * writes its file on the first pass only; the later passes find it there and read it (measured on TeX
12
+ * Live 2026: pass 1 `OUTPUT refs.bib`, passes 2 and 3 `INPUT refs.bib`, no OUTPUT). The last pass alone
13
+ * would list TeX's own copy of a `.bib` as a file of the paper. And the first pass reads no `.aux`,
14
+ * `.toc` or `.out` yet, so only a later pass lists what the paper reads and the passes after it write.
15
+ *
16
+ * 🔴 A FILE TeX WROTE IS AN INPUT ONLY WHEN IT IS TeX SOURCE OF THE PAPER: a `.tex`. A block that
17
+ * writes `body.tex` the body `\input`s puts that text in the PDF (measured: pass 1 `OUTPUT body.tex`
18
+ * then `INPUT ./body.tex` after `\begin{document}`; the later passes read it again), so it is an
19
+ * input like any other: hashed, spliced where its include stands. Every other file a pass wrote is
20
+ * TeX's bookkeeping or a database — its `.aux`, `.out` and `.toc`, a block's `.bib` — and is no input.
21
+ * A file another program wrote from TeX's output (`generated`: the `.bbl`, when bibtex ran) is none
22
+ * either.
23
+ */
24
+ import { isAbsolute, posix } from "node:path";
25
+ import type { RecordedInput } from "./sources-record.ts";
26
+
27
+ /** One line of an `.fls`: TeX opened a file to read it, or to write it. */
28
+ export interface FlsLine {
29
+ readonly op: "INPUT" | "OUTPUT";
30
+ /** As TeX named the file: `./a.tex` and `a.tex` for one file, an absolute path for the TeX tree's. */
31
+ readonly path: string;
32
+ }
33
+
34
+ /** A pdflatex `-recorder` file: where it ran, and every file it opened, in order. */
35
+ export interface Fls {
36
+ /** The directory pdflatex ran in, or null when the file names none. */
37
+ readonly pwd: string | null;
38
+ readonly lines: readonly FlsLine[];
39
+ }
40
+
41
+ export interface RunFiles {
42
+ readonly inputs: readonly RecordedInput[];
43
+ readonly written: readonly string[];
44
+ }
45
+
46
+ /**
47
+ * `path` as TeX named it → relative to the paper directory with forward slashes, or null when the file
48
+ * is not in it: the TeX tree's, a file of a sibling directory, an absolute path outside `pwd`.
49
+ */
50
+ function inPaperDir(path: string, pwd: string | null): string | null {
51
+ const rel = isAbsolute(path)
52
+ ? pwd === null
53
+ ? null
54
+ : posix.relative(pwd, path)
55
+ : posix.normalize(path);
56
+ // `..` or `../x`: a file of a sibling directory. (`..x` is a file of this one.)
57
+ return rel === null || rel.split("/")[0] === ".." ? null : rel;
58
+ }
59
+
60
+ const distinct = <T>(xs: readonly T[]): readonly T[] => [...new Set(xs)];
61
+
62
+ /** A file TeX writes that the record names: TeX source of the paper (`.tex`) or a database (`.bib`). */
63
+ const isTexSource = (path: string): boolean => posix.extname(path) === ".tex";
64
+ const isRecordedWrite = (path: string): boolean =>
65
+ isTexSource(path) || posix.extname(path) === ".bib";
66
+
67
+ type Opened = FlsLine & { readonly rel: string };
68
+
69
+ /** The files of the paper directory a pass opened, with the op, in order. */
70
+ const opened = (pass: Fls): readonly Opened[] =>
71
+ pass.lines.flatMap((l) => {
72
+ const rel = inPaperDir(l.path, pass.pwd);
73
+ return rel === null ? [] : [{ ...l, rel }];
74
+ });
75
+
76
+ export interface RunOptions {
77
+ /** The job name: `<jobname>.aux` is the file whose opening marks `\begin{document}`. */
78
+ readonly jobname: string;
79
+ /** Files other programs wrote from TeX's own output, relative to the paper directory (`paper.bbl`). */
80
+ readonly generated: readonly string[];
81
+ }
82
+
83
+ /** One read of a file of the paper, and whether it came after `\begin{document}`. */
84
+ interface Read {
85
+ readonly path: string;
86
+ readonly afterBegin: boolean;
87
+ }
88
+
89
+ /**
90
+ * The reads of the last pass: INPUT lines of files nothing wrote, or that a pass wrote as TeX source,
91
+ * each with its place against the line that opens `<jobname>.aux` — `\begin{document}`. No such line
92
+ * (the run stopped first): none is after it.
93
+ */
94
+ function readsOf(
95
+ last: readonly Opened[],
96
+ outputs: ReadonlySet<string>,
97
+ { jobname, generated }: RunOptions,
98
+ ): readonly Read[] {
99
+ const boundary = last.findIndex(
100
+ (l) => l.op === "OUTPUT" && l.rel === `${jobname}.aux`,
101
+ );
102
+ return last.flatMap((l, i) =>
103
+ l.op === "INPUT" &&
104
+ (!outputs.has(l.rel) || isTexSource(l.rel)) &&
105
+ !generated.includes(l.rel)
106
+ ? [{ path: l.rel, afterBegin: boundary >= 0 && i > boundary }]
107
+ : [],
108
+ );
109
+ }
110
+
111
+ export function filesOfRun(
112
+ passes: readonly Fls[],
113
+ options: RunOptions,
114
+ ): RunFiles {
115
+ const everyPass = passes.flatMap(opened);
116
+ const wrote = (l: Opened): boolean => l.op === "OUTPUT";
117
+ const outputs = new Set(everyPass.filter(wrote).map((l) => l.rel));
118
+ const last = passes.at(-1);
119
+ const reads = readsOf(
120
+ last === undefined ? [] : opened(last),
121
+ outputs,
122
+ options,
123
+ );
124
+ // The main file holds `\begin{document}` itself, so it is body wherever TeX opened it.
125
+ const body = (path: string): boolean =>
126
+ path === reads[0]?.path ||
127
+ reads.some((r) => r.path === path && r.afterBegin);
128
+ return {
129
+ inputs: distinct(reads.map((r) => r.path)).map((path): RecordedInput => ({
130
+ path,
131
+ role: body(path) ? "body" : "preamble",
132
+ })),
133
+ written: distinct(
134
+ everyPass
135
+ .filter((l) => wrote(l) && isRecordedWrite(l.rel))
136
+ .map((l) => l.rel),
137
+ ),
138
+ };
139
+ }