Module: Rubocop::Cop::VariableInspector

Included in:
Lint::ShadowingOuterLocalVariable, Lint::UselessAssignment
Defined in:
lib/rubocop/cop/variable_inspector.rb,
lib/rubocop/cop/variable_inspector/scope.rb,
lib/rubocop/cop/variable_inspector/variable.rb,
lib/rubocop/cop/variable_inspector/locatable.rb,
lib/rubocop/cop/variable_inspector/reference.rb,
lib/rubocop/cop/variable_inspector/assignment.rb,
lib/rubocop/cop/variable_inspector/variable_table.rb

Overview

This module provides a way to track local variables and scopes of Ruby. This is intended to be used as mix-in, and the user class may override some of the hook methods.

Defined Under Namespace

Modules: Locatable Classes: Assignment, Reference, Scope, Variable, VariableTable

Constant Summary collapse

VARIABLE_ASSIGNMENT_TYPE =
:lvasgn
REGEXP_NAMED_CAPTURE_TYPE =
:match_with_lvasgn
VARIABLE_ASSIGNMENT_TYPES =
[VARIABLE_ASSIGNMENT_TYPE, REGEXP_NAMED_CAPTURE_TYPE].freeze
METHOD_ARGUMENT_DECLARATION_TYPES =
[
  :arg, :optarg, :restarg,
  :kwarg, :kwoptarg, :kwrestarg
].freeze
BLOCK_ARGUMENT_DECLARATION_TYPE =
:blockarg
ARGUMENT_DECLARATION_TYPES =
(
  METHOD_ARGUMENT_DECLARATION_TYPES + [BLOCK_ARGUMENT_DECLARATION_TYPE]
).freeze
BLOCK_LOCAL_VARIABLE_DECLARATION_TYPE =
:shadowarg
DECLARATION_TYPES =
(
  ARGUMENT_DECLARATION_TYPES + [BLOCK_LOCAL_VARIABLE_DECLARATION_TYPE]
).freeze
LOGICAL_OPERATOR_ASSIGNMENT_TYPES =
[:or_asgn, :and_asgn].freeze
OPERATOR_ASSIGNMENT_TYPES =
(LOGICAL_OPERATOR_ASSIGNMENT_TYPES + [:op_asgn]).freeze
MULTIPLE_ASSIGNMENT_TYPE =
:masgn
VARIABLE_REFERENCE_TYPE =
:lvar
POST_CONDITION_LOOP_TYPES =
[:while_post, :until_post].freeze
LOOP_TYPES =
(POST_CONDITION_LOOP_TYPES + [:while, :until, :for]).freeze
RESCUE_TYPE =
:rescue
ZERO_ARITY_SUPER_TYPE =
:zsuper
TWISTED_SCOPE_TYPES =
[:block, :class, :sclass, :defs].freeze
SCOPE_TYPES =
(TWISTED_SCOPE_TYPES + [:top_level, :module, :def]).freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.wrap_with_top_level_node(node) ⇒ Object



59
60
61
62
# File 'lib/rubocop/cop/variable_inspector.rb', line 59

def wrap_with_top_level_node(node)
  # This is a custom node type, not defined in Parser.
  Parser::AST::Node.new(:top_level, [node])
end

Instance Method Details

#after_declaring_variable(variable_variable) ⇒ Object



359
360
# File 'lib/rubocop/cop/variable_inspector.rb', line 359

def after_declaring_variable(variable_variable)
end

#after_entering_scope(scope) ⇒ Object



347
348
# File 'lib/rubocop/cop/variable_inspector.rb', line 347

def after_entering_scope(scope)
end

#after_leaving_scope(scope) ⇒ Object



353
354
# File 'lib/rubocop/cop/variable_inspector.rb', line 353

def after_leaving_scope(scope)
end

#before_declaring_variable(variable_variable) ⇒ Object



356
357
# File 'lib/rubocop/cop/variable_inspector.rb', line 356

def before_declaring_variable(variable_variable)
end

#before_entering_scope(scope) ⇒ Object

Hooks



344
345
# File 'lib/rubocop/cop/variable_inspector.rb', line 344

def before_entering_scope(scope)
end

#before_leaving_scope(scope) ⇒ Object



350
351
# File 'lib/rubocop/cop/variable_inspector.rb', line 350

def before_leaving_scope(scope)
end

#dispatch_node(node) ⇒ Object

rubocop:disable MethodLength, CyclomaticComplexity



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rubocop/cop/variable_inspector.rb', line 93

