|
| 1 | +import assert from "node:assert/strict" |
| 2 | +import { describe, it } from "node:test" |
| 3 | + |
| 4 | +import { Linter } from "eslint" |
| 5 | +import typescriptEslint from "typescript-eslint" |
| 6 | + |
| 7 | +import { i18nLanguageSwitchSelectors } from "./i18n-language-switch.js" |
| 8 | + |
| 9 | +const linter = new Linter({ configType: "flat" }) |
| 10 | +const config = [ |
| 11 | + { |
| 12 | + files: ["**/*.tsx"], |
| 13 | + languageOptions: { parser: typescriptEslint.parser, parserOptions: { ecmaFeatures: { jsx: true } } }, |
| 14 | + rules: { "no-restricted-syntax": ["error", ...i18nLanguageSwitchSelectors] }, |
| 15 | + }, |
| 16 | +] |
| 17 | + |
| 18 | +const lint = (code) => linter.verify(code, config, { filename: "example.tsx" }).map((message) => message.message) |
| 19 | + |
| 20 | +describe("i18n language switch: global t", () => { |
| 21 | + for (const code of [ |
| 22 | + 'i18next.t("chat:about")', |
| 23 | + 'import i18n from "i18next"\ni18n.t("chat:about")', |
| 24 | + 'i18n?.t("chat:about")', |
| 25 | + 'i18next["t"]("chat:about")', |
| 26 | + 'useTranslation().i18n.t("chat:about")', |
| 27 | + "const { t } = i18next", |
| 28 | + "const { t: translate } = i18n", |
| 29 | + "const { t } = useTranslation().i18n", |
| 30 | + ]) { |
| 31 | + it(`flags: ${code}`, () => { |
| 32 | + assert.equal(lint(code).length, 1) |
| 33 | + }) |
| 34 | + } |
| 35 | + |
| 36 | + for (const code of [ |
| 37 | + 'const { t } = useTranslation()\nt("chat:about")', |
| 38 | + 'i18next.changeLanguage("es")', |
| 39 | + "i18n.language", |
| 40 | + "i18next[t]", |
| 41 | + "const { language } = i18n", |
| 42 | + 'other.t("chat:about")', |
| 43 | + ]) { |
| 44 | + it(`allows: ${code}`, () => { |
| 45 | + assert.deepEqual(lint(code), []) |
| 46 | + }) |
| 47 | + } |
| 48 | +}) |
| 49 | + |
| 50 | +describe("i18n language switch: Trans", () => { |
| 51 | + for (const code of [ |
| 52 | + '<Trans i18nKey="chat:about" />', |
| 53 | + "<Trans components={{ b: <Foo t={1} /> }} />", |
| 54 | + "<Trans {...props} />", |
| 55 | + ]) { |
| 56 | + it(`flags a Trans without its own t: ${code}`, () => { |
| 57 | + assert.equal(lint(code).length, 1) |
| 58 | + }) |
| 59 | + } |
| 60 | + |
| 61 | + for (const code of [ |
| 62 | + '<Trans t={t} i18nKey="chat:about" />', |
| 63 | + "<Trans t={t} components={{ b: <Foo t={1} /> }} />", |
| 64 | + '<Other i18nKey="chat:about" />', |
| 65 | + ]) { |
| 66 | + it(`allows: ${code}`, () => { |
| 67 | + assert.deepEqual(lint(code), []) |
| 68 | + }) |
| 69 | + } |
| 70 | +}) |
0 commit comments