From 8511e0c7204df1b219b08915f444dc9d450a21c4 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Wed, 13 May 2026 17:58:47 -0700 Subject: [PATCH] refactor: deduplicate CSS parser helpers and drop redundant base test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Collapse extractRootBlock + extractDarkBlock into a single extractBlock helper; the two functions shared identical body logic (same regex, same return) and differed only in the string they received — now the call sites pass the appropriate slice inline. - Remove the second test in base.test.ts ("covers all three form control selectors in one rule"): the first test's regex already asserts all three selectors appear together with font-size: 16px, so the individual /input/, /textarea/, /select/ matches added no coverage. --- tests/base.test.ts | 5 ----- tests/tokens.test.ts | 12 +++--------- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/tests/base.test.ts b/tests/base.test.ts index 5f99dc5..b81ec25 100644 --- a/tests/base.test.ts +++ b/tests/base.test.ts @@ -8,9 +8,4 @@ describe('base.css', () => { expect(css).toMatch(/input\s*,\s*textarea\s*,\s*select\s*\{[^}]*font-size\s*:\s*16px/); }); - it('covers all three form control selectors in one rule', () => { - expect(css).toMatch(/input/); - expect(css).toMatch(/textarea/); - expect(css).toMatch(/select/); - }); }); diff --git a/tests/tokens.test.ts b/tests/tokens.test.ts index 41bffa2..14e789d 100644 --- a/tests/tokens.test.ts +++ b/tests/tokens.test.ts @@ -5,17 +5,11 @@ const css = readFileSync(new URL('../dist/tokens.css', import.meta.url), 'utf-8' // ---- CSS parser helpers ---- -function extractRootBlock(src: string): string { +function extractBlock(src: string): string { const m = src.match(/:root\s*\{([^}]+)\}/); return m?.[1] ?? ''; } -function extractDarkBlock(src: string): string { - const afterMedia = src.split('@media (prefers-color-scheme: dark)')[1] ?? ''; - const m = afterMedia.match(/:root\s*\{([^}]+)\}/); - return m?.[1] ?? ''; -} - function parseProps(block: string): Map { const map = new Map(); const re = /--([\w-]+)\s*:\s*([^;]+);/g; @@ -63,8 +57,8 @@ function contrast(hex1: string, hex2: string): number { // ---- Fixtures ---- -const rootProps = parseProps(extractRootBlock(css)); -const darkProps = parseProps(extractDarkBlock(css)); +const rootProps = parseProps(extractBlock(css)); +const darkProps = parseProps(extractBlock(css.split('@media (prefers-color-scheme: dark)')[1] ?? '')); // Merged: dark overrides on top of light primitives so var() refs resolve correctly. const mergedDark = new Map([...rootProps, ...darkProps]);