Skip to content

Commit

Permalink
Merge pull request #16745 from issyl0/rubocop-todos-2
Browse files Browse the repository at this point in the history
rubocop: Fix remaining TODOs for triaging Ruby 3 cops
  • Loading branch information
issyl0 authored Feb 26, 2024
2 parents d8d82d2 + e654af3 commit 7848bd3
Show file tree
Hide file tree
Showing 58 changed files with 159 additions and 172 deletions.
25 changes: 11 additions & 14 deletions Library/.rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ Lint/UnusedMethodArgument:
Metrics:
Enabled: false

# Disabled because it breaks Sorbet: "The declaration for `with` is missing parameter(s): & (RuntimeError)"
Naming/BlockForwarding:
Enabled: false

# Allow dashes in filenames.
Naming/FileName:
Regex: !ruby/regexp /^[\w\@\-\+\.]+(\.rb)?$/
Expand Down Expand Up @@ -289,6 +293,10 @@ Sorbet/TrueSigil:
Style/AndOr:
EnforcedStyle: always

# Disabled because it breaks Sorbet: "The declaration for `with` is missing parameter(s): & (RuntimeError)"
Style/ArgumentsForwarding:
Enabled: false

# Avoid leaking resources.
Style/AutoResourceCleanup:
Enabled: true
Expand Down Expand Up @@ -358,6 +366,9 @@ Style/HashAsLastArrayItem:
- "/**/Formula/**/*.rb"
- "**/Formula/**/*.rb"

Style/HashSyntax:
EnforcedShorthandSyntax: either

Style/InverseMethods:
InverseMethods:
:blank?: :present?
Expand Down Expand Up @@ -460,17 +471,3 @@ Style/UnlessLogicalOperators:
# a bit confusing to non-Rubyists but useful for longer arrays
Style/WordArray:
MinSize: 4

# TODO: Enable these cops once https://github.com/Homebrew/brew/pull/16337#issuecomment-1855668516 is done.
Lint/RedundantDirGlobSort:
Enabled: false
Naming/BlockForwarding:
Enabled: false
Performance/BindCall:
Enabled: false
Performance/MapCompact:
Enabled: false
Style/ArgumentsForwarding:
Enabled: false
Style/HashSyntax:
EnforcedShorthandSyntax: either
4 changes: 2 additions & 2 deletions Library/Homebrew/cask/artifact/abstract_uninstall.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ def find_launchctl_with_wildcard(search)
regex = Regexp.escape(search).gsub("\\*", ".*")
system_command!("/bin/launchctl", args: ["list"])
.stdout.lines.drop(1) # skip stdout column headers
.map do |line|
.filter_map do |line|
pid, _state, id = line.chomp.split(/\s+/)
id if pid.to_i.nonzero? && id.match?(regex)
end.compact
end
end

sig { returns(String) }
Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/cask/cask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ def self.all(eval_all: false)
# Load core casks from tokens so they load from the API when the core cask is not tapped.
tokens_and_files = CoreCaskTap.instance.cask_tokens
tokens_and_files += Tap.reject(&:core_cask_tap?).flat_map(&:cask_files)
tokens_and_files.map do |token_or_file|
tokens_and_files.filter_map do |token_or_file|
CaskLoader.load(token_or_file)
rescue CaskUnreadableError => e
opoo e.message

nil
end.compact
end
end

def tap
Expand Down
3 changes: 1 addition & 2 deletions Library/Homebrew/cask/cask_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,7 @@ def self.try_new(ref, warn: false)

token = ref

loaders = Tap.map { |tap| super("#{tap}/#{token}", warn: warn) }
.compact
loaders = Tap.filter_map { |tap| super("#{tap}/#{token}", warn: warn) }
.select { _1.path.exist? }

case loaders.count
Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/cask/caskroom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ def self.ensure_caskroom_exists

sig { params(config: T.nilable(Config)).returns(T::Array[Cask]) }
def self.casks(config: nil)
tokens.sort.map do |token|
tokens.sort.filter_map do |token|
CaskLoader.load(token, config: config, warn: false)
rescue TapCaskAmbiguityError => e
T.must(e.loaders.first).load(config: config)
rescue
# Don't blow up because of a single unavailable cask.
nil
end.compact
end
end
end
end
3 changes: 1 addition & 2 deletions Library/Homebrew/cask/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,11 @@ def language_eval
raise CaskInvalidError.new(cask, "No default language specified.") if @language_blocks.default.nil?

