Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | 1x 1x 51x 51x 51x 51x 51x 60x 60x 60x 3x 3x 3x 3x 3x 60x 60x 51x 51x 51x 51x 51x 51x 51x 51x | /**
* Go parseProject — tree-sitter-go.
*
* Per contract invariant I-7 (parseProject is total over `files`):
* every file in `input.files` either parses successfully or surfaces in
* `parseErrors`. Tree-sitter recovers from syntax errors by inserting
* MISSING nodes; we surface a ParseError when the root `hasError` so
* users see the file had problems but keep the partial tree for the
* walk.
*
* Parsed-project shape mirrors graph-rust: `Map<absoluteFilePath, {
* tree, source }>`. The source string is held alongside the tree so
* body slices can be extracted without re-parsing.
*/
import { readFileSync } from 'node:fs';
import { relative } from 'node:path';
import { logger } from '@opensip-tools/core';
import Parser from 'tree-sitter';
import Go from 'tree-sitter-go';
import type { ParseInput, ParseOutput, ParseError } from '@opensip-tools/graph';
export interface GoParsedFile {
readonly tree: Parser.Tree;
readonly source: string;
}
export interface GoParsedProject {
readonly files: ReadonlyMap<string, GoParsedFile>;
}
export function parseProject(input: ParseInput): ParseOutput<GoParsedProject> {
const parser = new Parser();
// tree-sitter-go's `Language` type and tree-sitter's `Language` type
// both come from CJS .d.ts files; they're structurally compatible at
// runtime but don't unify under TS's `--strict` checks. Same cast as
// graph-rust / graph-python.
parser.setLanguage(Go as unknown as Parser.Language);
const files = new Map<string, GoParsedFile>();
const parseErrors: ParseError[] = [];
for (const path of input.files) {
let source: string;
/* v8 ignore start */
try {
source = readFileSync(path, 'utf8');
} catch (error) {
parseErrors.push({
filePath: relative(input.projectDirAbs, path),
message: `read failed: ${error instanceof Error ? error.message : String(error)}`,
});
continue;
}
/* v8 ignore stop */
let tree: Parser.Tree;
/* v8 ignore start */
try {
tree = parser.parse(source);
} catch (error) {
parseErrors.push({
filePath: relative(input.projectDirAbs, path),
message: error instanceof Error ? error.message : String(error),
});
continue;
}
/* v8 ignore stop */
if (tree.rootNode.hasError) {
parseErrors.push({
filePath: relative(input.projectDirAbs, path),
message: 'tree-sitter reported syntax errors; partial tree retained',
});
}
files.set(path, { tree, source });
}
logger.info({
evt: 'graph.parse.complete',
module: 'graph:parse:go',
files: files.size,
parseErrors: parseErrors.length,
});
return { project: { files }, parseErrors };
}
|