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

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/simplecov-ai/ast_resolver.rb,
lib/simplecov-ai/ast_resolver/semantic_node.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

Constant Summary collapse

NAMESPACE_SEPARATOR =

Separator used to denote namespace nesting (e.g., Module::Class)

T.let('::', String)
INSTANCE_SEPARATOR =

Separator used to denote instance methods (e.g., Class#method)

T.let('#', String)
SINGLETON_SEPARATOR =

Separator used to denote singleton/class methods (e.g., Class.method)

T.let('.', String)
TYPE_INSTANCE_METHOD =

Label applied to nodes representing instance methods

T.let('Instance Method', String)
TYPE_SINGLETON_METHOD =

Label applied to nodes representing singleton methods

T.let('Singleton Method', String)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.resolve(file_path) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/simplecov-ai/ast_resolver.rb', line 34

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

  source = File.read(file_path)
  ast, comments = Parser::CurrentRuby.parse_with_comments(source)

  resolver = new
  nodes = resolver.traverse(ast)
  resolver.assign_bypasses(nodes, comments)
  nodes
end

Instance Method Details

#assign_bypasses(nodes, comments) ⇒ Object



71
72
73
74
75
76
# File 'lib/simplecov-ai/ast_resolver.rb', line 71

def assign_bypasses(nodes, comments)
  comments.each do |comment|
    comment_text = T.cast(comment.text, String)
    assign_bypass(nodes, comment, comment_text.strip) if comment_text.include?(Constants::NOCOV_DIRECTIVE)
  end
end

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



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/simplecov-ai/ast_resolver.rb', line 56

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

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

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

  nodes
end