Skip to content

Commit 3edf7b1

Browse files
committed
fix: don't override another rules in eslint config + fix matching t in the nest components of Trans + add checks for both i18n.t + add config-eslint tests
1 parent 18ed77b commit 3edf7b1

6 files changed

Lines changed: 112 additions & 30 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Guards that keep translated output in step with a language switch. Built-in no-restricted-syntax only.
2+
// Translations must come from the component's `t` (react-i18next's `useTranslation`), which changes identity
3+
// on a language switch; the global instance's `t` gives React (and React Compiler caches) nothing to
4+
// invalidate on, so memoized output would keep the previous language.
5+
6+
// Selectors cannot follow bindings, so the instance is recognised by name: a default import or
7+
// `useTranslation().i18n` held as `i18n`/`i18next`. Any other alias is not caught.
8+
const i18nInstanceName = "/^(i18n|i18next)$/"
9+
const i18nInstance = `:matches([name=${i18nInstanceName}], MemberExpression[property.name=${i18nInstanceName}])`
10+
11+
const globalTMessage = "Use t from react-i18next's useTranslation so memoized output follows language changes."
12+
13+
export const i18nLanguageSwitchSelectors = [
14+
{
15+
selector: `MemberExpression:matches([property.name='t'][computed=false], [property.value='t']):has(> ${i18nInstance}.object)`,
16+
message: globalTMessage,
17+
},
18+
{
19+
selector: `VariableDeclarator:has(> ${i18nInstance}.init) > ObjectPattern > Property[key.name='t']`,
20+
message: globalTMessage,
21+
},
22+
{
23+
selector: "JSXOpeningElement[name.name='Trans']:not(:has(> JSXAttribute[name.name='t']))",
24+
message:
25+
"Pass t={t} from useTranslation: Trans does not subscribe to language changes, so a cached <Trans> keeps the previous language.",
26+
},
27+
]
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
})

‎packages/config-eslint/package.json‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
"./react": "./react.js",
88
"./next-js": "./next.js",
99
"./provider-identifiers": "./provider-identifiers.js",
10-
"./api-provider-hygiene": "./api-provider-hygiene.js"
10+
"./api-provider-hygiene": "./api-provider-hygiene.js",
11+
"./i18n-language-switch": "./i18n-language-switch.js"
1112
},
1213
"scripts": {
13-
"test": "node --test provider-identifiers.test.js api-provider-hygiene.test.js"
14+
"test": "node --test *.test.js"
1415
},
1516
"devDependencies": {
1617
"@eslint/js": "9.39.5",

‎webview-ui/eslint.config.mjs‎

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { reactConfig } from "@roo-code/config-eslint/react"
2+
import { i18nLanguageSwitchSelectors } from "@roo-code/config-eslint/i18n-language-switch"
23
import { createProviderIdentifierConfig } from "@roo-code/config-eslint/provider-identifiers"
34
import { providerIdentifiers, retiredProviderIdentifiers } from "@roo-code/types/provider-identifiers"
45

@@ -52,22 +53,6 @@ export default [
5253
message:
5354
"axios was removed from the webview bundle in favour of global fetch. Use fetch() for network calls. The dependency is no longer declared in webview-ui/package.json, so an import here only resolves through the hoisted store and would fail at build time.",
5455
},
55-
],
56-
},
57-
],
58-
},
59-
},
60-
{
61-
// Translations must come from the component's `t` (react-i18next's `useTranslation`), which changes
62-
// identity on a language switch; the global `t` gives React (and React Compiler caches) nothing to
63-
// invalidate on, so memoized output would keep the previous language.
64-
files: ["src/**/*.{ts,tsx}"],
65-
ignores: ["src/i18n/**"],
66-
rules: {
67-
"no-restricted-imports": [
68-
"error",
69-
{
70-
paths: [
7156
{
7257
name: "i18next",
7358
importNames: ["t"],
@@ -77,18 +62,7 @@ export default [
7762
],
7863
},
7964
],
80-
"no-restricted-syntax": [
81-
"error",
82-
{
83-
selector: "MemberExpression[object.name='i18next'][property.name='t']",
84-
message: "Use t from react-i18next's useTranslation so memoized output follows language changes.",
85-
},
86-
{
87-
selector: "JSXOpeningElement[name.name='Trans']:not(:has(JSXAttribute[name.name='t']))",
88-
message:
89-
"Pass t={t} from useTranslation: Trans does not subscribe to language changes, so a cached <Trans> keeps the previous language.",
90-
},
91-
],
65+
"no-restricted-syntax": ["error", ...i18nLanguageSwitchSelectors],
9266
},
9367
},
9468
{
@@ -119,6 +93,12 @@ export default [
11993
message:
12094
"axios was removed from the webview bundle in favour of global fetch. Use fetch() for network calls. The dependency is no longer declared in webview-ui/package.json, so an import here only resolves through the hoisted store and would fail at build time.",
12195
},
96+
{
97+
name: "i18next",
98+
importNames: ["t"],
99+
message:
100+
"Use t from react-i18next's useTranslation so memoized output follows language changes.",
101+
},
122102
{
123103
name: "@tanstack/react-query",
124104
importNames: ["QueryClientProvider"],

‎webview-ui/src/i18n/__tests__/hydrationLanguage.spec.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ describe("applyLanguage", () => {
8989
applyLanguage(i18next, "es")
9090
applyLanguage(i18next, "en")
9191
expect(i18next.language).toBe("en")
92+
// eslint-disable-next-line no-restricted-syntax -- checks loaded resources outside React; nothing is memoized
9293
expect(i18next.t("history:recentTasks")).toBe("Recent Tasks")
9394
})
9495

‎webview-ui/src/i18n/__tests__/languageSwitch.spec.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ describe("language switch without remounting", () => {
5050

5151
expect(screen.getByTestId("plain")).toHaveTextContent("Recent Tasks")
5252
expect(screen.getByTestId("namespaced")).toHaveTextContent(
53+
// eslint-disable-next-line no-restricted-syntax -- expected text computed outside React; nothing is memoized
5354
i18next.t("chat:rooTips.customizableModes.title", { lng: "en" }),
5455
)
5556
expect(screen.getByTestId("trans")).toHaveTextContent("Zoo is a whole AI dev team in your editor")
@@ -58,8 +59,10 @@ describe("language switch without remounting", () => {
5859

5960
await waitFor(() => expect(screen.getByTestId("plain")).toHaveTextContent("Tareas"))
6061
expect(screen.getByTestId("namespaced")).toHaveTextContent(
62+
// eslint-disable-next-line no-restricted-syntax -- expected text computed outside React; nothing is memoized
6163
i18next.t("chat:rooTips.customizableModes.title", { lng: "es" }),
6264
)
65+
// eslint-disable-next-line no-restricted-syntax -- expected text computed outside React; nothing is memoized
6366
expect(screen.getByTestId("trans")).toHaveTextContent(i18next.t("chat:about", { lng: "es" }))
6467
})
6568

0 commit comments

Comments
 (0)