mirror of
https://github.com/bitbaum/kits.git
synced 2026-12-04 05:57:28 +01:00
- rememberLocale (+ switchLocaleHref, localeCookie, isSafeNextPath): the route that remembers a reader's language in an app with no language in its URL; relative 303, checked next, Secure over HTTPS (sbb-fundbuero's lessons) - alternateLanguages: hreflang alternates for Next metadata - langkit check --code <dirs> [--fail-unused]: messages no code appears to ask for; measured on eight apps, hand-traced samples were dead messages - fix: with nineteen languages the switcher widened a 390px page to 1081px inside sitekit's masthead; now it scrolls inside itself. Checked in a real browser at 320/390/1280px - first public version: private removed, 0.2.0 (0.1.0 was never published) Claude-Session: https://claude.ai/code/session_019MDtmsTRGc34xnG6koVuAY Co-authored-by: Claude <noreply@anthropic.com>
114 lines
4.1 KiB
JavaScript
114 lines
4.1 KiB
JavaScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { spawnSync } from "node:child_process";
|
|
import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
import { findUnused, literalsOf } from "@bitbaum/langkit/check";
|
|
|
|
test("literals: strings and template parts, never comments", () => {
|
|
const { strings, dynamic, endings } = literalsOf(`
|
|
// don't be fooled by "a comment"
|
|
/* nor 'this' */
|
|
const a = t("Nav.home"), b = 'it\\'s';
|
|
const c = t(\`forms.\${kind}\`);
|
|
const d = t(\`\${g.key}.body\`);
|
|
const e = \`outer \${ inner({ x: "in-template" }) } tail\`;
|
|
const url = "https://example.ch/x"; // a URL is a string, not a comment
|
|
`);
|
|
for (const s of ["Nav.home", "it's", "forms.", ".body", "in-template", "https://example.ch/x"]) {
|
|
assert.ok(strings.has(s), s);
|
|
}
|
|
assert.equal(strings.has("a comment"), false);
|
|
assert.equal(strings.has("this"), false);
|
|
assert.deepEqual([...dynamic], ["forms."]);
|
|
assert.deepEqual([...endings], [".body"]);
|
|
});
|
|
|
|
const source = {
|
|
Nav: { home: "Home", cases: "Cases", menu: "Menu" },
|
|
Security: { money: { body: "…" }, keys: { body: "…" } },
|
|
Forms: { rewrite: "…", response: "…" },
|
|
landing: { trust: ["a", "b"], cards: [{ title: "t", body: "b" }] },
|
|
mega: { programmes: [{ label: "x" }], footer: "y" },
|
|
Unused: { old: "Nobody asks for this." },
|
|
};
|
|
|
|
const code = [
|
|
{
|
|
path: "nav.tsx",
|
|
text: `const t = useTranslations("Nav"); t("home"); t("menu");`,
|
|
},
|
|
{
|
|
path: "security.tsx",
|
|
text: `const t = useTranslations("Security");
|
|
const GROUPS = [{ key: "money" }];
|
|
const STATES = { keys: "planned" };
|
|
GROUPS.map((g) => t(\`\${g.key}.body\`));`,
|
|
},
|
|
{ path: "forms.tsx", text: "const t = useTranslations(); t(`Forms.${form}`);" },
|
|
{
|
|
path: "landing.tsx",
|
|
text: `const t = useTranslations("landing"); t.raw("trust"); t.raw("cards");`,
|
|
},
|
|
{ path: "mega.tsx", text: `const t = useTranslations(); const m = t.raw("mega");` },
|
|
];
|
|
|
|
test("a key is asked for whole, by namespace and rest, or built at run time", () => {
|
|
const { unused, total } = findUnused(source, code);
|
|
assert.equal(total, 14);
|
|
assert.deepEqual(unused, ["Nav.cases", "Unused.old"]);
|
|
});
|
|
|
|
test("without the code that asks, the messages are reported", () => {
|
|
const { unused } = findUnused(source, code.slice(0, 1));
|
|
assert.ok(unused.includes("Security.money.body"));
|
|
assert.ok(unused.includes("landing.trust.0"));
|
|
assert.ok(unused.includes("mega.footer"));
|
|
});
|
|
|
|
const bin = fileURLToPath(new URL("../bin/langkit.js", import.meta.url));
|
|
|
|
test("--code reports unused messages and still passes; --fail-unused fails", () => {
|
|
const root = mkdtempSync(join(tmpdir(), "langkit-usage-"));
|
|
mkdirSync(join(root, "messages"));
|
|
mkdirSync(join(root, "src", "node_modules", "x"), { recursive: true });
|
|
const en = { Nav: { home: "Home", cases: "Cases" } };
|
|
writeFileSync(join(root, "messages", "en.json"), JSON.stringify(en));
|
|
writeFileSync(
|
|
join(root, "messages", "de.json"),
|
|
JSON.stringify({ Nav: { home: "Start", cases: "Fälle" } }),
|
|
);
|
|
writeFileSync(join(root, "src", "nav.tsx"), `useTranslations("Nav"); t("home");`);
|
|
// node_modules is not the app's code.
|
|
writeFileSync(join(root, "src", "node_modules", "x", "i.js"), `"Nav.cases"`);
|
|
|
|
const run = (...extra) =>
|
|
spawnSync(
|
|
"node",
|
|
[bin, "check", join(root, "messages"), "--code", join(root, "src"), ...extra],
|
|
{
|
|
encoding: "utf8",
|
|
},
|
|
);
|
|
const report = run();
|
|
assert.equal(report.status, 0);
|
|
assert.match(
|
|
report.stdout,
|
|
/1 of 2 "en" message\(s\) no code in .* \(1 files\) appears to ask for:/,
|
|
);
|
|
assert.match(report.stdout, /unused {5}Nav\.cases/);
|
|
assert.equal(run("--fail-unused").status, 1);
|
|
const json = JSON.parse(run("--json").stdout);
|
|
assert.deepEqual(json.unused, { unused: ["Nav.cases"], total: 2, files: 1 });
|
|
assert.equal(run("--code").status, 2);
|
|
assert.equal(
|
|
spawnSync("node", [bin, "check", join(root, "messages"), "--code", "/nonexistent-langkit"], {
|
|
encoding: "utf8",
|
|
}).status,
|
|
2,
|
|
);
|
|
});
|