From 4285bada87579299a35f1f9604180d9963d9f515 Mon Sep 17 00:00:00 2001 From: scuttletoby <123843277+scuttletoby@users.noreply.github.com> Date: Tue, 17 Nov 2026 22:43:11 +0100 Subject: [PATCH 1/2] fork-changes-list: fail closed on unusual forkDefaults rows (#396) The FORK_SETTING_DEFAULTS text reader only counted `scope:`, so a spread, a helper call or a shorthand `{ scope, ... }` row was skipped without an error, and a column-0 `];` inside a row ended the table early. Each entry between rows must now be a `{ ... }` object literal, each row must read as exactly one scope and key, and a row still open at the `];` throws. Errors name the line in forkDefaults.ts. fork-owned-reads.test.py: drop the `"*" not in l` filter on OWNED_DOCS. Names compare exactly, so it changed nothing for the current list. Co-Authored-By: Claude Opus 5.5 --- .github/scripts/fork-changes-list.cjs | 71 +++++++++++++++++----- .github/scripts/fork-changes-list.test.cjs | 49 ++++++++++++++- .github/scripts/fork-owned-reads.test.py | 5 +- 3 files changed, 106 insertions(+), 19 deletions(-) diff --git a/.github/scripts/fork-changes-list.cjs b/.github/scripts/fork-changes-list.cjs index 4cb407c58d129..bc2101002b10a 100644 --- a/.github/scripts/fork-changes-list.cjs +++ b/.github/scripts/fork-changes-list.cjs @@ -14,33 +14,72 @@ const SCOPES = ["server", "projectOverride", "client"]; /** A setting as the docs write it: `server.`, `projectOverride.` or `client.`. */ const SETTING_ID = /`((?:server|projectOverride|client)\.[A-Za-z0-9]+)`/gu; +/** A row of the table as the formatter writes it: `{` on its own line, or the whole row on one line. */ +const ROW_OPEN = /^ {2}\{$/u; +const ROW_CLOSE = /^ {2}\},?$/u; +const ROW_ONE_LINE = /^ {2}\{.*\},?$/u; +const ROW_SCOPE_KEY = /\bscope:\s*(["'])(\w+)\1,\s*key:\s*(["'])([A-Za-z0-9]+)\3/gu; + /** - * `.` of each row of FORK_SETTING_DEFAULTS in forkDefaults.ts's text. Throws when the table is not - * found, is empty, or has a `scope:` the parse cannot pair with its `key:` (fail closed: a row it cannot read - * would otherwise go unchecked). + * `.` of each row of FORK_SETTING_DEFAULTS in forkDefaults.ts's text. Fails closed (a row it cannot read + * would otherwise go unchecked): throws when the table is not found or is empty, when an entry between rows is not a + * `{ ... }` object literal (a spread, a helper call), when a row is still open at the table's `];` (a column-0 `];` + * inside a row ends the text table early), and when a row does not read as exactly one `scope: "", key: + * ""` (shorthand `{ scope, ... }`, a computed scope). Blank lines and `//` comments between rows are passed. */ function pinnedIds(text) { - const start = text.search(/^export const FORK_SETTING_DEFAULTS\b[^=]*=\s*\[/mu); - if (start === -1) + const head = /^export const FORK_SETTING_DEFAULTS\b[^=]*=\s*\[/mu.exec(text); + if (head === null) throw new Error(`${DEFAULTS}: no "export const FORK_SETTING_DEFAULTS ... = [" table`); - const end = text.indexOf("\n];", start); + const end = text.indexOf("\n];", head.index); if (end === -1) throw new Error(`${DEFAULTS}: the FORK_SETTING_DEFAULTS table has no closing "];" line`); - const table = text.slice(start, end); - const scopes = [...table.matchAll(/\bscope:/gu)].length; - const rows = [...table.matchAll(/\bscope:\s*(["'])(\w+)\1,\s*key:\s*(["'])([A-Za-z0-9]+)\3/gu)]; - if (rows.length === 0) throw new Error(`${DEFAULTS}: no row read from FORK_SETTING_DEFAULTS`); - if (rows.length !== scopes) { + const firstLine = text.slice(0, head.index).split("\n").length; + const lines = text.slice(head.index, end).split("\n"); + const where = (index) => `${DEFAULTS}: FORK_SETTING_DEFAULTS line ${firstLine + index}: `; + const rest = text.slice(head.index + head[0].length, text.indexOf("\n", head.index)).trim(); + if (rest !== "") throw new Error( - `${DEFAULTS}: ${scopes} "scope:" in FORK_SETTING_DEFAULTS but ${rows.length} read as ` + - '`scope: "", key: ""` (write each row that way)', + `${where(0)}\`${rest}\` after the table's "[" (start the rows on the next line)`, ); + const rows = []; + let open = -1; + for (let index = 1; index < lines.length; index++) { + const line = lines[index]; + if (open !== -1) { + if (ROW_CLOSE.test(line)) { + rows.push({ at: open, text: lines.slice(open, index + 1).join("\n") }); + open = -1; + } + } else if (ROW_OPEN.test(line)) open = index; + else if (ROW_ONE_LINE.test(line)) rows.push({ at: index, text: line }); + else if (line.trim() !== "" && !line.trim().startsWith("//")) { + throw new Error( + `${where(index)}\`${line.trim()}\` is not a \`{ ... }\` row (write each setting as its own object literal)`, + ); + } } - const unknown = rows.filter((row) => !SCOPES.includes(row[2])); + if (open !== -1) + throw new Error( + `${where(open)}a row is still open at the table's "];" (a "];" at column 0 inside a row?)`, + ); + if (rows.length === 0) throw new Error(`${DEFAULTS}: no row read from FORK_SETTING_DEFAULTS`); + const ids = rows.map((row) => { + const read = [...row.text.matchAll(ROW_SCOPE_KEY)]; + const scopes = [...row.text.matchAll(/\bscope:/gu)].length; + if (read.length !== 1 || scopes !== 1) { + throw new Error( + `${where(row.at)}${scopes} "scope:" but ${read.length} read as ` + + '`scope: "", key: ""` (write each row that way)', + ); + } + return { scope: read[0][2], key: read[0][4] }; + }); + const unknown = ids.filter((id) => !SCOPES.includes(id.scope)); if (unknown.length > 0) { - throw new Error(`${DEFAULTS}: unknown scope ${unknown.map((row) => row[2]).join(", ")}`); + throw new Error(`${DEFAULTS}: unknown scope ${unknown.map((id) => id.scope).join(", ")}`); } - return rows.map((row) => `${row[2]}.${row[4]}`).sort(); + return ids.map((id) => `${id.scope}.${id.key}`).sort(); } /** The settings FORK_CHANGES.md names, sorted and without repeats. */ diff --git a/.github/scripts/fork-changes-list.test.cjs b/.github/scripts/fork-changes-list.test.cjs index 89563b7c4ddc6..cd7b24591b923 100644 --- a/.github/scripts/fork-changes-list.test.cjs +++ b/.github/scripts/fork-changes-list.test.cjs @@ -59,12 +59,59 @@ test("pinnedIds fails closed: no table, an empty one, a row it cannot read, an u assert.throws( () => pinnedIds(TABLE(ROW("server", "waitFor") + ` {\n scope: SCOPE,\n key: "x",\n },\n`)), - /2 "scope:" .* but 1 read/u, + /line \d+: 1 "scope:" but 0 read/u, ); assert.throws(() => pinnedIds(TABLE(ROW("machine", "x"))), /unknown scope machine/u); assert.throws(() => pinnedIds("export const FORK_SETTING_DEFAULTS = [\n"), /no closing/u); }); +test("pinnedIds fails closed on a table entry that is not a `{ ... }` object literal (#396)", () => { + const first = ROW("server", "waitFor"); + // Each shape is skipped by a parse that only looks for `scope:` text, so its setting would go unchecked. + // TABLE puts the first row on line 6, so the entry after it starts on line 12. + const shapes = { + spread: [" ...EXTRA_ROWS,\n", /line 12: `\.\.\.EXTRA_ROWS,` is not a `\{ \.\.\. \}` row/u], + "helper call": [' row("server", "x"),\n', /line 12: `row\("server", "x"\),` is not a/u], + "shorthand on one line": [' { scope, key: "x" },\n', /line 12: 0 "scope:" but 0 read/u], + "shorthand over several lines": [ + ' {\n scope,\n key: "x",\n off: true,\n },\n', + /line 12: 0 "scope:" but 0 read/u, + ], + }; + for (const [shape, [entry, error]] of Object.entries(shapes)) { + assert.throws(() => pinnedIds(TABLE(first + entry + ROW("client", "after"))), error, shape); + } + // An entry on the table's own first line, and a one-line empty table followed by another array. + assert.throws( + () => pinnedIds(`export const FORK_SETTING_DEFAULTS = [...EXTRA_ROWS,\n${first}];\n`), + /line 1: `\.\.\.EXTRA_ROWS,` after the table's "\["/u, + ); + assert.throws( + () => pinnedIds(`export const FORK_SETTING_DEFAULTS = [];\nconst after = [\n${first}];\n`), + /line 1: `\];` after the table's "\["/u, + ); + // A `];` at column 0 inside a row ends the text table early: the rows after it would go unread. + const cut = + ' {\n scope: "server",\n key: "cut",\n default: [\n];\n off: true,\n },\n'; + assert.throws( + () => pinnedIds(TABLE(first + cut + ROW("client", "after"))), + /FORK_SETTING_DEFAULTS line \d+: a row is still open at the table's "\];"/u, + ); +}); + +test("pinnedIds reads a one-line row and passes a comment between rows", () => { + assert.deepEqual( + pinnedIds( + TABLE( + ROW("server", "waitFor") + + " // a note between rows\n\n" + + ' { scope: "client", key: "theme", default: "dark", off: "light" },\n', + ), + ), + ["client.theme", "server.waitFor"], + ); +}); + test("namedIds takes only `scope.key` in backticks; compare finds missing and unknown settings", () => { const named = namedIds( "`server.waitFor`, `server.waitFor`, server.bare, `other.key`, `client.theme`\n", diff --git a/.github/scripts/fork-owned-reads.test.py b/.github/scripts/fork-owned-reads.test.py index 6b59c940dc077..f10933938daf6 100644 --- a/.github/scripts/fork-owned-reads.test.py +++ b/.github/scripts/fork-owned-reads.test.py @@ -58,8 +58,9 @@ def owned(paths): ROOTS = sorted({l.lstrip("!").split("/")[0] for l in LIST_LINES}) PATH_RE = re.compile(r"(? Date: Tue, 17 Nov 2026 22:51:49 +0100 Subject: [PATCH 2/2] fork-changes-list: keep pinnedIds' doc comment to 4 lines Co-Authored-By: Claude Opus 5.5 --- .github/scripts/fork-changes-list.cjs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/.github/scripts/fork-changes-list.cjs b/.github/scripts/fork-changes-list.cjs index bc2101002b10a..ba8b145956a80 100644 --- a/.github/scripts/fork-changes-list.cjs +++ b/.github/scripts/fork-changes-list.cjs @@ -21,11 +21,9 @@ const ROW_ONE_LINE = /^ {2}\{.*\},?$/u; const ROW_SCOPE_KEY = /\bscope:\s*(["'])(\w+)\1,\s*key:\s*(["'])([A-Za-z0-9]+)\3/gu; /** - * `.` of each row of FORK_SETTING_DEFAULTS in forkDefaults.ts's text. Fails closed (a row it cannot read - * would otherwise go unchecked): throws when the table is not found or is empty, when an entry between rows is not a - * `{ ... }` object literal (a spread, a helper call), when a row is still open at the table's `];` (a column-0 `];` - * inside a row ends the text table early), and when a row does not read as exactly one `scope: "", key: - * ""` (shorthand `{ scope, ... }`, a computed scope). Blank lines and `//` comments between rows are passed. + * `.` of each row of FORK_SETTING_DEFAULTS in forkDefaults.ts's text. Fails closed, so no row goes + * unchecked: throws on a missing or empty table, an entry between rows that is not a `{ ... }` row (spread, helper + * call), a row still open at the `];`, or a row that is not exactly one `scope: "", key: ""`. */ function pinnedIds(text) { const head = /^export const FORK_SETTING_DEFAULTS\b[^=]*=\s*\[/mu.exec(text);