Class: Unparser::AST::LocalVariableScope
- Inherits:
-
Object
- Object
- Unparser::AST::LocalVariableScope
- 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
-
#first_assignment?(node) ⇒ true, false
private
Test if local variable was first at given assignment.
-
#first_assignment_in_body_and_used_in_condition?(body, condition) ⇒ Boolean
private
Test if local variables where first assigned in body and read by conditional.
-
#initialize(node) ⇒ undefined
constructor
private
Initialize object.
-
#local_variable_defined_for_node?(node, name) ⇒ true, false
private
Test if local variable is defined for given node.
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
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) ⇒ true, false
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
39 40 41 42 43 44 |
# File 'lib/unparser/ast/local_variable_scope.rb', line 39 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
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/unparser/ast/local_variable_scope.rb', line 72 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) ⇒ true, false
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
59 60 61 62 63 |
# File 'lib/unparser/ast/local_variable_scope.rb', line 59 def local_variable_defined_for_node?(node, name) match(node) do |current| current.include?(name) end end |