|
| 1 | +import { readdirSync, readFileSync, statSync } from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | +import { describe, expect, it } from "vitest"; |
| 4 | + |
| 5 | +const WEB_SRC_ROOT = path.resolve(import.meta.dirname, "../.."); |
| 6 | + |
| 7 | +const REQUIRED_ANCESTORS = { |
| 8 | + AutocompleteGroupLabel: "AutocompleteGroup", |
| 9 | + ComboboxGroupLabel: "ComboboxGroup", |
| 10 | + MenuGroupLabel: "MenuGroup", |
| 11 | + MenuRadioItem: "MenuRadioGroup", |
| 12 | + MenuSubPopup: "MenuSub", |
| 13 | + MenuSubTrigger: "MenuSub", |
| 14 | + SelectGroupLabel: "SelectGroup", |
| 15 | +} as const; |
| 16 | + |
| 17 | +const TARGET_COMPONENTS = new Set(Object.keys(REQUIRED_ANCESTORS)); |
| 18 | +const JSX_TAG_PATTERN = /<(?<closing>\/)?(?<name>[A-Z][A-Za-z0-9]*)\b[^>]*?(?<selfClosing>\/)?>/g; |
| 19 | + |
| 20 | +const WRAPPER_SOURCE_ASSERTIONS = [ |
| 21 | + { |
| 22 | + filePath: path.resolve(import.meta.dirname, "./select.tsx"), |
| 23 | + message: "Select item wrappers keep item indicator and text inside SelectPrimitive.Item", |
| 24 | + patterns: [ |
| 25 | + /<SelectPrimitive\.Item\b[\s\S]*?<SelectPrimitive\.ItemIndicator\b[\s\S]*?<\/SelectPrimitive\.ItemIndicator>[\s\S]*?<SelectPrimitive\.ItemText\b[\s\S]*?<\/SelectPrimitive\.ItemText>[\s\S]*?<\/SelectPrimitive\.Item>/, |
| 26 | + ], |
| 27 | + }, |
| 28 | + { |
| 29 | + filePath: path.resolve(import.meta.dirname, "./combobox.tsx"), |
| 30 | + message: "Combobox item wrappers keep item indicators inside ComboboxPrimitive.Item", |
| 31 | + patterns: [ |
| 32 | + /<ComboboxPrimitive\.Item\b[\s\S]*?<ComboboxPrimitive\.ItemIndicator\b[\s\S]*?<\/ComboboxPrimitive\.ItemIndicator>[\s\S]*?<\/ComboboxPrimitive\.Item>/, |
| 33 | + ], |
| 34 | + }, |
| 35 | + { |
| 36 | + filePath: path.resolve(import.meta.dirname, "./menu.tsx"), |
| 37 | + message: "Menu item wrappers keep radio and checkbox indicators inside their item parents", |
| 38 | + patterns: [ |
| 39 | + /<MenuPrimitive\.RadioItem\b[\s\S]*?<MenuPrimitive\.RadioItemIndicator\b[\s\S]*?<\/MenuPrimitive\.RadioItemIndicator>[\s\S]*?<\/MenuPrimitive\.RadioItem>/, |
| 40 | + /<MenuPrimitive\.CheckboxItem\b[\s\S]*?<MenuPrimitive\.CheckboxItemIndicator\b[\s\S]*?<\/MenuPrimitive\.CheckboxItemIndicator>[\s\S]*?<\/MenuPrimitive\.CheckboxItem>/, |
| 41 | + ], |
| 42 | + }, |
| 43 | + { |
| 44 | + filePath: path.resolve(import.meta.dirname, "./scroll-area.tsx"), |
| 45 | + message: "ScrollArea wrappers keep thumbs inside scrollbars", |
| 46 | + patterns: [ |
| 47 | + /<ScrollAreaPrimitive\.Scrollbar\b[\s\S]*?<ScrollAreaPrimitive\.Thumb\b[\s\S]*?\/>[\s\S]*?<\/ScrollAreaPrimitive\.Scrollbar>/, |
| 48 | + ], |
| 49 | + }, |
| 50 | +] as const; |
| 51 | + |
| 52 | +function collectTsxFiles(rootDir: string): string[] { |
| 53 | + const entries = readdirSync(rootDir); |
| 54 | + const files: string[] = []; |
| 55 | + |
| 56 | + for (const entry of entries) { |
| 57 | + const absolutePath = path.join(rootDir, entry); |
| 58 | + const stats = statSync(absolutePath); |
| 59 | + |
| 60 | + if (stats.isDirectory()) { |
| 61 | + files.push(...collectTsxFiles(absolutePath)); |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + if ( |
| 66 | + absolutePath.endsWith(".tsx") && |
| 67 | + !absolutePath.includes(`${path.sep}components${path.sep}ui${path.sep}`) |
| 68 | + ) { |
| 69 | + files.push(absolutePath); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return files; |
| 74 | +} |
| 75 | + |
| 76 | +function findMissingAncestorViolations(source: string): string[] { |
| 77 | + const stack: string[] = []; |
| 78 | + const violations: string[] = []; |
| 79 | + |
| 80 | + for (const match of source.matchAll(JSX_TAG_PATTERN)) { |
| 81 | + const name = match.groups?.name; |
| 82 | + if (!name) continue; |
| 83 | + |
| 84 | + const isClosing = Boolean(match.groups?.closing); |
| 85 | + const isSelfClosing = Boolean(match.groups?.selfClosing); |
| 86 | + |
| 87 | + if (isClosing) { |
| 88 | + const lastIndex = stack.lastIndexOf(name); |
| 89 | + if (lastIndex >= 0) { |
| 90 | + stack.splice(lastIndex, 1); |
| 91 | + } |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + if (TARGET_COMPONENTS.has(name)) { |
| 96 | + const requiredAncestor = REQUIRED_ANCESTORS[name as keyof typeof REQUIRED_ANCESTORS]; |
| 97 | + if (!stack.includes(requiredAncestor)) { |
| 98 | + violations.push(`${name} must be rendered within ${requiredAncestor}`); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if (!isSelfClosing) { |
| 103 | + stack.push(name); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return violations; |
| 108 | +} |
| 109 | + |
| 110 | +describe("Base UI wrapper invariants", () => { |
| 111 | + it("keeps label and item wrapper parts inside their required parent components", () => { |
| 112 | + const failures = collectTsxFiles(WEB_SRC_ROOT).flatMap((filePath) => { |
| 113 | + const source = readFileSync(filePath, "utf8"); |
| 114 | + const violations = findMissingAncestorViolations(source); |
| 115 | + |
| 116 | + return violations.map( |
| 117 | + (violation) => `${path.relative(WEB_SRC_ROOT, filePath)}: ${violation}`, |
| 118 | + ); |
| 119 | + }); |
| 120 | + |
| 121 | + expect(failures).toEqual([]); |
| 122 | + }); |
| 123 | + |
| 124 | + it("keeps second-tier primitive relationships intact inside shared UI wrappers", () => { |
| 125 | + const failures = WRAPPER_SOURCE_ASSERTIONS.flatMap(({ filePath, message, patterns }) => { |
| 126 | + const source = readFileSync(filePath, "utf8"); |
| 127 | + const missingPattern = patterns.some((pattern) => !pattern.test(source)); |
| 128 | + |
| 129 | + if (!missingPattern) { |
| 130 | + return []; |
| 131 | + } |
| 132 | + |
| 133 | + return `${path.relative(WEB_SRC_ROOT, filePath)}: ${message}`; |
| 134 | + }); |
| 135 | + |
| 136 | + expect(failures).toEqual([]); |
| 137 | + }); |
| 138 | +}); |
0 commit comments