
- Previously I thought that comments were fine to discourage people from wasting their time trying to bump things that used `undef` that Sorbet didn't support. But RuboCop is better at this since it'll complain if the comments are unnecessary. - Suggested in https://github.com/Homebrew/brew/pull/18018#issuecomment-2283369501. - I've gone for a mixture of `rubocop:disable` for the files that can't be `typed: strict` (use of undef, required before everything else, etc) and `rubocop:todo` for everything else that should be tried to make strictly typed. There's no functional difference between the two as `rubocop:todo` is `rubocop:disable` with a different name. - And I entirely disabled the cop for the docs/ directory since `typed: strict` isn't going to gain us anything for some Markdown linting config files. - This means that now it's easier to track what needs to be done rather than relying on checklists of files in our big Sorbet issue: ```shell $ git grep 'typed: true # rubocop:todo Sorbet/StrictSigil' | wc -l 268 ``` - And this is confirmed working for new files: ```shell $ git status On branch use-rubocop-for-sorbet-strict-sigils Untracked files: (use "git add <file>..." to include in what will be committed) Library/Homebrew/bad.rb Library/Homebrew/good.rb nothing added to commit but untracked files present (use "git add" to track) $ brew style Offenses: bad.rb:1:1: C: Sorbet/StrictSigil: Sorbet sigil should be at least strict got true. ^^^^^^^^^^^^^ 1340 files inspected, 1 offense detected ```
106 lines
3.5 KiB
Ruby
106 lines
3.5 KiB
Ruby
# typed: true # rubocop:todo Sorbet/StrictSigil
|
|
# frozen_string_literal: true
|
|
|
|
module Homebrew
|
|
# Auditor for checking common violations in {Tap}s.
|
|
class TapAuditor
|
|
attr_reader :name, :path, :formula_names, :formula_aliases, :formula_renames, :cask_tokens,
|
|
: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:)
|
|
Homebrew.with_no_api_env do
|
|
@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 = []
|
|
|
|
@cask_tokens = tap.cask_tokens.map do |cask_token|
|
|
cask_token.split("/").last
|
|
end
|
|
@formula_aliases = tap.aliases.map do |formula_alias|
|
|
formula_alias.split("/").last
|
|
end
|
|
@formula_renames = tap.formula_renames
|
|
@formula_names = tap.formula_names.map do |formula_name|
|
|
formula_name.split("/").last
|
|
end
|
|
end
|
|
end
|
|
|
|
sig { void }
|
|
def audit
|
|
audit_json_files
|
|
audit_tap_formula_lists
|
|
audit_aliases_renames_duplicates
|
|
end
|
|
|
|
sig { void }
|
|
def audit_json_files
|
|
json_patterns = Tap::HOMEBREW_TAP_JSON_FILES.map { |pattern| @path/pattern }
|
|
Pathname.glob(json_patterns).each do |file|
|
|
JSON.parse file.read
|
|
rescue JSON::ParserError
|
|
problem "#{file.to_s.delete_prefix("#{@path}/")} contains invalid JSON"
|
|
end
|
|
end
|
|
|
|
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
|
|
|
|
sig { void }
|
|
def audit_aliases_renames_duplicates
|
|
duplicates = formula_aliases & formula_renames.keys
|
|
return if duplicates.none?
|
|
|
|
problem "The following should either be an alias or a rename, not both: #{duplicates.to_sentence}"
|
|
end
|
|
|
|
sig { params(message: String).void }
|
|
def problem(message)
|
|
@problems << ({ message:, location: nil, corrected: false })
|
|
end
|
|
|
|
private
|
|
|
|
sig { params(list_file: String, list: T.untyped).void }
|
|
def check_formula_list(list_file, list)
|
|
unless [Hash, Array].include? list.class
|
|
problem <<~EOS
|
|
#{list_file}.json should contain a JSON array
|
|
of formula names or a JSON object mapping formula names to values
|
|
EOS
|
|
return
|
|
end
|
|
|
|
list = list.keys if list.is_a? Hash
|
|
invalid_formulae_casks = list.select do |formula_or_cask_name|
|
|
formula_names.exclude?(formula_or_cask_name) &&
|
|
formula_aliases.exclude?(formula_or_cask_name) &&
|
|
cask_tokens.exclude?(formula_or_cask_name)
|
|
end
|
|
|
|
return if invalid_formulae_casks.empty?
|
|
|
|
problem <<~EOS
|
|
#{list_file}.json references
|
|
formulae or casks that are not found in the #{@name} tap.
|
|
Invalid formulae or casks: #{invalid_formulae_casks.join(", ")}
|
|
EOS
|
|
end
|
|
|
|
sig { params(directory_name: String, lists: Hash).void }
|
|
def check_formula_list_directory(directory_name, lists)
|
|
lists.each do |list_name, list|
|
|
check_formula_list "#{directory_name}/#{list_name}", list
|
|
end
|
|
end
|
|
end
|
|
end
|