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 84 85 86 87 88 89 90 911x                                                                       1x 34x         34x   34x 34x   34x 47x                       47x                       47x 1x 1x 1x 1x 1x 47x 47x   34x 34x 34x 34x 34x 34x   34x 34x  
/**
 * Python parseProject — tree-sitter-python.
 *
 * 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 gracefully
 * (it builds a partial tree marked with ERROR nodes); we surface a
 * ParseError when the root node `hasError` so the user knows the file
 * had problems, but we still keep the partial tree for the walk.
 *
 * The parsed-project shape is `Map<absoluteFilePath, { tree, source }>`.
 * Both the tree and the original source text are needed: tree-sitter's
 * SyntaxNode.text already extracts text from the source it was parsed
 * with, but we hold the raw bytes ourselves to compute body slices for
 * hashing 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 Python from 'tree-sitter-python';
 
import type { ParseInput, ParseOutput, ParseError } from '@opensip-tools/graph';
 
export interface PythonParsedFile {
  readonly tree: Parser.Tree;
  readonly source: string;
}
 
export interface PythonParsedProject {
  /** Keyed by the absolute, realpath-normalized file path from discover. */
  readonly files: ReadonlyMap<string, PythonParsedFile>;
}
 
export function parseProject(input: ParseInput): ParseOutput<PythonParsedProject> {
  const parser = new Parser();
  // tree-sitter-python's `Language` type and tree-sitter's `Language`
  // type both come from CJS .d.ts files; their structural shapes match
  // at runtime but don't unify under TS's `--strict` checks. The cast
  // is safe — we exercised it in the test fixture parses end-to-end.
  parser.setLanguage(Python as unknown as Parser.Language);
 
  const files = new Map<string, PythonParsedFile>();
  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:python',
    files: files.size,
    parseErrors: parseErrors.length,
  });
 
  return { project: { files }, parseErrors };
}