Class: Unparser::AST::LocalVariableScope

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Unparser::Adamantium
Defined in:
lib/unparser/ast/local_variable_scope.rb

Overview

Calculated local variable scope for a given node

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize object

Parameters:

  • node (Parser::AST::Node)


18
19
20
21
22
23
24
# File 'lib/unparser/ast/local_variable_scope.rb', line 18

def initialize(node)
  items = []
  LocalVariableScopeEnumerator.each(node) do |*scope|
    items << scope
  end
  @items = items
end

Instance Method Details

#first_assignment?(node) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Test if local variable was first at given assignment

Parameters:

  • node (Parser::AST::Node)

Returns:

  • (Boolean)


34
35
36
37
38
39
# File 'lib/unparser/ast/local_variable_scope.rb', line 34

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) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

  • body (Parser::AST::Node)
  • condition (Parser::AST::Node)

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
# File 'lib/unparser/ast/local_variable_scope.rb', line 63

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) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Test if local variable is defined for given node

Parameters:

  • node (Parser::AST::Node)
  • name (Symbol)

Returns:

  • (Boolean)


50
51
52
53
54
# File 'lib/unparser/ast/local_variable_scope.rb', line 50

def local_variable_defined_for_node?(node, name)
  match(node) do |current|
    current.include?(name)
  end
end