Class: RuboCop::Cop::VariableForce::Analysis Private
| Relationships & Source Files | |
| Inherits: | Object |
| Defined in: | lib/rubocop/cop/variable_force/analysis.rb |
Overview
Class Method Summary
- .new(processed_source) ⇒ Analysis constructor Internal use only
Instance Attribute Summary
-
#scopes ⇒ Array<Scope>
readonly
Internal use only
The scopes of the file, in the order the analysis left them, so an inner scope comes before the scope around it.
-
#shadowings ⇒ Array<Array(Variable, Variable)>
readonly
Internal use only
Each variable that was declared while a variable of the same name was visible from an outer scope, with that outer variable.
Instance Method Summary
- #after_leaving_scope(scope, _variable_table) Internal use only
- #before_declaring_variable(variable, variable_table) Internal use only
-
#variable_visible?(name, node) ⇒ Boolean
Internal use only
Whether the code at the node can see a local variable of the given name: one that is declared in front of the node, in the scope the node is in or in a scope around it that a block lets it see.
- #variables ⇒ Array<Variable> Internal use only
- #declared_before?(variable, node) ⇒ Boolean private Internal use only
-
#scopes_by_node
private
Internal use only
A file that is a single definition has two scopes for its root node, and the inner one, which comes first, is the one with the variables.
Constructor Details
.new(processed_source) ⇒ Analysis
# File 'lib/rubocop/cop/variable_force/analysis.rb', line 27
def initialize(processed_source) @scopes = [] @shadowings = [] VariableForce.new([self]).investigate(processed_source) end
Instance Attribute Details
#scopes ⇒ Array<Scope> (readonly)
The scopes of the file, in the order the analysis left them, so an inner scope comes before the scope around it.
# File 'lib/rubocop/cop/variable_force/analysis.rb', line 19
attr_reader :scopes
#shadowings ⇒ Array<Array(Variable, Variable)> (readonly)
Each variable that was declared while a variable of the same name was visible from an outer scope, with that outer variable.
# File 'lib/rubocop/cop/variable_force/analysis.rb', line 25
attr_reader :shadowings
Instance Method Details
#after_leaving_scope(scope, _variable_table)
[ GitHub ]# File 'lib/rubocop/cop/variable_force/analysis.rb', line 57
def after_leaving_scope(scope, _variable_table) @scopes << scope end
#before_declaring_variable(variable, variable_table)
[ GitHub ]# File 'lib/rubocop/cop/variable_force/analysis.rb', line 62
def before_declaring_variable(variable, variable_table) outer_variable = variable_table.find_variable(variable.name) @shadowings << [variable, outer_variable] if outer_variable end
#declared_before?(variable, node) ⇒ Boolean (private)
# File 'lib/rubocop/cop/variable_force/analysis.rb', line 70
def declared_before?(variable, node) variable.declaration_node.source_range.begin_pos < node.source_range.begin_pos end
#scopes_by_node (private)
A file that is a single definition has two scopes for its root node, and the inner one, which comes first, is the one with the variables.
# File 'lib/rubocop/cop/variable_force/analysis.rb', line 76
def scopes_by_node @scopes_by_node ||= scopes.each_with_object({}.compare_by_identity) do |scope, by_node| by_node[scope.node] ||= scope end end
#variable_visible?(name, node) ⇒ Boolean
Whether the code at the node can see a local variable of the given name: one that is declared in front of the node, in the scope the node is in or in a scope around it that a block lets it see. This is when Ruby reads the bare name as the variable and not as a method call.
# File 'lib/rubocop/cop/variable_force/analysis.rb', line 45
def variable_visible?(name, node) scope_node = node while (scope_node = scope_node.parent) next unless (scope = scopes_by_node[scope_node]) return declared_before?(scope.variables[name], node) if scope.variables.key?(name) return false unless scope_node.any_block_type? end false end
#variables ⇒ Array<Variable>
# File 'lib/rubocop/cop/variable_force/analysis.rb', line 34
def variables @variables ||= scopes.flat_map { |scope| scope.variables.values } end