Hardcoded colours, font sizes and interface strings. Spacing that does not adapt to right-to-left layouts.
Add "Design convention lint rules" from ntent to this repo.
Fetch https://ntent.app/r/f/eslint-rules as plain text. Write it verbatim to scripts/eslint-rules.mjs. Then import it from eslint.config.mjs. Then make exactly these edits and no others: Wire it into eslint.config.mjs the way the header of the file shows, and make these edits. (1) Merge, never add a second block: flat config keeps only the LAST block that sets a rule, so a second no-restricted-syntax block silently replaces the repo’s own. If the config already sets no-restricted-syntax, put its existing entries into the same array as designRules and copyRules and keep one block. Count the lint warnings before and after wiring; a count that drops means a block replaced theirs. The same goes for no-restricted-imports: see the import-boundaries step. (2) If this plan has no check-locale-keys.mjs, the repo has no t(), so delete the first entry of copyRules in scripts/eslint-rules.mjs (the aria-label, placeholder and alt rule that demands t()). Left in, it errors on every string in the app: 102 errors in one repo. Keep the em dash entry. (3) typescript-eslint must be a direct dependency and eslint must be importable from the install root, or the probes cannot load the config. Run `pnpm add -D eslint typescript-eslint` (npm: `npm install -D eslint typescript-eslint`, yarn: `yarn add -D eslint typescript-eslint`, bun: `bun add -d eslint typescript-eslint`) for whichever of the two the repo lacks as a direct dependency, then `node -e "import('eslint')"` from the root must print nothing. (4) Run lint once and report the error count. Do not fix what it finds in this install. It needs ESLint flat config. If this repo does not have it, stop and tell me.
Then check it: Write text-[13px] in a component. pnpm lint fails and points at it. In a repo that already linted, the warnings it had before are still there after: wiring the rules replaced nothing.Reads https://ntent.app/r/f/eslint-rules
Read the code
eslint-rules.mjsscripts/eslint-rules.mjs · import it from eslint.config.mjs
243 lines
/**
* Design-system and copy guardrails as ESLint errors.
*
* These are the rules from the constitution that a machine can check. Import
* this from your eslint.config.mjs:
*
* import { designRules, copyRules, boundaryRules, sandboxBoundaryRules, SANDBOX }
* from './scripts/eslint-rules.mjs'
*
* export default [
* ...tseslint.configs.recommended,
* { files: ['**\/*.{ts,tsx}'],
* rules: { 'no-restricted-syntax': ['error', ...designRules, ...copyRules],
* ...boundaryRules } },
* { files: ['**\/*.stories.tsx', '**\/kitchen-sink/**'],
* rules: { 'no-restricted-syntax': ['error', ...designRules] } }, // see below
* { files: [`**\/${SANDBOX}/**`], rules: { ...sandboxBoundaryRules } }, // see BOUNDARIES
* ]
*
* THE STRUCTURE MATTERS MORE THAN THE RULES.
*
* The rules are split into named groups rather than one flat list, because
* exemptions differ. A component catalogue legitimately hardcodes specimen
* strings ("you@company.com") while still being held to every token rule. The
* second config block above re-declares no-restricted-syntax WITHOUT the copy
* half, rather than switching the rule off. A hex in the catalogue still fails.
*
* The general move: when a rule is right in general and wrong in one place,
* narrow it by re-declaring a subset. Never disable it wholesale, and never
* reach for an inline eslint-disable, which is invisible three weeks later.
*
* SEPARATE CORRECTNESS FROM TASTE. rtlRules below are correctness: a component
* using pl-4 has not told you whether it works in Arabic. designRules are taste:
* a hardcoded hex is ugly and unmaintainable but it renders. A sandbox or an
* experiment may relax taste. Nothing relaxes correctness.
*
* EVERY RULE HERE HAS A PROBE. See scripts/probes/. If you add a rule, add a
* probe line that must fail and one that must pass, then run the probe. A
* selector that silently matches nothing reads exactly like a passing rule.
* check-probes refuses a rule here with no FAIL line of its own, so a probe
* line is never deleted to get a commit through. A rule goes with its probes.
*/
// ------------------------------------------------------- CORRECTNESS -------
// These never relax, in any override, anywhere.
export const rtlRules = [
{
// Physical direction does not mirror in a right-to-left language. Using the
// logical property costs nothing today and is the entire difference between
// "we support Arabic" being a week and being a quarter.
selector: "Literal[value=/(?:^|\\s)-?(?:p|m)(?:l|r)-/]",
message:
'No physical directional spacing. Use ps-/pe-/ms-/me- so the layout mirrors in RTL. ' +
'This is a correctness rule, not a style preference: pl-4 is wrong in Arabic.',
},
{
selector: "Literal[value=/(?:^|\\s)(?:left|right)-[0-9]/]",
message: 'No physical left-/right- insets. Use start-/end-.',
},
{
selector: "Literal[value=/(?:^|\\s)text-(?:left|right)(?:\\s|$)/]",
message: "No physical text alignment. Use text-start / text-end.",
},
{
selector: "Literal[value=/(?:^|\\s)(?:border-l|border-r)(?:-|\\s|$)/]",
message: 'No physical border sides. Use border-s / border-e.',
},
]
// ------------------------------------------------------------- TASTE -------
// A hardcoded value that a token already names.
export const tokenRules = [
{
// Arbitrary hex inside a Tailwind class. Colour is authored in one place,
// your globals.css token block, and nowhere else.
selector: "Literal[value=/\\[#[0-9a-fA-F]{3,8}\\]/]",
message:
'No arbitrary hex colour. Use a semantic token (bg-card, text-muted-foreground). ' +
'If the colour you need does not exist, add a token to globals.css rather than a one-off.',
},
{
// The same thing in a style object or a CSS-in-JS value.
selector: "Property[key.name=/^(color|background|backgroundColor|borderColor|fill|stroke)$/] > Literal[value=/^#[0-9a-fA-F]{3,8}$/]",
message: 'No hex colour literal. Use a token from the theme.',
},
{
// Arbitrary type sizes bypass the type scale, which is what keeps the
// hierarchy legible and what dynamic-type accessibility settings scale.
selector: "Literal[value=/(?:^|\\s)text-\\[[0-9.]+(?:px|rem|em)\\]/]",
message: 'No arbitrary font size. Use the type scale (text-sm, text-base, text-lg).',
},
{
// The descendant form, not [value.type='Literal'], on purpose. The narrow
// version catches fontSize: 12 and sails straight past
// fontSize: isCompact ? 11 : 13, which is exactly how a raw size survives an
// audit. Any numeric literal anywhere inside the value is a raw size,
// however it is wrapped. theme.type.sm reads as a MemberExpression and passes.
selector: "Property[key.name=/^(fontSize|lineHeight)$/] Literal[raw=/^[0-9.]+$/]",
message:
'No raw fontSize or lineHeight, including inside a ternary. ' +
'Use the type scale. Chrome that must NOT scale with dynamic type gets its own named token.',
},
{
// raw, not value. esquery regex tests do not apply to NUMERIC attribute
// values, so [value=/^[0-9]+$/] silently matches nothing on a number and
// reads as a working rule. This one cost an earlier repo a real audit.
selector:
"Property[key.name=/^(padding|paddingTop|paddingBottom|margin|marginTop|marginBottom|gap|rowGap|columnGap)$/] > Literal[raw=/^(4|8|12|16|20|24|32|40)$/]",
message:
'This spacing value has an exact token. Use it. ' +
'Off-scale optical nudges (1, 2, 3, 5, 6) are legitimate and are deliberately not flagged, ' +
'so the rule stays worth obeying.',
},
]
export const designRules = [...tokenRules, ...rtlRules]
// -------------------------------------------------------------- COPY -------
// Split out because the component catalogue is exempt from these and from
// nothing else.
export const copyRules = [
{
// Accessibility text is user-visible text. It is the half everyone forgets,
// because it never appears in a screenshot review: a hardcoded aria-label
// sails past design review, past QA, and reaches a screen reader user in
// English on an Arabic build.
//
// Scoped to the four unambiguous props. title and label are too overloaded
// (chart configs, demo specs, option objects) to error on.
selector:
"JSXAttribute[name.name=/^(aria-label|aria-description|placeholder|alt)$/] > Literal[value=/[A-Za-z]{2}/]",
message:
'User-visible strings go through t(), and accessibility labels are user-visible: ' +
'a screen reader reads them aloud. Add the key to every locale file.',
},
{
// Em dashes read as machine-written and do not localise cleanly. Rephrase:
// two sentences, or a comma, colon or parentheses.
selector: "JSXText[value=/\\u2014/]",
message: 'No em dash in user-facing copy. Use a period, comma, colon, or parentheses.',
},
]
// ---------------------------------------------------------- BOUNDARIES -----
/**
* The exploration sandbox: the one folder where a design may break the rules
* above on purpose, to invent a component or a colour before anyone agrees to
* it. A directory NAME, matched at any depth, so src/explorations/ and
* app/explorations/ are both the sandbox. Change it here if yours is called
* something else, and pick a name nothing else in the repo uses: the boundary
* below reads import text, so a second folder or module with the same name is
* fenced off too.
*
* Not `experiments`, which is what this was called first. That word also
* means A/B tests, and `@/lib/experiments` is a feature-flag module in half
* the product repos this lands in.
*/
export const SANDBOX = 'explorations'
const serverBoundary = {
group: ['**/lib/server/*', '**/lib/server/**'],
message:
'Server-only code must not be imported from a client component. ' +
'It will be bundled and shipped to the browser, secrets included. ' +
'Move the call behind a server action or a route handler.',
}
// The folder itself as well as what is in it, because a barrel is the leak
// people actually write: a nav that lists every exploration imports
// '@/explorations' and nothing deeper.
const sandboxBoundary = {
group: [`**/${SANDBOX}`, `**/${SANDBOX}/**`],
message:
`Nothing outside ${SANDBOX}/ may import from inside it. ` +
'An exploration is allowed to break the design system precisely because none of it ' +
'can leak. Promote what is approved into src/components, rewritten to obey the rules.',
}
/**
* One-way import boundaries. Spread these into a config block's `rules`, and
* spread sandboxBoundaryRules into a block scoped to the sandbox after it:
*
* { files: ['**\/*.{ts,tsx}'], rules: { ...boundaryRules } },
* { files: [`**\/${SANDBOX}/**`], rules: { ...sandboxBoundaryRules } },
*
* One way only. An exploration imports the real screens and the real
* components, which is what makes it a comparison rather than a mock-up, and it
* may import a sibling exploration. The rest of the app may import none of it.
* The second block re-declares the rule without the sandbox half rather than
* switching it off, so server code is still fenced off inside the sandbox.
*
* The pattern match here is the readable half: it catches the shapes people
* actually write and misses ones they could contrive. It reads import and
* export-from statements, not import() or require(), so a lazy-loaded
* exploration walks round it. For a boundary that really matters, pair it with
* a script in pre-commit that resolves paths properly. Two layers on purpose:
* the lint rule for fast feedback in the editor, the script for the gate you
* actually trust.
*/
export const boundaryRules = {
'no-restricted-imports': ['error', { patterns: [serverBoundary, sandboxBoundary] }],
}
/**
* READ THIS BEFORE SPREADING boundaryRules INTO A REPO THAT ALREADY LINTS.
*
* Flat config keeps the LAST block that sets a rule, and does not merge them.
* `{ rules: { ...boundaryRules } }` therefore REPLACES any no-restricted-imports
* the repo already had: its own patterns and its own paths stop being enforced,
* with no error and no warning to say so. One repo went from 379 warnings to 0
* this way and read it as a cleanup.
*
* When the repo already sets no-restricted-imports, pass its entry through this
* instead. It keeps the severity, the paths and the patterns that were there and
* adds ours. Count the warnings before and after: the number must not drop.
*
* { rules: { 'no-restricted-imports': mergeBoundaryRules(['warn', { paths: ['lodash'] }]) } }
*
* ESLint refuses a patterns array that mixes strings and objects, so string
* patterns are rewritten as { group: [pattern] } on the way through.
*/
export const mergeBoundaryRules = (existing, ours = [serverBoundary, sandboxBoundary]) => {
const [severity = 'error', ...options] = Array.isArray(existing) ? existing : [existing]
const first = options[0]
// `no-restricted-imports': ['error', 'a', 'b']` is the shorthand for paths.
const config =
typeof first === 'string' || options.length > 1
? { paths: options }
: first && typeof first === 'object'
? { ...first }
: {}
const patterns = (config.patterns ?? []).map((p) => (typeof p === 'string' ? { group: [p] } : p))
return [severity, { ...config, patterns: [...patterns, ...ours] }]
}
export const sandboxBoundaryRules = {
'no-restricted-imports': ['error', { patterns: [serverBoundary] }],
}
How to tell it works: Write text-[13px] in a component. pnpm lint fails and points at it. In a repo that already linted, the warnings it had before are still there after: wiring the rules replaced nothing.