Class: RuboCop::Cop::Lint::UselessSetterCall::MethodVariableTracker
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/rubocop/cop/lint/useless_setter_call.rb |
Overview
This class tracks variable assignments in a method body and if a variable contains object passed as argument at the end of the method.
Class Method Summary
- .new(body_node) ⇒ MethodVariableTracker constructor
Instance Method Summary
- #constructor?(node) ⇒ Boolean
- #contain_local_object?(variable_name) ⇒ Boolean
- #process_assignment(asgn_node, rhs_node)
- #process_assignment_node(node)
- #process_binary_operator_assignment(op_asgn_node)
- #process_logical_operator_assignment(asgn_node)
- #process_multiple_assignment(masgn_node)
- #scan(node, &block)
Constructor Details
.new(body_node) ⇒ MethodVariableTracker
# File 'lib/rubocop/cop/lint/useless_setter_call.rb', line 73
def initialize(body_node) @body_node = body_node @local = nil end
Instance Method Details
#constructor?(node) ⇒ Boolean
# File 'lib/rubocop/cop/lint/useless_setter_call.rb', line 148
def constructor?(node) return true if node.literal? return false unless node.send_type? node.method?(:new) end
#contain_local_object?(variable_name) ⇒ Boolean
# File 'lib/rubocop/cop/lint/useless_setter_call.rb', line 78
def contain_local_object?(variable_name) return @local[variable_name] if @local @local = {} scan(@body_node) { |node| process_assignment_node(node) } @local[variable_name] end
#process_assignment(asgn_node, rhs_node)
[ GitHub ]# File 'lib/rubocop/cop/lint/useless_setter_call.rb', line 140
def process_assignment(asgn_node, rhs_node) @local[asgn_node.name] = if rhs_node.variable? @local[rhs_node.name] else constructor?(rhs_node) end end
#process_assignment_node(node)
[ GitHub ]# File 'lib/rubocop/cop/lint/useless_setter_call.rb', line 96
def process_assignment_node(node) case node.type when :masgn process_multiple_assignment(node) when :or_asgn, :and_asgn process_logical_operator_assignment(node) when :op_asgn process_binary_operator_assignment(node) when *ASSIGNMENT_TYPES process_assignment(node, node.rhs) if node.rhs end end
#process_binary_operator_assignment(op_asgn_node)
[ GitHub ]# File 'lib/rubocop/cop/lint/useless_setter_call.rb', line 131
def process_binary_operator_assignment(op_asgn_node) lhs_node = op_asgn_node.lhs return unless ASSIGNMENT_TYPES.include?(lhs_node.type) @local[lhs_node.name] = true throw :skip_children end
#process_logical_operator_assignment(asgn_node)
[ GitHub ]# File 'lib/rubocop/cop/lint/useless_setter_call.rb', line 123
def process_logical_operator_assignment(asgn_node) return unless ASSIGNMENT_TYPES.include?(asgn_node.lhs.type) process_assignment(asgn_node.lhs, asgn_node.rhs) throw :skip_children end
#process_multiple_assignment(masgn_node)
[ GitHub ]# File 'lib/rubocop/cop/lint/useless_setter_call.rb', line 109
def process_multiple_assignment(masgn_node) masgn_node.assignments.each_with_index do |lhs_node, index| next unless ASSIGNMENT_TYPES.include?(lhs_node.type) if masgn_node.rhs.array_type? && (rhs_node = masgn_node.rhs.children[index]) process_assignment(lhs_node, rhs_node) else @local[lhs_node.name] = true end end throw :skip_children end
#scan(node, &block)
[ GitHub ]# File 'lib/rubocop/cop/lint/useless_setter_call.rb', line 88
def scan(node, &block) catch(:skip_children) do yield node node.each_child_node { |child_node| scan(child_node, &block) } end end