locales = cask.config.languages
.map do |language|
.filter_map do |language|
Locale.parse(language)
rescue Locale::ParserError
nil
end
.compact

locales.each do |locale|
key = locale.detect(@language_blocks.keys)
Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/caveats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ def keg_only_text(skip_reason: false)
private

def keg
@keg ||= [formula.prefix, formula.opt_prefix, formula.linked_keg].map do |d|
@keg ||= [formula.prefix, formula.opt_prefix, formula.linked_keg].filter_map do |d|
Keg.new(d.resolved_path)
rescue
nil
end.compact.first
end.first
end

def function_completion_caveats(shell)
Expand Down
16 changes: 8 additions & 8 deletions Library/Homebrew/cli/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -447,11 +447,11 @@ def hide_from_man_page!
def generate_usage_banner
command_names = ["`#{@command_name}`"]
aliases_to_skip = %w[instal uninstal]
command_names += Commands::HOMEBREW_INTERNAL_COMMAND_ALIASES.map do |command_alias, command|
command_names += Commands::HOMEBREW_INTERNAL_COMMAND_ALIASES.filter_map do |command_alias, command|
next if aliases_to_skip.include? command_alias

"`#{command_alias}`" if command == @command_name
end.compact.sort
end.sort

options = if @non_global_processed_options.empty?
""
Expand All @@ -469,12 +469,12 @@ def generate_usage_banner
named_args = ""
if @named_args_type.present? && @named_args_type != :none
arg_type = if @named_args_type.is_a? Array
types = @named_args_type.map do |type|
types = @named_args_type.filter_map do |type|
next unless type.is_a? Symbol
next SYMBOL_TO_USAGE_MAPPING[type] if SYMBOL_TO_USAGE_MAPPING.key?(type)

"<#{type}>"
end.compact
end
types << "<subcommand>" if @named_args_type.any?(String)
types.join("|")
elsif SYMBOL_TO_USAGE_MAPPING.key? @named_args_type
Expand Down Expand Up @@ -593,11 +593,11 @@ def check_constraint_violations
end

def check_named_args(args)
types = Array(@named_args_type).map do |type|
types = Array(@named_args_type).filter_map do |type|
next type if type.is_a? Symbol

:subcommand
end.compact.uniq
end.uniq

exception = if @min_named_args && @max_named_args && @min_named_args == @max_named_args &&
args.size != @max_named_args
Expand Down Expand Up @@ -650,15 +650,15 @@ def formulae(argv)
end

# Only lowercase names, not paths, bottle filenames or URLs
named_args.map do |arg|
named_args.filter_map do |arg|
next if arg.match?(HOMEBREW_CASK_TAP_CASK_REGEX)

begin
Formulary.factory(arg, spec, flags: argv.select { |a| a.start_with?("--") })
rescue FormulaUnavailableError, FormulaSpecificationError
nil
end
end.compact.uniq(&:name)
end.uniq(&:name)
end

def only_casks?(argv)
Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/cmd/--prefix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ def self.__prefix
puts HOMEBREW_PREFIX
else
formulae = args.named.to_resolved_formulae
prefixes = formulae.map do |f|
prefixes = formulae.filter_map do |f|
next nil if args.installed? && !f.opt_prefix.exist?

# this case will be short-circuited by brew.sh logic for a single formula
f.opt_prefix
end.compact
end
puts prefixes
if args.installed?
missing_formulae = formulae.reject(&:optlinked?)
Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/cmd/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def link
}

kegs = if args.HEAD?
args.named.to_kegs.group_by(&:name).map do |name, resolved_kegs|
args.named.to_kegs.group_by(&:name).filter_map do |name, resolved_kegs|
head_keg = resolved_kegs.find { |keg| keg.version.head? }
next head_keg if head_keg.present?

