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.



435
436
437
438
439
# File 'lib/pink_spoon/constant_resolver.rb', line 435

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.



433
434
435
# File 'lib/pink_spoon/constant_resolver.rb', line 433

def enclosing_call
  @enclosing_call
end

Instance Method Details

#visit_block_node(node) ⇒ Object



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/pink_spoon/constant_resolver.rb', line 447

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



441
442
443
444
445
# File 'lib/pink_spoon/constant_resolver.rb', line 441

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