Class: RuboCop::Cop::FormulaAudit::PythonVersions Private

Inherits:
RuboCop::Cop::FormulaCop show all
Extended by:
AutoCorrector
Defined in:
rubocops/lines.rb,
sorbet/rbi/dsl/rubo_cop/cop/formula_audit/python_versions.rbi

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 Python versions are consistent.

Constant Summary collapse

PYTHON_VERSION_REFERENCE_REGEX =

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

/\Apython(@)?(\d\.\d+)\z/

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, #dependency_name_hash_match?, #dependency_type_hash_match?, #depends_on?, #depends_on_name_type?, #formula_name, #formula_tap, #get_checksum_node, #on_class, #required_dependency?, #required_dependency_name?, #style_exceptions_dir, #tap_style_exception?, #versioned_formula?

Methods included from HelperFunctions

#block_method_called_in_block?, #check_precedence, #class_name, #component_precedes?, descendant_send_nodes, descendant_send_nodes_by_method_name, #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, #source_buffer, #start_column, #string_content

Instance Method Details

#audit_formula(formula_nodes) ⇒ 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:



633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
# File 'rubocops/lines.rb', line 633

def audit_formula(formula_nodes)
  return if (body_node = formula_nodes.body_node).nil?

  python_versions = find_every_method_call_by_name(body_node, :depends_on).filter_map do |dep|
    first_param = parameters(dep).first
    first_param = first_param.keys.first if first_param.is_a?(RuboCop::AST::HashNode)
    match = string_content(first_param).rpartition("/").last.match(PYTHON_VERSION_REFERENCE_REGEX)
    match[2] if match && match[1]
  end.uniq
  return if python_versions.size != 1

  python_version = python_versions.fetch(0)
  if python_version.start_with?("3.") && !find_method_def(body_node, :python3)
    hardcoded_python_assignment(body_node) do |value|
      next unless (match = string_content(value).match(PYTHON_VERSION_REFERENCE_REGEX))
      next if match[1]
      next unless value.each_ancestor(:def, :block).any? do |node|
        node.def_type? || (node.is_a?(RuboCop::AST::BlockNode) && node.method_name == :test)
      end

      assignment = value.parent
      offending_node(value)
      if assignment.children.first == :python
        problem "Use `python = python3` instead of a hardcoded Python executable." do |corrector|
          corrector.replace(value.source_range, "python3")
        end
      else
        problem "Use `python3` directly instead of assigning a hardcoded Python executable." do |corrector|
          corrector.remove(range_by_whole_lines(assignment.source_range, include_final_newline: true))
        end
      end
    end
  end

  find_strings(body_node).each do |str|
    content = string_content(str)

    next unless (match = content.match(PYTHON_VERSION_REFERENCE_REGEX))
    next if python_version == match[2]

    fix = if match[1]
      "python@#{python_version}"
    else
      "python#{python_version}"
    end

    offending_node(str)
    problem "References to `#{content}` should " \
            "match the specified python dependency (`#{fix}`)" do |corrector|
      corrector.replace(str.source_range, "\"#{fix}\"")
    end
  end
end

#hardcoded_python_assignment(node, *pattern, **kwargs, &block) ⇒ T.untyped

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:

  • (T.untyped)


17
# File 'sorbet/rbi/dsl/rubo_cop/cop/formula_audit/python_versions.rbi', line 17

def hardcoded_python_assignment(node, *pattern, **kwargs, &block); end