Expand All @@ -49,7 +49,7 @@ def link
To install, run:
brew install --HEAD #{name}
EOS
end.compact
end
else
args.named.to_latest_kegs
end
Expand Down
8 changes: 4 additions & 4 deletions Library/Homebrew/cmd/untap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def untap

if Homebrew::EnvConfig.no_install_from_api? || (!tap.core_tap? && !tap.core_cask_tap?)
installed_formula_names = T.let(nil, T.nilable(T::Set[String]))
installed_tap_formulae = tap.formula_names.map do |formula_name|
installed_tap_formulae = tap.formula_names.filter_map do |formula_name|
# initialise lazily in case there's no formulae in this tap
installed_formula_names ||= Set.new(Formula.installed_formula_names)
next unless installed_formula_names.include?(formula_name)
Expand All @@ -43,10 +43,10 @@ def untap
# Can't use Formula#any_version_installed? because it doesn't consider
# taps correctly.
formula if formula.installed_kegs.any? { |keg| keg.tab.tap == tap }
end.compact
end

installed_cask_tokens = T.let(nil, T.nilable(T::Set[String]))
installed_tap_casks = tap.cask_tokens.map do |cask_token|
installed_tap_casks = tap.cask_tokens.filter_map do |cask_token|
# initialise lazily in case there's no casks in this tap
installed_cask_tokens ||= Set.new(Cask::Caskroom.tokens)
next unless installed_cask_tokens.include?(cask_token)
Expand All @@ -59,7 +59,7 @@ def untap
end

cask if cask.installed?
end.compact
end

if installed_tap_formulae.present? || installed_tap_casks.present?
installed_names = (installed_tap_formulae + installed_tap_casks.map(&:token)).join("\n")
Expand Down
16 changes: 8 additions & 8 deletions Library/Homebrew/cmd/update-report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -711,15 +711,15 @@ def diff
header_regex = /^(---|\+\+\+) /
add_delete_characters = ["+", "-"].freeze

diff_output.lines.map do |line|
diff_output.lines.filter_map do |line|
next if line.match?(header_regex)
next unless add_delete_characters.include?(line[0])

line.sub(/^\+/, "A #{api_dir_prefix.basename}/")
.sub(/^-/, "D #{api_dir_prefix.basename}/")
.sub(/$/, ".rb")
.chomp
end.compact.join("\n")
end.join("\n")
else
Utils.popen_read(
"git", "-C", tap.path, "diff-tree", "-r", "--name-status", "--diff-filter=AMDR",
Expand Down Expand Up @@ -845,9 +845,9 @@ def dump_new_formula_report
end

def dump_new_cask_report
casks = select_formula_or_cask(:AC).sort.map do |name|
casks = select_formula_or_cask(:AC).sort.filter_map do |name|
name.split("/").last unless cask_installed?(name)
end.compact
end

output_dump_formula_or_cask_report "New Casks", casks
end
Expand All @@ -873,13 +873,13 @@ def dump_renamed_cask_report
end

def dump_deleted_formula_report(report_all)
formulae = select_formula_or_cask(:D).sort.map do |name|
formulae = select_formula_or_cask(:D).sort.filter_map do |name|
if installed?(name)
pretty_uninstalled(name)
elsif report_all
name
end
end.compact
end

title = if report_all
"Deleted Formulae"
Expand All @@ -890,14 +890,14 @@ def dump_deleted_formula_report(report_all)
end

def dump_deleted_cask_report(report_all)
casks = select_formula_or_cask(:DC).sort.map do |name|
casks = select_formula_or_cask(:DC).sort.filter_map do |name|
name = name.split("/").last
if cask_installed?(name)
pretty_uninstalled(name)
elsif report_all
name
end
end.compact
end

title = if report_all
"Deleted Casks"
Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,11 @@ def self.command_options(command)
return if path.blank?

if (cmd_parser = Homebrew::CLI::Parser.from_cmd_path(path))
cmd_parser.processed_options.map do |short, long, _, desc, hidden|
cmd_parser.processed_options.filter_map do |short, long, _, desc, hidden|
next if hidden

[long || short, desc]
end.compact
end
else
options = []
comment_lines = path.read.lines.grep(/^#:/)
Expand Down
Loading

0 comments on commit 7848bd3

Please sign in to comment.