paperlint 18.0.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 (130) hide show
  1. package/README.md +3 -3
  2. package/dist/adapters/hotcrp/index.d.ts +11 -1
  3. package/dist/adapters/hotcrp/index.d.ts.map +1 -1
  4. package/dist/adapters/hotcrp/index.js +11 -1
  5. package/dist/adapters/hotcrp/index.js.map +1 -1
  6. package/dist/adapters/hotcrp/site.d.ts.map +1 -1
  7. package/dist/adapters/hotcrp/site.js +3 -1
  8. package/dist/adapters/hotcrp/site.js.map +1 -1
  9. package/dist/adapters/hotcrp/token.d.ts +11 -0
  10. package/dist/adapters/hotcrp/token.d.ts.map +1 -1
  11. package/dist/adapters/hotcrp/token.js +34 -7
  12. package/dist/adapters/hotcrp/token.js.map +1 -1
  13. package/dist/adapters/latex/document-class.d.ts +28 -5
  14. package/dist/adapters/latex/document-class.d.ts.map +1 -1
  15. package/dist/adapters/latex/document-class.js +208 -21
  16. package/dist/adapters/latex/document-class.js.map +1 -1
  17. package/dist/adapters/latex/index.d.ts +4 -4
  18. package/dist/adapters/latex/index.d.ts.map +1 -1
  19. package/dist/adapters/latex/index.js +5 -2
  20. package/dist/adapters/latex/index.js.map +1 -1
  21. package/dist/adapters/latex/parse.d.ts.map +1 -1
  22. package/dist/adapters/latex/parse.js +5 -0
  23. package/dist/adapters/latex/parse.js.map +1 -1
  24. package/dist/cli.d.ts.map +1 -1
  25. package/dist/cli.js +4 -3
  26. package/dist/cli.js.map +1 -1
  27. package/dist/domain/body-pages.d.ts.map +1 -1
  28. package/dist/domain/body-pages.js +4 -2
  29. package/dist/domain/body-pages.js.map +1 -1
  30. package/dist/domain/portal-token.d.ts +26 -10
  31. package/dist/domain/portal-token.d.ts.map +1 -1
  32. package/dist/domain/portal-token.js +19 -12
  33. package/dist/domain/portal-token.js.map +1 -1
  34. package/dist/domain/tex-document.d.ts +52 -4
  35. package/dist/domain/tex-document.d.ts.map +1 -1
  36. package/dist/domain/tex-document.js +9 -0
  37. package/dist/domain/tex-document.js.map +1 -1
  38. package/dist/eslint-rules/latex-language.d.ts +10 -0
  39. package/dist/eslint-rules/latex-language.d.ts.map +1 -1
  40. package/dist/eslint-rules/latex-language.js +41 -2
  41. package/dist/eslint-rules/latex-language.js.map +1 -1
  42. package/dist/ports/latex.d.ts +13 -1
  43. package/dist/ports/latex.d.ts.map +1 -1
  44. package/dist/ports/submission-portal.d.ts +7 -0
  45. package/dist/ports/submission-portal.d.ts.map +1 -1
  46. package/dist/presets.d.ts +15 -0
  47. package/dist/presets.d.ts.map +1 -1
  48. package/dist/presets.js +9 -0
  49. package/dist/presets.js.map +1 -1
  50. package/dist/submission.d.ts +3 -2
  51. package/dist/submission.d.ts.map +1 -1
  52. package/dist/submission.js +24 -12
  53. package/dist/submission.js.map +1 -1
  54. package/dist/tex-requirements.d.ts +2 -0
  55. package/dist/tex-requirements.d.ts.map +1 -1
  56. package/dist/tex-requirements.js +7 -2
  57. package/dist/tex-requirements.js.map +1 -1
  58. package/dist/tex-venue-rules.d.ts +36 -3
  59. package/dist/tex-venue-rules.d.ts.map +1 -1
  60. package/dist/tex-venue-rules.js +181 -33
  61. package/dist/tex-venue-rules.js.map +1 -1
  62. package/docs/rules/cycle/port-done.md +3 -0
  63. package/docs/rules/tex/template.md +96 -1
  64. package/docs/rules.md +18 -13
  65. package/docs/submission.md +26 -9
  66. package/docs/venues.md +51 -12
  67. package/eslint-rules/latex-language.test.mjs +64 -1
  68. package/eslint-rules/latex-language.ts +44 -2
  69. package/eslint.config.mjs +1 -0
  70. package/fixtures/build-e2e/neurips/PIPELINE-STATUS.md +3 -0
  71. package/fixtures/build-e2e/neurips/expect.json +20 -0
  72. package/fixtures/build-e2e/neurips/paper.tex +60 -0
  73. package/fixtures/build-e2e/neurips/paperlint.json +1 -0
  74. package/fixtures/build-e2e/neurips-nostyle/PIPELINE-STATUS.md +3 -0
  75. package/fixtures/build-e2e/neurips-nostyle/expect.json +26 -0
  76. package/fixtures/build-e2e/neurips-nostyle/paper.tex +56 -0
  77. package/fixtures/build-e2e/neurips-nostyle/paperlint.json +1 -0
  78. package/fixtures/build-e2e/neurips-over/PIPELINE-STATUS.md +3 -0
  79. package/fixtures/build-e2e/neurips-over/expect.json +24 -0
  80. package/fixtures/build-e2e/neurips-over/paper.tex +56 -0
  81. package/fixtures/build-e2e/neurips-over/paperlint.json +1 -0
  82. package/fixtures/real-papers.test.ts +17 -6
  83. package/package.json +3 -1
  84. package/presets/aaai-2027.jsonc +126 -0
  85. package/presets/aaai-2027.md +127 -0
  86. package/presets/aaai-press.jsonc +167 -0
  87. package/presets/aaai-press.md +126 -0
  88. package/presets/icml-2027.jsonc +111 -0
  89. package/presets/icml-2027.md +130 -0
  90. package/presets/icml-style.jsonc +125 -0
  91. package/presets/icml-style.md +77 -0
  92. package/presets/neurips-2026.jsonc +123 -0
  93. package/presets/neurips-2026.md +137 -0
  94. package/presets/neurips-style.jsonc +113 -0
  95. package/presets/neurips-style.md +85 -0
  96. package/presets/venue-profile.schema.json +6 -0
  97. package/skills/build-label-page/scripts/label-page-browser.test.ts +275 -114
  98. package/src/adapters/hotcrp/cassette/README.md +1 -1
  99. package/src/adapters/hotcrp/index.ts +12 -0
  100. package/src/adapters/hotcrp/site.ts +4 -0
  101. package/src/adapters/hotcrp/token.test.ts +39 -1
  102. package/src/adapters/hotcrp/token.ts +40 -7
  103. package/src/adapters/latex/document-class.ts +292 -20
  104. package/src/adapters/latex/index.test.ts +263 -0
  105. package/src/adapters/latex/index.ts +12 -3
  106. package/src/adapters/latex/parse.ts +5 -0
  107. package/src/cli.test.ts +6 -1
  108. package/src/cli.ts +4 -3
  109. package/src/domain/body-pages.test.ts +22 -6
  110. package/src/domain/body-pages.ts +4 -2
  111. package/src/domain/portal-token.test.ts +121 -2
  112. package/src/domain/portal-token.ts +39 -15
  113. package/src/domain/tex-document.ts +64 -5
  114. package/src/folder-venue-rule.test.ts +51 -0
  115. package/src/new-paper.test.ts +2 -2
  116. package/src/paper-settings-commands.test.ts +1 -1
  117. package/src/ports/latex.ts +14 -0
  118. package/src/ports/submission-portal.ts +7 -0
  119. package/src/preset-card-rule.test.ts +44 -27
  120. package/src/presets.test.ts +177 -1
  121. package/src/presets.ts +27 -0
  122. package/src/rule-cases.test.ts +52 -0
  123. package/src/submission-show.test.ts +1 -1
  124. package/src/submission.test.ts +98 -7
  125. package/src/submission.ts +28 -11
  126. package/src/tex-requirements.harness.ts +17 -3
  127. package/src/tex-requirements.ts +13 -2
  128. package/src/tex-venue-rules.test.ts +1020 -0
  129. package/src/tex-venue-rules.ts +248 -38
  130. package/src/venue-rules.test.ts +369 -2
