All files parse.ts

100% Statements 25/25
100% Branches 5/5
100% Functions 1/1
100% Lines 25/25

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 841x                                                               1x 57x   57x   57x 57x   57x 69x                       69x                       69x 3x 3x 3x 3x 3x 69x 69x   57x 57x 57x 57x 57x 57x   57x 57x  
/**
 * Java parseProject — tree-sitter-java.
 *
 * 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-go: `Map<absoluteFilePath,
 * { tree, source }>`.
 */
 
import { readFileSync } from 'node:fs';
import { relative } from 'node:path';
 
import { logger } from '@opensip-tools/core';
import Parser from 'tree-sitter';
import Java from 'tree-sitter-java';
 
import type { ParseInput, ParseOutput, ParseError } from '@opensip-tools/graph';
 
export interface JavaParsedFile {
  readonly tree: Parser.Tree;
  readonly source: string;
}
 
export interface JavaParsedProject {
  readonly files: ReadonlyMap<string, JavaParsedFile>;
}
 
export function parseProject(input: ParseInput): ParseOutput<JavaParsedProject> {
  const parser = new Parser();
  // Same CJS-typing cast as graph-rust / graph-python / graph-go.
  parser.setLanguage(Java as unknown as Parser.Language);
 
  const files = new Map<string, JavaParsedFile>();
  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:java',
    files: files.size,
    parseErrors: parseErrors.length,
  });
 
  return { project: { files }, parseErrors };
}