Class: SimpleCov::Formatter::AIFormatter::ASTResolver

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/simplecov-ai/ast_resolver.rb

Overview

Employs statically-parsed Abstract Syntax Tree processing via the ‘parser` gem to correlate raw line-based deficits with high-level semantically meaningful concepts like Classes and Methods. This negates the line-number volatility often experienced by Large Language Models when patching test coverage.

Defined Under Namespace

Classes: SemanticNode

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.resolve(file_path) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/simplecov-ai/ast_resolver.rb', line 54

def self.resolve(file_path)
  return [] unless File.exist?(file_path)

  begin
    source = File.read(file_path)
    ast, comments = Parser::CurrentRuby.parse_with_comments(source)
    new.traverse(ast, comments)
  rescue Parser::SyntaxError
    []
  end
end

Instance Method Details

#traverse(node, comments, context = '') ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/simplecov-ai/ast_resolver.rb', line 77

def traverse(node, comments, context = '')
  return [] unless node.is_a?(Parser::AST::Node)

  nodes = T.let([], T::Array[SemanticNode])
  current_context, semantic_node = (node, comments, context)
  nodes << semantic_node if semantic_node

  node.children.grep(Parser::AST::Node).each do |child|
    nodes.concat(traverse(child, comments, current_context))
  end

  nodes
end