Class: Rubocop::Cop::Lint::ShadowingOuterLocalVariable

Inherits:
Cop
  • Object
show all
Includes:
VariableInspector
Defined in:
lib/rubocop/cop/lint/shadowing_outer_local_variable.rb

Overview

This cop looks for use of the same name as outer local variables for block arguments or block local variables. This is a mimic of the warning "shadowing outer local variable - foo" from ruby -cw.

Constant Summary collapse

MSG =
'Shadowing outer local variable - %s'

Constants included from VariableInspector

VariableInspector::ARGUMENT_DECLARATION_TYPES, VariableInspector::BLOCK_ARGUMENT_DECLARATION_TYPE, VariableInspector::BLOCK_LOCAL_VARIABLE_DECLARATION_TYPE, VariableInspector::DECLARATION_TYPES, VariableInspector::LOGICAL_OPERATOR_ASSIGNMENT_TYPES, VariableInspector::LOOP_TYPES, VariableInspector::METHOD_ARGUMENT_DECLARATION_TYPES, VariableInspector::MULTIPLE_ASSIGNMENT_TYPE, VariableInspector::OPERATOR_ASSIGNMENT_TYPES, VariableInspector::POST_CONDITION_LOOP_TYPES, VariableInspector::REGEXP_NAMED_CAPTURE_TYPE, VariableInspector::RESCUE_TYPE, VariableInspector::SCOPE_TYPES, VariableInspector::TWISTED_SCOPE_TYPES, VariableInspector::VARIABLE_ASSIGNMENT_TYPE, VariableInspector::VARIABLE_ASSIGNMENT_TYPES, VariableInspector::VARIABLE_REFERENCE_TYPE, VariableInspector::ZERO_ARITY_SUPER_TYPE

Constants inherited from Cop

Cop::OPERATOR_METHODS

Instance Attribute Summary

Attributes inherited from Cop

#config, #corrections, #offences, #processed_source

Instance Method Summary collapse

Methods included from VariableInspector

#after_declaring_variable, #after_entering_scope, #after_leaving_scope, #before_entering_scope, #before_leaving_scope, #dispatch_node, #inspect_variables, #inspect_variables_in_scope, #mark_assignments_as_referenced_in_loop, #process_children, #process_loop, #process_node, #process_regexp_named_captures, #process_rescue, #process_scope, #process_variable_assignment, #process_variable_declaration, #process_variable_multiple_assignment, #process_variable_operator_assignment, #process_variable_referencing, #process_zero_arity_super, #scan, #scanned_node?, #scanned_nodes, #skip_children!, #variable_table

Methods inherited from Cop

#add_offence, all, #autocorrect?, #convention, #cop_config, cop_name, #cop_name, cop_type, #debug?, #ignore_node, inherited, #initialize, lint?, #message, non_rails, rails?, style?, #support_autocorrect?, #warning

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#before_declaring_variable(variable) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/rubocop/cop/lint/shadowing_outer_local_variable.rb', line 19

def before_declaring_variable(variable)
  return if variable.name.to_s.start_with?('_')

  outer_local_variable = variable_table.find_variable(variable.name)
  return unless outer_local_variable

  message = sprintf(MSG, variable.name)
  warning(variable.declaration_node, :expression, message)
end

#investigate(processed_source) ⇒ Object



15
16
17
# File 'lib/rubocop/cop/lint/shadowing_outer_local_variable.rb', line 15

def investigate(processed_source)
  inspect_variables(processed_source.ast)
end