|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { readFileSync, readdirSync, statSync } from "fs"; |
| 3 | +import { resolve, join } from "path"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Regression tests for issue #101: TypeError in Node.contains() from event handlers. |
| 7 | + * |
| 8 | + * MouseEvent.relatedTarget and Event.target are typed as EventTarget | null, |
| 9 | + * which can be a non-Node object (e.g. when mouse leaves to a cross-origin |
| 10 | + * iframe). Casting these to Node/HTMLElement and passing them to Node.contains() |
| 11 | + * or Element.closest() throws a TypeError. |
| 12 | + * |
| 13 | + * These tests scan source files for unsafe `as Node` or `as HTMLElement` casts |
| 14 | + * on event target properties, ensuring instanceof guards are used instead. |
| 15 | + */ |
| 16 | + |
| 17 | +/** Recursively collect .ts/.tsx files under a directory */ |
| 18 | +function collectSourceFiles(dir: string): string[] { |
| 19 | + const files: string[] = []; |
| 20 | + for (const entry of readdirSync(dir)) { |
| 21 | + const full = join(dir, entry); |
| 22 | + if (statSync(full).isDirectory()) { |
| 23 | + files.push(...collectSourceFiles(full)); |
| 24 | + } else if (/\.tsx?$/.test(entry) && !entry.includes(".test.")) { |
| 25 | + files.push(full); |
| 26 | + } |
| 27 | + } |
| 28 | + return files; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Matches patterns like: |
| 33 | + * event.target as Node |
| 34 | + * event.relatedTarget as HTMLElement |
| 35 | + * e.target as HTMLElement | null |
| 36 | + * event.relatedTarget as HTMLElement | null |
| 37 | + * |
| 38 | + * These are unsafe because EventTarget is not necessarily a Node. |
| 39 | + */ |
| 40 | +const UNSAFE_EVENT_TARGET_CAST = |
| 41 | + /\.\s*(?:target|relatedTarget)\s+as\s+(?:Node|HTMLElement)(?:\s*\|\s*null)?/g; |
| 42 | + |
| 43 | +describe("DOM API type safety — no unsafe event target casts", () => { |
| 44 | + const srcRoot = resolve(__dirname, "../.."); |
| 45 | + const sourceFiles = collectSourceFiles(join(srcRoot, "components")); |
| 46 | + |
| 47 | + it("no files cast event.target or event.relatedTarget as Node/HTMLElement", () => { |
| 48 | + const violations: string[] = []; |
| 49 | + |
| 50 | + for (const filePath of sourceFiles) { |
| 51 | + const content = readFileSync(filePath, "utf-8"); |
| 52 | + const lines = content.split("\n"); |
| 53 | + |
| 54 | + for (let i = 0; i < lines.length; i++) { |
| 55 | + const matches = lines[i].match(UNSAFE_EVENT_TARGET_CAST); |
| 56 | + if (matches) { |
| 57 | + const relative = filePath.replace(srcRoot + "/", ""); |
| 58 | + violations.push(`${relative}:${i + 1}: ${matches[0].trim()}`); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + expect( |
| 64 | + violations, |
| 65 | + "Use `instanceof Node` or `instanceof HTMLElement` guards instead of `as` casts on event targets. " + |
| 66 | + "See issue #101.\n\nViolations:\n" + |
| 67 | + violations.join("\n") |
| 68 | + ).toHaveLength(0); |
| 69 | + }); |
| 70 | +}); |
0 commit comments