Class: RuboCop::Cop::FormulaCop

Inherits:
RuboCop::Cop show all
Defined in:
Library/Homebrew/rubocops/extend/formula_cop.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#file_pathObject

Returns the value of attribute file_path



7
8
9
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 7

def file_path
  @file_path
end

Instance Method Details

#audit_commentsObject

Yields to a block with comment text as parameter



381
382
383
384
385
386
387
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 381

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

Yields to block when there is a match Parameters: urls : Array of url/mirror method call nodes

regex: regex pattern to match urls


45
46
47
48
49
50
51
52
53
54
# File 'Library/Homebrew/rubocops/extend/formula_cop.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

Returns the block length of the block node



427
428
429
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 427

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

#caveats_stringsObject

Return all the caveats' string nodes in an array



342
343
344
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 342

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

#check_precedence(first_nodes, next_nodes) ⇒ Object

Checks for precedence, returns the first pair of precedence violating nodes



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

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

#class_name(node) ⇒ Object

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



410
411
412
413
414
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 410

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

#component_precedes?(first_node, next_node) ⇒ Boolean

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

Returns:

  • (Boolean)


327
328
329
330
331
332
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 327

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

Returns nil if does not depend on dependency_name args: node - dependency_name - dependency's name

Returns:

  • (Boolean)


148
149
150
151
152
153
154
155
156
157
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 148

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

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)


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 161

def depends_on_name_type?(node, name = nil, type = :required)
  if name
    name_match = false
  else
    name_match = 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, :optional, :recommended, :run
    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

Returns the ending position of the node in source code



400
401
402
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 400

def end_column(node)
  node.source_range.end_pos
end

#expression_negated?(node) ⇒ Boolean

Check if negation is present in the given node

Returns:

  • (Boolean)


335
336
337
338
339
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 335

def expression_negated?(node)
  return false if node.parent.nil?
  return false unless node.parent.method_name.equal?(:!)
  offending_node(node.parent)
end

#find_all_blocks(node, block_name) ⇒ Object

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!



248
249
250
251
252
253
254
255
256
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 248

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

Returns a block named block_name inside node



226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 226

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

Returns an array of block nodes named block_name inside node



241
242
243
244
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 241

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

Find CONSTANTs in the source if block given, yield matching nodes



192
193
194
195
196
197
198
199
200
201
202
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 192

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_method_call_by_name(node, method_name = nil) ⇒ Object

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



92
93
94
95
96
97
98
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 92

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

Matches receiver part of method, EX: to match `ARGV.<whatever>()` call `find_instance_call(node, “ARGV”)` yields to a block with parent node of receiver



134
135
136
137
138
139
140
141
142
143
144
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 134

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.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

Matches a method with a receiver, EX: to match `Formula.factory(name)` call `find_instance_method_call(node, “Formula”, :factory)` EX: to match `build.head?` call `find_instance_method_call(node, :build, :head?)` yields to a block with matching method node



117
118
119
120
121
122
123
124
125
126
127
128
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 117

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.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

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



85
86
87
88
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 85

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

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



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 260

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

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



102
103
104
105
106
107
108
109
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 102

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

Returns method_node matching method_name



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 64

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

Returns all string nodes among the descendants of given node



57
58
59
60
61
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 57

def find_strings(node)
  return [] if node.nil?
  return node if node.str_type?
  node.each_descendant(:str)
end

#format_component(component_node) ⇒ Object

Returns printable component name



458
459
460
461
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 458

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

Returns the formula tap



464
465
466
467
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 464

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

#get_checksum_node(call) ⇒ Object

Returns the sha256 str node given a sha256 call node



370
371
372
373
374
375
376
377
378
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 370

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

#line_number(node) ⇒ Object

Returns the line number of the node



405
406
407
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 405

def line_number(node)
  node.loc.line
end

#line_start_column(node) ⇒ Object

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



390
391
392
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 390

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

#method_called?(node, method_name) ⇒ Boolean

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)


290
291
292
293
294
295
296
297
298
299
300
301
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 290

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

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

Returns:

  • (Boolean)


304
305
306
307
308
309
310
311
312
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 304

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

Check if a method is called inside a block

Returns:

  • (Boolean)


277
278
279
280
281
282
283
284
285
286
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 277

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

Returns the method name for a def node



417
418
419
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 417

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

#node_equals?(node, var) ⇒ Boolean

To compare node with appropriate Ruby variable

Returns:

  • (Boolean)


221
222
223
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 221

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

#offending_node(node) ⇒ Object

Set the given node as the offending node when required in custom cops



79
80
81
82
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 79

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

#on_class(node) ⇒ Object

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



11
12
13
14
15
16
17
18
19
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 11

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

Returns the array of arguments of the method_node



347
348
349
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 347

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

#parameters_passed?(method_node, *params) ⇒ Boolean

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)


354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 354

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.class == Regexp
        regex_match_group(method_param, given_param)
      else
        node_equals?(method_param, given_param)
      end
    end
  end
end

#problem(msg) ⇒ Object



469
470
471
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 469

def problem(msg)
  add_offense(@offensive_node, location: @offense_source_range, message: msg)
end

#regex_match_group(node, pattern) ⇒ Object

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 23

def regex_match_group(node, pattern)
  string_repr = string_content(node)
  match_object = string_repr.match(pattern)
  return unless match_object
  node_begin_pos = start_column(node)
  line_begin_pos = line_start_column(node)
  if node_begin_pos == line_begin_pos
    @column = node_begin_pos + match_object.begin(0) - line_begin_pos
  else
    @column = node_begin_pos + match_object.begin(0) - line_begin_pos + 1
  end
  @length = match_object.to_s.length
  @line_no = line_number(node)
  @source_buf = source_buffer(node)
  @offense_source_range = source_range(@source_buf, @line_no, @column, @length)
  @offensive_node = node
  match_object
end

#size(node) ⇒ Object

Returns the node size in the source code



422
423
424
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 422

def size(node)
  node.source_range.size
end

#source_buffer(node) ⇒ Object

Source buffer is required as an argument to report style violations



432
433
434
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 432

def source_buffer(node)
  node.source_range.source_buffer
end

#start_column(node) ⇒ Object

Returns the begin position of the node in source code



395
396
397
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 395

def start_column(node)
  node.source_range.begin_pos
end

#string_content(node) ⇒ Object

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



437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 437

def string_content(node)
  case node.type
  when :str
    node.str_content
  when :dstr
    node.each_child_node(:str).map(&:str_content).join("")
  when :const
    node.const_name
  when :sym
    node.children.first.to_s
  else
    ""
  end
end

#versioned_formula?Boolean

Returns true if the formula is versioned

Returns:

  • (Boolean)


453
454
455
# File 'Library/Homebrew/rubocops/extend/formula_cop.rb', line 453

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