Module: RuboCop::Cop::HelperFunctions Private

This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.descendant_send_nodes(processed_source, node) ⇒ Array<RuboCop::AST::SendNode>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (Array<RuboCop::AST::SendNode>)


31
32
33
34
35
36
37
38
39
# File 'rubocops/shared/helper_functions.rb', line 31

def self.descendant_send_nodes(processed_source, node)
  unless @processed_source.equal?(processed_source)
    @processed_source = processed_source
    @descendant_send_nodes.clear
    @descendant_send_nodes_by_method_name.clear
  end

  @descendant_send_nodes[node] ||= node.each_descendant(:send).to_a
end

.descendant_send_nodes_by_method_name(processed_source, node) ⇒ Hash{Symbol => Array<RuboCop::AST::SendNode>}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



45
46
47
48
49
# File 'rubocops/shared/helper_functions.rb', line 45

def self.descendant_send_nodes_by_method_name(processed_source, node)
  descendant_send_nodes(processed_source, node)
  @descendant_send_nodes_by_method_name[node] ||=
    @descendant_send_nodes.fetch(node).group_by(&:method_name)
end

Instance Method Details

#block_method_called_in_block?(node, method_name) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Check if a block method is called inside a block.

Parameters:

Returns:

  • (Boolean)


413
414
415
416
417
418
419
420
421
422
# File 'rubocops/shared/helper_functions.rb', line 413

def block_method_called_in_block?(node, method_name)
  node.body.each_child_node do |call_node|
    next if !call_node.block_type? && !call_node.send_type?
    next if call_node.method_name != method_name

    @offensive_node = call_node
    return true
  end
  false
end

#check_precedence(first_nodes, next_nodes) ⇒ Array<(RuboCop::AST::Node, RuboCop::AST::Node)>?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Checks for precedence; returns the first pair of precedence-violating nodes.

Parameters:

Returns:



456
457
458
459
460
461
462
463
# File 'rubocops/shared/helper_functions.rb', line 456

def check_precedence(first_nodes, next_nodes)
  next_nodes.each do |each_next_node|
    first_nodes.each do |each_first_node|
      return [each_first_node, each_next_node] if component_precedes?(each_first_node, each_next_node)
    end
  end
  nil
end

#class_name(node) ⇒ String?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns the class node's name, or nil if not a class node.

Parameters:

Returns:



519
520
521
522
# File 'rubocops/shared/helper_functions.rb', line 519

def class_name(node)
  @offensive_node = node
  node.const_name
end

#component_precedes?(first_node, next_node) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

If first node does not precede next_node, sets appropriate instance variables for reporting.

Parameters:

Returns:

  • (Boolean)


467
468
469
470
471
472
# File 'rubocops/shared/helper_functions.rb', line 467

def component_precedes?(first_node, next_node)
  return false if line_number(first_node) < line_number(next_node)

  @offensive_node = first_node
  true
end

#end_column(node) ⇒ Integer

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns the ending position of the node in source code.

Parameters:

Returns:



513
514
515
# File 'rubocops/shared/helper_functions.rb', line 513

def end_column(node)
  node.source_range.end_pos
end

#expression_negated?(node) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Check if negation is present in the given node.

Parameters:

Returns:

  • (Boolean)


476
477
478
479
480
481
# File 'rubocops/shared/helper_functions.rb', line 476

def expression_negated?(node)
  return false unless node.parent&.send_type?
  return false unless node.parent.method_name.equal?(:!)

  !!offending_node(node.parent)
end

#find_all_blocks(node, block_name, &_block) ⇒ Array<RuboCop::AST::BlockNode>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns an array of block nodes of any depth below node in AST. If a block is given then yields matching block node to the block!

Parameters:

Returns:



376
377
378
379
380
381
382
383
384
385
386
# File 'rubocops/shared/helper_functions.rb', line 376

def find_all_blocks(node, block_name, &_block)
  return [] if node.nil?

  blocks = node.each_descendant(:block).select { |block_node| block_name == block_node.method_name }
  return blocks unless block_given?

  blocks.each do |block_node|
    offending_node(block_node)
    yield block_node
  end
end

#find_block(node, block_name) ⇒ RuboCop::AST::BlockNode?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns a block named block_name inside node.

Parameters:

Returns:



343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'rubocops/shared/helper_functions.rb', line 343

def find_block(node, block_name)
  return if node.nil?

  node.each_child_node(:block) do |block_node|
    next if block_node.method_name != block_name

    @offensive_node = block_node
    return block_node
  end
  # If not found then, parent node becomes the offensive node
  @offensive_node = node.parent
  nil
end

