Class: Rubocop::Cop::Lint::UselessSetterCall::MethodVariableTracker

Inherits:
Object
  • Object
show all
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.

Instance Method Summary collapse

Constructor Details

#initialize(args_node, body_node) ⇒ MethodVariableTracker

Returns a new instance of MethodVariableTracker.



57
58
59
60
# File 'lib/rubocop/cop/lint/useless_setter_call.rb', line 57

def initialize(args_node, body_node)
  @args_node = args_node
  @body_node = body_node
end

Instance Method Details

#contain_object_passed_as_argument?(variable_name) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rubocop/cop/lint/useless_setter_call.rb', line 62

def contain_object_passed_as_argument?(variable_name)
  return @table[variable_name] if @table

  @table = {}

  @args_node.children.each do |arg_node|
    arg_name, = *arg_node
    @table[arg_name] = true
  end

  scan(@body_node) do |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
      _, rhs_node = *node
      process_assignment(node, rhs_node)
    end
  end

  @table[variable_name]
end

#process_assignment(asgn_node, rhs_node) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'lib/rubocop/cop/lint/useless_setter_call.rb', line 136

def process_assignment(asgn_node, rhs_node)
  lhs_variable_name, = *asgn_node

  if [:lvar, :ivar, :cvar, :gvar].include?(rhs_node.type)
    rhs_variable_name, = *rhs_node
    @table[lhs_variable_name] = @table[rhs_variable_name]
  else
    @table[lhs_variable_name] = false
  end
end

#process_binary_operator_assignment(op_asgn_node) ⇒ Object



127
128
129
130
131
132
133
134
# File 'lib/rubocop/cop/lint/useless_setter_call.rb', line 127

def process_binary_operator_assignment(op_asgn_node)
  lhs_node, = *op_asgn_node
  return unless ASSIGNMENT_TYPES.include?(lhs_node.type)
  lhs_variable_name, = *lhs_node
  @table[lhs_variable_name] = false

  throw :skip_children
end

#process_logical_operator_assignment(asgn_node) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/rubocop/cop/lint/useless_setter_call.rb', line 119

def process_logical_operator_assignment(asgn_node)
  lhs_node, rhs_node = *asgn_node
  return unless ASSIGNMENT_TYPES.include?(lhs_node.type)
  process_assignment(lhs_node, rhs_node)

  throw :skip_children
end

#process_multiple_assignment(masgn_node) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rubocop/cop/lint/useless_setter_call.rb', line 100

def process_multiple_assignment(masgn_node)
  mlhs_node, mrhs_node = *masgn_node

  mlhs_node.children.each_with_index do |lhs_node, index|
    next unless ASSIGNMENT_TYPES.include?(lhs_node.type)

    lhs_variable_name, = *lhs_node
    rhs_node = mrhs_node.children[index]

    if mrhs_node.type == :array && rhs_node
      process_assignment(lhs_variable_name, rhs_node)
    else
      @table[lhs_variable_name] = false
    end
  end

  throw :skip_children
end

#scan(node, &block) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/rubocop/cop/lint/useless_setter_call.rb', line 89

def scan(node, &block)
  catch(:skip_children) do
    yield node

    node.children.each do |child|
      next unless child.is_a?(Parser::AST::Node)
      scan(child, &block)
    end
  end
end