|
| 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: aliasing the instance", () => { |
| 51 | + for (const code of [ |
| 52 | + 'import lang from "i18next"', |
| 53 | + 'import { default as lang } from "i18next"', |
| 54 | + 'import * as lang from "i18next"', |
| 55 | + 'import lang from "@src/i18n/setup"', |
| 56 | + 'import lang from "@/i18n/setup"', |
| 57 | + 'import lang from "../setup"', |
| 58 | + 'export { default as lang } from "i18next"', |
| 59 | + "const lang = i18next", |
| 60 | + "const lang = useTranslation().i18n", |
| 61 | + "lang = i18n", |
| 62 | + "const { i18n: lang } = useTranslation()", |
| 63 | + "const { i18n: { t } } = useTranslation()", |
| 64 | + "function Foo({ i18n: lang }) {}", |
| 65 | + ]) { |
| 66 | + it(`flags: ${code}`, () => { |
| 67 | + assert.equal(lint(code).length, 1) |
| 68 | + }) |
| 69 | + } |
| 70 | + |
| 71 | + for (const code of [ |
| 72 | + 'import i18next from "i18next"', |
| 73 | + 'import i18n from "i18next"', |
| 74 | + 'import i18next, { type TFunction } from "i18next"', |
| 75 | + 'import { initReactI18next } from "react-i18next"', |
| 76 | + 'import i18next from "@src/i18n/setup"', |
| 77 | + 'import { default as i18next } from "i18next"', |
| 78 | + 'import config from "./config"', |
| 79 | + "const i18n = useTranslation().i18n", |
| 80 | + "const { i18n } = useTranslation()", |
| 81 | + "const { language } = i18next", |
| 82 | + "const instance = i18next.createInstance()", |
| 83 | + "changeLanguage(i18next)", |
| 84 | + ]) { |
| 85 | + it(`allows: ${code}`, () => { |
| 86 | + assert.deepEqual(lint(code), []) |
| 87 | + }) |
| 88 | + } |
| 89 | +}) |
| 90 | + |
| 91 | +describe("i18n language switch: Trans", () => { |
| 92 | + for (const code of [ |
| 93 | + '<Trans i18nKey="chat:about" />', |
| 94 | + "<Trans components={{ b: <Foo t={1} /> }} />", |
| 95 | + "<Trans {...props} />", |
| 96 | + ]) { |
| 97 | + it(`flags a Trans without its own t: ${code}`, () => { |
| 98 | + assert.equal(lint(code).length, 1) |
| 99 | + }) |
| 100 | + } |
| 101 | + |
| 102 | + for (const code of [ |
| 103 | + '<Trans t={t} i18nKey="chat:about" />', |
| 104 | + "<Trans t={t} components={{ b: <Foo t={1} /> }} />", |
| 105 | + '<Other i18nKey="chat:about" />', |
| 106 | + ]) { |
| 107 | + it(`allows: ${code}`, () => { |
| 108 | + assert.deepEqual(lint(code), []) |
| 109 | + }) |
| 110 | + } |
| 111 | +}) |
0 commit comments