def dispatch_node(node)
  case node.type
  when *DECLARATION_TYPES
    process_variable_declaration(node)
  when VARIABLE_ASSIGNMENT_TYPE
    process_variable_assignment(node)
  when REGEXP_NAMED_CAPTURE_TYPE
    process_regexp_named_captures(node)
  when *OPERATOR_ASSIGNMENT_TYPES
    process_variable_operator_assignment(node)
  when MULTIPLE_ASSIGNMENT_TYPE
    process_variable_multiple_assignment(node)
  when VARIABLE_REFERENCE_TYPE
    process_variable_referencing(node)
  when *LOOP_TYPES
    process_loop(node)
  when RESCUE_TYPE
    process_rescue(node)
  when ZERO_ARITY_SUPER_TYPE
    process_zero_arity_super(node)
  when *SCOPE_TYPES
    process_scope(node)
  end
end

#find_variables_in_loop(loop_node) ⇒ Object



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/rubocop/cop/variable_inspector.rb', line 298

def find_variables_in_loop(loop_node)
  referenced_variable_names_in_loop = []
  assignment_nodes_in_loop = []

  # #scan does not consider scope,
  # but we don't need to care about it here.
  scan(loop_node) do |node|
    case node.type
    when :lvar
      referenced_variable_names_in_loop << node.children.first
    when *OPERATOR_ASSIGNMENT_TYPES
      asgn_node = node.children.first
      if asgn_node.type == :lvasgn
        referenced_variable_names_in_loop << asgn_node.children.first
      end
    when :lvasgn
      assignment_nodes_in_loop << node
    end
  end

  [referenced_variable_names_in_loop, assignment_nodes_in_loop]
end

#inspect_variables(root_node) ⇒ Object

Starting point.



50
51
52
53
54
55
56
57
# File 'lib/rubocop/cop/variable_inspector.rb', line 50

def inspect_variables(root_node)
  return unless root_node

  # Wrap the root node with :top_level scope node.
  top_level_node = wrap_with_top_level_node(root_node)

  inspect_variables_in_scope(top_level_node)
end

#inspect_variables_in_scope(scope_node) ⇒ Object

This is called for each scope recursively.



67
68
69
70
71
# File 'lib/rubocop/cop/variable_inspector.rb', line 67

def inspect_variables_in_scope(scope_node)
  variable_table.push_scope(scope_node)
  process_children(scope_node)
  variable_table.pop_scope
end

#mark_assignments_as_referenced_in_loop(node) ⇒ Object

Mark all assignments which are referenced in the same loop as referenced by ignoring AST order since they would be referenced in next iteration.



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/rubocop/cop/variable_inspector.rb', line 280

def mark_assignments_as_referenced_in_loop(node)
  referenced_variable_names_in_loop, assignment_nodes_in_loop =
    find_variables_in_loop(node)

  referenced_variable_names_in_loop.each do |name|
    variable = variable_table.find_variable(name)
    # Non related references which are catched in the above scan
    # would be skipped here.
    next unless variable
    variable.assignments.each do |assignment|
      next if assignment_nodes_in_loop.none? do |assignment_node|
                assignment_node.equal?(assignment.node)
              end
      assignment.reference!
    end
  end
end

#process_children(origin_node) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/rubocop/cop/variable_inspector.rb', line 73

def process_children(origin_node)
  origin_node.children.each do |child|
    next unless child.is_a?(Parser::AST::Node)
    next if scanned_node?(child)
    process_node(child)
  end
end

#process_loop(node) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/rubocop/cop/variable_inspector.rb', line 222

def process_loop(node)
  if POST_CONDITION_LOOP_TYPES.include?(node.type)
    # See the comment at the end of file for this behavior.
    condition_node, body_node = *node
    process_node(body_node)
    process_node(condition_node)
  else
    process_children(node)
  end

  mark_assignments_as_referenced_in_loop(node)

  skip_children!
end

#process_node(node) ⇒ Object



81
82
83
84
85
86
# File 'lib/rubocop/cop/variable_inspector.rb', line 81

def process_node(node)
  catch(:skip_children) do
    dispatch_node(node)
    process_children(node)
  end
end

#process_regexp_named_captures(node) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/rubocop/cop/variable_inspector.rb', line 150

def process_regexp_named_captures(node)
  regexp_node, rhs_node = *node

  regexp_string = regexp_node.children[0].children[0]
  regexp = Regexp.new(regexp_string)
  variable_names = regexp.named_captures.keys

  variable_names.each do |name|
    unless variable_table.variable_exist?(name)
      variable_table.declare_variable(name, node)
    end
  end

  process_node(rhs_node)
  process_node(regexp_node)

  variable_names.each do |name|
    variable_table.assign_to_variable(name, node)
  end

  skip_children!
end

#process_rescue(node) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/rubocop/cop/variable_inspector.rb', line 237

