paperlint 18.1.0 → 18.2.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 (93) hide show
  1. package/README.md +2 -2
  2. package/dist/adapters/latex/document-class.d.ts +28 -5
  3. package/dist/adapters/latex/document-class.d.ts.map +1 -1
  4. package/dist/adapters/latex/document-class.js +208 -21
  5. package/dist/adapters/latex/document-class.js.map +1 -1
  6. package/dist/adapters/latex/index.d.ts +4 -4
  7. package/dist/adapters/latex/index.d.ts.map +1 -1
  8. package/dist/adapters/latex/index.js +5 -2
  9. package/dist/adapters/latex/index.js.map +1 -1
  10. package/dist/adapters/latex/parse.d.ts.map +1 -1
  11. package/dist/adapters/latex/parse.js +5 -0
  12. package/dist/adapters/latex/parse.js.map +1 -1
  13. package/dist/domain/body-pages.d.ts.map +1 -1
  14. package/dist/domain/body-pages.js +4 -2
  15. package/dist/domain/body-pages.js.map +1 -1
  16. package/dist/domain/tex-document.d.ts +52 -4
  17. package/dist/domain/tex-document.d.ts.map +1 -1
  18. package/dist/domain/tex-document.js +9 -0
  19. package/dist/domain/tex-document.js.map +1 -1
  20. package/dist/eslint-rules/latex-language.d.ts +10 -0
  21. package/dist/eslint-rules/latex-language.d.ts.map +1 -1
  22. package/dist/eslint-rules/latex-language.js +41 -2
  23. package/dist/eslint-rules/latex-language.js.map +1 -1
  24. package/dist/ports/latex.d.ts +13 -1
  25. package/dist/ports/latex.d.ts.map +1 -1
  26. package/dist/presets.d.ts +15 -0
  27. package/dist/presets.d.ts.map +1 -1
  28. package/dist/presets.js +9 -0
  29. package/dist/presets.js.map +1 -1
  30. package/dist/tex-requirements.d.ts +2 -0
  31. package/dist/tex-requirements.d.ts.map +1 -1
  32. package/dist/tex-requirements.js +7 -2
  33. package/dist/tex-requirements.js.map +1 -1
  34. package/dist/tex-venue-rules.d.ts +36 -3
  35. package/dist/tex-venue-rules.d.ts.map +1 -1
  36. package/dist/tex-venue-rules.js +181 -33
  37. package/dist/tex-venue-rules.js.map +1 -1
  38. package/docs/rules/cycle/port-done.md +3 -0
  39. package/docs/rules/tex/template.md +96 -1
  40. package/docs/rules.md +18 -13
  41. package/docs/venues.md +51 -12
  42. package/eslint-rules/latex-language.test.mjs +64 -1
  43. package/eslint-rules/latex-language.ts +44 -2
  44. package/eslint.config.mjs +1 -0
  45. package/fixtures/build-e2e/neurips/PIPELINE-STATUS.md +3 -0
  46. package/fixtures/build-e2e/neurips/expect.json +20 -0
  47. package/fixtures/build-e2e/neurips/paper.tex +60 -0
  48. package/fixtures/build-e2e/neurips/paperlint.json +1 -0
  49. package/fixtures/build-e2e/neurips-nostyle/PIPELINE-STATUS.md +3 -0
  50. package/fixtures/build-e2e/neurips-nostyle/expect.json +26 -0
  51. package/fixtures/build-e2e/neurips-nostyle/paper.tex +56 -0
  52. package/fixtures/build-e2e/neurips-nostyle/paperlint.json +1 -0
  53. package/fixtures/build-e2e/neurips-over/PIPELINE-STATUS.md +3 -0
  54. package/fixtures/build-e2e/neurips-over/expect.json +24 -0
  55. package/fixtures/build-e2e/neurips-over/paper.tex +56 -0
  56. package/fixtures/build-e2e/neurips-over/paperlint.json +1 -0
  57. package/fixtures/real-papers.test.ts +17 -6
  58. package/package.json +3 -1
  59. package/presets/aaai-2027.jsonc +126 -0
  60. package/presets/aaai-2027.md +127 -0
  61. package/presets/aaai-press.jsonc +167 -0
  62. package/presets/aaai-press.md +126 -0
  63. package/presets/icml-2027.jsonc +111 -0
  64. package/presets/icml-2027.md +130 -0
  65. package/presets/icml-style.jsonc +125 -0
  66. package/presets/icml-style.md +77 -0
  67. package/presets/neurips-2026.jsonc +123 -0
  68. package/presets/neurips-2026.md +137 -0
  69. package/presets/neurips-style.jsonc +113 -0
  70. package/presets/neurips-style.md +85 -0
  71. package/presets/venue-profile.schema.json +6 -0
  72. package/skills/build-label-page/scripts/label-page-browser.test.ts +275 -114
  73. package/src/adapters/latex/document-class.ts +292 -20
  74. package/src/adapters/latex/index.test.ts +263 -0
  75. package/src/adapters/latex/index.ts +12 -3
  76. package/src/adapters/latex/parse.ts +5 -0
  77. package/src/cli.test.ts +6 -1
  78. package/src/domain/body-pages.test.ts +22 -6
  79. package/src/domain/body-pages.ts +4 -2
  80. package/src/domain/tex-document.ts +64 -5
  81. package/src/folder-venue-rule.test.ts +51 -0
  82. package/src/new-paper.test.ts +2 -2
  83. package/src/paper-settings-commands.test.ts +1 -1
  84. package/src/ports/latex.ts +14 -0
  85. package/src/preset-card-rule.test.ts +44 -27
  86. package/src/presets.test.ts +177 -1
  87. package/src/presets.ts +27 -0
  88. package/src/rule-cases.test.ts +52 -0
  89. package/src/tex-requirements.harness.ts +17 -3
  90. package/src/tex-requirements.ts +13 -2
  91. package/src/tex-venue-rules.test.ts +1020 -0
  92. package/src/tex-venue-rules.ts +248 -38
  93. package/src/venue-rules.test.ts +369 -2
