|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * objectui#3025 ratchet (AGENTS.md Commandment #6 — "No `any`"). Same shape as |
| 6 | + * the ADR-0054 and #2269 ratchets: runs in the gating `pnpm test` job and fails |
| 7 | + * if the anti-pattern reappears. |
| 8 | + * |
| 9 | + * The bug this guards. Two call sites carried |
| 10 | + * `const PanelGroup = ResizablePanelGroup as React.FC<any>`. The cast erased |
| 11 | + * the component's props to `any`, so when react-resizable-panels v4 renamed |
| 12 | + * `PanelGroup`'s `direction` prop to `orientation`, the stale `direction` kept |
| 13 | + * type-checking against nothing — for a whole major version. It silently became |
| 14 | + * a dead prop that React forwarded to the DOM as a stray attribute. Removing |
| 15 | + * the casts is what turned it back into a hard `TS2322`. |
| 16 | + * |
| 17 | + * Both comments blamed the toolchain (vite-plugin-dts "not resolving the prop |
| 18 | + * type correctly"; a prop that "does not always narrow cleanly in our TS |
| 19 | + * config"). Neither was true. That is the real hazard here: a cast like this |
| 20 | + * reads as a workaround for a tooling quirk while actually disabling the check |
| 21 | + * that would have caught a breaking upstream rename. |
| 22 | + * |
| 23 | + * If this fails: do not widen a component's props to `any` to make an error go |
| 24 | + * away. Read the dependency's current prop types — the prop was probably |
| 25 | + * renamed or removed. If a genuinely-invalid prop must be passed, type the |
| 26 | + * escape hatch narrowly (`as ComponentProps<typeof X> & { extra?: T }`), never |
| 27 | + * with a blanket `any`. |
| 28 | + * |
| 29 | + * SCOPE — repo-wide over `packages/<pkg>/src` and `apps/<app>/src`, production |
| 30 | + * sources only. Test files are excluded, both to match the existing ratchets |
| 31 | + * and because this file necessarily contains the pattern in its own regex. |
| 32 | + */ |
| 33 | + |
| 34 | +import { describe, it, expect } from 'vitest'; |
| 35 | +import { readdirSync, readFileSync, statSync } from 'node:fs'; |
| 36 | +import path from 'node:path'; |
| 37 | +import { fileURLToPath } from 'node:url'; |
| 38 | + |
| 39 | +const here = path.dirname(fileURLToPath(import.meta.url)); |
| 40 | +// packages/app-shell/src -> repo root |
| 41 | +const repoRoot = path.resolve(here, '../../..'); |
| 42 | + |
| 43 | +/** |
| 44 | + * Banned: casting a value to a component type parameterised with `any`. |
| 45 | + * |
| 46 | + * Covers the three spellings that erase props identically — `FC`, |
| 47 | + * `FunctionComponent`, `ComponentType` — with or without the `React.` |
| 48 | + * qualifier, and tolerant of whitespace inside the type arguments. |
| 49 | + */ |
| 50 | +const COMPONENT_ANY_CAST = |
| 51 | + /\bas\s+(?:React\.)?(?:FC|FunctionComponent|ComponentType)\s*<\s*any\s*>/; |
| 52 | + |
| 53 | +function collectSourceFiles(): string[] { |
| 54 | + const out: string[] = []; |
| 55 | + const walk = (dir: string) => { |
| 56 | + for (const entry of readdirSync(dir, { withFileTypes: true })) { |
| 57 | + const name = entry.name; |
| 58 | + if (name === 'node_modules' || name === 'dist' || name.startsWith('.wt-') || name === '.next') { |
| 59 | + continue; |
| 60 | + } |
| 61 | + const full = path.join(dir, name); |
| 62 | + if (entry.isDirectory()) { |
| 63 | + walk(full); |
| 64 | + } else if (/\.tsx?$/.test(name) && !/\.(test|spec)\.tsx?$/.test(name)) { |
| 65 | + out.push(full); |
| 66 | + } |
| 67 | + } |
| 68 | + }; |
| 69 | + for (const group of ['packages', 'apps']) { |
| 70 | + const groupDir = path.join(repoRoot, group); |
| 71 | + let pkgs: string[]; |
| 72 | + try { |
| 73 | + pkgs = readdirSync(groupDir); |
| 74 | + } catch { |
| 75 | + continue; |
| 76 | + } |
| 77 | + for (const pkg of pkgs) { |
| 78 | + const srcDir = path.join(groupDir, pkg, 'src'); |
| 79 | + try { |
| 80 | + if (statSync(srcDir).isDirectory()) walk(srcDir); |
| 81 | + } catch { |
| 82 | + /* package has no src/ */ |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + return out; |
| 87 | +} |
| 88 | + |
| 89 | +describe('objectui#3025 — no component-props-to-any cast ratchet', () => { |
| 90 | + it('finds the repo sources (guards against a broken scan path)', () => { |
| 91 | + // The repo has thousands of source files; if this collapses, the scan |
| 92 | + // globs have gone stale and the ratchet would silently pass on nothing. |
| 93 | + expect(collectSourceFiles().length).toBeGreaterThan(500); |
| 94 | + }); |
| 95 | + |
| 96 | + it('detects the pattern it is meant to ban (guards against a dead regex)', () => { |
| 97 | + // A ratchet whose regex silently stops matching is worse than no ratchet, |
| 98 | + // so pin it against the exact casts #3025 removed, plus spelling variants. |
| 99 | + for (const sample of [ |
| 100 | + 'const PanelGroup = ResizablePanelGroup as React.FC<any>;', |
| 101 | + 'const X = Y as FC<any>', |
| 102 | + 'const X = Y as React.ComponentType< any >', |
| 103 | + 'const X = Y as FunctionComponent<any>', |
| 104 | + ]) { |
| 105 | + expect(COMPONENT_ANY_CAST.test(sample), sample).toBe(true); |
| 106 | + } |
| 107 | + // ...and must not fire on legitimate neighbours. |
| 108 | + for (const ok of [ |
| 109 | + 'const x = y as Record<string, any>', |
| 110 | + 'const x = y as ComponentProps<typeof Button>', |
| 111 | + 'const C: React.FC<Props> = (p) => null', |
| 112 | + ]) { |
| 113 | + expect(COMPONENT_ANY_CAST.test(ok), ok).toBe(false); |
| 114 | + } |
| 115 | + }); |
| 116 | + |
| 117 | + it('has zero component-props-to-any casts in production sources', () => { |
| 118 | + const offenders: string[] = []; |
| 119 | + for (const file of collectSourceFiles()) { |
| 120 | + const src = readFileSync(file, 'utf8'); |
| 121 | + for (const line of src.split('\n')) { |
| 122 | + if (COMPONENT_ANY_CAST.test(line)) { |
| 123 | + offenders.push(`${path.relative(repoRoot, file)} :: ${line.trim()}`); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + // If this fails: you widened a component's props to `any`. That disables |
| 128 | + // the check that catches renamed/removed props across a dependency major — |
| 129 | + // exactly how #3025's dead `direction` prop survived. Read the current |
| 130 | + // prop types instead, or type the escape hatch narrowly. Do not allowlist. |
| 131 | + expect(offenders).toEqual([]); |
| 132 | + }); |
| 133 | +}); |
0 commit comments