()
| 35 | } |
| 36 | |
| 37 | async function initTreeSitter(): Promise<void> { |
| 38 | if (TreeSitter) return; |
| 39 | try { |
| 40 | const mod: any = await import('web-tree-sitter'); |
| 41 | // web-tree-sitter changed its public shape at v0.25: |
| 42 | // - old (<=0.24): default export is the Parser class; Parser.init(); |
| 43 | // grammars load via Parser.Language.load(path). |
| 44 | // - new (>=0.25): named exports `Parser` and `Language`; Parser.init(); |
| 45 | // grammars load via Language.load(path). |
| 46 | // Support BOTH so a runtime bump to read ABI-15 grammars doesn't break us. |
| 47 | const ParserCtor = mod.Parser ?? mod.default ?? mod; |
| 48 | TreeSitter = ParserCtor; |
| 49 | TSLanguage = mod.Language ?? ParserCtor?.Language ?? null; |
| 50 | if (typeof ParserCtor.init === 'function') { |
| 51 | await ParserCtor.init(); |
| 52 | } |
| 53 | } catch (e: any) { |
| 54 | throw new Error(`Failed to initialize Tree-sitter: ${e.message}. AST tools will be unavailable.`); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | export async function getParser(language: string): Promise<{ parser: any; language: any } | null> { |
| 59 | try { |
no test coverage detected