#find_blocks(node, block_name) ⇒ Array<RuboCop::AST::BlockNode>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns an array of block nodes named block_name inside node.

Parameters:

Returns:



361
362
363
364
365
# File 'rubocops/shared/helper_functions.rb', line 361

def find_blocks(node, block_name)
  return [] if node.nil?

  node.each_child_node(:block).select { |block_node| block_name == block_node.method_name }
end

#find_const(node, const_name, &_block) ⇒ T.anything

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Find CONSTANTs in the source. If block given, yield matching nodes.

Parameters:

Returns:

  • (T.anything)


320
321
322
323
324
325
326
327
328
329
330
331
# File 'rubocops/shared/helper_functions.rb', line 320

def find_const(node, const_name, &_block)
  return if node.nil?

  node.each_descendant(:const) do |const_node|
    next if const_node.const_name != const_name

    @offensive_node = const_node
    yield const_node if block_given?
    return true
  end
  nil
end

#find_every_func_call_by_name(node, func_name = nil) ⇒ Array<RuboCop::AST::BlockNode, RuboCop::AST::SendNode>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns array of function call nodes matching func_name in every descendant of node.

  • matches function call: foo(*args, **kwargs)
  • does not match method calls: foo.bar(*args, **kwargs)
  • returns every function call if no func_name is passed

Parameters:

Returns:



214
215
216
217
218
219
220
221
222
223
# File 'rubocops/shared/helper_functions.rb', line 214

def find_every_func_call_by_name(node, func_name = nil)
  return [] if node.nil?

  nodes = if func_name.nil?
    HelperFunctions.descendant_send_nodes(processed_source, node)
  else
    HelperFunctions.descendant_send_nodes_by_method_name(processed_source, node).fetch(func_name, [])
  end
  nodes.select { |func_node| func_node.receiver.nil? }
end

#find_every_method_call_by_name(node, method_name = nil) ⇒ Array<RuboCop::AST::SendNode>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns an array of method call nodes matching method_name in every descendant of node. Returns every method call if no method_name is passed.

Parameters:

Returns:

  • (Array<RuboCop::AST::SendNode>)


198
199
200
201
202
203
# File 'rubocops/shared/helper_functions.rb', line 198

def find_every_method_call_by_name(node, method_name = nil)
  return [] if node.nil?
  return HelperFunctions.descendant_send_nodes(processed_source, node) if method_name.nil?

  HelperFunctions.descendant_send_nodes_by_method_name(processed_source, node).fetch(method_name, [])
end

#find_instance_call(node, name, &_block) ⇒ T.anything

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Matches receiver part of method. Yields to a block with parent node of receiver.

Example

Match ARGV.<whatever>().

find_instance_call(node, "ARGV")

Parameters:

Returns:

  • (T.anything)


298
299
300
301
302
303
304
305
306
307
308
309
# File 'rubocops/shared/helper_functions.rb', line 298

def find_instance_call(node, name, &_block)
  HelperFunctions.descendant_send_nodes(processed_source, node).each do |method_node|
    next if method_node.receiver.nil?
    next if method_node.receiver.const_name != name &&
            !(method_node.receiver.send_type? && method_node.receiver.method_name == name)

    @offensive_node = method_node.receiver
    return true unless block_given?

    yield method_node
  end
end

#find_instance_method_call(node, instance, method_name, &_block) ⇒ T.anything

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Matches a method with a receiver. Yields to a block with matching method node.

Examples

Match Formula.factory(name).

find_instance_method_call(node, "Formula", :factory)

Match build.head?.

find_instance_method_call(node, :build, :head?)

Parameters:

Returns:

  • (T.anything)


268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'rubocops/shared/helper_functions.rb', line 268

def find_instance_method_call(node, instance, method_name, &_block)
  methods = find_every_method_call_by_name(node, method_name)
  methods.each do |method|
    next if method.receiver.nil?
    next if method.receiver.const_name != instance &&
            !(method.receiver.send_type? && method.receiver.method_name == instance)

    @offensive_node = method
    return true unless block_given?

    yield method
  end
end

#find_method_calls_by_name(node, method_name) ⇒ Array<RuboCop::AST::SendNode>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns an array of method call nodes matching method_name inside node with depth first order (child nodes).

Parameters:

Returns:

  • (Array<RuboCop::AST::SendNode>)


181
182
183
184
185
186
187
188
189
190
# File 'rubocops/shared/helper_functions.rb', line 181

def find_method_calls_by_name(node, method_name)
  return [] if node.nil?

  nodes = node.each_child_node(:send).select { |method_node| method_name == method_node.method_name }

  # The top level node can be a method
  nodes << node if node.is_a?(RuboCop::AST::SendNode) && node.method_name == method_name

  nodes
