Module: SimpleCov::Formatter::AIFormatter::ASTResolver::DynamicMethodResolver

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

Overview

Recognizes define_method(:name) do … end and define_singleton_method(:name) { … } blocks whose method name is a literal symbol or string. Such a block is the body of the method it defines, so deficits inside it belong to that method rather than to the whole enclosing class. Dynamic names (define_method(name)), explicit foreign receivers (klass.define_method) and block-less forms (define_method(:a, method(:b))) are not recognized and stay transparent.

Constant Summary collapse

DEFINERS =

Method-defining calls mapped to whether they define a singleton method.

T.let({ define_method: false, define_singleton_method: true }.freeze,
T::Hash[Symbol, T::Boolean])
LITERAL_NAME_TYPES =

Node types accepted as a literal method name.

T.let(%i[sym str].freeze, T::Array[Symbol])

Class Method Summary collapse

Class Method Details

.definition(node) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/simplecov-ai/ast_resolver/dynamic_method_resolver.rb', line 28

def self.definition(node)
  call = MetaclassResolver.block_call(node)
  return nil unless call && implicit_receiver?(call)

  singleton_definer = DEFINERS[NodeChildren.symbol_at(call, 1)]
  name = literal_name(call)
  return nil if singleton_definer.nil? || name.nil?

  [name, singleton_definer]
end