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.



515
516
517
518
519
# File 'lib/pink_spoon/constant_resolver.rb', line 515

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.



513
514
515
# File 'lib/pink_spoon/constant_resolver.rb', line 513

def enclosing_call
  @enclosing_call
end

Instance Method Details

#visit_block_node(node) ⇒ Object



527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# File 'lib/pink_spoon/constant_resolver.rb', line 527

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



521
522
523
524
525
# File 'lib/pink_spoon/constant_resolver.rb', line 521

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