(code)
| 203 | * array of `{ name, line }` objects for each finding. |
| 204 | */ |
| 205 | export function findUnmemoizedClosureDeps(code) { |
| 206 | if (!code) return []; |
| 207 | |
| 208 | const lines = code.split("\n"); |
| 209 | |
| 210 | // Pass 1: collect every name used in a $[N] !== name dep check. |
| 211 | const depNames = new Set(); |
| 212 | for (const line of lines) { |
| 213 | for (const m of line.matchAll(DEP_CHECK)) { |
| 214 | depNames.add(m[1]); |
| 215 | } |
| 216 | } |
| 217 | if (depNames.size === 0) return []; |
| 218 | |
| 219 | // Pass 2: find closure definitions that are directly assigned a |
| 220 | // function value (not assigned from a temp like `const x = t1`). |
| 221 | // A memoized closure uses the temp pattern: |
| 222 | // if ($[N] !== dep) { t1 = () => {...}; } else { t1 = $[N]; } |
| 223 | // const name = t1; |
| 224 | // An unmemoized closure is assigned the function directly: |
| 225 | // const name = () => {...}; |
| 226 | const findings = []; |
| 227 | for (let i = 0; i < lines.length; i++) { |
| 228 | const match = lines[i].match(CLOSURE_RHS); |
| 229 | if (!match) continue; |
| 230 | |
| 231 | const name = match[1]; |
| 232 | if (!depNames.has(name)) continue; |
| 233 | |
| 234 | // Compiler temporaries are named t0, t1, ... tN. If the |
| 235 | // variable name matches that pattern it's an intermediate, |
| 236 | // not a user-visible declaration. |
| 237 | if (/^t\d+$/.test(name)) continue; |
| 238 | |
| 239 | findings.push({ name, line: i + 1 }); |
| 240 | } |
| 241 | |
| 242 | return findings; |
| 243 | } |
| 244 | |
| 245 | // --------------------------------------------------------------------------- |
| 246 | // Report |
no test coverage detected