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