Phone hit kit: read IconButton className off the TypeScript AST
All checks were successful
Build pull request / Build pull request (pull_request) Successful in 3m53s

className must appear once, with no spread after it, as the imported PhoneHit*
identifier bare, a classNames/clsx/cx direct argument, or a template span, and
the identifier must occur nowhere else in the expression. Tag enumeration uses
the same AST. expectPhoneHitClasses takes (string|null)[] (null = no class), as
the phone-hit-pages branch does, and is exercised by a test on a temp source.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Void 2026-11-13 07:28:37 +00:00
commit 4eaa6f9c55
2 changed files with 191 additions and 84 deletions

View file

@ -1,5 +1,14 @@
import { describe, expect, it } from 'vitest';
import { drawnPx, expectSizeTie, iconButtons, phoneHitOf } from './phoneHitTestKit';
import { mkdtempSync, rmSync, writeFileSync } from 'fs';
import { tmpdir } from 'os';
import { relative } from 'path';
import {
drawnPx,
expectPhoneHitClasses,
expectSizeTie,
iconButtons,
phoneHitOf,
} from './phoneHitTestKit';
describe('phone hit test kit', () => {
it('reads <IconButton only, not a longer component name', () => {
@ -39,38 +48,60 @@ describe('phone hit test kit', () => {
expect(() => expectSizeTie('styles/PhoneHit.css.ts')).toThrow(/no IconButton/);
});
describe('phoneHitOf reads the imported class, applied unconditionally', () => {
describe('phoneHitOf reads the imported class off the AST, applied unconditionally', () => {
const tag = (expr: string) => `<IconButton size="300" className={${expr}}>`;
const ok = (expr: string, name: string) => expect(phoneHitOf(tag(expr))).toBe(name);
const no = (expr: string) => expect(phoneHitOf(tag(expr))).toBeNull();
it('accepts the bare identifier and merges that always include it', () => {
expect(phoneHitOf(tag('PhoneHitSpread26'))).toBe('PhoneHitSpread26');
expect(phoneHitOf(tag('classNames(PhoneHitRoom36, css.other)'))).toBe('PhoneHitRoom36');
expect(phoneHitOf(tag('`${PhoneHit} ${css.other}`'))).toBe('PhoneHit');
expect(phoneHitOf(tag("[PhoneHit, 'a b'].join(' ')"))).toBe('PhoneHit');
expect(phoneHitOf(tag('classNames(css.a, { [css.b]: on }, PhoneHit)'))).toBe('PhoneHit');
ok('PhoneHitSpread26', 'PhoneHitSpread26');
ok('classNames(PhoneHitRoom36, css.other)', 'PhoneHitRoom36');
ok('clsx(css.a, { [css.b]: on }, PhoneHit)', 'PhoneHit');
ok('cx(PhoneHit)', 'PhoneHit');
ok('`${PhoneHit} ${css.other}`', 'PhoneHit');
ok('classNames(/* PhoneHit */ PhoneHit)', 'PhoneHit');
});
it('rejects the name as a string literal', () => {
expect(phoneHitOf(tag("'PhoneHitSpread26'"))).toBeNull();
expect(phoneHitOf(tag('`PhoneHitSpread26`'))).toBeNull();
});
it('rejects a name built by concatenation', () => {
expect(phoneHitOf(tag('"PhoneHitSpread26" + "x"'))).toBeNull();
expect(phoneHitOf(tag("'PhoneHit' + 'Spread26'"))).toBeNull();
it('rejects the name as a string, a concatenation or a comment', () => {
no("'PhoneHitSpread26'");
no('`PhoneHitSpread26`');
no('"PhoneHitSpread26" + "x"');
no("'PhoneHit' + 'Spread26'");
no('/* PhoneHit */ undefined');
no('css.a // PhoneHit');
});
it('rejects a conditionally applied name', () => {
expect(phoneHitOf(tag('cond ? PhoneHitSpread26 : undefined'))).toBeNull();
expect(phoneHitOf(tag('cond && PhoneHitSpread26'))).toBeNull();
expect(phoneHitOf(tag('cond || PhoneHitSpread26'))).toBeNull();
expect(phoneHitOf(tag('maybe ?? PhoneHitSpread26'))).toBeNull();
expect(phoneHitOf(tag("classNames(cond ? PhoneHit : '', css.a)"))).toBeNull();
no('cond ? PhoneHitSpread26 : undefined');
no('cond && PhoneHitSpread26');
no('cond || PhoneHitSpread26');
no('maybe ?? PhoneHitSpread26');
no("classNames(cond ? PhoneHit : '', css.a)");
no('classNames(PhoneHit, cond && PhoneHit)');
});
it('rejects a member of another object and a tag without className', () => {
expect(phoneHitOf(tag('other.PhoneHit'))).toBeNull();
it('rejects every other expression shape the name can hide in', () => {
no('classNames({ PhoneHit: on })');
no('clsx({ [PhoneHit]: on })');
no('`${`${PhoneHit}`}`');
no('typeof PhoneHit');
no("(PhoneHit = 'x')");
no('(PhoneHit, css.a)');
no('(PhoneHit)');
no('() => PhoneHit');
no('PhoneHit.foo');
no('other.PhoneHit');
no('classNames(css.a, [PhoneHit])');
no('wrap(PhoneHit)');
no('PhoneHitSpread26é');
no('`${css.a}`');
});
it('rejects a tag without className, with two, or with a non-expression one', () => {
expect(phoneHitOf('<IconButton size="300">')).toBeNull();
expect(phoneHitOf('<IconButton className={PhoneHit} className={css.a}>')).toBeNull();
expect(phoneHitOf('<IconButton className="PhoneHit">')).toBeNull();
expect(phoneHitOf('<IconButton className={PhoneHit} />')).toBe('PhoneHit');
});
it('rejects a spread after className, which can override it, and accepts one before', () => {
@ -80,7 +111,44 @@ describe('phone hit test kit', () => {
});
it('reads a className past braces inside strings', () => {
expect(phoneHitOf(tag("classNames('{', PhoneHit)"))).toBe('PhoneHit');
ok("classNames('{', PhoneHit)", 'PhoneHit');
});
});
describe('expectPhoneHitClasses', () => {
const check = (src: string, want: (string | null)[]) => {
const dir = mkdtempSync(`${tmpdir()}/phone-hit-kit-`);
try {
writeFileSync(`${dir}/Probe.tsx`, src);
expectPhoneHitClasses(relative(`${process.cwd()}/src/app`, `${dir}/Probe.tsx`), want);
} finally {
rmSync(dir, { recursive: true });
}
};
const imp = "import { PhoneHit, PhoneHitSpread26 } from '../../styles/PhoneHit.css';\n";
const src =
imp +
'const A = () => (<><IconButton className={PhoneHit} /><IconButton className={classNames(PhoneHitSpread26, a)} /><IconButton /></>);';
it('pins each button in order, null meaning no class', () => {
check(src, ['PhoneHit', 'PhoneHitSpread26', null]);
expect(() => check(src, ['PhoneHit', 'PhoneHit', null])).toThrow();
expect(() => check(src, ['PhoneHit', 'PhoneHitSpread26', 'PhoneHit'])).toThrow();
expect(() => check(src, ['PhoneHit', 'PhoneHitSpread26'])).toThrow();
});
it('fails a name that is not read from PhoneHit.css or is applied conditionally', () => {
const local = "const PhoneHit = 'x';\n<IconButton className={PhoneHit} />";
expect(() => check(local, ['PhoneHit'])).toThrow();
expect(() => check(local, [null])).not.toThrow();
const cond = imp + '<IconButton className={on ? PhoneHit : undefined} />';
expect(() => check(cond, ['PhoneHit'])).toThrow();
const aliased =
"import { Other as PhoneHit } from '../../styles/PhoneHit.css';\n<IconButton className={PhoneHit} />";
expect(() => check(aliased, ['PhoneHit'])).toThrow();
const wrongModule =
"import { PhoneHit } from './other';\n<IconButton className={PhoneHit} />";
expect(() => check(wrongModule, ['PhoneHit'])).toThrow();
});
});
});