123456789_123456789_123456789_123456789_123456789_

Class: SimpleCov::StaticCoverageExtractor::Visitor

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Prism::Visitor
Instance Chain:
self, Prism::Visitor
Inherits: Prism::Visitor
  • Object
Defined in: lib/simplecov/static_coverage_extractor/visitor.rb

Overview

Prism visitor that accumulates branch and method tuples in the shape Ruby's Coverage reports. Tuple ids are sequential across the file — Coverage uses sequential ids too, so this matches the conventional shape. Only defined when Prism is loadable; available? is the runtime gate.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.newVisitor

[ GitHub ]

  
# File 'lib/simplecov/static_coverage_extractor/visitor.rb', line 29

def initialize
  super
  @branches = {}
  @methods = {}
  @next_id = 0
  @class_stack = []
end

Instance Attribute Details

#branches (readonly)

[ GitHub ]

  
# File 'lib/simplecov/static_coverage_extractor/visitor.rb', line 27

attr_reader :branches, :methods

#methods (readonly)

[ GitHub ]

  
# File 'lib/simplecov/static_coverage_extractor/visitor.rb', line 27

attr_reader :branches, :methods

Instance Method Details

#arm_location(statements, fallback_location) (private)

Body location for an arm. Prism's statements is a StatementsNode whose span covers the contained expressions; fall back to the parent when the arm body is empty (e.g., if cond then end).

[ GitHub ]

  
# File 'lib/simplecov/static_coverage_extractor/visitor.rb', line 155

def arm_location(statements, fallback_location)
  statements&.location || fallback_location
end

#build_tuple(type, location) (private)

simplecov:enable branch

[ GitHub ]

  
# File 'lib/simplecov/static_coverage_extractor/visitor.rb', line 167

def build_tuple(type, location)
  id = @next_id
  @next_id += 1
  [type, id, location.start_line, location.start_column, location.end_line, location.end_column]
end

#constant_name(node) (private)

Render a constant path (e.g., Foo::Bar) as its source-form string. Defensive nil / to_s fallbacks: ClassNode and ModuleNode always carry a constant_path in practice. simplecov:disable

[ GitHub ]

  
# File 'lib/simplecov/static_coverage_extractor/visitor.rb', line 177

def constant_name(node)
  return "<anonymous>" if node.nil?
  return node.slice if node.respond_to?(:slice)

  node.to_s
end

#else_arm_location(node) (private)

Resolve the source range Coverage attributes to a synthetic-or-real :else arm of a case construct: the body of an explicit else, or the case's full range when no else is present.

[ GitHub ]

  
# File 'lib/simplecov/static_coverage_extractor/visitor.rb', line 140

def else_arm_location(node)
  return node.location unless node.else_clause

  arm_location(else_body_of(node.else_clause), node.else_clause.location)
end

#else_body_of(else_node) (private)

simplecov:disable branch The else_node fallback is defensive: every Prism node passed in here in practice responds to :statements.

[ GitHub ]

  
# File 'lib/simplecov/static_coverage_extractor/visitor.rb', line 162

def else_body_of(else_node)
  else_node.respond_to?(:statements) ? else_node.statements : else_node
end

#emit_case_like(node, when_type) (private)

[ GitHub ]

  
# File 'lib/simplecov/static_coverage_extractor/visitor.rb', line 128

def emit_case_like(node, when_type)
  arms = node.conditions.to_h do |when_node|
    loc = arm_location(when_node.statements, when_node.location)
    [build_tuple(when_type, loc), 0]
  end
  arms[build_tuple(:else, else_arm_location(node))] = 0
  @branches[build_tuple(:case, node.location)] = arms
end

#emit_if_like(node) (private)

IfNode and UnlessNode share a shape (predicate + then body + optional else/elsif) but expose the trailing arm under different accessors. #if_like_else_location hides that split.

[ GitHub ]

  
# File 'lib/simplecov/static_coverage_extractor/visitor.rb', line 105

def emit_if_like(node)
  then_loc = arm_location(node.statements, node.location)
  else_loc = if_like_else_location(node)
  @branches[build_tuple(:if, node.location)] = {
    build_tuple(:then, then_loc) => 0,
    build_tuple(:else, else_loc) => 0
  }
end

#emit_loop(node, type) (private)

