|
| 1 | +/** |
| 2 | + * Finds t("ns:key", { ... }) calls that do not pass a variable the English string needs. |
| 3 | + * Example: the string has {{exitCode}} but the call passes { exitStatus }. |
| 4 | + * |
| 5 | + * Usage: node scripts/check-i18n-interpolation.js |
| 6 | + * |
| 7 | + * Only calls with a string-literal key and an object-literal second argument are checked. |
| 8 | + * A spread in the object skips the call. |
| 9 | + */ |
| 10 | + |
| 11 | +const path = require("path") |
| 12 | +const fs = require("fs") |
| 13 | +const { execFileSync } = require("child_process") |
| 14 | +const ts = require("typescript") |
| 15 | + |
| 16 | +const ROOT = path.join(__dirname, "..") |
| 17 | +const PACKAGES = ["src", "webview-ui/src"] |
| 18 | + |
| 19 | +const files = execFileSync( |
| 20 | + "git", |
| 21 | + ["ls-files", "-z", "--", ...PACKAGES.map((p) => `${p}/**/*.ts`), ...PACKAGES.map((p) => `${p}/**/*.tsx`)], |
| 22 | + { |
| 23 | + cwd: ROOT, |
| 24 | + encoding: "utf8", |
| 25 | + }, |
| 26 | +) |
| 27 | + .split("\0") |
| 28 | + .filter((f) => f && !/(\/__tests__\/|\.(spec|test)\.tsx?$)/.test(f)) |
| 29 | + |
| 30 | +const resources = {} |
| 31 | +function getResource(pkg, ns) { |
| 32 | + const id = `${pkg}:${ns}` |
| 33 | + if (!(id in resources)) { |
| 34 | + const file = path.join(ROOT, pkg === "src" ? "src" : "webview-ui/src", "i18n/locales/en", `${ns}.json`) |
| 35 | + resources[id] = fs.existsSync(file) ? JSON.parse(fs.readFileSync(file, "utf8")) : undefined |
| 36 | + } |
| 37 | + return resources[id] |
| 38 | +} |
| 39 | + |
| 40 | +function lookup(resource, keyPath) { |
| 41 | + let node = resource |
| 42 | + for (const part of keyPath.split(".")) { |
| 43 | + if (node === null || typeof node !== "object" || !(part in node)) return undefined |
| 44 | + node = node[part] |
| 45 | + } |
| 46 | + return node |
| 47 | +} |
| 48 | + |
| 49 | +// "Exited with {{exitCode}}" and "{{- name}}" and "{{n, number}}" -> exitCode, name, n |
| 50 | +function placeholders(value) { |
| 51 | + return [...value.matchAll(/\{\{-?\s*([\w$]+)[^}]*\}\}/g)].map((m) => m[1]) |
| 52 | +} |
| 53 | + |
| 54 | +const problems = [] |
| 55 | + |
| 56 | +for (const rel of files) { |
| 57 | + const pkg = rel.startsWith("src/") ? "src" : "webview-ui" |
| 58 | + const source = fs.readFileSync(path.join(ROOT, rel), "utf8") |
| 59 | + const sf = ts.createSourceFile( |
| 60 | + rel, |
| 61 | + source, |
| 62 | + ts.ScriptTarget.Latest, |
| 63 | + true, |
| 64 | + rel.endsWith("x") ? ts.ScriptKind.TSX : ts.ScriptKind.TS, |
| 65 | + ) |
| 66 | + |
| 67 | + const visit = (node) => { |
| 68 | + if (ts.isCallExpression(node)) { |
| 69 | + const callee = node.expression |
| 70 | + const isT = |
| 71 | + (ts.isIdentifier(callee) && callee.text === "t") || |
| 72 | + (ts.isPropertyAccessExpression(callee) && callee.name.text === "t") |
| 73 | + const [keyArg, optionsArg] = node.arguments |
| 74 | + if ( |
| 75 | + isT && |
| 76 | + keyArg && |
| 77 | + ts.isStringLiteralLike(keyArg) && |
| 78 | + keyArg.text.includes(":") && |
| 79 | + optionsArg && |
| 80 | + ts.isObjectLiteralExpression(optionsArg) && |
| 81 | + !optionsArg.properties.some(ts.isSpreadAssignment) |
| 82 | + ) { |
| 83 | + // i18next splits on the first ":" and joins the rest with "." |
| 84 | + const [ns, ...rest] = keyArg.text.split(":") |
| 85 | + const keyPath = rest.join(".") |
| 86 | + const resource = getResource(pkg, ns) |
| 87 | + if (resource) { |
| 88 | + const passed = new Set( |
| 89 | + optionsArg.properties.map( |
| 90 | + (p) => p.name && (ts.isIdentifier(p.name) || ts.isStringLiteral(p.name)) && p.name.text, |
| 91 | + ), |
| 92 | + ) |
| 93 | + // A plural key lives under key_one, key_other, and so on. |
| 94 | + const values = [keyPath, `${keyPath}_one`, `${keyPath}_other`] |
| 95 | + .map((k) => lookup(resource, k)) |
| 96 | + .filter((v) => typeof v === "string") |
| 97 | + const missing = new Set(values.flatMap(placeholders).filter((name) => !passed.has(name))) |
| 98 | + if (missing.size > 0) { |
| 99 | + const { line } = sf.getLineAndCharacterOfPosition(node.getStart()) |
| 100 | + problems.push( |
| 101 | + `${rel}:${line + 1} t("${keyArg.text}") does not pass: ${[...missing].join(", ")}`, |
| 102 | + ) |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + ts.forEachChild(node, visit) |
| 108 | + } |
| 109 | + visit(sf) |
| 110 | +} |
| 111 | + |
| 112 | +if (problems.length > 0) { |
| 113 | + console.error(`Found ${problems.length} t() call(s) missing an interpolation variable:\n${problems.join("\n")}`) |
| 114 | + process.exit(1) |
| 115 | +} |
| 116 | +console.log(`OK: checked ${files.length} files, no missing interpolation variables.`) |
0 commit comments