class Unparser::AST::LocalVariableScope

Calculated local variable scope for a given node

Public Class Methods

new(node) click to toggle source

Initialize object

@param [Parser::AST::Node] node

@return [undefined]

@api private

Calls superclass method
# File lib/unparser/ast/local_variable_scope.rb, line 18
def initialize(node)
  items = []
  LocalVariableScopeEnumerator.each(node) do |*scope|
    items << scope
  end
  @items = items
  super(node)
end

Public Instance Methods

first_assignment?(node) click to toggle source

Test if local variable was first at given assignment

@param [Parser::AST::Node] node

@return [Boolean]

@api private

# File lib/unparser/ast/local_variable_scope.rb, line 35
def first_assignment?(node)
  name = node.children.first
  match(node) do |current, before|
    current.include?(name) && !before.include?(name)
  end
end
first_assignment_in?(left, right) click to toggle source

Test if local variables where first assigned in body and read by conditional

@param [Parser::AST::Node] body @param [Parser::AST::Node] condition

@api private

# File lib/unparser/ast/local_variable_scope.rb, line 64
def first_assignment_in?(left, right)
  condition_reads = AST.local_variable_reads(right)

  candidates = AST.local_variable_assignments(left).select do |node|
    condition_reads.include?(node.children.first)
  end

  candidates.any?(&public_method(:first_assignment?))
end
local_variable_defined_for_node?(node, name) click to toggle source

Test if local variable is defined for given node

@param [Parser::AST::Node] node @param [Symbol] name

@return [Boolean]

@api private

# File lib/unparser/ast/local_variable_scope.rb, line 51
def local_variable_defined_for_node?(node, name)
  match(node) do |current|
    current.include?(name)
  end
end

Private Instance Methods

match(needle) { |current, before| ... } click to toggle source
# File lib/unparser/ast/local_variable_scope.rb, line 76
def match(needle)
  @items.each do |node, current, before|
    return yield(current, before) if node.equal?(needle)
  end
end