@@ -1,21 +1,29 @@
1
1
  /** The paper's `\documentclass`, a preset's template, and setting a class in a source. */
2
2
  import {
3
3
  documentClassLine,
4
+ optionsInEffect,
5
+ readWhole,
4
6
  venueClass,
5
7
  type ClassCandidate,
6
8
  type ClassLine,
7
9
  type DocumentClass,
10
+ type PackageLoad,
11
+ type PassedOptions,
12
+ type StylePackage,
8
13
  } from "../../domain/tex-document.ts";
14
+ import { documentBodyOf, includesOf } from "./includes.ts";
9
15
  import {
10
16
  collapse,
17
+ isNode,
11
18
  macroPlace,
12
19
  mandatory,
13
20
  optional,
14
21
  textOf,
22
+ visited,
15
23
  type Node,
16
24
  type Macro,
17
25
  } from "./nodes.ts";
18
- import { parseLatex, type ParsedTex } from "./parse.ts";
26
+ import { liveRoot, parseLatex, type ParsedTex } from "./parse.ts";
19
27
 
20
28
  const isDocumentClass = (n: Node): n is Macro =>
21
29
  n.type === "macro" && n.content === "documentclass";
@@ -53,31 +61,110 @@ const optionText = (src: string, part: readonly Node[]): string =>
53
61
  .join(""),
54
62
  );
55
63
 
64
+ /** A node at the top of the tree, with its index there: what stands before what. */
65
+ interface AtTop {
66
+ readonly node: Macro;
67
+ readonly index: number;
68
+ }
69
+
56
70
  /** Every `\documentclass` at the top of the tree, in source order. */
57
- const classNodes = (t: ParsedTex): readonly Macro[] =>
58
- t.root.content.filter(isDocumentClass);
71
+ const classNodes = (t: ParsedTex): readonly AtTop[] =>
72
+ t.root.content.flatMap((node, index) =>
73
+ isDocumentClass(node) ? [{ node, index }] : [],
74
+ );
75
+
76
+ /** Whether nodes hold a macro, inside braces too: what TeX reads there is the macro's expansion. */
77
+ const holdsMacro = (nodes: readonly Node[]): boolean =>
78
+ nodes.some(
79
+ (n) => n.type === "macro" || (n.type === "group" && holdsMacro(n.content)),
80
+ );
81
+
82
+ /** A list of options (`a, b={c,d}`), each as written, and the ones of them built by a macro. */
83
+ function optionList(
84
+ src: string,
85
+ nodes: readonly Node[],
86
+ ): Pick<PassedOptions, "options" | "unread"> {
87
+ const parts = splitWhere(nodes, isComma)
88
+ .map((part) => ({ text: optionText(src, part), unread: holdsMacro(part) }))
89
+ .filter((o) => o.text !== "");
90
+ return {
91
+ options: parts.map((o) => o.text),
92
+ unread: parts.filter((o) => o.unread).map((o) => o.text),
93
+ };
94
+ }
59
95
 