end

#find_method_def(node, method_name = nil) ⇒ RuboCop::AST::Node?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns a method definition node with method_name. Returns first method def if method_name is nil.

Parameters:

Returns:



394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'rubocops/shared/helper_functions.rb', line 394

def find_method_def(node, method_name = nil)
  return if node.nil?

  node.each_child_node(:def) do |def_node|
    def_method_name = method_name(def_node)
    next if method_name != def_method_name && method_name.present?

    @offensive_node = def_node
    return def_node
  end
  return if node.parent.nil?

  # If not found then, parent node becomes the offensive node
  @offensive_node = node.parent
  nil
end

#find_method_with_args(node, method_name, *args, &_block) ⇒ Array<RuboCop::AST::SendNode>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Given a method_name and arguments, yields to a block with matching method passed as a parameter to the block.

Parameters:

Returns:

  • (Array<RuboCop::AST::SendNode>)


235
236
237
238
239
240
241
242
243
# File 'rubocops/shared/helper_functions.rb', line 235

def find_method_with_args(node, method_name, *args, &_block)
  methods = find_every_method_call_by_name(node, method_name)
  methods.each do |method|
    next unless parameters_passed?(method, args)
    return [] unless block_given?

    yield method
  end
end

#find_node_method_by_name(node, method_name) ⇒ RuboCop::AST::Node?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns method_node matching method_name.

Parameters:

Returns:



155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'rubocops/shared/helper_functions.rb', line 155

def find_node_method_by_name(node, method_name)
  return if node.nil?

  node.each_child_node(:send) do |method_node|
    next if method_node.method_name != method_name

    @offensive_node = method_node
    return method_node
  end
  # If not found then, parent node becomes the offensive node
  @offensive_node = node.parent
  nil
end

#find_strings(node) ⇒ Array<RuboCop::AST::Node>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns all string nodes among the descendants of given node.

Parameters:

Returns:



146
147
148
149
150
151
# File 'rubocops/shared/helper_functions.rb', line 146

def find_strings(node)
  return [] if node.nil?
  return [node] if node.str_type?

  node.each_descendant(:str).to_a
end

#format_component(component_node) ⇒ Symbol?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns printable component name.

Parameters:

Returns:



532
533
534
535
536
537
538
# File 'rubocops/shared/helper_functions.rb', line 532

def format_component(component_node)
  if component_node.is_a?(RuboCop::AST::SendNode) || component_node.is_a?(RuboCop::AST::BlockNode)
    component_node.method_name
  elsif component_node.def_type?
    method_name(component_node)
  end
end

#line_number(node) ⇒ Integer

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns the line number of the node.

Parameters:

Returns:



94
95
96
# File 'rubocops/shared/helper_functions.rb', line 94

def line_number(node)
  node.loc.line
end

#line_start_column(node) ⇒ Integer

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns the begin position of the node's line in source code.

Parameters:

Returns:



82
83
84
# File 'rubocops/shared/helper_functions.rb', line 82

def line_start_column(node)
  node.source_range.source_buffer.line_range(node.loc.line).begin_pos
end

#method_called?(node, method_name) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Check if method_name is called among the direct children nodes in the given node. Check if the node itself is the method.

Parameters:

Returns:

  • (Boolean)


427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'rubocops/shared/helper_functions.rb', line 427

def method_called?(node, method_name)
  if node.is_a?(RuboCop::AST::SendNode) && node.method_name == method_name
    offending_node(node)
    return true
  end
  node.each_child_node(:send) do |call_node|
    next if call_node.method_name != method_name

    offending_node(call_node)
    return true
  end
  false
end

#method_called_ever?(node, method_name) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Check if method_name is called among every descendant node of given node.

Parameters:

Returns:

  • (Boolean)


443
444
445
446
447
448
449
# File 'rubocops/shared/helper_functions.rb', line 443

def method_called_ever?(node, method_name)
  call_node = HelperFunctions.descendant_send_nodes_by_method_name(processed_source, node)[method_name]&.first
  return false unless call_node

  @offensive_node = call_node
  true
end

#method_name(node) ⇒ Symbol?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns the method name for a def node.

Parameters:

Returns:



526
527
528
# File 'rubocops/shared/helper_functions.rb', line 526

def method_name(node)
  node.children[0] if node.def_type?
end

#node_equals?(node, var) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

To compare node with appropriate Ruby variable.

Parameters:

Returns:

  • (Boolean)


335
336
337
# File 'rubocops/shared/helper_functions.rb', line 335