@@ -4,7 +4,7 @@
4
4
  */
5
5
  import { map, unique } from "remeda";
6
6
  import { expect, test } from "vitest";
7
- import { tokenVariable } from "./index.ts";
7
+ import { isSiteOnlyToken, sharedVariable, tokenVariable } from "./index.ts";
8
8
 
9
9
  test("tokenVariable: on hotcrp.com the subdomain alone names the site", () => {
10
10
  expect(tokenVariable("https://msr2027.hotcrp.com")).toBe(
@@ -67,3 +67,41 @@ test("tokenVariable: a port other than the scheme's default is part of the site;
67
67
  "HOTCRP_TOKEN_127_0_0_1_9",
68
68
  ]);
69
69
  });
70
+
71
+ test("sharedVariable: every *.hotcrp.com site has the one hosted-service variable", () => {
72
+ expect(
73
+ map(
74
+ [
75
+ "https://msr2027.hotcrp.com",
76
+ "https://msr2027.hotcrp.com/",
77
+ "https://ase-2027.hotcrp.com/paper/7",
78
+ "msr2027.hotcrp.com",
79
+ ],
80
+ sharedVariable,
81
+ ),
82
+ ).toStrictEqual(
83
+ map(Array.from({ length: 4 }), () => "HOTCRP_TOKEN_HOTCRP_COM"),
84
+ );
85
+ });
86
+
87
+ test("🔴 sharedVariable: a self-hosted install has none, however its host looks", () => {
88
+ expect(
89
+ map(
90
+ [
91
+ "https://aidc.submit.acsac.org",
92
+ "https://hotcrp.example.edu/conf2027",
93
+ "https://review.example.org:8443/conf",
94
+ "https://hotcrp.com.example.org",
95
+ "https://nothotcrp.com",
96
+ "http://127.0.0.1:9",
97
+ ],
98
+ sharedVariable,
99
+ ),
100
+ ).toStrictEqual(map(Array.from({ length: 6 }), () => null));
101
+ });
102
+
103
+ test("isSiteOnlyToken: the prefix tells — lowercase hct_ is one site's, capital hcT_ is every site's", () => {
104
+ expect(
105
+ map(["hct_fake", "hcT_fake", "fake", "", "HCT_FAKE"], isSiteOnlyToken),
106
+ ).toStrictEqual([true, false, false, false, false]);
107
+ });
@@ -2,12 +2,24 @@
2
2
  * WHICH ENVIRONMENT VARIABLE HOLDS A HOTCRP SITE'S API TOKEN — HotCRP's fact, so HotCRP's adapter.
