Repository navigation
Keep the message catalogs out of client files with a lint rule - #891
Conversation
Part of #620: the core. next-intl and use-intl 4.14.9, pinned together (icu-minify too, through an override), with next.config.mjs wrapped in next-intl's plugin. src/messages/en.json is the English catalog, and next-intl's AppConfig is typed from it, through the declaration the plugin writes, so a missing key or a wrong argument fails typecheck. src/i18n holds the pure locale config (CATALOG_LOCALES, SHIPPED), a synchronous translator on use-intl/core with English filled in under any other language, the server wrapper that logs what goes wrong, the request config the plugin needs, and an English translator for tests that throws. The site footer's labels are the first text in the catalog, so the translator is proven in a server component, on static and dynamic pages. A new check runs it under the worker's toolchain. No page's first-load JavaScript changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016YZ9SVnYjqfPQCMgeA7pDt
A local ESLint plugin in eslint.config.mjs, with one rule so far: opensignup/no-catalogs-in-client. In a file that opens with a 'use client' directive, it reports any import of a catalog in src/messages, or of the translators that load every catalog (src/i18n/translator.ts, server.ts and testing/translator.ts), since either would put that text in the browser's JavaScript. It reads the directive rather than matching paths, follows @/ and relative paths, re-exports, import(), require() and import paths built from a template, and lets type-only imports through, since those are compiled away. The message says why and what to do instead. The tests lint fixtures through the repo's own config, so they also prove the rule is on for the app's files. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016YZ9SVnYjqfPQCMgeA7pDt
Tophat: passedRan d5f43d9 locally. Nothing to click here: the PR changes only
In the editor: the error sits on the import path itself, so the squiggle goes under No UX change: nothing a person using OpenSignup sees or does is affected. No screenshots: there's nothing on screen to show. Session: https://claude.ai/code/session_016YZ9SVnYjqfPQCMgeA7pDt Generated by Claude Code |
richshaw
left a comment
There was a problem hiding this comment.
Review: passed (round 1)
This is the second half of #620: a local ESLint plugin, opensignup, in eslint.config.mjs, with one rule, opensignup/no-catalogs-in-client, that fails a 'use client' file importing a catalog in src/messages/, src/i18n/translator.ts, src/i18n/server.ts or src/i18n/testing/translator.ts. It does it well. The directive is read from the prologue the way Next reads it, stopping at the first statement that isn't a directive. @/ and relative specifiers resolve to absolute paths against the config's own folder, so the rule doesn't depend on the cwd. Static, side-effect, export … from, export *, import(), require() and template-built import() paths are all checked. Type-only imports are skipped, which is safe because tsconfig.json doesn't set verbatimModuleSyntax, so import { type X } is elided whole. The plugin is registered once for every file and each rule is turned on in its own files block, so #623's text-prop rule and #629's provider rule each slot in as one entry in opensignup.rules plus one block. #620's "Done when" item "A client file that imports the translator fails lint" is met, and tested through the real config.
I probed it with 24 fixtures of my own through the repo's config. There were no false positives on typeof import('@/messages/en.json') in a type, @/i18n/config, import() from other folders, or a 'use client' that isn't in the prologue (Next ignores that one too). .ts client files and odd paths (@/i18n/translator.js, ./translator from inside src/i18n/) are caught. The tests prove what they claim: 16 forbidden imports in a client and in a server file, 10 allowed, the directive variants and the message, 44 in all. They run through lintText with the real config at a path under src/components/, so they'd also fail if the rule were switched off or its files glob stopped covering app code, and a fixture that doesn't parse throws instead of passing unseen.
Blocking (0): none.
Optional (3):
eslint.config.mjs:90(check()):import('@/messages/' + locale + '.json')isn't caught (the tophat found it). It matters a little, not much. Webpack makes a context module from a+chain just as it does from a template. Typecheck lets it through (Promise<any>), and since each catalog would be a lazy chunk, the first-load budget wouldn't see it either, so nothing guards it. But the template form is the usual way to write it, and that one is caught, so this isn't worth a round. When the rule is next touched (#623 adds to this plugin), a+chain whose leftmost operand is a string literal could be treated like a template head, with one moreFORBIDDENcase.eslint.config.mjs:21(CATALOG_IMPORTERS): the list fails open. A new module undersrc/i18n/that loads catalogs passes until someone adds it here, as the PR body already plans for #629'smessagesFor. Denying all ofsrc/i18n/except an allowlist (config, later the client provider) would fail closed instead. It's a judgement call, noted, not asked for.- PR body: it ends with the footer alone. The stack's two attribution lines before it,
🤖 Generated with [Claude Code](https://claude.com/claude-code)and the session link, are missing. That's metadata, not code.
Earlier threads: none. Copilot couldn't review (quota used up), so this went ahead without it.
Checks: CI is green on d5f43d9 (lint-type-test, e2e). Locally I ran pnpm test src/i18n/no-catalogs-in-client.test.ts (44 passed) and the 24-fixture probe above through the real config. The tophat passed on this same SHA, so no runtime code has changed since.
Gate is auto: only eslint.config.mjs and a new test change, with no visible change and nothing high-risk. Per "Merge after #889", it lands after #889.
Session: https://claude.ai/code/session_016YZ9SVnYjqfPQCMgeA7pDt
Generated by Claude Code
Fixes #620.
Merge after #889. This is the second half of #620: #889 adds the translator and catalogs; this adds the lint rule that keeps them out of client bundles.
The stack
PR 2 of the localization stack (#619). Based on #889 (
claude/issue-620-i18n-core); review against that branch.The problem
src/i18n/translator.tsimports every language's catalog, and so does everything that imports it. If a'use client'file imported the translator,src/i18n/server.tsor a catalog itself, every language's text would go into that page's JavaScript.server-onlycan't guard them, because it throws undertsxand vitest, and client files live all oversrc/, so a path-based rule would miss some.The fix
A local ESLint plugin,
opensignup, ineslint.config.mjs, with one rule so far:opensignup/no-catalogs-in-client. It looks only at files whose directive prologue holds'use client'(the way Next decides), and reports an import of:src/messages/(en.json, and any language added later);src/i18n/translator.ts,src/i18n/server.ts, andsrc/i18n/testing/translator.ts, which the issue doesn't name but carries every catalog for the same reason.However it's imported:
@/or relative paths, with or without an extension, side-effect imports, import attributes,export … fromandexport * from,import(),require(), and animport()whose path is built from a template (import(`@/messages/${locale}.json`)), which the bundler resolves to every file in that folder.Type-only imports pass (
import type,import { type X },export type): they're compiled away and bring nothing into the bundle. I checked that SWC dropsimport { type X } from '…'entirely, not leavingimport '…'behind.The generated declaration (
src/messages/en.d.json.ts) is allowed: it imports nothing, so importing it can't pull a catalog in. The rule's.jsoncheck is written so@/messages/en.d.json(which resolves to it) isn't mistaken for a catalog.The message says why and what to do instead, for example:
Built to take more rules. The plugin is registered in a block of its own for every file, and each rule is turned on in a block for the files it's meant for. Nothing checks the message catalogs, or catches new hard-coded text #623 adds its text-prop rule to
opensignup.rulesand turns it on per directory. The public signup page and the sign-up dialog are English-only #629 adds its provider rule the same way and updatesCLIENT_TEXT_INSTEADto nameClientMessages. A module that The public signup page and the sign-up dialog are English-only #629 adds to load catalogs on the server (messagesFor) goes intoCATALOG_IMPORTERS.What it doesn't catch
A module without the directive that a client file imports, which itself imports the translator. Lint looks at one file at a time. As the issue says,
pnpm budgetsis the guard there: a client chunk with every catalog in it goes over the budget.Where the issue was out of date
Nothing for this half. The issue names the translator,
server.tsand catalogs;src/i18n/testing/translator.tscame with #889 and is covered too.No CHANGELOG entry: nothing anyone sees or sets up changes.
Tests
src/i18n/no-catalogs-in-client.test.tslints fixtures with ESLint'slintTextthrough the repo's owneslint.config.mjs, notRuleTester. The repo has no pattern for lint tests yet,tsconfig.jsonhasallowJs: false(so a TypeScript test can't import the rule from the.mjswithout a declaration file), and running the real config also proves the rule is on for the app's files. It fails if a fixture doesn't parse, so a broken fixture can't pass without being looked at.'use client'file: each forbidden module, by each kind of import above.'use client'file: React,next-intl,@/i18n/config, four kinds of type-only import, the generated declaration, a JSON file outsidesrc/messages, and animport()built from a template in a folder without catalogs.'use strict'; a'use server'file passes.I broke the rule on purpose to check the tests catch it: with the rule off, the 16 client cases fail; with every file treated as a client file, 17 server cases fail; with type-only imports not skipped, the 4 type cases fail; with template paths not followed, the 2 template cases fail; with a plain
.jsoncheck, the declaration case fails. I also addedimport { getServerTranslator } from '@/i18n/server'to a real client component (src/components/ui/Popover.tsx) for a moment, andeslintreported it with the message above.Ran in this session, all passing:
pnpm lint,pnpm format:check,pnpm typecheck,pnpm knip,pnpm test(189 files, 2253 tests). Nopnpm build: nothing that runs in the app changed.Generated by Claude Code