diff --git a/Library/Homebrew/rubocops/extend/formula.rb b/Library/Homebrew/rubocops/extend/formula.rb index 45e23a1b95..d3e75ea34f 100644 --- a/Library/Homebrew/rubocops/extend/formula.rb +++ b/Library/Homebrew/rubocops/extend/formula.rb @@ -35,6 +35,7 @@ module RuboCop 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 @@ -472,6 +473,31 @@ module RuboCop match_obj[1] end + # Returns whether the current formula exists in the given style exception list + def tap_style_exception?(list) + if @tap_style_exceptions.blank? && formula_tap.present? + @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.blank? + + @tap_style_exceptions[list_name] = list_contents + end + end + + return false if @tap_style_exceptions.blank? + return false unless @tap_style_exceptions.key? list + + @tap_style_exceptions[list].include? @formula_name + end + private def formula_class?(node) diff --git a/Library/Homebrew/tap.rb b/Library/Homebrew/tap.rb index c45f6d09a9..b2c87dccdc 100644 --- a/Library/Homebrew/tap.rb +++ b/Library/Homebrew/tap.rb @@ -21,12 +21,14 @@ class Tap HOMEBREW_TAP_FORMULA_RENAMES_FILE = "formula_renames.json" HOMEBREW_TAP_MIGRATIONS_FILE = "tap_migrations.json" HOMEBREW_TAP_AUDIT_EXCEPTIONS_DIR = "audit_exceptions" + HOMEBREW_TAP_STYLE_EXCEPTIONS_DIR = "style_exceptions" HOMEBREW_TAP_PYPI_FORMULA_MAPPINGS = "pypi_formula_mappings.json" HOMEBREW_TAP_JSON_FILES = %W[ #{HOMEBREW_TAP_FORMULA_RENAMES_FILE} #{HOMEBREW_TAP_MIGRATIONS_FILE} #{HOMEBREW_TAP_AUDIT_EXCEPTIONS_DIR}/*.json + #{HOMEBREW_TAP_STYLE_EXCEPTIONS_DIR}/*.json #{HOMEBREW_TAP_PYPI_FORMULA_MAPPINGS} ].freeze @@ -114,6 +116,7 @@ class Tap @formula_renames = nil @tap_migrations = nil @audit_exceptions = nil + @style_exceptions = nil @pypi_formula_mappings = nil @config = nil remove_instance_variable(:@private) if instance_variable_defined?(:@private) @@ -567,6 +570,12 @@ class Tap @audit_exceptions = read_formula_list_directory "#{HOMEBREW_TAP_AUDIT_EXCEPTIONS_DIR}/*" end + # Hash with style exceptions + sig { returns(Hash) } + def style_exceptions + @style_exceptions = read_formula_list_directory "#{HOMEBREW_TAP_STYLE_EXCEPTIONS_DIR}/*" + end + # Hash with pypi formula mappings sig { returns(Hash) } def pypi_formula_mappings @@ -760,6 +769,15 @@ class CoreTap < Tap end end + # @private + def style_exceptions + @style_exceptions ||= begin + self.class.ensure_installed! + super + end + end + + # @private def pypi_formula_mappings @pypi_formula_mappings ||= begin self.class.ensure_installed! diff --git a/Library/Homebrew/tap_auditor.rb b/Library/Homebrew/tap_auditor.rb index 5936980919..aa08ace9e7 100644 --- a/Library/Homebrew/tap_auditor.rb +++ b/Library/Homebrew/tap_auditor.rb @@ -8,13 +8,14 @@ module Homebrew class TapAuditor extend T::Sig - attr_reader :name, :path, :tap_audit_exceptions, :tap_pypi_formula_mappings, :problems + attr_reader :name, :path, :tap_audit_exceptions, :tap_style_exceptions, :tap_pypi_formula_mappings, :problems sig { params(tap: Tap, strict: T.nilable(T::Boolean)).void } def initialize(tap, strict:) @name = tap.name @path = tap.path @tap_audit_exceptions = tap.audit_exceptions + @tap_style_exceptions = tap.style_exceptions @tap_pypi_formula_mappings = tap.pypi_formula_mappings @problems = [] end @@ -38,6 +39,7 @@ module Homebrew sig { void } def audit_tap_formula_lists check_formula_list_directory "audit_exceptions", @tap_audit_exceptions + check_formula_list_directory "style_exceptions", @tap_style_exceptions check_formula_list "pypi_formula_mappings", @tap_pypi_formula_mappings end