From 4addaaf4df9a7b48c3490b806c195fcb7be999b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Mon, 18 Nov 2024 20:51:41 +0100 Subject: [PATCH] [rubygems/rubygems] More aggressive `Performance/FlatMap` cop configuration https://github.com/rubygems/rubygems/commit/d8d68cc00e --- lib/bundler.rb | 4 ++-- lib/bundler/cli/doctor.rb | 4 ++-- lib/bundler/definition.rb | 2 +- lib/bundler/installer/standalone.rb | 4 ++-- lib/bundler/resolver.rb | 2 +- lib/bundler/resolver/spec_group.rb | 4 ++-- lib/bundler/source_list.rb | 2 +- lib/rubygems.rb | 12 ++++++------ lib/rubygems/commands/cleanup_command.rb | 4 ++-- lib/rubygems/commands/contents_command.rb | 4 ++-- lib/rubygems/commands/pristine_command.rb | 4 ++-- lib/rubygems/commands/rdoc_command.rb | 4 ++-- lib/rubygems/resolver.rb | 4 ++-- lib/rubygems/resolver/composed_set.rb | 6 +++--- lib/rubygems/resolver/index_set.rb | 4 ++-- lib/rubygems/specification.rb | 6 +++--- .../support/artifice/helpers/compact_index.rb | 4 ++-- 17 files changed, 37 insertions(+), 37 deletions(-) diff --git a/lib/bundler.rb b/lib/bundler.rb index 1140de5467..4ede881446 100644 --- a/lib/bundler.rb +++ b/lib/bundler.rb @@ -581,12 +581,12 @@ module Bundler Bundler.rubygems.load_plugins requested_path_gems = definition.requested_specs.select {|s| s.source.is_a?(Source::Path) } - path_plugin_files = requested_path_gems.map do |spec| + path_plugin_files = requested_path_gems.flat_map do |spec| spec.matches_for_glob("rubygems_plugin#{Bundler.rubygems.suffix_pattern}") rescue TypeError error_message = "#{spec.name} #{spec.version} has an invalid gemspec" raise Gem::InvalidSpecificationException, error_message - end.flatten + end Bundler.rubygems.load_plugin_files(path_plugin_files) Bundler.rubygems.load_env_plugins @load_plugins_ran = true diff --git a/lib/bundler/cli/doctor.rb b/lib/bundler/cli/doctor.rb index 1f6fc93c16..76b37d81cd 100644 --- a/lib/bundler/cli/doctor.rb +++ b/lib/bundler/cli/doctor.rb @@ -89,11 +89,11 @@ module Bundler if broken_links.any? message = "The following gems are missing OS dependencies:" - broken_links.map do |spec, paths| + broken_links.flat_map do |spec, paths| paths.uniq.map do |path| "\n * #{spec.name}: #{path}" end - end.flatten.sort.each {|m| message += m } + end.sort.each {|m| message += m } raise ProductionError, message elsif !permissions_valid Bundler.ui.info "No issues found with the installed bundle" diff --git a/lib/bundler/definition.rb b/lib/bundler/definition.rb index cde87134de..871e860886 100644 --- a/lib/bundler/definition.rb +++ b/lib/bundler/definition.rb @@ -345,7 +345,7 @@ module Bundler end def groups - dependencies.map(&:groups).flatten.uniq + dependencies.flat_map(&:groups).uniq end def lock(file_or_preserve_unknown_sections = false, preserve_unknown_sections_or_unused = false) diff --git a/lib/bundler/installer/standalone.rb b/lib/bundler/installer/standalone.rb index cf5993448c..8b4de64df5 100644 --- a/lib/bundler/installer/standalone.rb +++ b/lib/bundler/installer/standalone.rb @@ -28,7 +28,7 @@ module Bundler private def paths - @specs.map do |spec| + @specs.flat_map do |spec| next if spec.name == "bundler" Array(spec.require_paths).map do |path| gem_path(path, spec). @@ -36,7 +36,7 @@ module Bundler sub(extensions_dir, 'extensions/\k/#{Gem.extension_api_version}') # This is a static string intentionally. It's interpolated at a later time. end - end.flatten.compact + end.compact end def version_dir diff --git a/lib/bundler/resolver.rb b/lib/bundler/resolver.rb index a38b6974f8..c37eabdccc 100644 --- a/lib/bundler/resolver.rb +++ b/lib/bundler/resolver.rb @@ -79,7 +79,7 @@ module Bundler def solve_versions(root:, logger:) solver = PubGrub::VersionSolver.new(source: self, root: root, logger: logger) result = solver.solve - resolved_specs = result.map {|package, version| version.to_specs(package) }.flatten + resolved_specs = result.flat_map {|package, version| version.to_specs(package) } resolved_specs |= @base.specs_compatible_with(SpecSet.new(resolved_specs)) rescue PubGrub::SolveFailure => e incompatibility = e.incompatibility diff --git a/lib/bundler/resolver/spec_group.rb b/lib/bundler/resolver/spec_group.rb index f950df6eda..d26f9b7ff2 100644 --- a/lib/bundler/resolver/spec_group.rb +++ b/lib/bundler/resolver/spec_group.rb @@ -38,9 +38,9 @@ module Bundler end def dependencies - @dependencies ||= @specs.map do |spec| + @dependencies ||= @specs.flat_map do |spec| __dependencies(spec) + metadata_dependencies(spec) - end.flatten.uniq.sort + end.uniq.sort end def ==(other) diff --git a/lib/bundler/source_list.rb b/lib/bundler/source_list.rb index 5f9dd68f17..343bb73975 100644 --- a/lib/bundler/source_list.rb +++ b/lib/bundler/source_list.rb @@ -91,7 +91,7 @@ module Bundler end def rubygems_remotes - rubygems_sources.map(&:remotes).flatten.uniq + rubygems_sources.flat_map(&:remotes).uniq end def all_sources diff --git a/lib/rubygems.rb b/lib/rubygems.rb index 22dd895e3f..8a5728d831 100644 --- a/lib/rubygems.rb +++ b/lib/rubygems.rb @@ -496,9 +496,9 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} gem_specifications = @gemdeps ? Gem.loaded_specs.values : Gem::Specification.stubs - files.concat gem_specifications.map {|spec| + files.concat gem_specifications.flat_map {|spec| spec.matches_for_glob("#{glob}#{Gem.suffix_pattern}") - }.flatten + } # $LOAD_PATH might contain duplicate entries or reference # the spec dirs directly, so we prune. @@ -509,9 +509,9 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} def self.find_files_from_load_path(glob) # :nodoc: glob_with_suffixes = "#{glob}#{Gem.suffix_pattern}" - $LOAD_PATH.map do |load_path| + $LOAD_PATH.flat_map do |load_path| Gem::Util.glob_files_in_dir(glob_with_suffixes, load_path) - end.flatten.select {|file| File.file? file } + end.select {|file| File.file? file } end ## @@ -531,9 +531,9 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} files = find_files_from_load_path glob if check_load_path - files.concat Gem::Specification.latest_specs(true).map {|spec| + files.concat Gem::Specification.latest_specs(true).flat_map {|spec| spec.matches_for_glob("#{glob}#{Gem.suffix_pattern}") - }.flatten + } # $LOAD_PATH might contain duplicate entries or reference # the spec dirs directly, so we prune. diff --git a/lib/rubygems/commands/cleanup_command.rb b/lib/rubygems/commands/cleanup_command.rb index bc24aaf753..c89a24eee9 100644 --- a/lib/rubygems/commands/cleanup_command.rb +++ b/lib/rubygems/commands/cleanup_command.rb @@ -113,9 +113,9 @@ If no gems are named all gems in GEM_HOME are cleaned. @candidate_gems = if options[:args].empty? Gem::Specification.to_a else - options[:args].map do |gem_name| + options[:args].flat_map do |gem_name| Gem::Specification.find_all_by_name gem_name - end.flatten + end end end diff --git a/lib/rubygems/commands/contents_command.rb b/lib/rubygems/commands/contents_command.rb index bd0cbce8ba..390ae2a923 100644 --- a/lib/rubygems/commands/contents_command.rb +++ b/lib/rubygems/commands/contents_command.rb @@ -189,8 +189,8 @@ prefix or only the files that are requireable. end def specification_directories # :nodoc: - options[:specdirs].map do |i| + options[:specdirs].flat_map do |i| [i, File.join(i, "specifications")] - end.flatten + end end end diff --git a/lib/rubygems/commands/pristine_command.rb b/lib/rubygems/commands/pristine_command.rb index ec791d310a..97f1646ba0 100644 --- a/lib/rubygems/commands/pristine_command.rb +++ b/lib/rubygems/commands/pristine_command.rb @@ -120,9 +120,9 @@ extensions will be restored. elsif options[:only_missing_extensions] specification_record.select(&:missing_extensions?) else - get_all_gem_names.sort.map do |gem_name| + get_all_gem_names.sort.flat_map do |gem_name| specification_record.find_all_by_name(gem_name, options[:version]).reverse - end.flatten + end end specs = specs.select {|spec| spec.platform == RUBY_ENGINE || Gem::Platform.local === spec.platform || spec.platform == Gem::Platform::RUBY } diff --git a/lib/rubygems/commands/rdoc_command.rb b/lib/rubygems/commands/rdoc_command.rb index 977c90b8c4..62c4bf8ce9 100644 --- a/lib/rubygems/commands/rdoc_command.rb +++ b/lib/rubygems/commands/rdoc_command.rb @@ -64,9 +64,9 @@ Use --overwrite to force rebuilding of documentation. specs = if options[:all] Gem::Specification.to_a else - get_all_gem_names.map do |name| + get_all_gem_names.flat_map do |name| Gem::Specification.find_by_name name, options[:version] - end.flatten.uniq + end.uniq end if specs.empty? diff --git a/lib/rubygems/resolver.rb b/lib/rubygems/resolver.rb index 115c716b6b..6b74c2527a 100644 --- a/lib/rubygems/resolver.rb +++ b/lib/rubygems/resolver.rb @@ -59,7 +59,7 @@ class Gem::Resolver def self.compose_sets(*sets) sets.compact! - sets = sets.map do |set| + sets = sets.flat_map do |set| case set when Gem::Resolver::BestSet then set @@ -68,7 +68,7 @@ class Gem::Resolver else set end - end.flatten + end case sets.length when 0 then diff --git a/lib/rubygems/resolver/composed_set.rb b/lib/rubygems/resolver/composed_set.rb index 8a714ad447..e67dd41754 100644 --- a/lib/rubygems/resolver/composed_set.rb +++ b/lib/rubygems/resolver/composed_set.rb @@ -44,16 +44,16 @@ class Gem::Resolver::ComposedSet < Gem::Resolver::Set end def errors - @errors + @sets.map(&:errors).flatten + @errors + @sets.flat_map(&:errors) end ## # Finds all specs matching +req+ in all sets. def find_all(req) - @sets.map do |s| + @sets.flat_map do |s| s.find_all req - end.flatten + end end ## diff --git a/lib/rubygems/resolver/index_set.rb b/lib/rubygems/resolver/index_set.rb index 0b4f376452..cddaf8773f 100644 --- a/lib/rubygems/resolver/index_set.rb +++ b/lib/rubygems/resolver/index_set.rb @@ -65,11 +65,11 @@ class Gem::Resolver::IndexSet < Gem::Resolver::Set q.breakable - names = @all.values.map do |tuples| + names = @all.values.flat_map do |tuples| tuples.map do |_, tuple| tuple.full_name end - end.flatten + end q.seplist names do |name| q.text name diff --git a/lib/rubygems/specification.rb b/lib/rubygems/specification.rb index 275483ccc6..aea763abe8 100644 --- a/lib/rubygems/specification.rb +++ b/lib/rubygems/specification.rb @@ -1014,7 +1014,7 @@ class Gem::Specification < Gem::BasicSpecification end def self.unresolved_specs - unresolved_deps.values.map(&:to_specs).flatten + unresolved_deps.values.flat_map(&:to_specs) end private_class_method :unresolved_specs @@ -1073,7 +1073,7 @@ class Gem::Specification < Gem::BasicSpecification result[spec.name] = spec end - result.map(&:last).flatten.sort_by(&:name) + result.flat_map(&:last).sort_by(&:name) end ## @@ -1770,7 +1770,7 @@ class Gem::Specification < Gem::BasicSpecification # Returns all specs that matches this spec's runtime dependencies. def dependent_specs - runtime_dependencies.map(&:to_specs).flatten + runtime_dependencies.flat_map(&:to_specs) end ## diff --git a/spec/bundler/support/artifice/helpers/compact_index.rb b/spec/bundler/support/artifice/helpers/compact_index.rb index f045b71bcf..ba331e483f 100644 --- a/spec/bundler/support/artifice/helpers/compact_index.rb +++ b/spec/bundler/support/artifice/helpers/compact_index.rb @@ -67,14 +67,14 @@ class CompactIndexAPI < Endpoint @gems ||= {} @gems[gem_repo] ||= begin specs = Bundler::Deprecate.skip_during do - %w[specs.4.8 prerelease_specs.4.8].map do |filename| + %w[specs.4.8 prerelease_specs.4.8].flat_map do |filename| spec_index = gem_repo.join(filename) next [] unless File.exist?(spec_index) Marshal.load(File.binread(spec_index)).map do |name, version, platform| load_spec(name, version, platform, gem_repo) end - end.flatten + end end specs.group_by(&:name).map do |name, versions|