Class: PinkSpoon::ConstantResolver::EnclosingDefFinder

Inherits:
Prism::Visitor
  • Object
show all
Defined in:
lib/pink_spoon/constant_resolver.rb

Overview

Finds the name of the innermost def containing a target line.

Instance Method Summary collapse

Constructor Details

#initialize(target_line) ⇒ EnclosingDefFinder

Returns a new instance of EnclosingDefFinder.



419
420
421
422
423
# File 'lib/pink_spoon/constant_resolver.rb', line 419

def initialize(target_line)
  @target_line = target_line
  @best_name   = nil
  @best_span   = nil
end

Instance Method Details

#find(ast) ⇒ Object



425
426
427
428
# File 'lib/pink_spoon/constant_resolver.rb', line 425

def find(ast)
  visit(ast)
  @best_name
end

#visit_def_node(node) ⇒ Object



430
431
432
433
434
435
436
437
438
439
440
# File 'lib/pink_spoon/constant_resolver.rb', line 430

def visit_def_node(node)
  loc = node.location
  return super unless @target_line >= loc.start_line && @target_line <= loc.end_line

  span = loc.end_line - loc.start_line
  if @best_span.nil? || span < @best_span
    @best_span = span
    @best_name = node.name.to_s
  end
  super
end