3
3
  * Pure.
4
4
  *
5
- * A HotCRP token works on the site it was made on and nowhere else (measured 2026-10-19: a token
6
- * made on one HotCRP site gets `401 Unauthorized` from `msr2027.hotcrp.com`'s `/api/whoami`). So the
7
- * variable is named after the site, and no two sites share one — `HOTCRP_TOKEN_<subdomain>` on the
8
- * hosted service, `HOTCRP_TOKEN_<host and path>` anywhere else — and an author with live submissions
9
- * at two venues sets two variables (#236). The site-less `HOTCRP_TOKEN` is no longer read; the policy that refuses it
10
- * by name is the domain's (`src/domain/portal-token.ts`).
5
+ * What HotCRP's source says (github.com/kohler/hotcrp, read 2026-11-19; #547). A token is one of two
6
+ * kinds, and its prefix tells which: `hcT_` is kept in the installation's shared contact database and
7
+ * works on every conference of that installation — all of hotcrp.com; `hct_` is kept in one
8
+ * conference's own database and works there only (`src/init.php`, where a bearer token is looked up
9
+ * by its prefix; `test/t_authorize.php`: "Bearer tokens start with hct_ (local) or hcT_ (cdb)").
10
+ * An installation with a shared contact database offers "Site availability: All sites / This site
11
+ * only" when a token is made, "All sites" preselected (`src/userinfo/u_developer.php`), so on
12
+ * hotcrp.com a token is shared unless the author picks otherwise. A self-hosted install is a separate
13
+ * installation with its own databases and cannot know a hotcrp.com token — what was measured on
14
+ * 2026-10-19: a token made on a self-hosted install got `401 Unauthorized` from
15
+ * `msr2027.hotcrp.com`'s `/api/whoami`. That crossed two installations; it says nothing about two
16
+ * hosted sites.
17
+ *
18
+ * So a `*.hotcrp.com` site reads `HOTCRP_TOKEN_<subdomain>` first, as an override, then the one
19
+ * variable of the whole service, `HOTCRP_TOKEN_HOTCRP_COM` — refused when it holds a site-only
20
+ * token. A self-hosted install reads `HOTCRP_TOKEN_<host and path>` only, never the service's. The
21
+ * site-less `HOTCRP_TOKEN` is no longer read; the policy that refuses it by name is the domain's
22
+ * (`src/domain/portal-token.ts`).
11
23
  */
12
24
  import { hostOf } from "../../domain/portal-token.ts";
13
25
 
@@ -20,6 +32,27 @@ export const VARIABLE_PATTERN = `${SITE_LESS_VARIABLE}_<LABEL>`;
20
32
  /** The hosted service, where each conference is a subdomain of its own. */
21
33
  const HOSTED = ".hotcrp.com";
22
34
 
35
+ /** The one variable of the hosted service: its sites share an account system, so one token. */
36
+ const SHARED_VARIABLE = `${SITE_LESS_VARIABLE}_HOTCRP_COM`;
37
+
38
+ /**
39
+ * Whether a token works on one site only: HotCRP's lowercase `hct_` prefix (an all-sites token starts
40
+ * `hcT_`).
41
+ */
42
+ export const isSiteOnlyToken = (token: string): boolean =>
43
+ token.startsWith("hct_");
44
+
45
+ /** Whether `url` is a site of the hosted service. */
46
+ const isHosted = (url: string): boolean => hostOf(url).endsWith(HOSTED);
47
+
48
+ /**
49
+ * The variable shared by every site of the hosted service, for a site on it; null for a self-hosted
50
+ * install, whose token the hosted service does not accept (measured 2026-10-19: 401) and which never
51
+ * reads this one.
52
+ */
53
+ export const sharedVariable = (url: string): string | null =>
54
+ isHosted(url) ? SHARED_VARIABLE : null;
55
+
23
56
  /**
24
57
  * What names a site, and no other: on the hosted service the subdomain alone
25
58
  * (`msr2027.hotcrp.com` → `msr2027`); anywhere else the whole host, with a port other than the
@@ -29,7 +62,7 @@ const HOSTED = ".hotcrp.com";
29
62
  */
30
63
  function siteLabel(url: string): string {
31
64
  const host = hostOf(url);
32
- if (host.endsWith(HOSTED)) return host.slice(0, -HOSTED.length);
65
+ if (isHosted(url)) return host.slice(0, -HOSTED.length);
33
66
  const path = URL.canParse(url) ? new URL(url).pathname : "";
34
67
  return `${host}${path}`;
35
68
  }
@@ -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;