[ GitHub ]

  
# File 'lib/simplecov/static_coverage_extractor/visitor.rb', line 146

def emit_loop(node, type)
  cond_tuple = build_tuple(type, node.location)
  body_loc = arm_location(node.statements, node.location)
  @branches[cond_tuple] = {build_tuple(:body, body_loc) => 0}
end

#if_like_else_location(node) (private)

Resolve the source range Coverage attributes to a real-or-synthetic :else arm of an if-like construct. IfNode uses subsequent / consequent depending on Prism version (resolved to IF_NODE_SUBSEQUENT_METHOD at load time); UnlessNode uses else_clause. When neither is present, the synthesized else inherits the whole condition's range (matches Coverage's convention).

[ GitHub ]

  
# File 'lib/simplecov/static_coverage_extractor/visitor.rb', line 121

def if_like_else_location(node)
  sub = node.is_a?(::Prism::IfNode) ? node.public_send(IF_NODE_SUBSEQUENT_METHOD) : node.else_clause
  return node.location unless sub

  arm_location(else_body_of(sub), sub.location)
end

#visit_case_match_node(node)

[ GitHub ]

  
# File 'lib/simplecov/static_coverage_extractor/visitor.rb', line 61

def visit_case_match_node(node)
  emit_case_like(node, :in)
  super
end

#visit_case_node(node)

case/when and case/in (pattern matching) parse as CaseNode and CaseMatchNode respectively. When there's no explicit else, Coverage synthesizes one at the case's range.

[ GitHub ]

  
# File 'lib/simplecov/static_coverage_extractor/visitor.rb', line 56

def visit_case_node(node)
  emit_case_like(node, :when)
  super
end

#visit_class_node(node)

Track class/module nesting so method tuples carry the lexical class name. Module + Class are both treated as namespaces here since Coverage reports both as the constant.

[ GitHub ]

  
# File 'lib/simplecov/static_coverage_extractor/visitor.rb', line 81

def visit_class_node(node)
  with_class(constant_name(node.constant_path)) { super }
end

#visit_def_node(node)

def name(...) and def self.name(...) both produce DefNode. The class context is the surrounding lexical class/module (or Object at the top level, matching Coverage's convention).

[ GitHub ]

  
# File 'lib/simplecov/static_coverage_extractor/visitor.rb', line 92

def visit_def_node(node)
  loc = node.location
  class_name = @class_stack.last || "Object"
  key = [class_name, node.name, loc.start_line, loc.start_column, loc.end_line, loc.end_column]
  @methods[key] = 0
  super
end

#visit_if_node(node)

if / unless / postfix-if / postfix-unless / ternary all parse as IfNode (or UnlessNode). Both carry a then arm (the statements body) and an optional subsequent (an ElseNode for else, another IfNode for elsif). When the subsequent is missing, Coverage synthesizes a :else arm attributed to the whole condition's range — we do the same.

[ GitHub ]

  
# File 'lib/simplecov/static_coverage_extractor/visitor.rb', line 43

def visit_if_node(node)
  emit_if_like(node)
  super
end

#visit_module_node(node)

[ GitHub ]

  
# File 'lib/simplecov/static_coverage_extractor/visitor.rb', line 85

def visit_module_node(node)
  with_class(constant_name(node.constant_path)) { super }
end

#visit_unless_node(node)

[ GitHub ]

  
# File 'lib/simplecov/static_coverage_extractor/visitor.rb', line 48

def visit_unless_node(node)
  emit_if_like(node)
  super
end

#visit_until_node(node)

[ GitHub ]

  
# File 'lib/simplecov/static_coverage_extractor/visitor.rb', line 73

def visit_until_node(node)
  emit_loop(node, :until)
  super
end

#visit_while_node(node)

while / until loops get a single :body arm. No synthetic else (the loop either runs the body or doesn't).

[ GitHub ]

  
# File 'lib/simplecov/static_coverage_extractor/visitor.rb', line 68

def visit_while_node(node)
  emit_loop(node, :while)
  super
end

#with_class(name) (private)

simplecov:enable

[ GitHub ]

  
# File 'lib/simplecov/static_coverage_extractor/visitor.rb', line 185

def with_class(name)
  @class_stack.push(name)
  yield
ensure
  @class_stack.pop
end