tap: add style_exceptions configuration

This commit is contained in:
Rylan Polster 2020-11-27 01:23:07 -05:00
parent 8fe91aacc3
commit 80a46edee4
3 changed files with 47 additions and 1 deletions

View File

@ -35,6 +35,7 @@ module RuboCop
class_node, parent_class_node, @body = *node class_node, parent_class_node, @body = *node
@formula_name = Pathname.new(@file_path).basename(".rb").to_s @formula_name = Pathname.new(@file_path).basename(".rb").to_s
@tap_style_exceptions = nil
audit_formula(node, class_node, parent_class_node, @body) audit_formula(node, class_node, parent_class_node, @body)
end end
@ -472,6 +473,31 @@ module RuboCop
match_obj[1] match_obj[1]
end 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 private
def formula_class?(node) def formula_class?(node)

View File

@ -21,12 +21,14 @@ class Tap
HOMEBREW_TAP_FORMULA_RENAMES_FILE = "formula_renames.json" HOMEBREW_TAP_FORMULA_RENAMES_FILE = "formula_renames.json"
HOMEBREW_TAP_MIGRATIONS_FILE = "tap_migrations.json" HOMEBREW_TAP_MIGRATIONS_FILE = "tap_migrations.json"
HOMEBREW_TAP_AUDIT_EXCEPTIONS_DIR = "audit_exceptions" 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_PYPI_FORMULA_MAPPINGS = "pypi_formula_mappings.json"
HOMEBREW_TAP_JSON_FILES = %W[ HOMEBREW_TAP_JSON_FILES = %W[
#{HOMEBREW_TAP_FORMULA_RENAMES_FILE} #{HOMEBREW_TAP_FORMULA_RENAMES_FILE}
#{HOMEBREW_TAP_MIGRATIONS_FILE} #{HOMEBREW_TAP_MIGRATIONS_FILE}
#{HOMEBREW_TAP_AUDIT_EXCEPTIONS_DIR}/*.json #{HOMEBREW_TAP_AUDIT_EXCEPTIONS_DIR}/*.json
#{HOMEBREW_TAP_STYLE_EXCEPTIONS_DIR}/*.json
#{HOMEBREW_TAP_PYPI_FORMULA_MAPPINGS} #{HOMEBREW_TAP_PYPI_FORMULA_MAPPINGS}
].freeze ].freeze
@ -114,6 +116,7 @@ class Tap
@formula_renames = nil @formula_renames = nil
@tap_migrations = nil @tap_migrations = nil
@audit_exceptions = nil @audit_exceptions = nil
@style_exceptions = nil
@pypi_formula_mappings = nil @pypi_formula_mappings = nil
@config = nil @config = nil
remove_instance_variable(:@private) if instance_variable_defined?(:@private) 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}/*" @audit_exceptions = read_formula_list_directory "#{HOMEBREW_TAP_AUDIT_EXCEPTIONS_DIR}/*"
end 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 # Hash with pypi formula mappings
sig { returns(Hash) } sig { returns(Hash) }
def pypi_formula_mappings def pypi_formula_mappings
@ -760,6 +769,15 @@ class CoreTap < Tap
end end
end end
# @private
def style_exceptions
@style_exceptions ||= begin
self.class.ensure_installed!
super
end
end
# @private
def pypi_formula_mappings def pypi_formula_mappings
@pypi_formula_mappings ||= begin @pypi_formula_mappings ||= begin
self.class.ensure_installed! self.class.ensure_installed!

View File

@ -8,13 +8,14 @@ module Homebrew
class TapAuditor class TapAuditor
extend T::Sig 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 } sig { params(tap: Tap, strict: T.nilable(T::Boolean)).void }
def initialize(tap, strict:) def initialize(tap, strict:)
@name = tap.name @name = tap.name
@path = tap.path @path = tap.path
@tap_audit_exceptions = tap.audit_exceptions @tap_audit_exceptions = tap.audit_exceptions
@tap_style_exceptions = tap.style_exceptions
@tap_pypi_formula_mappings = tap.pypi_formula_mappings @tap_pypi_formula_mappings = tap.pypi_formula_mappings
@problems = [] @problems = []
end end
@ -38,6 +39,7 @@ module Homebrew
sig { void } sig { void }
def audit_tap_formula_lists def audit_tap_formula_lists
check_formula_list_directory "audit_exceptions", @tap_audit_exceptions 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 check_formula_list "pypi_formula_mappings", @tap_pypi_formula_mappings
end end