/** * Where each message of a catalog module sits in its source (for RFC-0032 codemods): the key path * (`menus.save.summary`), the decoded value and the span of its whole entry (from the start of the * key's line to the end of the line with its comma). Catalogs are `export const … = { … }` object * literals of strings, so a small scanner is enough. */ export interface CatalogEntry { path: string; value: string; /** Start of the line holding the key. */ lineStart: number; /** Where the key itself starts (after the indentation). */ keyStart: number; /** End of the entry, after its trailing comma and newline. */ lineEnd: number; /** The `/** … *\/` comment right above the key, if any: what translators read as its description. */ comment?: string; } /** * The body of a string literal (either quote) as the body of a JSON string: `\'` becomes `'`, a bare `"` * is escaped, every other escape (`\\`, `\"`, `\n`, `\u…`) stays as written. One pass, so an * escaped backslash before a quote is never taken apart (the replace chain it replaced turned * `'a\\"b'`'s `\"` into `\\"`, which ends the JSON string early). */ export function stringBodyAsJson(body: string): string { let out = ""; for (let k = 0; k < body.length; k++) { const c = body[k] as string; if (c === "\\") { const next = body[k + 1] ?? ""; out += next === "'" ? "'" : `\\${next}`; k++; } else out += c === '"' ? '\\"' : c; } return out; } class Scanner { i = 0; readonly src: string; readonly entries: CatalogEntry[] = []; /** The last block comment `skip` passed over, until a key takes it. */ comment: string | undefined; constructor(src: string) { this.src = src; } skip() { for (;;) { while (/\s/.test(this.src[this.i] ?? "")) this.i++; if (this.src.startsWith("//", this.i)) this.i = this.src.indexOf("\n", this.i); else if (this.src.startsWith("/*", this.i)) { const end = this.src.indexOf("*/", this.i); this.comment = this.src .slice(this.i + 2, end) .replace(/^\*/, "") .split("\n") .map((l) => l.replace(/^\s*\*?\s?/, "").trim()) .filter(Boolean) .join(" "); this.i = end + 2; } else return; if (this.i < 0) this.i = this.src.length; } } /** A string literal at `i`: its decoded value; `i` moves past it. */ string(): string { const quote = this.src[this.i] as string; let j = this.i + 1; while (this.src[j] !== quote) j += this.src[j] === "\\" ? 2 : 1; const body = this.src.slice(this.i + 1, j); this.i = j + 1; // Either quote: `\'` is legal in a double-quoted literal too, and not in JSON. return JSON.parse(`"${stringBodyAsJson(body)}"`) as string; } key(): string { const ch = this.src[this.i]; if (ch === '"' || ch === "'") return this.string(); const m = /[\w$]+/y; m.lastIndex = this.i; const found = m.exec(this.src); if (!found) throw new Error(`expected a key at ${this.i}`); this.i += found[0].length; return found[0]; } object(prefix: string) { this.i++; // { for (;;) { this.skip(); if (this.src[this.i] === "}") { this.i++; return; } const keyStart = this.i; const comment = this.comment; this.comment = undefined; const lineStart = this.src.lastIndexOf("\n", keyStart - 1) + 1; const key = this.key(); this.skip(); this.i++; // : this.skip(); const path = `${prefix}${key}`; if (this.src[this.i] === "{") this.object(`${path}.`); else { const value = this.string(); this.skip(); if (this.src[this.i] === ",") this.i++; const nl = this.src.indexOf("\n", this.i); const lineEnd = nl < 0 ? this.src.length : nl + 1; this.entries.push({ path, value, lineStart, keyStart, lineEnd, ...(comment ? { comment } : {}), }); continue; } this.skip(); if (this.src[this.i] === ",") this.i++; } } } /** Every message of a catalog module, in source order. Lazy modules' paths start with their namespace. */ export function catalogEntries(source: string): CatalogEntry[] { const start = source.search(/export const \w+\s*(?::[^=]+)?=\s*\{/); if (start < 0) return []; const scanner = new Scanner(source); scanner.i = source.indexOf("{", source.indexOf("=", start)); scanner.object(""); return scanner.entries; }