Class: RuboCop::Cop::FormulaCop Abstract Private
- Extended by:
- T::Helpers
- Includes:
- RangeHelp, HelperFunctions
- Defined in:
- rubocops/extend/formula_cop.rb,
sorbet/rbi/dsl/rubo_cop/cop/formula_cop.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.
It cannot be directly instantiated. Subclasses must implement the abstract methods below.
Abstract base class 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::CMakeArgs, 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::FullDependencyCheck, RuboCop::Cop::FormulaAudit::GenerateCompletionsDSL, RuboCop::Cop::FormulaAudit::GitUrls, RuboCop::Cop::FormulaAudit::Homepage, RuboCop::Cop::FormulaAudit::HttpUrls, RuboCop::Cop::FormulaAudit::InstallSteps, RuboCop::Cop::FormulaAudit::JavaVersions, RuboCop::Cop::FormulaAudit::JpegCheck, RuboCop::Cop::FormulaAudit::KegOnly, RuboCop::Cop::FormulaAudit::LibiconvCheck, RuboCop::Cop::FormulaAudit::LicenseArrays, RuboCop::Cop::FormulaAudit::Licenses, 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::MacOSOnLinux, RuboCop::Cop::FormulaAudit::Miscellaneous, RuboCop::Cop::FormulaAudit::MpiCheck, RuboCop::Cop::FormulaAudit::NoAutobump, RuboCop::Cop::FormulaAudit::NonPublicApiUsage, RuboCop::Cop::FormulaAudit::OnSystemConditionals, RuboCop::Cop::FormulaAudit::OptionDeclarations, RuboCop::Cop::FormulaAudit::Options, RuboCop::Cop::FormulaAudit::Patches, RuboCop::Cop::FormulaAudit::ProvidedByMacos, RuboCop::Cop::FormulaAudit::PyPiUrls, RuboCop::Cop::FormulaAudit::PyoxidizerCheck, RuboCop::Cop::FormulaAudit::PythonVersions, RuboCop::Cop::FormulaAudit::QuicTLSCheck, RuboCop::Cop::FormulaAudit::RedundantGenerateCompletionsShells, RuboCop::Cop::FormulaAudit::ResourceRequiresDependencies, RuboCop::Cop::FormulaAudit::RustCheck, RuboCop::Cop::FormulaAudit::SafePopenCommands, RuboCop::Cop::FormulaAudit::Service, RuboCop::Cop::FormulaAudit::ShellVariables, RuboCop::Cop::FormulaAudit::SingleGenerateCompletionsDSLCall, RuboCop::Cop::FormulaAudit::StdGoArgs, RuboCop::Cop::FormulaAudit::StdNpmArgs, RuboCop::Cop::FormulaAudit::Test, RuboCop::Cop::FormulaAudit::TestResource, RuboCop::Cop::FormulaAudit::Text, RuboCop::Cop::FormulaAudit::Urls, RuboCop::Cop::FormulaAudit::UsesFromMacos, RuboCop::Cop::FormulaAudit::Version, RuboCop::Cop::FormulaAudit::ZeroZeroZeroZero, RuboCop::Cop::FormulaAuditStrict::GitUrls, RuboCop::Cop::FormulaAuditStrict::MakeCheck, RuboCop::Cop::FormulaAuditStrict::Text
Defined Under Namespace
Classes: FormulaNodes
Instance Attribute Summary collapse
- #file_path ⇒ String? private
Instance Method Summary collapse
-
#audit_comments(&_block) ⇒ void
private
Yields to a block with comment text as parameter.
-
#audit_depends_on(dependency_name, *types, &_block) ⇒ void
private
Yields to block each
depends_onnode for dependency_name. - #audit_formula(formula_nodes) ⇒ void abstract private
-
#audit_urls(urls, regex, &_block) ⇒ void
private
Yields to block when there is a match.
-
#caveats_strings ⇒ Array<RuboCop::AST::Node>
private
Return all the caveats' string nodes in an array.
- #dependency_name_hash_match?(node, *pattern, **kwargs, &block) ⇒ Boolean private
- #dependency_type_hash_match?(node, *pattern, **kwargs, &block) ⇒ Boolean private
-
#depends_on?(dependency_name, *types) ⇒ Boolean
private
Returns if the formula depends on dependency_name.
-
#depends_on_name_type?(node, name = nil, type = :required) ⇒ Boolean
private
Returns true if given dependency name and dependency type exist in given dependency method call node.
-
#formula_name ⇒ String
private
The name of the formula being audited.
-
#formula_tap ⇒ String?
private
Returns the formula tap.
-
#get_checksum_node(call) ⇒ RuboCop::AST::Node?
private
Returns the sha256 str node given a sha256 call node.
-
#on_class(node) ⇒ void
private
This method is called by RuboCop and is the main entry point.
- #required_dependency?(node, *pattern, **kwargs, &block) ⇒ Boolean private
- #required_dependency_name?(node, *pattern, **kwargs, &block) ⇒ Boolean private
-
#style_exceptions_dir ⇒ String?
private
Returns the style exceptions directory from the file path.
-
#tap_style_exception?(list, formula = nil) ⇒ Boolean
private
Returns whether the given formula exists in the given style exception list.
-
#versioned_formula? ⇒ Boolean
private
Returns true if the formula is versioned.
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 Attribute Details
#file_path ⇒ String?
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.
18 19 20 |
# File 'rubocops/extend/formula_cop.rb', line 18 def file_path @file_path end |
Instance Method Details
#audit_comments(&_block) ⇒ 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.
Yields to a block with comment text as parameter.
184 185 186 187 188 189 |
# File 'rubocops/extend/formula_cop.rb', line 184 def audit_comments(&_block) processed_source.comments.each do |comment_node| @offensive_node = comment_node yield comment_node.text end end |
#audit_depends_on(dependency_name, *types, &_block) ⇒ 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.
Yields to block each depends_on node for dependency_name.
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'rubocops/extend/formula_cop.rb', line 79 def audit_depends_on(dependency_name, *types, &_block) return if @body.nil? types = [:any] if types.empty? dependency_nodes = find_every_method_call_by_name(@body, :depends_on) dependency_nodes.each do |node| next if types.none? { |type| depends_on_name_type?(node, dependency_name, type) } offending_node(node) yield node end end |
#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.
45 |
# File 'rubocops/extend/formula_cop.rb', line 45 def audit_formula(formula_nodes); end |
#audit_urls(urls, regex, &_block) ⇒ 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.
Yields to block when there is a match.
57 58 59 60 61 62 63 64 65 66 67 |
# File 'rubocops/extend/formula_cop.rb', line 57 def audit_urls(urls, regex, &_block) urls.each_with_index do |url_node, index| url_string_node = parameters(url_node).fetch(0) 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, index end end |
#caveats_strings ⇒ Array<RuboCop::AST::Node>
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.
151 152 153 154 155 |
# File 'rubocops/extend/formula_cop.rb', line 151 def caveats_strings return [] if @body.nil? find_strings(find_method_def(@body, :caveats)).to_a end |
#dependency_name_hash_match?(node, *pattern, **kwargs, &block) ⇒ 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.
17 |
# File 'sorbet/rbi/dsl/rubo_cop/cop/formula_cop.rbi', line 17 def dependency_name_hash_match?(node, *pattern, **kwargs, &block); end |
#dependency_type_hash_match?(node, *pattern, **kwargs, &block) ⇒ 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.
27 |
# File 'sorbet/rbi/dsl/rubo_cop/cop/formula_cop.rbi', line 27 def dependency_type_hash_match?(node, *pattern, **kwargs, &block); 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 if the formula depends on dependency_name.
96 97 98 99 100 101 |
# File 'rubocops/extend/formula_cop.rb', line 96 def depends_on?(dependency_name, *types) audit_depends_on(dependency_name, *types) do return true end false 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.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'rubocops/extend/formula_cop.rb', line 111 def depends_on_name_type?(node, name = nil, type = :required) name_match = !name # Match only by type when name is nil 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) || false name_match ||= dependency_name_hash_match?(node, name) || false else type_match = false end @offensive_node = node if type_match || name_match type_match && name_match end |
#formula_name ⇒ String
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.
The name of the formula being audited.
193 194 195 |
# File 'rubocops/extend/formula_cop.rb', line 193 def formula_name @formula_name || raise("`formula_name` is only available inside `audit_formula`") end |
#formula_tap ⇒ String?
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.
207 208 209 210 211 |
# File 'rubocops/extend/formula_cop.rb', line 207 def formula_tap return unless (match_obj = @file_path&.match(%r{(?:/Taps/[\w-]+|^)/(homebrew-[\w-]+)/})) match_obj[1] end |
#get_checksum_node(call) ⇒ RuboCop::AST::Node?
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.
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'rubocops/extend/formula_cop.rb', line 159 def get_checksum_node(call) return if parameters(call).empty? || parameters(call).nil? if parameters(call).fetch(0).str_type? parameters(call).first # sha256 is passed as a key-value pair in bottle blocks elsif parameters(call).fetch(0).hash_type? hash_node = parameters(call).fetch(0) hash_node = T.cast(hash_node, RuboCop::AST::HashNode) if hash_node.keys.first.value == :cellar # sha256 :cellar :any, :tag "hexdigest" hash_node.values.last elsif hash_node.keys.first.is_a?(RuboCop::AST::SymbolNode) # sha256 :tag "hexdigest" hash_node.values.first else # Legacy bottle block syntax # sha256 "hexdigest" => :tag hash_node.keys.first end end end |
#on_class(node) ⇒ 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.
This method is called by RuboCop and is the main entry point.
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'rubocops/extend/formula_cop.rb', line 31 def on_class(node) @file_path = T.let(processed_source.file_path, T.nilable(String)) return unless file_path_allowed? return unless formula_class?(node) class_node, parent_class_node, body = *node @body = T.let(body, T.nilable(RuboCop::AST::Node)) formula_name = Pathname.new(@file_path).basename(".rb").to_s @formula_name = T.let(formula_name, T.nilable(String)) audit_formula(FormulaNodes.new(node:, class_node:, parent_class_node:, body_node: body)) end |
#required_dependency?(node, *pattern, **kwargs, &block) ⇒ 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.
37 |
# File 'sorbet/rbi/dsl/rubo_cop/cop/formula_cop.rbi', line 37 def required_dependency?(node, *pattern, **kwargs, &block); end |
#required_dependency_name?(node, *pattern, **kwargs, &block) ⇒ 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.
47 |
# File 'sorbet/rbi/dsl/rubo_cop/cop/formula_cop.rbi', line 47 def required_dependency_name?(node, *pattern, **kwargs, &block); end |
#style_exceptions_dir ⇒ String?
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 style exceptions directory from the file path.
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'rubocops/extend/formula_cop.rb', line 215 def style_exceptions_dir file_directory = File.dirname(@file_path) if @file_path return unless file_directory # if we're in a sharded subdirectory, look below that. directory_name = File.basename(file_directory) formula_directory = if directory_name.length == 1 || directory_name == "lib" File.dirname(file_directory) else file_directory end # if we're in a Formula or HomebrewFormula subdirectory, look below that. formula_directory_names = ["Formula", "HomebrewFormula"].freeze directory_name = File.basename(formula_directory) tap_root_directory = if formula_directory_names.include?(directory_name) File.dirname(formula_directory) else formula_directory end "#{tap_root_directory}/style_exceptions" 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.
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'rubocops/extend/formula_cop.rb', line 242 def tap_style_exception?(list, formula = nil) return false if formula_tap.nil? || (exceptions_dir = style_exceptions_dir).nil? @tap_style_exceptions = T.let( @tap_style_exceptions, T.nilable(T::Hash[String, T::Hash[Symbol, T::Array[String]]]), ) @tap_style_exceptions ||= {} unless @tap_style_exceptions.key?(exceptions_dir) tap_style_exceptions = T.let({}, T::Hash[Symbol, T::Array[String]]) Pathname.glob("#{exceptions_dir}/*.json").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.none? tap_style_exceptions[list_name] = list_contents end @tap_style_exceptions[exceptions_dir] = tap_style_exceptions end tap_style_exceptions = @tap_style_exceptions.fetch(exceptions_dir) return false unless tap_style_exceptions.key? list tap_style_exceptions.fetch(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.
199 200 201 202 203 |
# File 'rubocops/extend/formula_cop.rb', line 199 def versioned_formula? return false if @formula_name.nil? @formula_name.include?("@") end |