Class: PinkSpoon::ConstantResolver::BlockParamFinder

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

Overview

Finds the CallNode whose block introduces ‘var_name` as a parameter. e.g. entries.each { |entry| } → enclosing_call = entries.each(…)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(var_name) ⇒ BlockParamFinder

Returns a new instance of BlockParamFinder.



541
542
543
544
545
# File 'lib/pink_spoon/constant_resolver.rb', line 541

def initialize(var_name)
  @var_name       = var_name
  @enclosing_call = nil
  @call_stack     = []
end

Instance Attribute Details

#enclosing_callObject (readonly)

Returns the value of attribute enclosing_call.



539
540
541
# File 'lib/pink_spoon/constant_resolver.rb', line 539

def enclosing_call
  @enclosing_call
end

Instance Method Details

#visit_block_node(node) ⇒ Object



553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# File 'lib/pink_spoon/constant_resolver.rb', line 553

def visit_block_node(node)
  return super if @enclosing_call

  # BlockParametersNode wraps a ParametersNode which holds .requireds
  block_params = node.parameters
  return super unless block_params

  inner = block_params.is_a?(Prism::BlockParametersNode) ? block_params.parameters : block_params
  return super unless inner

  found = inner.requireds&.any? do |p|
    p.is_a?(Prism::RequiredParameterNode) && p.name.to_s == @var_name
  end

  if found
    @enclosing_call = @call_stack.last
  else
    super
  end
end

#visit_call_node(node) ⇒ Object



547
548
549
550
551
# File 'lib/pink_spoon/constant_resolver.rb', line 547

def visit_call_node(node)
  @call_stack.push(node)
  super
  @call_stack.pop
end