60
- /** A `\documentclass` node as a class line: empty, or the class with its options as read. */
61
- function classLine(src: string, node: Macro): ClassCandidate {
96
+ /** One `\PassOptionsToClass{options}{class}`: the class it names, its options, its index at the top. */
97
+ interface Passed extends PassedOptions {
98
+ readonly cls: string;
99
+ readonly index: number;
100
+ }
101
+
102
+ /**
103
+ * Every `\PassOptionsToClass` at the top of the tree: the options it gives a class count for a
104
+ * `\documentclass` of that class after it, as LaTeX reads them. One missing an argument passes
105
+ * nothing to any class.
106
+ */
107
+ const passedOf = (t: ParsedTex): readonly Passed[] =>
108
+ t.root.content.flatMap((n, index) => {
109
+ if (n.type !== "macro" || n.content !== "PassOptionsToClass") return [];
110
+ const [opts, cls] = mandatory(n);
111
+ if (opts === undefined || cls === undefined) return [];
112
+ const list = optionList(t.src, opts.content);
113
+ const name = collapse(textOf(cls.content));
114
+ return [{ ...list, cls: name, place: macroPlace(n), index }];
115
+ });
116
+
117
+ /**
118
+ * A `\documentclass` node as a class line: empty; a class built by a macro, unread; or the class
119
+ * with its options as read, and the options `passed` to that class before the line.
120
+ */
121
+ function classLine(
122
+ src: string,
123
+ { node, index }: AtTop,
124
+ passed: readonly Passed[],
125
+ ): ClassCandidate {
62
126
  const place = macroPlace(node);
63
- const cls = collapse(textOf(mandatory(node)[0]?.content));
127
+ const name = mandatory(node)[0]?.content;
128
+ if (name !== undefined && holdsMacro(name)) return { kind: "unread", place };
129
+ const cls = collapse(textOf(name));
64
130
  if (cls === "") return { kind: "empty", place };
65
- const options = splitWhere(optional(node)?.content ?? [], isComma)
66
- .map((part) => optionText(src, part))
67
- .filter(Boolean);
68
- return { kind: "class", cls, options, place };
131
+ const before = passed
132
+ .filter((p) => p.cls === cls && p.index < index)
133
+ .map(({ options, unread, place: where }) => ({
134
+ options,
135
+ unread,
136
+ place: where,
137
+ }));
138
+ const own = optionList(src, optional(node)?.content ?? []);
139
+ return { kind: "class", cls, ...own, passed: before, place };
140
+ }
141
+
142
+ /** A `\documentclass` anywhere in the tree: in another macro's argument, a definition's body. */
143
+ const classAnywhere = (t: ParsedTex): boolean =>
144
+ visited(t.root, () => false).some((v) => isNode(v) && isDocumentClass(v));
145
+
146
+ /** An `\input` or `\include` in the preamble, left as written: a file this reader was not given. */
147
+ function includesInPreamble(t: ParsedTex): boolean {
148
+ const body = documentBodyOf(t)?.start ?? Number.POSITIVE_INFINITY;
149
+ return includesOf(t).some((i) => i.span.start < body);
69
150
  }
70
151
 
71
152
  /**
72
- * The `\documentclass` at the top of the tree: missing; empty (`\documentclass{}`); the class with
73
- * each of its options as written (`foo={a,b}` stays one option, braces kept), whitespace collapsed,
74
- * its place spanning the macro and its arguments; or, when there are several (behind a TeX switch),
75
- * each of them as a candidate.
153
+ * The `\documentclass` at the top of the tree: missing; unseen, when there is none at the top but
154
+ * one may stand where this reader does not look (`classAnywhere`, `includesInPreamble`); empty
155
+ * (`\documentclass{}`); unread (`\documentclass{\cls}`); the class with each of its options as
156
+ * written (`foo={a,b}` stays one option, braces kept), whitespace collapsed, its place spanning the
157
+ * macro and its arguments; or, when there are several (behind a TeX switch), each of them as a
158
+ * candidate.
76
159
  */
77
160
  export function documentClassOf(t: ParsedTex): ClassLine {
78
- const lines = classNodes(t).map((n) => classLine(t.src, n));
161
+ const passed = passedOf(t);
162
+ const lines = classNodes(t).map((n) => classLine(t.src, n, passed));
79
163
  const [first, second, ...rest] = lines;
80
- if (first === undefined) return { kind: "missing" };
164
+ if (first === undefined)
165
+ return classAnywhere(t) || includesInPreamble(t)
166
+ ? { kind: "unseen" }
167
+ : { kind: "missing" };
81
168
  return second === undefined
82
169
  ? first
83
170
  : { kind: "ambiguous", candidates: [first, second, ...rest] };
@@ -98,6 +185,188 @@ export function parseTemplate(text: string): DocumentClass | null {
98
185
  return only.type === "string" ? { cls: only.content, options: [] } : null;
99
186
  }
100
187
 
188
+ const isPackageLine = (n: Node): n is Macro =>
189
+ n.type === "macro" &&
190
+ (n.content === "usepackage" ||
191
+ n.content === "RequirePackage" ||
192
+ n.content === "RequirePackageWithOptions");
193
+
194
+ /** A comma list of names (`a, b`), each whitespace-collapsed, empty ones left out. */
195
+ const namesIn = (nodes: readonly Node[] | undefined): readonly string[] =>
196
+ splitWhere(nodes ?? [], isComma)
197
+ .map((part) => collapse(textOf(part)))
198
+ .filter(Boolean);
199
+
200
+ /** Options as LaTeX hands them on: each as written, and the ones built by a macro. */
201
+ type Options = Pick<PassedOptions, "options" | "unread">;
202
+
203
+ const NO_OPTIONS: Options = { options: [], unread: [] };
204
+
205
+ /** Several lists of options, one after the other. */
206
+ const joined = (lists: readonly Options[]): Options => ({
207
+ options: lists.flatMap((l) => l.options),
208
+ unread: lists.flatMap((l) => l.unread),
209
+ });
210
+
211
+ /** One `\PassOptionsToPackage{options}{packages}`: the packages it names, its options, its index. */
212
+ interface PassedToPackage extends Options {
213
+ readonly packages: readonly string[];
214
+ readonly index: number;
215
+ }
216
+
217
+ /**
218
+ * Every `\PassOptionsToPackage` at the top of the tree, none after `\end{document}` (`liveRoot`):
219
+ * LaTeX adds its options to each package's own list (`\@pass@ptions`, latex.ltx), so they count for
220
+ * a load of one of its packages after it. One missing an argument passes nothing.
221
+ */
222
+ const passedToPackagesOf = (t: ParsedTex): readonly PassedToPackage[] =>
223
+ liveRoot(t).content.flatMap((n, index) => {
224
+ if (n.type !== "macro" || n.content !== "PassOptionsToPackage") return [];
225
+ const [opts, pkgs] = mandatory(n);
226
+ if (opts === undefined || pkgs === undefined) return [];
227
+ const list = optionList(t.src, opts.content);
228
+ return [{ ...list, packages: namesIn(pkgs.content), index }];
229
+ });
230
+
231
+ /**
232
+ * The global options in effect at `index`: those of the `\documentclass` lines before it. LaTeX
233
+ * keeps the class line's own options as `\@classoptionslist`, which every package's
234
+ * `\ProcessOptions` reads; options given with `\PassOptionsToClass` are the class's alone. Several
235
+ * lines behind a TeX switch: each one's, as any of them may be the one that builds.
236
+ */
237
+ const globalAt = (t: ParsedTex, index: number): Options =>
238
+ joined(
239
+ classNodes(t)
240
+ .filter((c) => c.index < index)
241
+ .map((c) => optionList(t.src, optional(c.node)?.content ?? [])),
242
+ );
243
+
244
+ /**
245
+ * One package line, at `index` at the top: each package it names, with the options LaTeX hands it
246
+ * — the global ones, then those passed to it before the line, then the line's own — and its place.
247
+ * `\RequirePackageWithOptions` gives the package the options of the file that loads it instead of
248
+ * its own list: in a paper's preamble, none, so the global ones alone.
249
+ */
250
+ function packageLoads(
251
+ src: string,
252
+ { node, index }: AtTop,
253
+ {
254
+ passed,
255
+ global,
256
+ }: { readonly passed: readonly PassedToPackage[]; readonly global: Options },
257
+ ): readonly PackageLoad[] {
258
+ const place = macroPlace(node);
259
+ const withOptions = node.content === "RequirePackageWithOptions";
260
+ const own = withOptions
261
+ ? NO_OPTIONS
262
+ : optionList(src, optional(node)?.content ?? []);
263
+ const before = (name: string): readonly Options[] =>
264
+ withOptions
265
+ ? []
266
+ : passed.filter((p) => p.index < index && p.packages.includes(name));
267
+ // A package loaded from a folder (`sty/aaai2027`) is the package of its file's base name: LaTeX
268
+ // parses the path and names the package by the base (`\@onefilewithoptions`, latex.ltx).
269
+ return namesIn(mandatory(node)[0]?.content)
270
+ .map((path) => path.slice(path.lastIndexOf("/") + 1))
271
+ .map((name) => ({
272
+ name,
273
+ ...joined([global, ...before(name), own]),
274
+ place,
275
+ }));
276
+ }
277
+
278
+ /**
279
+ * Every package the `\usepackage`, `\RequirePackage` and `\RequirePackageWithOptions` lines at the
280
+ * top of the tree load, in source order, none after `\end{document}` (TeX stops there): the
281
+ * preamble's, as the class line is read, each with the options LaTeX hands it (`packageLoads`). A
282
+ * comment is not a line; several lines behind a TeX switch are each read. A package loaded twice keeps the options of its first load in LaTeX, and a
283
+ * second load asking for more stops the build («Option clash»), so each line is read as its own.
284
+ */
285
+ export function packagesOf(t: ParsedTex): readonly PackageLoad[] {
286
+ const passed = passedToPackagesOf(t);
287
+ return liveRoot(t)
288
+ .content.flatMap((node, index) =>
289
+ isPackageLine(node) ? [{ node, index }] : [],
290
+ )
291
+ .flatMap((at) =>
292
+ packageLoads(t.src, at, { passed, global: globalAt(t, at.index) }),
293
+ );
294
+ }
295
+
296
+ /**
297
+ * Whether the preamble may load a package, or pass it options, where this reader does not see: an
298
+ * `\input` or `\include` there left as written (no build record yet, or a stale one), a package line
299
+ * or a `\PassOptionsToPackage` inside another macro's argument or a definition's body
300
+ * (`\newcommand{\load}{\usepackage{…}}`), one whose package name is built by a macro
301
+ * (`\usepackage{\style}`), or a file read through another loader (`\InputIfFileExists`, `\import`).
302
+ */
303
+ export function packagesUnseen(t: ParsedTex): boolean {
304
+ const live = liveRoot(t);
305
+ const top = new Set<unknown>(live.content);
306
+ const hidden = visited(live, () => false).some(
307
+ (v) =>
308
+ isNode(v) &&
309
+ (isPackageLine(v) || isPassToPackage(v)) &&
310
+ (!top.has(v) || namedByMacro(v)),
311
+ );
312
+ return hidden || includesInPreamble(t) || otherLoaderInPreamble(t);
313
+ }
314
+
315
+ /** Macros that read a file this reader never opens: the `import` package's and the kernel's. */
316
+ const OTHER_LOADERS: ReadonlySet<string> = new Set([
317
+ "InputIfFileExists",
318
+ "import",
319
+ "subimport",
320
+ "inputfrom",
321
+ "subinputfrom",
322
+ ]);
323
+
324
+ /**
325
+ * Whether the preamble reads a file through a loader `includesOf` does not follow, or names a
326
+ * package line through `\csname` (`\csname usepackage\endcsname{…}`).
327
+ */
328
+ function otherLoaderInPreamble(t: ParsedTex): boolean {
329
+ const body = documentBodyOf(t)?.start ?? Number.POSITIVE_INFINITY;
330
+ const loader = visited(t.root, () => false).some(
331
+ (v) =>
332
+ isNode(v) &&
333
+ v.type === "macro" &&
334
+ OTHER_LOADERS.has(v.content) &&
335
+ // A node with no place (none the parser makes) reads NaN, which is before nothing.
336
+ Number(v.position?.start.offset) < body,
337
+ );
338
+ return loader || CSNAME_LOAD.test(t.src.slice(0, body));
339
+ }
340
+
341
+ const CSNAME_LOAD = /\\csname\s*(?:usepackage|RequirePackage)/u;
342
+
343
+ const isPassToPackage = (n: Node): n is Macro =>
344
+ n.type === "macro" && n.content === "PassOptionsToPackage";
345
+
346
+ /** Whether the packages a line names are built by a macro: its last mandatory argument. */
347
+ const namedByMacro = (m: Macro): boolean =>
348
+ holdsMacro(mandatory(m).at(isPassToPackage(m) ? 1 : 0)?.content ?? []);
349
+
350
+ /**
351
+ * A preset's `template_style` → the package it names and the options it requires: one whole
352
+ * `\usepackage[…]{…}` line naming one package. Anything else is null.
353
+ */
354
+ export function parseTemplateStyle(text: string): StylePackage | null {
355
+ const t = parseLatex(text);
356
+ const nodes = t.root.content.filter(
357
+ (n) => n.type !== "whitespace" && n.type !== "comment",
358
+ );
359
+ const [only, ...rest] = nodes;
360
+ if (only === undefined || rest.length > 0 || !isPackageLine(only))
361
+ return null;
362
+ const none = { passed: [], global: NO_OPTIONS };
363
+ const loads = packageLoads(t.src, { node: only, index: 0 }, none);
364
+ const [load, ...more] = loads;
365
+ return load === undefined || more.length > 0
366
+ ? null
367
+ : { name: load.name, options: load.options };
368
+ }
369
+
101
370
  /** `src` with `text` in place of `[from, to)`. */
102
371
  const splice = (src: string, from: number, to: number, text: string): string =>
103
372
  src.slice(0, from) + text + src.slice(to);
@@ -133,12 +402,15 @@ export function replaceDocumentClass(
133
402
  want: DocumentClass,
134
403
  ): string {
135
404
  // Several lines behind a switch: which one builds is not known, so none is rewritten.
136
- const [node, ...others] = classNodes(t);
137
- if (node === undefined || others.length > 0) return t.src;
138
- const line = classLine(t.src, node);
405
+ const [top, ...others] = classNodes(t);
406
+ if (top === undefined || others.length > 0) return t.src;
407
+ const { node } = top;
408
+ const line = classLine(t.src, top, passedOf(t));
139
409
  if (line.kind !== "class" || line.place.kind === "unplaced") return t.src;
410
+ // A line with an option built by a macro: what it lacks is not known, so it is not rewritten.
411
+ if (!readWhole(line)) return t.src;
140
412
  const { start, end } = line.place.span;
141
- const change = venueClass(line, want);
413
+ const change = venueClass(optionsInEffect(line), want);
142
414
  switch (change.kind) {
143
415
  case "keep":
144
416
  return t.src;
@@ -25,7 +25,10 @@ import {
25
25
  optional,
26
26
  outlineOf,
27
27
  parseLatex,
28
+ packagesOf,
29
+ packagesUnseen,
28
30
  parseTemplate,
31
+ parseTemplateStyle,
29
32
  placeOf,
30
33
  renderedRuns,
31
34
  replaceDocumentClass,
@@ -41,6 +44,8 @@ describe("documentClassOf — the class line's three states", () => {
41
44
  kind: "class",
42
45
  cls: "IEEEtran",
43
46
  options: ["conference", "compsoc"],
47
+ unread: [],
48
+ passed: [],
44
49
  place: {
45
50
  kind: "at",
46
51
  span: { start: 4, end: src.indexOf("}") + 1 },
@@ -77,6 +82,81 @@ describe("documentClassOf — the class line's three states", () => {
77
82
  });
78
83
  });
79
84
 
85
+ describe("documentClassOf — options passed before the line, and what a macro builds", () => {
86
+ it("🔴 `\\PassOptionsToClass` before the line, for its class: each option as written, with its place", () => {
87
+ const src =
88
+ "\\PassOptionsToClass{compsoc}{IEEEtran}\n\\PassOptionsToClass{x}{article}\n\\documentclass[conference]{IEEEtran}\n\\PassOptionsToClass{late}{IEEEtran}";
89
+ expect(documentClassOf(parseLatex(src))).toEqual({
90
+ kind: "class",
91
+ cls: "IEEEtran",
92
+ options: ["conference"],
93
+ unread: [],
94
+ passed: [
95
+ {
96
+ options: ["compsoc"],
97
+ unread: [],
98
+ place: { kind: "at", span: { start: 0, end: 38 } },
99
+ },
100
+ ],
101
+ place: {
102
+ kind: "at",
103
+ span: {
104
+ start: src.indexOf("\\documentclass"),
105
+ end: src.indexOf("}\n\\PassOptionsToClass{late}") + 1,
106
+ },
107
+ },
108
+ });
109
+ });
110
+
111
+ it("an option built by a macro is unread, in the line and in a passed list; braces do not hide one", () => {
112
+ const src =
113
+ "\\PassOptionsToClass{\\more}{IEEEtran}\\documentclass[\\opts, a, b={\\c}]{IEEEtran}";
114
+ const line = documentClassOf(parseLatex(src));
115
+ expect(
116
+ line.kind === "class" && [
117
+ line.options,
118
+ line.unread,
119
+ line.passed.map((p) => p.unread),
120
+ ],
121
+ ).toEqual([
122
+ ["\\opts", "a", "b={\\c}"],
123
+ ["\\opts", "b={\\c}"],
124
+ [["\\more"]],
125
+ ]);
126
+ });
127
+ });
128
+
129
+ describe("documentClassOf — a line with a part missing, a class from a macro, a class out of reach", () => {
130
+ it("a \\PassOptionsToClass missing an argument passes nothing; a \\documentclass with no name is empty", () => {
131
+ const line = (src: string) => documentClassOf(parseLatex(src));
132
+ // At the end of the file: before a line, the parser would take the line as its argument.
133
+ for (const pass of ["\\PassOptionsToClass", "\\PassOptionsToClass{a}"]) {
134
+ const l = line(`\\documentclass{IEEEtran}\n${pass}`);
135
+ expect(l.kind === "class" && l.passed).toEqual([]);
136
+ }
137
+ expect(line("\\documentclass[a]").kind).toBe("empty");
138
+ });
139
+
140
+ it("🔴 a class name built by a macro is unread, not empty", () => {
141
+ expect(documentClassOf(parseLatex("\\documentclass[a]{\\cls}")).kind).toBe(
142
+ "unread",
143
+ );
144
+ });
145
+
146
+ it("🔴 unseen: no class line at the top, and one inside another macro, or an \\input in the preamble", () => {
147
+ const kind = (src: string) => documentClassOf(parseLatex(src)).kind;
148
+ expect(kind("{\\documentclass{article}}")).toBe("unseen");
149
+ expect(kind("\\input{preamble}\n\\begin{document}\\end{document}")).toBe(
150
+ "unseen",
151
+ );
152
+ expect(kind("\\input{preamble}")).toBe("unseen");
153
+ // The other half: an \input in the body does not stand where a class line goes.
154
+ expect(kind("\\begin{document}\\input{intro}\\end{document}")).toBe(
155
+ "missing",
156
+ );
157
+ });
158
+ });
159
+
80
160
  describe("placeOf / macroPlace — a node the parser gave no position is unplaced, never offset 0", () => {
81
161
  const at = (start: number, end: number) => ({
82
162
  start: { offset: start, line: 1, column: start + 1 },
@@ -152,6 +232,173 @@ describe("parseTemplate — a preset's template", () => {
152
232
  });
153
233
  });
154
234
 
235
+ describe("parseTemplateStyle — a preset's template_style", () => {
236
+ it.each<[string, { name: string; options: string[] } | null]>([
237
+ ["\\usepackage{neurips_2026}", { name: "neurips_2026", options: [] }],
238
+ [
239
+ "% c\n\\usepackage[ main , final ]{neurips_2026}\n",
240
+ { name: "neurips_2026", options: ["main", "final"] },
241
+ ],
242
+ ["\\RequirePackage{icml2027}", { name: "icml2027", options: [] }],
243
+ ["\\usepackage{a,b}", null],
244
+ ["\\usepackage{}", null],
245
+ ["\\usepackage{a} \\usepackage{b}", null],
246
+ ["\\documentclass{article}", null],
247
+ ["neurips_2026", null],
248
+ ["", null],
249
+ ])("%j → %j", (text, want) => {
250
+ expect(parseTemplateStyle(text)).toEqual(want);
251
+ });
252
+ });
253
+
254
+ describe("packagesOf — options passed to a package before it is loaded (`\\PassOptionsToPackage`)", () => {
255
+ const optionsOf = (src: string) =>
256
+ packagesOf(parseLatex(src)).map((p) => [p.name, p.options]);
257
+
258
+ it("🔴 options passed before the load are in effect, to each package of a comma list", () => {
259
+ expect(
260
+ optionsOf(
261
+ "\\documentclass{article}\n\\PassOptionsToPackage{submission, x}{other, aaai2027}\n\\usepackage[y]{aaai2027}\n\\usepackage{other}\n",
262
+ ),
263
+ ).toEqual([
264
+ ["aaai2027", ["submission", "x", "y"]],
265
+ ["other", ["submission", "x"]],
266
+ ]);
267
+ });
268
+
269
+ it("options passed after the load, or to another package, are not", () => {
270
+ expect(
271
+ optionsOf(
272
+ "\\documentclass{article}\n\\PassOptionsToPackage{a}{other}\n\\usepackage{aaai2027}\n\\PassOptionsToPackage{submission}{aaai2027}\n\\PassOptionsToPackage{b}\n",
273
+ ),
274
+ ).toEqual([["aaai2027", []]]);
275
+ });
276
+ });
277
+
278
+ describe("packagesOf — only what TeX reads: nothing after \\end{document}", () => {
279
+ const doc =
280
+ "\\documentclass{article}\n\\usepackage{a}\n\\begin{document}\nx\n\\end{document}\n";
281
+
282
+ it("🔴 a load and a \\PassOptionsToPackage parked after it are not read", () => {
283
+ const src = `${doc}\\PassOptionsToPackage{o}{a}\n\\usepackage[p]{b}\n`;
284
+ expect(packagesOf(parseLatex(src)).map((p) => [p.name, p.options])).toEqual(
285
+ [["a", []]],
286
+ );
287
+ });
288
+
289
+ it("🔴 a load inside a macro after it hides nothing; one in the preamble's macro still does", () => {
290
+ const after = "\\newcommand{\\x}{\\usepackage{c}}\n";
291
+ expect(packagesUnseen(parseLatex(`${doc}${after}`))).toBe(false);
292
+ expect(packagesUnseen(parseLatex(`${after}${doc}`))).toBe(true);
293
+ });
294
+ });
295
+
296
+ describe("packagesOf — the options LaTeX hands a package: global, passed, its own (latex.ltx)", () => {
297
+ const loads = (src: string) =>
298
+ packagesOf(parseLatex(src)).map((p) => [p.name, p.options, p.unread]);
299
+
300
+ it("🔴 the class line's options are global: every package after it gets them, before its own", () => {
301
+ expect(
302
+ loads(
303
+ "\\documentclass[letterpaper, submission]{article}\n\\PassOptionsToPackage{x}{aaai2027}\n\\usepackage[y]{aaai2027}\n",
304
+ ),
305
+ ).toEqual([["aaai2027", ["letterpaper", "submission", "x", "y"], []]]);
306
+ });
307
+
308
+ it("options passed to the class with \\PassOptionsToClass are the class's alone, not global", () => {
309
+ expect(
310
+ loads(
311
+ "\\PassOptionsToClass{submission}{article}\n\\documentclass[letterpaper]{article}\n\\usepackage{aaai2027}\n",
312
+ ),
313
+ ).toEqual([["aaai2027", ["letterpaper"], []]]);
314
+ });
315
+
316
+ it("🔴 \\RequirePackageWithOptions loads the package with the global options, and what was passed to it is replaced", () => {
317
+ expect(
318
+ loads(
319
+ "\\documentclass[submission]{article}\n\\PassOptionsToPackage{x}{aaai2027}\n\\RequirePackageWithOptions{aaai2027}\n",
320
+ ),
321
+ ).toEqual([["aaai2027", ["submission"], []]]);
322
+ });
323
+
324
+ it("🔴 an option built by a macro, on the class line, passed, or on the load, is unread", () => {
325
+ expect(
326
+ loads(
327
+ "\\documentclass[\\cls]{article}\n\\PassOptionsToPackage{\\pass}{aaai2027}\n\\usepackage[\\own, y]{aaai2027}\n",
328
+ ),
329
+ ).toEqual([
330
+ [
331
+ "aaai2027",
332
+ ["\\cls", "\\pass", "\\own", "y"],
333
+ ["\\cls", "\\pass", "\\own"],
334
+ ],
335
+ ]);
336
+ });
337
+ });
338
+
339
+ describe("packagesUnseen — a preamble that may load what the reader does not see", () => {
340
+ const unseen = (preamble: string, body = "Text.") =>
341
+ packagesUnseen(
342
+ parseLatex(
343
+ `\\documentclass{article}\n${preamble}\n\\begin{document}\n${body}\n\\end{document}\n`,
344
+ ),
345
+ );
346
+
347
+ it("🔴 an \\input in the preamble, a package line inside a macro, a name a macro builds", () => {
348
+ expect(unseen("\\input{preamble}")).toBe(true);
349
+ expect(unseen("\\newcommand{\\load}{\\usepackage{x}}\n\\load")).toBe(true);
350
+ expect(unseen("\\usepackage{\\style}")).toBe(true);
351
+ });
352
+
353
+ it("🔴 options passed where the reader does not see them: inside a macro, or to a package a macro names", () => {
354
+ expect(
355
+ unseen("\\newcommand{\\opts}{\\PassOptionsToPackage{final}{x}}\n\\opts"),
356
+ ).toBe(true);
357
+ expect(unseen("\\PassOptionsToPackage{final}{\\style}")).toBe(true);
358
+ });
359
+
360
+ it("🔴 a file read through a loader the include reader does not follow, or a line built by \\csname", () => {
361
+ expect(unseen("\\InputIfFileExists{setup.tex}{}{}")).toBe(true);
362
+ expect(unseen("\\import{sections/}{preamble}")).toBe(true);
363
+ expect(unseen("\\csname usepackage\\endcsname{x}")).toBe(true);
364
+ expect(unseen("\\usepackage{x}", "\\import{sections/}{intro}")).toBe(false);
365
+ });
366
+
367
+ it("the whole preamble in view: plain lines and an \\input in the body; a bare \\usepackage hides nothing", () => {
368
+ expect(unseen("\\usepackage[a]{x,y}", "\\input{intro}")).toBe(false);
369
+ expect(packagesUnseen(parseLatex("\\usepackage"))).toBe(false);
370
+ expect(unseen("\\PassOptionsToPackage{\\opt}{x}\n\\usepackage{x}")).toBe(
371
+ false,
372
+ );
373
+ });
374
+ });
375
+
376
+ describe("packagesOf — the packages the preamble loads", () => {
377
+ it("a \\usepackage with no argument loads nothing", () => {
378
+ expect(
379
+ packagesOf(parseLatex("\\documentclass{article}\n\\usepackage\n")),
380
+ ).toEqual([]);
381
+ });
382
+
383
+ it("each package of each line, with the line's options and place; a comment loads nothing", () => {
384
+ const src =
385
+ "\\documentclass{article}\n% \\usepackage{x}\n\\usepackage[a, b]{p,q}\n\\RequirePackage{r}\n";
386
+ expect(
387
+ packagesOf(parseLatex(src)).map((p) => [
388
+ p.name,
389
+ p.options,
390
+ p.place.kind === "at"
391
+ ? src.slice(p.place.span.start, p.place.span.end)
392
+ : "",
393
+ ]),
394
+ ).toEqual([
395
+ ["p", ["a", "b"], "\\usepackage[a, b]{p,q}"],
396
+ ["q", ["a", "b"], "\\usepackage[a, b]{p,q}"],
397
+ ["r", [], "\\RequirePackage{r}"],
398
+ ]);
399
+ });
400
+ });
401
+
155
402
  const replaced = (src: string) => replaceDocumentClass(parseLatex(src), IEEE);
156
403
 
157
404
  describe("replaceDocumentClass — the class `paperlint new` writes", () => {
@@ -189,6 +436,22 @@ describe("replaceDocumentClass — the class `paperlint new` writes", () => {
189
436
  expect(replaced("\\documentclass{}\nx")).toBe("\\documentclass{}\nx");
190
437
  });
191
438
 
439
+ it("🔴 an option passed with \\PassOptionsToClass is not added again; only the one still missing is", () => {
440
+ expect(
441
+ replaced(
442
+ "\\PassOptionsToClass{compsoc}{IEEEtran}\n\\documentclass{IEEEtran}",
443
+ ),
444
+ ).toBe(
445
+ "\\PassOptionsToClass{compsoc}{IEEEtran}\n\\documentclass[conference]{IEEEtran}",
446
+ );
447
+ });
448
+
449
+ it("🔴 a line built by a macro is returned as it is: what it lacks is not known", () => {
450
+ const opts = "\\documentclass[\\opts]{IEEEtran}";
451
+ expect(replaced(opts)).toBe(opts);
452
+ expect(replaced("\\documentclass{\\cls}")).toBe("\\documentclass{\\cls}");
453
+ });
454
+
192
455
  it("the port does the same over a source", () => {
193
456
  expect(
194
457
  latexReader.withDocumentClass("\\documentclass{article}", IEEE),