A VariableTable manages the lifetime of all scopes and local variables in a program. This holds scopes as stack structure, and provides a way to add local variables to current scope and find local variables by considering variable visibility of the current scope.
# File lib/rubocop/cop/variable_force/variable_table.rb, line 110 def accessible_variables scope_stack.reverse_each.each_with_object([]) do |scope, variables| variables.concat(scope.variables.values) break variables unless scope.node.type == :block end end
# File lib/rubocop/cop/variable_force/variable_table.rb, line 55 def assign_to_variable(name, node) variable = find_variable(name) unless variable fail "Assigning to undeclared local variable \"#{name}\" " "at #{node.loc.expression}, #{node.inspect}" end variable.assign(node) mark_variable_as_captured_by_block_if_so(variable) end
# File lib/rubocop/cop/variable_force/variable_table.rb, line 39 def current_scope scope_stack.last end
# File lib/rubocop/cop/variable_force/variable_table.rb, line 43 def current_scope_level scope_stack.count end
# File lib/rubocop/cop/variable_force/variable_table.rb, line 47 def declare_variable(name, node) variable = Variable.new(name, node, current_scope) invoke_hook(:before_declaring_variable, variable) current_scope.variables[variable.name] = variable invoke_hook(:after_declaring_variable, variable) variable end
# File lib/rubocop/cop/variable_force/variable_table.rb, line 93 def find_variable(name) name = name.to_sym scope_stack.reverse_each do |scope| variable = scope.variables[name] return variable if variable # Only block scope allows referencing outer scope variables. return nil unless scope.node.type == :block end nil end
# File lib/rubocop/cop/variable_force/variable_table.rb, line 15 def invoke_hook(hook_name, *args) @hook_receiver.send(hook_name, *args) if @hook_receiver end
# File lib/rubocop/cop/variable_force/variable_table.rb, line 31 def pop_scope scope = current_scope invoke_hook(:before_leaving_scope, scope) scope_stack.pop invoke_hook(:after_leaving_scope, scope) scope end
# File lib/rubocop/cop/variable_force/variable_table.rb, line 23 def push_scope(scope_node) scope = Scope.new(scope_node) invoke_hook(:before_entering_scope, scope) scope_stack.push(scope) invoke_hook(:after_entering_scope, scope) scope end
# File lib/rubocop/cop/variable_force/variable_table.rb, line 67 def reference_variable(name, node) variable = find_variable(name) # In this code: # # foo = 1 unless foo # # (if # (lvar :foo) nil # (lvasgn :foo # (int 1))) # # Parser knows whether the foo is a variable or method invocation. # This means that if a :lvar node is shown in AST, the variable is # assumed to be already declared, even if we haven't seen any :lvasgn # or :arg node before the :lvar node. # # We don't invoke #declare_variable here otherwise # Variable#declaration_node will be :lvar node, that is actually not. # So just skip. return unless variable variable.reference!(node) mark_variable_as_captured_by_block_if_so(variable) end
Generated with the Darkfish Rdoc Generator 2.