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
64 changes: 60 additions & 4 deletions .github/scripts/fork-changes-list.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,46 @@ 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;
/** 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
* 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 };
}

/**
* `<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>"`.
* 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: "<scope>", key: "<key>"` (comments not counted).
*/
function pinnedIds(text) {
const head = /^export const FORK_SETTING_DEFAULTS\b[^=]*=\s*\[/mu.exec(text);
Expand All @@ -48,6 +82,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 });
Expand All @@ -63,9 +102,26 @@ 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;
if (read.length !== 1 || scopes !== 1) {
const { code, bare } = stripRow(row.text);
if (bare.includes("...")) {
throw new Error(
`${where(row.at)}a spread in a row (no spreads anywhere in a row: write it all out)`,
);
}
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 = [...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: "<scope>", key: "<key>"` (write each row that way)',
Expand Down
72 changes: 72 additions & 0 deletions .github/scripts/fork-changes-list.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,78 @@ 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,
],
// 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',
/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(
Expand Down
Loading