From f0215db9a7bf7eacd06aa8bd16714b1a795c4e4f Mon Sep 17 00:00:00 2001 From: scuttletoby <123843277+scuttletoby@users.noreply.github.com> Date: Fri, 20 Nov 2026 01:58:11 +0000 Subject: [PATCH 1/2] Settings-table reader: comments, spreads, two rows on a line, unclosed rows The FORK_SETTING_DEFAULTS reader (fork-changes-list.cjs) now strips comments before counting a row's "scope:", refuses a spread anywhere in a row and more than one top-level object on a row's line, and names an unrecognised closing line instead of reporting two rows read as one. Tests cover the scopes !== 1 check, each new refusal, and the two-space indent of a row's "},". Fixes #417 Co-Authored-By: Claude Opus 5.5 --- .github/scripts/fork-changes-list.cjs | 59 ++++++++++++++++++++-- .github/scripts/fork-changes-list.test.cjs | 59 ++++++++++++++++++++++ 2 files changed, 115 insertions(+), 3 deletions(-) diff --git a/.github/scripts/fork-changes-list.cjs b/.github/scripts/fork-changes-list.cjs index ba8b145956a8..d937c1857e89 100644 --- a/.github/scripts/fork-changes-list.cjs +++ b/.github/scripts/fork-changes-list.cjs @@ -18,12 +18,44 @@ const SETTING_ID = /`((?:server|projectOverride|client)\.[A-Za-z0-9]+)`/gu; const ROW_OPEN = /^ {2}\{$/u; const ROW_CLOSE = /^ {2}\},?$/u; const ROW_ONE_LINE = /^ {2}\{.*\},?$/u; +/** A line at the rows' own indent: inside a row, only its closing `},` may be there. */ +const ROW_INDENT = /^ {2}\S/u; const ROW_SCOPE_KEY = /\bscope:\s*(["'])(\w+)\1,\s*key:\s*(["'])([A-Za-z0-9]+)\3/gu; +/** + * A row's text without its comments (`code`), and also without the contents of its strings (`bare`), so neither a + * comment nor a string can stand in for the row's own `scope:`, spread or braces. + */ +function stripRow(text) { + let code = ""; + let bare = ""; + for (let index = 0; index < text.length; index++) { + const char = text[index]; + if (char === "/" && (text[index + 1] === "/" || text[index + 1] === "*")) { + const close = text[index + 1] === "/" ? "\n" : "*/"; + const end = text.indexOf(close, index + 2); + index = end === -1 ? text.length : close === "\n" ? end - 1 : end + 1; + code += " "; + bare += " "; + } else if (char === '"' || char === "'" || char === "`") { + let end = index + 1; + while (end < text.length && text[end] !== char) end += text[end] === "\\" ? 2 : 1; + code += text.slice(index, end + 1); + bare += char + char; + index = end; + } else { + code += char; + bare += char; + } + } + return { code, bare }; +} + /** * `.` 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: ""`. + * call), a row whose closing `},` line is not found, a row still open at the `];`, a spread or a second object on a + * row's line, or a row that is not exactly one `scope: "", key: ""` (comments not counted). */ function pinnedIds(text) { const head = /^export const FORK_SETTING_DEFAULTS\b[^=]*=\s*\[/mu.exec(text); @@ -48,6 +80,11 @@ function pinnedIds(text) { if (ROW_CLOSE.test(line)) { rows.push({ at: open, text: lines.slice(open, index + 1).join("\n") }); open = -1; + } else if (ROW_INDENT.test(line)) { + throw new Error( + `${where(index)}\`${line.trim()}\` at the rows' indent, but the row opened on line ` + + `${firstLine + open} has not closed (end each row with "},", alone on its line)`, + ); } } else if (ROW_OPEN.test(line)) open = index; else if (ROW_ONE_LINE.test(line)) rows.push({ at: index, text: line }); @@ -63,8 +100,24 @@ function pinnedIds(text) { ); 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; + const { code, bare } = stripRow(row.text); + if (bare.includes("...")) { + throw new Error( + `${where(row.at)}a spread in a row (write each row's scope and key in the row itself)`, + ); + } + const objects = bare.trim().replace(/,$/u, ""); + let depth = 0; + for (let index = 0; index < objects.length; index++) { + if (objects[index] === "{") depth++; + else if (objects[index] === "}" && --depth === 0 && index !== objects.length - 1) { + throw new Error( + `${where(row.at)}more than one \`{ ... }\` on a row's line (one row per line)`, + ); + } + } + const read = [...code.matchAll(ROW_SCOPE_KEY)]; + const scopes = [...code.matchAll(/\bscope:/gu)].length; if (read.length !== 1 || scopes !== 1) { throw new Error( `${where(row.at)}${scopes} "scope:" but ${read.length} read as ` + diff --git a/.github/scripts/fork-changes-list.test.cjs b/.github/scripts/fork-changes-list.test.cjs index cd7b24591b92..554272ace3ed 100644 --- a/.github/scripts/fork-changes-list.test.cjs +++ b/.github/scripts/fork-changes-list.test.cjs @@ -99,6 +99,65 @@ test("pinnedIds fails closed on a table entry that is not a `{ ... }` object lit ); }); +test("pinnedIds fails closed on a row whose scope and key could be read from the wrong place (#417)", () => { + const first = ROW("server", "waitFor"); + const after = ROW("client", "after"); + // TABLE puts the first row on line 6, so the entry after it starts on line 12. + const shapes = { + // Without the count of "scope:", the nested note's scope and key would read as the row's. + "nested scope and key": [ + ' { scope: SCOPE, key: "x", note: { scope: "server", key: "a" } },\n', + /line 12: 2 "scope:" but 1 read/u, + ], + // The comment's "scope:" no longer counts, so the row's real (spread) scope is refused. + "spread with a comment": [ + ' {\n ...BASE, // scope: "server", key: "a"\n off: true,\n },\n', + /line 12: a spread in a row/u, + ], + "spread on one line": [ + ' { ...BASE, scope: "server", key: "a" },\n', + /line 12: a spread in a row/u, + ], + "a comment only": [ + ' {\n /* scope: "server", key: "a" */\n key: "x",\n },\n', + /line 12: 0 "scope:" but 0 read/u, + ], + // Two entries on one line read as one row, so the second would go unread. + "two objects on one line": [ + ' { scope: "client", key: "b" }, { scope: "server", key: "c" },\n', + /line 12: more than one `\{ \.\.\. \}` on a row's line/u, + ], + // A closing line the reader does not know: name it, not the two rows it then reads as one. + "unrecognised close": [ + ' {\n scope: "server",\n key: "a",\n }, // end\n', + /line 15: `\}, \/\/ end` at the rows' indent, but the row opened on line 12 has not closed/u, + ], + }; + for (const [shape, [entry, error]] of Object.entries(shapes)) { + assert.throws(() => pinnedIds(TABLE(first + entry + after)), error, shape); + } +}); + +test("pinnedIds reads past comments, strings and nested objects in a row (#417)", () => { + // A nested object's `},` sits deeper than the rows' two-space indent, so it does not end the row. + const nested = + ' {\n scope: "server",\n key: "nested",\n default: {\n enabled: false,\n },\n' + + " off: true,\n },\n"; + const commented = + ' {\n // the scope: is "server"\n scope: "server", // see https://example.invalid/a\n' + + ' key: "commented",\n reason: "waits... { not a brace }",\n },\n'; + assert.deepEqual( + pinnedIds( + TABLE( + nested + + commented + + ' { scope: "client", key: "theme", default: { mode: "dark" }, off: "light" },\n', + ), + ), + ["client.theme", "server.commented", "server.nested"], + ); +}); + test("pinnedIds reads a one-line row and passes a comment between rows", () => { assert.deepEqual( pinnedIds( From ec65d793f4cb75a79d9d05b8ef751ac387a62b0d Mon Sep 17 00:00:00 2001 From: scuttletoby <123843277+scuttletoby@users.noreply.github.com> Date: Fri, 20 Nov 2026 02:12:48 +0000 Subject: [PATCH 2/2] settings reader: a string cannot stand in for a row's scope and key (#417) Review minor on #571: count scope: outside strings and require exactly one scope/key pair outside strings, so a row whose own scope is shorthand or a quoted key is refused instead of read from a string. Clearer message for a nested spread. Co-Authored-By: Claude Opus 5.5 --- .github/scripts/fork-changes-list.cjs | 9 ++++++--- .github/scripts/fork-changes-list.test.cjs | 13 +++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/.github/scripts/fork-changes-list.cjs b/.github/scripts/fork-changes-list.cjs index d937c1857e89..2d441ef1d435 100644 --- a/.github/scripts/fork-changes-list.cjs +++ b/.github/scripts/fork-changes-list.cjs @@ -21,6 +21,8 @@ const ROW_ONE_LINE = /^ {2}\{.*\},?$/u; /** A line at the rows' own indent: inside a row, only its closing `},` may be there. */ const ROW_INDENT = /^ {2}\S/u; const ROW_SCOPE_KEY = /\bscope:\s*(["'])(\w+)\1,\s*key:\s*(["'])([A-Za-z0-9]+)\3/gu; +/** The same shape once strings are emptied, so the one read from `code` is the row's own and not a string's. */ +const ROW_SCOPE_KEY_BARE = /\bscope:\s*(""|''),\s*key:\s*(""|'')/gu; /** * A row's text without its comments (`code`), and also without the contents of its strings (`bare`), so neither a @@ -103,7 +105,7 @@ function pinnedIds(text) { const { code, bare } = stripRow(row.text); if (bare.includes("...")) { throw new Error( - `${where(row.at)}a spread in a row (write each row's scope and key in the row itself)`, + `${where(row.at)}a spread in a row (no spreads anywhere in a row: write it all out)`, ); } const objects = bare.trim().replace(/,$/u, ""); @@ -117,8 +119,9 @@ function pinnedIds(text) { } } const read = [...code.matchAll(ROW_SCOPE_KEY)]; - const scopes = [...code.matchAll(/\bscope:/gu)].length; - if (read.length !== 1 || scopes !== 1) { + const scopes = [...bare.matchAll(/\bscope:/gu)].length; + const shaped = [...bare.matchAll(ROW_SCOPE_KEY_BARE)].length; + if (read.length !== 1 || scopes !== 1 || shaped !== 1) { throw new Error( `${where(row.at)}${scopes} "scope:" but ${read.length} read as ` + '`scope: "", key: ""` (write each row that way)', diff --git a/.github/scripts/fork-changes-list.test.cjs b/.github/scripts/fork-changes-list.test.cjs index 554272ace3ed..e8d75454caf6 100644 --- a/.github/scripts/fork-changes-list.test.cjs +++ b/.github/scripts/fork-changes-list.test.cjs @@ -122,6 +122,19 @@ test("pinnedIds fails closed on a row whose scope and key could be read from the ' {\n /* scope: "server", key: "a" */\n key: "x",\n },\n', /line 12: 0 "scope:" but 0 read/u, ], + // A string's "scope:" does not count, so a row whose own scope is not written as `scope:` is refused. + "shorthand scope, a string with a scope and key": [ + ` { scope, key, note: "scope: 'server', key: 'fake'" },\n`, + /line 12: 0 "scope:" but 1 read/u, + ], + "quoted scope, a string with a scope and key": [ + ` { "scope": "client", "key": "real", note: "scope: 'server', key: 'fake'" },\n`, + /line 12: 0 "scope:" but 1 read/u, + ], + "real scope and key, a string with another pair": [ + ` { scope: "client", key: "real", note: "scope: 'server', key: 'fake'" },\n`, + /line 12: 1 "scope:" but 2 read/u, + ], // Two entries on one line read as one row, so the second would go unread. "two objects on one line": [ ' { scope: "client", key: "b" }, { scope: "server", key: "c" },\n',