Class: RuboCop::Cop::FormulaAudit::OSConditionals Private

Inherits:
RuboCop::Cop::FormulaCop show all
Extended by:
AutoCorrector
Defined in:
rubocops/lines.rb

Overview

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

This cop makes sure that OS conditionals are consistent.

Instance Attribute Summary

Attributes inherited from RuboCop::Cop::FormulaCop

#file_path

Instance Method Summary collapse

Methods inherited from RuboCop::Cop::FormulaCop

#audit_comments, #audit_urls, #caveats_strings, #depends_on?, #depends_on_name_type?, #formula_tap, #get_checksum_node, #on_class, #tap_style_exception?, #versioned_formula?

Methods included from HelperFunctions

#block_method_called_in_block?, #block_size, #check_precedence, #class_name, #component_precedes?, #end_column, #expression_negated?, #find_all_blocks, #find_block, #find_blocks, #find_const, #find_every_func_call_by_name, #find_every_method_call_by_name, #find_instance_call, #find_instance_method_call, #find_method_calls_by_name, #find_method_def, #find_method_with_args, #find_node_method_by_name, #find_strings, #format_component, #line_number, #line_start_column, #method_called?, #method_called_ever?, #method_name, #node_equals?, #offending_node, #parameters, #parameters_passed?, #problem, #regex_match_group, #size, #source_buffer, #start_column, #string_content

Instance Method Details

#audit_formula(_node, _class_node, _parent_class_node, body_node) ⇒ Object

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.



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'rubocops/lines.rb', line 381

def audit_formula(_node, _class_node, _parent_class_node, body_node)
  no_on_os_method_names = [:install, :post_install].freeze
  no_on_os_block_names = [:service, :test].freeze
  [[:on_macos, :mac?], [:on_linux, :linux?]].each do |on_method_name, if_method_name|
    if_method_and_class = "if OS.#{if_method_name}"
    no_on_os_method_names.each do |formula_method_name|
      method_node = find_method_def(body_node, formula_method_name)
      next unless method_node
      next unless method_called_ever?(method_node, on_method_name)

      problem "Don't use '#{on_method_name}' in 'def #{formula_method_name}', " \
              "use '#{if_method_and_class}' instead." do |corrector|
        block_node = offending_node.parent
        next if block_node.type != :block

        # TODO: could fix corrector to handle this but punting for now.
        next if block_node.single_line?

        source_range = offending_node.source_range.join(offending_node.parent.loc.begin)
        corrector.replace(source_range, if_method_and_class)
      end
    end

    no_on_os_block_names.each do |formula_block_name|
      block_node = find_block(body_node, formula_block_name)
      next unless block_node
      next unless block_method_called_in_block?(block_node, on_method_name)

      problem "Don't use '#{on_method_name}' in '#{formula_block_name} do', " \
              "use '#{if_method_and_class}' instead." do |corrector|
        # TODO: could fix corrector to handle this but punting for now.
        next if offending_node.single_line?

        source_range = offending_node.send_node.source_range.join(offending_node.body.source_range.begin)
        corrector.replace(source_range, "#{if_method_and_class}\n")
      end
    end

    # Don't restrict OS.mac? or OS.linux? usage in taps; they don't care
    # as much as we do about e.g. formulae.brew.sh generation, often use
    # platform-specific URLs and we don't want to add DSLs to support
    # that case.
    next if formula_tap != "homebrew-core"

    find_instance_method_call(body_node, "OS", if_method_name) do |method|
      valid = T.let(false, T::Boolean)
      method.each_ancestor do |ancestor|
        valid_method_names = case ancestor.type
        when :def
          no_on_os_method_names
        when :block
          no_on_os_block_names
        else
          next
        end
        next unless valid_method_names.include?(ancestor.method_name)

        valid = true
        break
      end
      next if valid

      offending_node(method)
      problem "Don't use '#{if_method_and_class}', use '#{on_method_name} do' instead." do |corrector|
        if_node = method.parent
        next if if_node.type != :if

        # TODO: could fix corrector to handle this but punting for now.
        next if if_node.unless?

        corrector.replace(if_node.source_range, "#{on_method_name} do\n#{if_node.body.source}\nend")
      end
    end
  end
end