Module: SimpleCov::Formatter::AIFormatter::ASTResolver::MetaclassResolver

Extended by:
T::Sig
Defined in:
lib/simplecov-ai/ast_resolver/metaclass_resolver.rb

Overview

Recognizes constant assignments whose value is a Struct.new / Class.new / Module.new / Data.define block. These bind an anonymous class or module to the constant, so methods defined inside the block belong to that constant rather than to the enclosing lexical scope.

Constant Summary collapse

BUILDERS =

Recognized builders mapped to the constructor method that signals the pattern.

T.let({ 'Struct' => :new, 'Class' => :new, 'Module' => :new, 'Data' => :define }.freeze,
T::Hash[String, Symbol])
BLOCK_TYPES =

Every AST node type that attaches a block to a call: do … end / { … } blocks, numbered-parameter blocks ({ _1 }) and Ruby 3.4 it blocks.

T.let(i[block numblock itblock].freeze, T::Array[Symbol])

Class Method Summary collapse

Class Method Details

.block?(node) ⇒ Boolean



40
41
42
# File 'lib/simplecov-ai/ast_resolver/metaclass_resolver.rb', line 40

def self.block?(node)
  BLOCK_TYPES.include?(node.type)
end

.block_call(node) ⇒ Object



50
51
52
53
# File 'lib/simplecov-ai/ast_resolver/metaclass_resolver.rb', line 50

def self.block_call(node)
  call = NodeChildren.required_node_at(node, 0)
  call if call.type == :send
end

.builder_kind(node) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/simplecov-ai/ast_resolver/metaclass_resolver.rb', line 26

def self.builder_kind(node)
  send_node = builder_send(node)
  return nil unless send_node

  receiver = NodeChildren.node_at(send_node, 0)
  return nil unless receiver && receiver.type == :const

  kind = NodeChildren.symbol_at(receiver, 1).to_s
  kind if BUILDERS[kind] == NodeChildren.symbol_at(send_node, 1)
end