def process_rescue(node)
  resbody_nodes = node.children.select do |child|
    next false unless child.is_a?(Parser::AST::Node)
    child.type == :resbody
  end

  contain_retry = resbody_nodes.any? do |resbody_node|
    scan(resbody_node) do |node_in_resbody|
      break true if node_in_resbody.type == :retry
    end
  end

  # Treat begin..rescue..end with retry as a loop.
  process_loop(node) if contain_retry
end

#process_scope(node) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/rubocop/cop/variable_inspector.rb', line 260

def process_scope(node)
  if TWISTED_SCOPE_TYPES.include?(node.type)
    # See the comment at the end of file for this behavior.
    twisted_nodes = [node.children[0]]
    twisted_nodes << node.children[1] if node.type == :class
    twisted_nodes.compact!

    twisted_nodes.each do |twisted_node|
      process_node(twisted_node)
      scanned_nodes << twisted_node
    end
  end

  inspect_variables_in_scope(node)
  skip_children!
end

#process_variable_assignment(node) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/rubocop/cop/variable_inspector.rb', line 130

def process_variable_assignment(node)
  name = node.children.first

  unless variable_table.variable_exist?(name)
    variable_table.declare_variable(name, node)
  end

  # Need to scan rhs before assignment so that we can mark previous
  # assignments as referenced if rhs has referencing to the variable
  # itself like:
  #
  #   foo = 1
  #   foo = foo + 1
  process_children(node)

  variable_table.assign_to_variable(name, node)

  skip_children!
end

#process_variable_declaration(node) ⇒ Object

rubocop:enable MethodLength, CyclomaticComplexity



119
120
121
122
123
124
125
126
127
128
# File 'lib/rubocop/cop/variable_inspector.rb', line 119

def process_variable_declaration(node)
  # restarg would have no name:
  #
  #   def initialize(*)
  #   end
  return if node.type == :restarg && node.children.empty?

  variable_name = node.children.first
  variable_table.declare_variable(variable_name, node)
end

#process_variable_multiple_assignment(node) ⇒ Object



210
211
212
213
214
215
# File 'lib/rubocop/cop/variable_inspector.rb', line 210

def process_variable_multiple_assignment(node)
  lhs_node, rhs_node = *node
  process_node(rhs_node)
  process_node(lhs_node)
  skip_children!
end

#process_variable_operator_assignment(node) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/rubocop/cop/variable_inspector.rb', line 173

def process_variable_operator_assignment(node)
  if LOGICAL_OPERATOR_ASSIGNMENT_TYPES.include?(node.type)
    asgn_node, rhs_node = *node
  else
    asgn_node, _operator, rhs_node = *node
  end

  return unless asgn_node.type == :lvasgn

  name = asgn_node.children.first

  unless variable_table.variable_exist?(name)
    variable_table.declare_variable(name, asgn_node)
  end

  # The following statements:
  #
  #   foo = 1
  #   foo += foo = 2
  #   # => 3
  #
  # are equivalent to:
  #
  #   foo = 1
  #   foo = foo + (foo = 2)
  #   # => 3
  #
  # So, at operator assignment node, we need to reference the variable
  # before processing rhs nodes.

  variable_table.reference_variable(name, node)
  process_node(rhs_node)
  variable_table.assign_to_variable(name, asgn_node)

  skip_children!
end

#process_variable_referencing(node) ⇒ Object



217
218
219
220
# File 'lib/rubocop/cop/variable_inspector.rb', line 217

def process_variable_referencing(node)
  name = node.children.first
  variable_table.reference_variable(name, node)
end

#process_zero_arity_super(node) ⇒ Object



253
254
255
256
257
258
# File 'lib/rubocop/cop/variable_inspector.rb', line 253

def process_zero_arity_super(node)
  variable_table.accessible_variables.each do |variable|
    next unless variable.method_argument?
    variable.reference!(node)
  end
end

#scan(node, &block) ⇒ Object

Simple recursive scan



322
323
324
325
326
327
328
329
# File 'lib/rubocop/cop/variable_inspector.rb', line 322

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

#scanned_node?(node) ⇒ Boolean

Use Node#equal? for accurate check.

Returns:

  • (Boolean)


332
333
334
335
336
# File 'lib/rubocop/cop/variable_inspector.rb', line 332

def scanned_node?(node)
  scanned_nodes.any? do |scanned_node|
    scanned_node.equal?(node)
  end
end

#scanned_nodesObject



338
339
340
# File 'lib/rubocop/cop/variable_inspector.rb', line 338

def scanned_nodes
  @scanned_nodes ||= []
end

#skip_children!Object



88
89
90
# File 'lib/rubocop/cop/variable_inspector.rb', line 88

def skip_children!
  throw :skip_children
end

#variable_tableObject



45
46
47
# File 'lib/rubocop/cop/variable_inspector.rb', line 45

def variable_table
  @variable_table ||= VariableTable.new(self)
end