From 71e9fc9cf611d4f2605dd3f58558b316b6194b4b Mon Sep 17 00:00:00 2001 From: Giri Kondisetty Date: Sun, 29 Nov 2026 11:44:59 -0600 Subject: [PATCH] fix(export): reject css-vars token names that are not CSS identifiers The css-vars export wrote token names into the stylesheet with only dots replaced, so a name such as 'x: 0; } a { color: red } :root { --y' closed the :root block and added a rule of its own. The Tailwind v4 export already rejects such names with INVALID_TOKEN_NAME. Check colors, spacing and rounded names before emitting, and report the error code the same way the other formats do. --- .../cli/src/commands/export-exit-code.test.ts | 11 +++++ packages/cli/src/commands/export.ts | 2 +- .../cli/src/linter/css-vars/handler.test.ts | 45 +++++++++++++++++++ packages/cli/src/linter/css-vars/handler.ts | 31 +++++++++++++ 4 files changed, 88 insertions(+), 1 deletion(-) diff --git a/packages/cli/src/commands/export-exit-code.test.ts b/packages/cli/src/commands/export-exit-code.test.ts index 7f682a32..aa2725d4 100644 --- a/packages/cli/src/commands/export-exit-code.test.ts +++ b/packages/cli/src/commands/export-exit-code.test.ts @@ -38,6 +38,17 @@ describe('export exit code', () => { expect(stdout.trim().length).toBeGreaterThan(0); }); + it('exits 1 with INVALID_TOKEN_NAME when a css-vars token name is not a CSS identifier', () => { + const file = join(dir, 'BAD-NAME.md'); + writeFileSync( + file, + '---\ncolors:\n primary: "#0000ff"\n "x: 0; } a { color: red } :root { --y": "#ffffff"\n---\n', + ); + const { code, stdout } = run(['export', '--format', 'css-vars', file]); + expect(code).toBe(1); + expect(stdout).toBe(''); + }); + it('lint still exits 1 on the same source (the validation gate)', () => { const { code } = run(['lint', badFile]); expect(code).toBe(1); diff --git a/packages/cli/src/commands/export.ts b/packages/cli/src/commands/export.ts index bb556328..a3e6ab28 100644 --- a/packages/cli/src/commands/export.ts +++ b/packages/cli/src/commands/export.ts @@ -113,7 +113,7 @@ export default defineCommand({ const result = handler.execute(report.designSystem); if (!result.success) { - console.error(JSON.stringify({ error: result.error.message })); + console.error(JSON.stringify({ error: result.error.code, message: result.error.message })); process.exitCode = 1; return; } diff --git a/packages/cli/src/linter/css-vars/handler.test.ts b/packages/cli/src/linter/css-vars/handler.test.ts index 36201503..7098077e 100644 --- a/packages/cli/src/linter/css-vars/handler.test.ts +++ b/packages/cli/src/linter/css-vars/handler.test.ts @@ -40,6 +40,51 @@ function makeDim(value: number, unit: string): ResolvedDimension { describe('CssVarsEmitterHandler', () => { const handler = new CssVarsEmitterHandler(); + test('rejects a token name that would break out of the declaration', () => { + const state = emptyState({ + colors: new Map([ + ['primary', makeColor('#0000FF', 0, 0, 255)], + ['x: 0; } a { color: red } :root { --y', makeColor('#FFFFFF', 255, 255, 255)], + ]), + }); + + const result = handler.execute(state); + expect(result.success).toBe(false); + if (result.success) return; + expect(result.error.code).toBe('INVALID_TOKEN_NAME'); + }); + + test('rejects invalid names in spacing and rounded too', () => { + for (const group of ['spacing', 'rounded'] as const) { + const state = emptyState({ [group]: new Map([['a; } b { c: d', makeDim(4, 'px')]]) }); + const result = handler.execute(state); + expect(result.success).toBe(false); + if (result.success) continue; + expect(result.error.code).toBe('INVALID_TOKEN_NAME'); + } + }); + + test('accepts hyphen, underscore, digit-leading, grouped and non-ASCII names', () => { + const state = emptyState({ + colors: new Map([ + ['on-primary_container', makeColor('#111111', 17, 17, 17)], + ['50', makeColor('#222222', 34, 34, 34)], + ['background.light', makeColor('#333333', 51, 51, 51)], + ['grün', makeColor('#444444', 68, 68, 68)], + ]), + }); + + const result = handler.execute(state); + expect(result.success).toBe(true); + if (!result.success) return; + expect(result.data.declarations.map((d) => d.name)).toEqual([ + 'color-on-primary_container', + 'color-50', + 'color-background-light', + 'color-grün', + ]); + }); + test('empty state produces valid empty :root block', () => { const result = handler.execute(emptyState()); expect(result.success).toBe(true); diff --git a/packages/cli/src/linter/css-vars/handler.ts b/packages/cli/src/linter/css-vars/handler.ts index d25b6e99..58107aae 100644 --- a/packages/cli/src/linter/css-vars/handler.ts +++ b/packages/cli/src/linter/css-vars/handler.ts @@ -15,6 +15,14 @@ import type { CssVarDeclaration, CssVarsEmitterSpec, CssVarsEmitterResult } from './spec.js'; import type { DesignSystemState, ResolvedDimension } from '../model/spec.js'; +/** + * Characters allowed in a CSS identifier: ASCII letters, digits, `-`, `_`, and + * non-ASCII characters. A token name is written into the stylesheet as part of + * a custom property name, so anything else (`;`, `}`, `:`, whitespace, ...) + * would end the declaration and add CSS of its own. + */ +const VALID_TOKEN_SEGMENT = /^(?:[a-zA-Z0-9_-]|[^\x00-\x7F])+$/; + /** * Pure function mapping DesignSystemState → CSS custom property declarations. * No side effects. @@ -23,6 +31,25 @@ export class CssVarsEmitterHandler implements CssVarsEmitterSpec { execute(state: DesignSystemState): CssVarsEmitterResult { const declarations: CssVarDeclaration[] = []; + const groups: ReadonlyArray]> = [ + ['colors', state.colors.keys()], + ['spacing', state.spacing.keys()], + ['rounded', state.rounded.keys()], + ]; + for (const [group, names] of groups) { + for (const name of names) { + if (!isValidTokenPath(name)) { + return { + success: false, + error: { + code: 'INVALID_TOKEN_NAME', + message: `Token name "${name}" in ${group} is not a valid CSS identifier for css-vars export (each dot-separated segment may contain only letters, digits, "-" and "_").`, + }, + }; + } + } + } + for (const [name, color] of state.colors) { declarations.push({ name: `color-${this.cssSafe(name)}`, @@ -62,3 +89,7 @@ export class CssVarsEmitterHandler implements CssVarsEmitterSpec { return name.replace(/\./g, '-'); } } + +function isValidTokenPath(name: string): boolean { + return name.split('.').every((segment) => VALID_TOKEN_SEGMENT.test(segment)); +}