Files
co-lite/solvway-admin-portal/app/tailwind3-classes.test.ts
ngqileandClaude Opus 5.5 6fda854df0 fix: neutral signed-out frame, visitor page polish and DESIGN.md update
Rest of the final-review fixes (the previous commit only removed the
duplicate breadcrumb test file, which moved into Breadcrumbs.test.tsx):
the signed-out frame drops the giraffe coat and 16px corners, the
loading icon spins, error headings fall back to the load-failed text,
invite registered and checked-in states are covered, the pinned-column
test renders a table, DESIGN.md describes the shipped look, and the
spec notes the pinned-column divider.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-12-01 23:44:36 +08:00

109 lines
4.5 KiB
TypeScript

// The ui/ primitives were generated by shadcn for Tailwind 4, but this portal builds with
// Tailwind 3.4. Tailwind silently drops any class it does not understand (outline-hidden,
// max-w-(--var), animate-in, an opacity modifier on a colour with no <alpha-value>), so a pasted
// v4 class compiles to nothing and nobody notices: the 1600px content width, the button hover and
// the input focus ring were all lost this way (fix-admin-shell-foundation). This test compiles
// every non-test .tsx under app/ with the real tailwind.config.ts and fails on any class-like
// token that produced no CSS.
import { readFileSync, globSync } from "fs";
import { resolve } from "path";
import postcss from "postcss";
import tailwindcss from "tailwindcss";
import { describe, expect, it } from "vitest";
import config from "../tailwind.config";
const ROOT = resolve(__dirname, "..");
const FILES = globSync("app/**/*.tsx", { cwd: ROOT }).filter((f) => !/\.test\.tsx$/.test(f)).sort();
// Classes defined by the project's own stylesheets (globals.css, org-chart.css) or used only as
// selectors/markers, not Tailwind utilities.
const PROJECT_CLASSES = new Set([
"tabular",
"scrollable",
"print-handover",
"print-picklist",
"admin-calendar",
"org-tree",
"dark",
"group",
"peer",
]);
const CLASS_TOKEN = /^!?-?[a-z@*[][a-z0-9:[\]()\-/.%#&_*@,'>+~=!]*$/;
/** Every string literal (and template-literal static part) inside [from, to) of src. */
function literalsIn(src: string, from: number, to: number): string[] {
const out: string[] = [];
for (let i = from; i < to; i++) {
const q = src[i];
if (q !== '"' && q !== "'" && q !== "`") continue;
let j = i + 1;
while (j < to && src[j] !== q) j += src[j] === "\\" ? 2 : 1;
const body = src.slice(i + 1, j);
// An object key ("icon-xs": …, a cva variant name) is not a class list.
if (/^\s*:/.test(src.slice(j + 1, j + 3))) {
i = j;
continue;
}
out.push(...(q === "`" ? body.split(/\$\{[^}]*\}/) : [body]));
i = j;
}
return out;
}
/** End index of the bracketed region opening at src[open] ("(" or "{"), skipping string contents. */
function regionEnd(src: string, open: number): number {
const close = src[open] === "(" ? ")" : "}";
let depth = 0;
for (let i = open; i < src.length; i++) {
const c = src[i];
if (c === '"' || c === "'" || c === "`") {
let j = i + 1;
while (j < src.length && src[j] !== c) j += src[j] === "\\" ? 2 : 1;
i = j;
} else if (c === src[open]) depth++;
else if (c === close && --depth === 0) return i;
}
return src.length;
}
// Class strings are the literals in a class context: className="…", className={…}, and the
// arguments of cn(…) / cva(…) (which covers cva variant maps). A literal there that is not
// class-shaped (a ternary's prose fallback, a variant key) is ignored.
function classTokens(src: string): string[] {
const literals: string[] = [];
for (const m of src.matchAll(/className="([^"]*)"/g)) literals.push(m[1]);
for (const m of src.matchAll(/className=\{|\b(?:cn|cva)\(/g)) {
const open = m.index! + m[0].length - 1;
literals.push(...literalsIn(src, open + 1, regionEnd(src, open)));
}
const tokens = literals
.map((s) => s.trim().split(/\s+/).filter(Boolean))
.filter((words) => words.length > 0 && words.every((w) => CLASS_TOKEN.test(w)))
// A lone plain word ("default", "popper") is a compared value, not a class; a lone real
// utility of that shape ("flex", "hidden") always compiles, so nothing is lost.
.filter((words) => words.length > 1 || /[-:[/]/.test(words[0]))
.flat();
return [...new Set(tokens)].filter((t) => {
const base = t.split(":").pop()!;
// group/name and peer/name only name a group for group-*/name variants; they emit no CSS.
return !PROJECT_CLASSES.has(base) && !/^(group|peer)\/[\w-]+$/.test(base);
});
}
// Tailwind escapes like cssesc: commas become the hex escape "\2c ".
const escapeClass = (c: string) =>
c.replace(/,/g, "\\2c ").replace(/([^a-zA-Z0-9_\\ -])/g, "\\$1");
describe.each(FILES)("%s under Tailwind 3", (file) => {
it("uses only classes that Tailwind 3 generates", async () => {
const src = readFileSync(resolve(ROOT, file), "utf8");
const { css } = await postcss([
tailwindcss({ ...config, content: [{ raw: src, extension: "tsx" }] }),
]).process("@tailwind base; @tailwind components; @tailwind utilities;", { from: undefined });
const missing = classTokens(src).filter((c) => !css.includes("." + escapeClass(c)));
expect(missing).toEqual([]);
});
});