class Rubocop::Cop::VariableInspector::VariableTable
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.
Public Class Methods
new(hook_receiver = nil)
click to toggle source
# File lib/rubocop/cop/variable_inspector.rb, line 67 def initialize(hook_receiver = nil) @hook_receiver = hook_receiver end
Public Instance Methods
add_variable_entry(variable_declaration_node)
click to toggle source
# File lib/rubocop/cop/variable_inspector.rb, line 103 def add_variable_entry(variable_declaration_node) entry = VariableEntry.new(variable_declaration_node) invoke_hook(:before_declaring_variable, entry) current_scope.variable_entries[entry.name] = entry invoke_hook(:after_declaring_variable, entry) entry end
current_scope()
click to toggle source
# File lib/rubocop/cop/variable_inspector.rb, line 95 def current_scope scope_stack.last end
current_scope_level()
click to toggle source
# File lib/rubocop/cop/variable_inspector.rb, line 99 def current_scope_level scope_stack.count end
find_variable_entry(variable_name)
click to toggle source
# File lib/rubocop/cop/variable_inspector.rb, line 111 def find_variable_entry(variable_name) scope_stack.reverse_each do |scope| entry = scope.variable_entries[variable_name] return entry if entry # Only block scope allows referencing outer scope variables. return nil unless scope.node.type == :block end nil end
invoke_hook(hook_name, *args)
click to toggle source
# File lib/rubocop/cop/variable_inspector.rb, line 71 def invoke_hook(hook_name, *args) @hook_receiver.send(hook_name, *args) if @hook_receiver end
pop_scope()
click to toggle source
# File lib/rubocop/cop/variable_inspector.rb, line 87 def pop_scope scope = current_scope invoke_hook(:before_leaving_scope, scope) scope_stack.pop invoke_hook(:after_leaving_scope, scope) scope end
push_scope(scope_node)
click to toggle source
# File lib/rubocop/cop/variable_inspector.rb, line 79 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
scope_stack()
click to toggle source
# File lib/rubocop/cop/variable_inspector.rb, line 75 def scope_stack @scope_stack ||= [] end