Class: RuboCop::Cop::FormulaCop Private

Inherits:
RuboCop::Cop show all
Includes:
RangeHelp, HelperFunctions
Defined in:
Library/Homebrew/rubocops/extend/formula.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Superclass for all formula cops.

Direct Known Subclasses

RuboCop::Cop::FormulaAudit::AssertStatements, RuboCop::Cop::FormulaAudit::Caveats, RuboCop::Cop::FormulaAudit::Checksum, RuboCop::Cop::FormulaAudit::ChecksumCase, RuboCop::Cop::FormulaAudit::ClassInheritance, RuboCop::Cop::FormulaAudit::ClassName, RuboCop::Cop::FormulaAudit::Comments, RuboCop::Cop::FormulaAudit::ComponentsOrder, RuboCop::Cop::FormulaAudit::ComponentsRedundancy, RuboCop::Cop::FormulaAudit::Conflicts, RuboCop::Cop::FormulaAudit::DependencyOrder, RuboCop::Cop::FormulaAudit::DeprecateDisableDate, RuboCop::Cop::FormulaAudit::DeprecateDisableReason, RuboCop::Cop::FormulaAudit::Desc, RuboCop::Cop::FormulaAudit::Files, RuboCop::Cop::FormulaAudit::GitUrls, RuboCop::Cop::FormulaAudit::Homepage, RuboCop::Cop::FormulaAudit::KegOnly, RuboCop::Cop::FormulaAudit::LicenseArrays, RuboCop::Cop::FormulaAudit::Licenses, RuboCop::Cop::FormulaAudit::Lines, RuboCop::Cop::FormulaAudit::LivecheckRegexCaseInsensitive, RuboCop::Cop::FormulaAudit::LivecheckRegexExtension, RuboCop::Cop::FormulaAudit::LivecheckRegexIfPageMatch, RuboCop::Cop::FormulaAudit::LivecheckRegexParentheses, RuboCop::Cop::FormulaAudit::LivecheckSkip, RuboCop::Cop::FormulaAudit::LivecheckUrlProvided, RuboCop::Cop::FormulaAudit::LivecheckUrlSymbol, RuboCop::Cop::FormulaAudit::Miscellaneous, RuboCop::Cop::FormulaAudit::MpiCheck, RuboCop::Cop::FormulaAudit::OptionDeclarations, RuboCop::Cop::FormulaAudit::Options, RuboCop::Cop::FormulaAudit::Patches, RuboCop::Cop::FormulaAudit::PyPiUrls, RuboCop::Cop::FormulaAudit::PythonVersions, RuboCop::Cop::FormulaAudit::SafePopenCommands, RuboCop::Cop::FormulaAudit::ShellVariables, RuboCop::Cop::FormulaAudit::Test, RuboCop::Cop::FormulaAudit::Text, RuboCop::Cop::FormulaAudit::Urls, RuboCop::Cop::FormulaAudit::UsesFromMacos, RuboCop::Cop::FormulaAudit::Version, RuboCop::Cop::FormulaAuditStrict::CoreRequirements, RuboCop::Cop::FormulaAuditStrict::GitUrls, RuboCop::Cop::FormulaAuditStrict::MakeCheck, RuboCop::Cop::FormulaAuditStrict::ShellCommands, RuboCop::Cop::FormulaAuditStrict::TestPresent, RuboCop::Cop::FormulaAuditStrict::Text

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HelperFunctions

#line_number, #line_start_column, #problem, #regex_match_group, #source_buffer, #start_column, #string_content

Instance Attribute Details

#file_pathObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


25
26
27
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 25

def file_path
  @file_path
end

Instance Method Details

#audit_commentsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Yields to a block with comment text as parameter.


421
422
423
424
425
426
427
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 421

def audit_comments
  @processed_source.comments.each do |comment_node|
    @offensive_node = comment_node
    @offense_source_range = :expression
    yield comment_node.text
  end
end

#audit_urls(urls, regex) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Yields to block when there is a match.

Parameters:

  • urls (Array)

    url/mirror method call nodes

  • regex (Regexp)

    pattern to match URLs


45
46
47
48
49
50
51
52
53
54
55
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 45

def audit_urls(urls, regex)
  urls.each do |url_node|
    url_string_node = parameters(url_node).first
    url_string = string_content(url_string_node)
    match_object = regex_match_group(url_string_node, regex)
    next unless match_object

    offending_node(url_string_node.parent)
    yield match_object, url_string
  end
