import { describe, expect, it } from "vitest"; import { eslint } from "./lintProbe.testkit"; // `local/locale-format` (eslint.config.js): docs/DESIGN.md "Copy" — a date, a number and an // alphabetical order are in the UI language, built in lib/format.ts (text compared in lib/collate.ts), // and a component takes the language off useTranslation, never the i18n singleton. This pins that the // rule fires on the ways each used to be done by hand, and where it stands down. const RULE = "local/locale-format"; async function hits(source: string, filePath = "src/components/__probe.tsx"): Promise { const [result] = await eslint.lintText(source, { filePath }); return (result?.messages ?? []).filter((m) => m.ruleId === RULE).length; } describe("a hand-formatted date or number", () => { it("is refused as toLocale*String or an Intl date or number formatter", async () => { expect(await hits("export const a = (d: Date) => d.toLocaleString('en');\n")).toBe(1); expect(await hits("export const b = (d: Date) => d.toLocaleDateString();\n")).toBe(1); expect(await hits("export const c = (d: Date) => d.toLocaleTimeString();\n")).toBe(1); expect( await hits("export const e = new Intl.DateTimeFormat('en', { month: 'short' });\n"), ).toBe(1); expect(await hits("export const f = new Intl.RelativeTimeFormat('en');\n")).toBe(1); expect(await hits("export const h = new Intl.NumberFormat('en');\n")).toBe(1); }); it("is left alone in lib/format.ts and in a test", async () => { const src = "export const g = (d: Date) => d.toLocaleString('en');\n"; expect(await hits(src, "src/lib/format.ts")).toBe(0); expect(await hits(src, "src/lib/format.test.ts")).toBe(0); expect(await hits("export const h = new Intl.NumberFormat('en');\n", "src/lib/format.ts")).toBe( 0, ); }); }); describe("a hand-rolled alphabetical order", () => { it("is refused as localeCompare, or an Intl.Collator outside lib/collate.ts", async () => { expect(await hits("export const s = (a: string, b: string) => a.localeCompare(b);\n")).toBe(1); expect( await hits( "export const s = (a: string, b: string) => a.localeCompare(b);\n", "src/lib/x.ts", ), ).toBe(1); expect(await hits("export const c = new Intl.Collator('hu');\n")).toBe(1); expect(await hits("export const c = new Intl.Collator('hu');\n", "src/lib/collate.ts")).toBe(0); }); }); describe("the language in a component", () => { it("is refused off the i18n singleton", async () => { const src = 'import i18n from "../i18n";\nexport const L = () => {i18n.language};\n'; expect(await hits(src)).toBe(1); const next = 'import i18next from "i18next";\nexport const L = () => {i18next.language};\n'; expect(await hits(next)).toBe(1); }); it("is taken from useTranslation, and the singleton stays usable outside components", async () => { const hook = 'import { useTranslation } from "react-i18next";\n' + "export const L = () => {\n const { i18n } = useTranslation();\n return {i18n.language};\n};\n"; expect(await hits(hook)).toBe(0); const lib = 'import i18n from "../i18n";\nexport const lang = () => i18n.language;\n'; expect(await hits(lib, "src/lib/__probe.ts")).toBe(0); }); });