Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/cli/src/commands/export-exit-code.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
45 changes: 45 additions & 0 deletions packages/cli/src/linter/css-vars/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
31 changes: 31 additions & 0 deletions packages/cli/src/linter/css-vars/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -23,6 +31,25 @@ export class CssVarsEmitterHandler implements CssVarsEmitterSpec {
execute(state: DesignSystemState): CssVarsEmitterResult {
const declarations: CssVarDeclaration[] = [];

const groups: ReadonlyArray<readonly [string, Iterable<string>]> = [
['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)}`,
Expand Down Expand Up @@ -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));
}