end

#block_size(block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the block length of the block node.


452
453
454
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 452

def block_size(block)
  block.loc.end.line - block.loc.begin.line
end

#caveats_stringsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return all the caveats' string nodes in an array.


381
382
383
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 381

def caveats_strings
  find_strings(find_method_def(@body, :caveats))
end

#check_precedence(first_nodes, next_nodes) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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


354
355
356
357
358
359
360
361
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 354

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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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


435
436
437
438
439
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 435

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

#component_precedes?(first_node, next_node) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:

  • (Boolean)

364
365
366
367
368
369
370
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 364

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

  @offense_source_range = first_node.source_range
  @offensive_node = first_node
  true
end

#depends_on?(dependency_name, *types) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns nil if does not depend on dependency_name.

Parameters:

  • dependency_name

    dependency's name

Returns:

  • (Boolean)

173
174
175
176
177
178
179
180
181
182
183
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 173

def depends_on?(dependency_name, *types)
  types = [:any] if types.empty?
  dependency_nodes = find_every_method_call_by_name(@body, :depends_on)
  idx = dependency_nodes.index do |n|
    types.any? { |type| depends_on_name_type?(n, dependency_name, type) }
  end
  return if idx.nil?

  @offense_source_range = dependency_nodes[idx].source_range
  @offensive_node = dependency_nodes[idx]
end

#depends_on_name_type?(node, name = nil, type = :required) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns true if given dependency name and dependency type exist in given dependency method call node. TODO: Add case where key of hash is an array

Returns:

  • (Boolean)

187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 187

def depends_on_name_type?(node, name = nil, type = :required)
  name_match = if name
    false
  else
    true # Match only by type when name is nil
  end

  case type
  when :required
    type_match = required_dependency?(node)
    name_match ||= required_dependency_name?(node, name) if type_match
  when :build, :test, :optional, :recommended
    type_match = dependency_type_hash_match?(node, type)
    name_match ||= dependency_name_hash_match?(node, name) if type_match
  when :any
    type_match = true
    name_match ||= required_dependency_name?(node, name)
    name_match ||= dependency_name_hash_match?(node, name)
  else
    type_match = false
  end

  if type_match || name_match
    @offensive_node = node
    @offense_source_range = node.source_range
  end
  type_match && name_match
end

#end_column(node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the ending position of the node in source code.


430
431
432
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 430

def end_column(node)
  node.source_range.end_pos
end

#expression_negated?(node) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if negation is present in the given node.

Returns:

  • (Boolean)

373
374
375
376
377
378
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 373

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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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!


279
280
281
282
283
284
285
286
287
288
289
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 279

def find_all_blocks(node, block_name)
  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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a block named block_name inside node.


254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 254

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
    @offense_source_range = block_node.source_range
    return block_node
  end
  # If not found then, parent node becomes the offensive node
  @offensive_node = node.parent
  @offense_source_range = node.parent.source_range
  nil
end

#find_blocks(node, block_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns an array of block nodes named block_name inside node.


271
272
273
274
275
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 271

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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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


218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 218

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

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

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

#find_every_func_call_by_name(node, func_name = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

111
112
113
114
115
116
117
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 111

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

  node.each_descendant(:send).select do |func_node|
    func_node.receiver.nil? && (func_name.nil? || func_name == func_node.method_name)
  end
end

#find_every_method_call_by_name(node, method_name = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.


97
98
99
100
101
102
103
104
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 97

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

  node.each_descendant(:send).select do |method_node|
    method_name.nil? ||
      method_name == method_node.method_name
  end
end

#find_instance_call(node, name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Examples:

to match ARGV.<whatever>()

find_instance_call(node, "ARGV")

156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 156

def find_instance_call(node, name)
  node.each_descendant(:send) 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)

    @offense_source_range = method_node.receiver.source_range
    @offensive_node = method_node.receiver
    return true unless block_given?

    yield method_node
  end
end

#find_instance_method_call(node, instance, method_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Examples:

to match Formula.factory(name)

find_instance_method_call(node, "Formula", :factory)

to match build.head?

find_instance_method_call(node, :build, :head?)

137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 137

