Class: RuboCop::Cop::FormulaCop Private

Inherits:
Base
  • Object
show all
Includes:
RangeHelp, HelperFunctions
Defined in:
rubocops/extend/formula.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.

Superclass for all formula cops.

Direct Known Subclasses

RuboCop::Cop::FormulaAudit::AssertStatements, RuboCop::Cop::FormulaAudit::BottleDigestIndentation, RuboCop::Cop::FormulaAudit::BottleFormat, RuboCop::Cop::FormulaAudit::BottleOrder, RuboCop::Cop::FormulaAudit::BottleTagIndentation, 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::ProvidedByMacos, 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::GitUrls, RuboCop::Cop::FormulaAuditStrict::MakeCheck, RuboCop::Cop::FormulaAuditStrict::Requirements, RuboCop::Cop::FormulaAuditStrict::TestPresent, RuboCop::Cop::FormulaAuditStrict::Text

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HelperFunctions

#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_called_in_block?, #method_name, #node_equals?, #offending_node, #parameters, #parameters_passed?, #problem, #regex_match_group, #size, #source_buffer, #start_column, #string_content

Instance Attribute Details

#file_pathObject

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.



25
26
27
# File '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. 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.

Yields to a block with comment text as parameter.



144
145
146
147
148
149
# File 'rubocops/extend/formula.rb', line 144

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

#audit_urls(urls, regex) ⇒ 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.

Yields to block when there is a match.

Parameters:

  • urls (Array)

    url/mirror method call nodes

  • regex (Regexp)

    pattern to match URLs



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

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

#caveats_stringsObject

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.

Return all the caveats’ string nodes in an array.



117
118
119
# File 'rubocops/extend/formula.rb', line 117

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

#depends_on?(dependency_name, *types) ⇒ Boolean

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.

Returns nil if does not depend on dependency_name.

Parameters:

  • dependency_name

    dependency’s name

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
70
# File 'rubocops/extend/formula.rb', line 61

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?

  @offensive_node = dependency_nodes[idx]
end

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

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.

Returns true if given dependency name and dependency type exist in given dependency method call node.

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'rubocops/extend/formula.rb', line 74

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

  @offensive_node = node if type_match || name_match
  type_match && name_match
end

#formula_tapObject

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.

Returns the formula tap.



157
158
159
160
161
# File 'rubocops/extend/formula.rb', line 157

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

Returns the sha256 str node given a sha256 call node.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'rubocops/extend/formula.rb', line 122

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?
    if parameters(call).first.keys.first.value == :cellar
      # sha256 :cellar :any, :tag "hexdigest"
      parameters(call).first.values.last
    elsif parameters(call).first.keys.first.is_a?(RuboCop::AST::SymbolNode)
      # sha256 :tag "hexdigest"
      parameters(call).first.values.first
    else
      # Legacy bottle block syntax
      # sha256 "hexdigest" => :tag
      parameters(call).first.keys.first
    end
  end
end

#on_class(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.

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



30
31
32
33
34
35
36
37
38
39
40
# File '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
  @tap_style_exceptions = nil
  audit_formula(node, class_node, parent_class_node, @body)
end

#tap_style_exception?(list, formula = nil) ⇒ Boolean

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.

Returns whether the given formula exists in the given style exception list. Defaults to the current formula being checked.

Returns:

  • (Boolean)


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'rubocops/extend/formula.rb', line 165

def tap_style_exception?(list, formula = nil)
  if @tap_style_exceptions.nil? && !formula_tap.nil?
    @tap_style_exceptions = {}

    style_exceptions_dir = "#{File.dirname(File.dirname(@file_path))}/style_exceptions/*.json"
    Pathname.glob(style_exceptions_dir).each do |exception_file|
      list_name = exception_file.basename.to_s.chomp(".json").to_sym
      list_contents = begin
        JSON.parse exception_file.read
      rescue JSON::ParserError
        nil
      end
      next if list_contents.nil? || list_contents.count.zero?

      @tap_style_exceptions[list_name] = list_contents
    end
  end

  return false if @tap_style_exceptions.nil? || @tap_style_exceptions.count.zero?
  return false unless @tap_style_exceptions.key? list

  @tap_style_exceptions[list].include?(formula || @formula_name)
end

#versioned_formula?Boolean

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.

Returns true if the formula is versioned.

Returns:

  • (Boolean)


152
153
154
# File 'rubocops/extend/formula.rb', line 152

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