Parse a file with tree-sitter; return an error string if it fails to parse.
(rel: string, content: string)
| 123 | |
| 124 | /** Parse a file with tree-sitter; return an error string if it fails to parse. */ |
| 125 | private async parseCheck(rel: string, content: string): Promise<string | null> { |
| 126 | const lang = detectLanguage(rel); |
| 127 | if (!lang) return null; // unknown language — skip (don't block on e.g. .md) |
| 128 | let parser; |
| 129 | try { parser = await getParser(lang); } catch { return null; } |
| 130 | if (!parser) return null; |
| 131 | let tree; |
| 132 | try { tree = parser.parser.parse(content); } catch (e: any) { |
| 133 | return `${rel}: parser threw — ${e?.message ?? 'unknown'}`; |
| 134 | } |
| 135 | if (this.hasError(tree.rootNode)) { |
| 136 | const loc = this.firstErrorLine(tree.rootNode, content); |
| 137 | return `${rel}: syntax error${loc ? ` near line ${loc}` : ''} (worker emitted unparseable code)`; |
| 138 | } |
| 139 | return null; |
| 140 | } |
| 141 | |
| 142 | private hasError(node: any): boolean { |
| 143 | if (node.type === 'ERROR' || node.isMissing?.()) return true; |
no test coverage detected