def find_instance_method_call(node, instance, method_name)
  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)

    @offense_source_range = method.source_range
    @offensive_node = method
    return true unless block_given?

    yield method
  end
end

#find_method_calls_by_name(node, method_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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


89
90
91
92
93
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 89

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

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

#find_method_def(node, method_name = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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


293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 293

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 unless method_name == def_method_name || method_name.nil?

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

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

#find_method_with_args(node, method_name, *args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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


121
122
123
124
125
126
127
128
129
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 121

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

    yield method
  end
end

#find_node_method_by_name(node, method_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns method_node matching method_name.


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 66

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

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

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

#find_strings(node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns all string nodes among the descendants of given node.


58
59
60
61
62
63
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 58

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

  node.each_descendant(:str)
end

#format_component(component_node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns printable component name.


462
463
464
465
466
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 462

def format_component(component_node)
  return component_node.method_name if component_node.send_type? || component_node.block_type?

  method_name(component_node) if component_node.def_type?
end

#formula_tapObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the formula tap.


469
470
471
472
473
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 469

def formula_tap
  return unless match_obj = @file_path.match(%r{/(homebrew-\w+)/})

  match_obj[1]
end

#get_checksum_node(call) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the sha256 str node given a sha256 call node.


409
410
411
412
413
414
415
416
417
418
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 409

def get_checksum_node(call)
  return if parameters(call).empty? || parameters(call).nil?

  if parameters(call).first.str_type?
    parameters(call).first
  # sha256 is passed as a key-value pair in bottle blocks
  elsif parameters(call).first.hash_type?
    parameters(call).first.keys.first
  end
end

#method_called?(node, method_name) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:

  • (Boolean)

327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 327

def method_called?(node, method_name)
  if node.send_type? && node.method_name == method_name
    offending_node(node)
    return true
  end
  node.each_child_node(:send) do |call_node|
    next unless 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. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:

  • (Boolean)

342
343
344
345
346
347
348
349
350
351
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 342

def method_called_ever?(node, method_name)
  node.each_descendant(:send) do |call_node|
    next unless call_node.method_name == method_name

    @offensive_node = call_node
    @offense_source_range = call_node.source_range
    return true
  end
  false
end

#method_called_in_block?(node, method_name) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if a method is called inside a block.

Returns:

  • (Boolean)

313
314
315
316
317
318
319
320
321
322
323
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 313

def method_called_in_block?(node, method_name)
  block_body = node.children[2]
  block_body.each_child_node(:send) do |call_node|
    next unless call_node.method_name == method_name

    @offensive_node = call_node
    @offense_source_range = call_node.source_range
    return true
  end
  false
end

#method_name(node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the method name for a def node.


442
443
444
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 442

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. You should avoid using this method if possible, as it may be removed or be changed in the future.

To compare node with appropriate Ruby variable.

Returns:

  • (Boolean)

249
250
251
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 249

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

#offending_node(node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sets the given node as the offending node when required in custom cops.


83
84
85
86
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 83

def offending_node(node)
  @offensive_node = node
  @offense_source_range = node.source_range
end

#on_class(node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method is called by RuboCop and is the main entry point.


30
31
32
33
34
35
36
37
38
39
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 30

def on_class(node)
  @file_path = processed_source.buffer.name
  return unless file_path_allowed?
  return unless formula_class?(node)
  return unless respond_to?(:audit_formula)

  class_node, parent_class_node, @body = *node
  @formula_name = Pathname.new(@file_path).basename(".rb").to_s
  audit_formula(node, class_node, parent_class_node, @body)
end

#parameters(method_node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the array of arguments of the method_node.


386
387
388
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 386

def parameters(method_node)
  method_node.arguments if method_node.send_type? || method_node.block_type?
end

#parameters_passed?(method_node, *params) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.

Returns:

  • (Boolean)

393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 393

def parameters_passed?(method_node, *params)
  method_params = parameters(method_node)
  @offensive_node = method_node
  @offense_source_range = method_node.source_range
  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

#size(node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the node size in the source code.


447
448
449
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 447

def size(node)
  node.source_range.size
end

#versioned_formula?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns true if the formula is versioned.

Returns:

  • (Boolean)

457
458
459
# File 'Library/Homebrew/rubocops/extend/formula.rb', line 457

def versioned_formula?
  @formula_name.include?("@")
end