* Parse a TS file for a TurboModule spec declaration. The spec file is * the JS↔native source-of-truth in the new architecture — its interface * lists every JS-visible method, and a `TurboModuleRegistry.get* (...)` * default export pins the module name. * * Returns `null` when the file isn
( source: string )
| 310 | * Returns `null` when the file isn't a TurboModule spec. |
| 311 | */ |
| 312 | function parseTurboModuleSpec( |
| 313 | source: string |
| 314 | ): { moduleName: string; methods: string[] } | null { |
| 315 | // `TurboModuleRegistry.getEnforcing<Spec>('ModuleName')` or |
| 316 | // `TurboModuleRegistry.get<Spec>('ModuleName')`. The literal must be a |
| 317 | // single-or-double-quoted string. |
| 318 | const regMatch = source.match( |
| 319 | /TurboModuleRegistry\.(?:getEnforcing|get)\s*<[^>]*>\s*\(\s*['"]([^'"]+)['"]\s*\)/ |
| 320 | ); |
| 321 | if (!regMatch || !regMatch[1]) return null; |
| 322 | const moduleName = regMatch[1]; |
| 323 | |
| 324 | // Find `export interface Spec extends TurboModule { … }` and pull each |
| 325 | // method declaration's name. We don't need types — just names. |
| 326 | const ifaceMatch = source.match( |
| 327 | /export\s+interface\s+Spec\b[^{]*\{([\s\S]*?)\n\}/ |
| 328 | ); |
| 329 | if (!ifaceMatch || !ifaceMatch[1]) return null; |
| 330 | const body = ifaceMatch[1]; |
| 331 | |
| 332 | const methods: string[] = []; |
| 333 | // Method shape: `name(args): ReturnType;` or `name(): void;`. Skip |
| 334 | // properties (no parens before colon). |
| 335 | const methodRegex = /^\s*([A-Za-z_][A-Za-z0-9_]*)\s*\(/gm; |
| 336 | let m: RegExpExecArray | null; |
| 337 | while ((m = methodRegex.exec(body)) !== null) { |
| 338 | const name = m[1]; |
| 339 | if (name) methods.push(name); |
| 340 | } |
| 341 | return { moduleName, methods }; |
| 342 | } |
| 343 | |
| 344 | // ─── Map building ─────────────────────────────────────────────────────────── |
| 345 |
no test coverage detected