Public: anyone can read this; the crew can change it.
Red after landing: test/worker-database-opens.test.ts fails on main #12825
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/12806-red-after-landing-test-worker-database-o → main 1 commit fork
1 file
changed +30
−10
test/worker-database-opens.test.ts +30 −10
| @@ -76,18 +76,20 @@ const NAMED = /(?:\bnew Worker|\bworkerAsker(?:<[^(]*>)?)\(\s*new URL\(\s*["']([ | ||
| 76 | 76 | const TYPE_ONLY = /^\s*(?:import|export)\s+type\s[^;]*?from\s*["'][^"']+["'];?\s*$/gm; |
| 77 | 77 | |
| 78 | 78 | /** `text` without its comment lines. */ |
| 79 | const stripped = new Map<string, string>(); | |
| 79 | 80 | function code(text: string): string { |
| 80 | return text | |
| 81 | .split("\n") | |
| 82 | .filter((raw) => { | |
| 83 | const line = raw.trim(); | |
| 84 | return !(line.startsWith("//") || line.startsWith("*") || line.startsWith("/*")); | |
| 85 | }) | |
| 86 | .map((raw) => raw.replace(/\s\/\/.*$/, "")) | |
| 87 | .join("\n"); | |
| 81 | const seen = stripped.get(text); | |
| 82 | if (seen !== undefined) return seen; | |
| 83 | const out = strip(text); | |
| 84 | stripped.set(text, out); | |
| 85 | return out; | |
| 88 | 86 | } |
| 89 | 87 | |
| 90 | const opensIn = (text: string): number => [...code(text).matchAll(OPEN)].length; | |
| 88 | function strip(text: string): string { | |
| 89 | return text.replace(/^[ \t]*(?:\/\/|\*|\/\*).*$/gm, "").replace(/\s\/\/.*$/gm, ""); | |
| 90 | } | |
| 91 | ||
| 92 | const opensIn = (text: string): number => (text.includes("Database") ? [...code(text).matchAll(OPEN)].length : 0); | |
| 91 | 93 | |
| 92 | 94 | /** |
| 93 | 95 | * The Worker modules under `files` and the file that starts each; `unnamed` are Workers made |
| @@ -97,6 +99,7 @@ function workers(files: ReadonlyMap<string, string>): { entries: Map<string, str | ||
| 97 | 99 | const entries = new Map<string, string>(); |
| 98 | 100 | const unnamed: string[] = []; |
| 99 | 101 | for (const [path, text] of files) { |
| 102 | if (!text.includes("Worker") && !text.includes("workerAsker")) continue; | |
| 100 | 103 | const body = code(text); |
| 101 | 104 | const named = [...body.matchAll(NAMED)]; |
| 102 | 105 | for (const match of named) { |
| @@ -110,6 +113,17 @@ function workers(files: ReadonlyMap<string, string>): { entries: Map<string, str | ||
| 110 | 113 | return { entries, unnamed }; |
| 111 | 114 | } |
| 112 | 115 | |
| 116 | const imported = new Map<string, string[]>(); | |
| 117 | function importsFrom(file: string, text: string): string[] { | |
| 118 | let found = imported.get(`${file}\0${text}`); | |
| 119 | if (found === undefined) { | |
| 120 | const body = code(text); | |
| 121 | found = importsOf(/\b(?:import|export)\s+type\s/.test(body) ? body.replace(TYPE_ONLY, "") : body, file); | |
| 122 | imported.set(`${file}\0${text}`, found); | |
| 123 | } | |
| 124 | return found; | |
| 125 | } | |
| 126 | ||
| 113 | 127 | /** Every file `entry` loads when it runs: its imports and theirs, types aside. */ |
| 114 | 128 | function reach(entry: string, files: ReadonlyMap<string, string>): Set<string> { |
| 115 | 129 | const seen = new Set<string>([entry]); |
| @@ -117,7 +131,7 @@ function reach(entry: string, files: ReadonlyMap<string, string>): Set<string> { | ||
| 117 | 131 | for (let file = todo.pop(); file !== undefined; file = todo.pop()) { |
| 118 | 132 | const text = files.get(file); |
| 119 | 133 | if (text === undefined) continue; |
| 120 | for (const found of importsOf(code(text).replace(TYPE_ONLY, ""), file)) { | |
| 134 | for (const found of importsFrom(file, text)) { | |
| 121 | 135 | const path = files.has(found) ? found : `${found}.ts`; |
| 122 | 136 | if (seen.has(path)) continue; |
| 123 | 137 | seen.add(path); |
| @@ -186,7 +200,13 @@ function audit(files: ReadonlyMap<string, string>, lists: Lists): string[] { | ||
| 186 | 200 | return problems; |
| 187 | 201 | } |
| 188 | 202 | |
| 203 | let scanned: Map<string, string> | undefined; | |
| 189 | 204 | function srcFiles(): Map<string, string> { |
| 205 | scanned ??= readSrc(); | |
| 206 | return scanned; | |
| 207 | } | |
| 208 | ||
| 209 | function readSrc(): Map<string, string> { | |
| 190 | 210 | const root = join(import.meta.dir, ".."); |
| 191 | 211 | const files = new Map<string, string>(); |
| 192 | 212 | for (const path of new Bun.Glob("src/**/*.{ts,tsx}").scanSync({ cwd: root })) { |
new above
offline