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.



445
446
447
448
449
# File 'lib/pink_spoon/constant_resolver.rb', line 445

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

Instance Method Details

#find(ast) ⇒ Object



451
452
453
454
# File 'lib/pink_spoon/constant_resolver.rb', line 451

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

#visit_def_node(node) ⇒ Object



456
457
458
459
460
461
462
463
464
465
466
# File 'lib/pink_spoon/constant_resolver.rb', line 456

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