Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 53 additions & 16 deletions .github/scripts/fork-changes-list.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,70 @@ const SCOPES = ["server", "projectOverride", "client"];
/** A setting as the docs write it: `server.<key>`, `projectOverride.<key>` or `client.<key>`. */
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;

/**
* `<scope>.<key>` 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).
* `<scope>.<key>` 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: "<scope>", key: "<key>"`.
*/
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: "<scope>", key: "<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: "<scope>", key: "<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. */
Expand Down
49 changes: 48 additions & 1 deletion .github/scripts/fork-changes-list.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions .github/scripts/fork-owned-reads.test.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ def owned(paths):
ROOTS = sorted({l.lstrip("!").split("/")[0] for l in LIST_LINES})
PATH_RE = re.compile(r"(?<![\w.-])(" + "|".join(map(re.escape, ROOTS)) + r")(/[\w.*/-]*)?")
# A doc the list owns by its own line (FORK_CHANGES.md): naming it is a read, unlike a runbook a message points to
# (a `.md` matched by a wider line such as `.github/fork-*`).
OWNED_DOCS = {l for l in LIST_LINES if l.endswith(".md") and not l.startswith("!") and "*" not in l}
# (a `.md` matched by a wider line such as `.github/fork-*`). A name counts only when it equals a line exactly, which
# is what keeps those runbook pointers out (a `*.md` line matches only a name that writes the same `*`).
OWNED_DOCS = {l for l in LIST_LINES if l.endswith(".md") and not l.startswith("!")}
# A fork-owned folder named alone, as in `path.join(root, "fork-ci", name)`: counts as a path inside it.
FOLDERS = {l.rstrip("/") for l in LIST_LINES if l.endswith("/") and l.count("/") == 1}
# A path built from quoted parts: `".github", "fork-x"` (path.join), `Path(".github") / "fork-x"`, `".github/" + "x"`.
Expand Down
Loading