Class: Unparser::AST::LocalVariableScope

Inherits:
Object
  • Object
show all
Includes:
Adamantium, Enumerable
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
25
# 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

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)


35
36
37
38
39
40
# 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_body_and_used_in_condition?(body, condition) ⇒ 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:

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

Returns:

  • (Boolean)


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

def first_assignment_in_body_and_used_in_condition?(body, condition)
  condition_reads = AST.local_variable_reads(condition)

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

  candidates.any? do |node|
    first_assignment?(node)
  end
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)


51
52
53
54
55
# 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