<commit_before>require_relative "./extend/formula_cop"

module RuboCop
  module Cop
    module FormulaAudit
      class Text < FormulaCop
        def audit_formula(_node, _class_node, _parent_class_node, body_node)
          if !find_node_method_by_name(body_node, :plist_options) &&
             find_method_def(body_node, :plist)
            problem "Please set plist_options when using a formula-defined plist."
          end

          if depends_on?(body_node, "openssl") && depends_on?(body_node, "libressl")
            problem "Formulae should not depend on both OpenSSL and LibreSSL (even optionally)."
          end

          if method_called_ever?(body_node, :virtualenv_create) ||
             method_called_ever?(body_node, :virtualenv_install_with_resources)
            resource_calls = find_every_method_call_by_name(body_node, :resource)
            resource_calls.each do |m|
              if parameters_passed?(m, "setuptools")
                problem "Formulae using virtualenvs do not need a `setuptools` resource."
              end
            end
          end

          unless method_called_ever?(body_node, :go_resource)
            # processed_source.ast is passed instead of body_node because `require` would be outside body_node
            require_calls = find_every_method_call_by_name(processed_source.ast, :require)
            require_calls.each do |m|
              if parameters_passed?(m, "language/go")
                problem "require \"language/go\" is unnecessary unless using `go_resource`s"
              end
            end
          end

          factory_calls = find_every_method_call_by_name(body_node, :factory)
          unless factory_calls.nil?
            factory_calls.each do |m|
              if !m.children.empty? && m.children[0] && string_content(m.children[0]) == "Formula"
                offending_node(m)
                problem "\"Formula.factory(name)\" is deprecated in favor of \"Formula[name]\""
              end
            end
          end

          xcodebuild_calls = find_every_method_call_by_name(body_node, :xcodebuild)
          unless xcodebuild_calls.nil?
            xcodebuild_calls.each do |m|
              params = parameters(m).map { |param| string_content(param) }
              if params.none? { |param| param.include?("SYMROOT=") } || params.empty?
                offending_node(m)
                problem 'xcodebuild should be passed an explicit "SYMROOT"'
              end
            end
          end

          system_calls = find_every_method_call_by_name(body_node, :system)
          return if system_calls.nil?
          system_calls.each do |m|
            if parameters_passed?(m, "go", "get")
              problem "Formulae should not use `go get`. If non-vendored resources are required use `go_resource`s."
            elsif parameters_passed?(m, "xcodebuild")
              problem %q(use "xcodebuild *args" instead of "system 'xcodebuild', *args")
            elsif parameters_passed?(m, "scons")
              problem "use \"scons *args\" instead of \"system 'scons', *args\""
            end
          end
        end
      end
    end
  end
end<commit_msg>Use relevant methods to consolidate logic in text_cop.rb<commit_after>require_relative "./extend/formula_cop"

module RuboCop
  module Cop
    module FormulaAudit
      class Text < FormulaCop
        def audit_formula(_node, _class_node, _parent_class_node, body_node)
          if !find_node_method_by_name(body_node, :plist_options) &&
             find_method_def(body_node, :plist)
            problem "Please set plist_options when using a formula-defined plist."
          end

          if depends_on?("openssl") && depends_on?("libressl")
            problem "Formulae should not depend on both OpenSSL and LibreSSL (even optionally)."
          end

          if method_called_ever?(body_node, :virtualenv_create) ||
             method_called_ever?(body_node, :virtualenv_install_with_resources)
            find_method_with_args(body_node, :resource, "setuptools") do
              problem "Formulae using virtualenvs do not need a `setuptools` resource."
            end
          end

          unless method_called_ever?(body_node, :go_resource)
            # processed_source.ast is passed instead of body_node because `require` would be outside body_node
            find_method_with_args(processed_source.ast, :require, "language/go") do
              problem "require \"language/go\" is unnecessary unless using `go_resource`s"
            end
          end

          find_instance_method_call(body_node, "Formula", :factory) do
            problem "\"Formula.factory(name)\" is deprecated in favor of \"Formula[name]\""
          end

          find_every_method_call_by_name(body_node, :xcodebuild).each do |m|
            next if parameters_passed?(m, /SYMROOT=/)
            problem 'xcodebuild should be passed an explicit "SYMROOT"'
          end

          find_method_with_args(body_node, :system, "xcodebuild") do
            problem %q(use "xcodebuild *args" instead of "system 'xcodebuild', *args")
          end

          find_method_with_args(body_node, :system, "scons") do
            problem "use \"scons *args\" instead of \"system 'scons', *args\""
          end

          find_method_with_args(body_node, :system, "go", "get") do
            problem "Formulae should not use `go get`. If non-vendored resources are required use `go_resource`s."
          end
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08496732026143791, 'fraction_numerical': 0.00036310820624546115, 'mean_word_length': 2.312687913409501, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5308444', 'n_tokens_mistral': 1579, 'n_tokens_neox': 1563, 'n_words': 409}