Phone hit kit: read the className expression, not its source text
Some checks failed
Build pull request / Build pull request (pull_request) Has been cancelled

A PhoneHit name inside a string, built by concatenation, behind ?:/&&/||/??, or
followed by a props spread that can override className no longer satisfies the pin.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Void 2026-11-13 07:21:00 +00:00
commit d13cbc9b2e
2 changed files with 88 additions and 2 deletions

View file

@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { drawnPx, expectSizeTie, iconButtons } from './phoneHitTestKit';
import { drawnPx, expectSizeTie, iconButtons, phoneHitOf } from './phoneHitTestKit';
describe('phone hit test kit', () => {
it('reads <IconButton only, not a longer component name', () => {
@ -38,4 +38,49 @@ describe('phone hit test kit', () => {
it('fails a file with no IconButton, which would otherwise pass vacuously', () => {
expect(() => expectSizeTie('styles/PhoneHit.css.ts')).toThrow(/no IconButton/);
});
describe('phoneHitOf reads the imported class, applied unconditionally', () => {
const tag = (expr: string) => `<IconButton size="300" className={${expr}}>`;
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');
});
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 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();
});
it('rejects a member of another object and a tag without className', () => {
expect(phoneHitOf(tag('other.PhoneHit'))).toBeNull();
expect(phoneHitOf('<IconButton size="300">')).toBeNull();
});
it('rejects a spread after className, which can override it, and accepts one before', () => {
expect(phoneHitOf('<IconButton className={PhoneHit} {...props}>')).toBeNull();
expect(phoneHitOf('<IconButton className={PhoneHit} size="300" {...props} />')).toBeNull();
expect(phoneHitOf('<IconButton {...props} className={PhoneHit}>')).toBe('PhoneHit');
});
it('reads a className past braces inside strings', () => {
expect(phoneHitOf(tag("classNames('{', PhoneHit)"))).toBe('PhoneHit');
});
});
});

View file

@ -85,7 +85,48 @@ export const iconButtons = (src: string) =>
export const iconButtonTags = (src: string) => iconButtons(src).map((b) => b.tag);
const phoneHitOf = (tag: string) => tag.match(/className=\{[^}]*?\b(PhoneHit\w*)\b/)?.[1] ?? null;
/** The expression of a tag's className={...} and the index of its closing brace, read to the balanced brace
* (strings and templates may hold braces); undefined when the tag has none. */
const classNameExpr = (tag: string) => {
const m = /\sclassName=\{/.exec(tag);
if (!m) return undefined;
const from = m.index + m[0].length;
let depth = 1;
let quote = '';
for (let j = from; j < tag.length; j += 1) {
const c = tag[j];
if (quote) {
if (c === '\\') j += 1;
else if (c === quote) quote = '';
} else if (c === '"' || c === "'" || c === '`') quote = c;
else if (c === '{') depth += 1;
else if (c === '}' && (depth -= 1) === 0) return { expr: tag.slice(from, j), end: j };
}
return undefined;
};
/** The expression with its string literals and the literal text of its templates removed (a `${...}` keeps its
* expression), so only identifiers and operators remain. */
const withoutLiterals = (expr: string) =>
expr
.replace(/'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"/g, ' ')
.replace(/`((?:[^`\\]|\\.)*)`/g, (_, body: string) =>
[...body.matchAll(/\$\{([^}]*)\}/g)].map((t) => ` ${t[1]} `).join(' ')
);
/** The PhoneHit* class a tag carries: a bare identifier in its className expression, applied unconditionally. A
* name inside a string, built by concatenation, or behind ?:, &&, || or ??, or followed by a {...spread}, is not the
* imported class (or not always applied), so it reads as null. */
export const phoneHitOf = (tag: string) => {
const found = classNameExpr(tag);
if (found === undefined) return null;
// props spread in after className can override it at runtime; before it they cannot
if (tag.slice(found.end).includes('{...')) return null;
const bare = withoutLiterals(found.expr);
if (/\?|&&|\|\|/.test(bare)) return null;
const names = [...bare.matchAll(/(?<![\w$.])(PhoneHit\w*)(?![\w$])/g)].map((m) => m[1]);
return names.length === 1 ? names[0] : null;
};
/** The class each IconButton must carry, in source order: the variant is chosen by the button's drawn size and
* whether a clipping container or neighbours surround it, so a swapped variant is a wrong hit area. Also checks