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.



356
357
358
359
360
# File 'lib/pink_spoon/constant_resolver.rb', line 356

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

Instance Method Details

#find(ast) ⇒ Object



362
363
364
365
# File 'lib/pink_spoon/constant_resolver.rb', line 362

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

#visit_def_node(node) ⇒ Object



367
368
369
370
371
372
373
374
375
376
377
# File 'lib/pink_spoon/constant_resolver.rb', line 367

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