def node_equals?(node, var)
  node == Parser::CurrentRuby.parse(var.inspect)
end

#offending_node(node = nil) ⇒ RuboCop::AST::Node?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Gets/sets the given node as the offending node when required in custom cops.

Parameters:

Returns:



171
172
173
174
175
# File 'rubocops/shared/helper_functions.rb', line 171

def offending_node(node = nil)
  return @offensive_node if node.nil?

  @offensive_node = node
end

#parameters(method_node) ⇒ Array<RuboCop::AST::Node>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns the array of arguments of the method_node.

Parameters:

Returns:



485
486
487
488
489
490
491
# File 'rubocops/shared/helper_functions.rb', line 485

def parameters(method_node)
  if method_node.is_a?(RuboCop::AST::SendNode) || method_node.is_a?(RuboCop::AST::BlockNode)
    method_node.arguments
  else
    []
  end
end

#parameters_passed?(method_node, params) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns true if the given parameters are present in method call and sets the method call as the offending node. Params can be string, symbol, array, hash, matching regex.

Parameters:

Returns:

  • (Boolean)


497
498
499
500
501
502
503
504
505
506
507
508
509
# File 'rubocops/shared/helper_functions.rb', line 497

def parameters_passed?(method_node, params)
  method_params = parameters(method_node)
  @offensive_node = method_node
  params.all? do |given_param|
    method_params.any? do |method_param|
      if given_param.instance_of?(Regexp)
        regex_match_group(method_param, given_param)
      else
        node_equals?(method_param, given_param)
      end
    end
  end
end

#problem(msg, &block) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:

  • msg (String)
  • block (T.proc.params(corrector: RuboCop::Cop::Corrector).void, nil)


140
141
142
# File 'rubocops/shared/helper_functions.rb', line 140

def problem(msg, &block)
  add_offense(@offensive_node, message: msg, &block)
end

#regex_match_group(node, pattern) ⇒ MatchData?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Checks for regex match of pattern in the node and sets the appropriate instance variables to report the match.

Parameters:

Returns:

  • (MatchData, nil)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'rubocops/shared/helper_functions.rb', line 54

def regex_match_group(node, pattern)
  string_repr = string_content(node).encode("UTF-8", invalid: :replace)
  match_object = string_repr.match(pattern)
  return unless match_object

  node_begin_pos = start_column(node)
  line_begin_pos = line_start_column(node)
  @column = T.let(
    if node_begin_pos == line_begin_pos
      node_begin_pos + match_object.begin(0) - line_begin_pos
    else
      node_begin_pos + match_object.begin(0) - line_begin_pos + 1
    end,
    T.nilable(Integer),
  )
  @length = T.let(match_object.to_s.length, T.nilable(Integer))
  @line_no = T.let(line_number(node), T.nilable(Integer))
  @source_buf = T.let(source_buffer(node), T.nilable(Parser::Source::Buffer))
  @offensive_node = T.let(node, T.nilable(RuboCop::AST::Node))
  @offensive_source_range = T.let(
    source_range(@source_buf, @line_no, @column, @length),
    T.nilable(Parser::Source::Range),
  )
  match_object
end

#source_buffer(node) ⇒ Parser::Source::Buffer

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Source buffer is required as an argument to report style violations.

Parameters:

Returns:



100
101
102
# File 'rubocops/shared/helper_functions.rb', line 100

def source_buffer(node)
  node.source_range.source_buffer
end

#start_column(node) ⇒ Integer

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns the begin position of the node in source code.

Parameters:

Returns:



88
89
90
# File 'rubocops/shared/helper_functions.rb', line 88

def start_column(node)
  node.source_range.begin_pos
end

#string_content(node, strip_dynamic: false) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns the string representation if node is of type str(plain) or dstr(interpolated) or const.

Parameters:

Returns:



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'rubocops/shared/helper_functions.rb', line 106

def string_content(node, strip_dynamic: false)
  case node.type
  when :str
    node.str_content
  when :dstr
    content = ""
    node.each_child_node(:str, :begin) do |child|
      content += if child.begin_type?
        strip_dynamic ? "" : child.source
      else
        child.str_content
      end
    end
    content
  when :send
    send_node = T.cast(node, RuboCop::AST::SendNode)
    if send_node.method?(:+) && (send_node.receiver.str_type? || send_node.receiver.dstr_type?)
      content = string_content(node.receiver)
      arg = send_node.arguments.first
      content += string_content(arg) if arg
      content
    else
      ""
    end
  when :const
    node.const_name
  when :sym
    node.children.first.to_s
  else
    ""
  end
end