Skip to content

Commit

Permalink
Merge pull request #6127 from nobu/safe-nav
Browse files Browse the repository at this point in the history
Use safe navigation operator
  • Loading branch information
deivid-rodriguez authored Dec 25, 2022
2 parents 6554406 + 05b4cd6 commit 72fd3dd
Show file tree
Hide file tree
Showing 29 changed files with 49 additions and 43 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ Style/BlockDelimiters:
Style/PercentLiteralDelimiters:
Enabled: true

Style/SafeNavigation:
Enabled: true
MaxChainLength: 1

Style/StringLiterals:
EnforcedStyle: double_quotes

Expand Down
4 changes: 4 additions & 0 deletions .rubocop_bundler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,10 @@ Style/RescueModifier:
Style/RescueStandardError:
Enabled: true

Style/SafeNavigation:
Enabled: true
MaxChainLength: 1

Style/Sample:
Enabled: true

Expand Down
2 changes: 1 addition & 1 deletion bundler/lib/bundler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ def load_gemspec(file, validate = false)
@gemspec_cache[key] ||= load_gemspec_uncached(file, validate)
# Protect against caching side-effected gemspecs by returning a
# new instance each time.
@gemspec_cache[key].dup if @gemspec_cache[key]
@gemspec_cache[key]&.dup
end

def load_gemspec_uncached(file, validate = false)
Expand Down
2 changes: 1 addition & 1 deletion bundler/lib/bundler/cli/binstubs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize(options, gems)
def run
Bundler.definition.validate_runtime!
path_option = options["path"]
path_option = nil if path_option && path_option.empty?
path_option = nil if path_option&.empty?
Bundler.settings.set_command_option :bin, path_option if options["path"]
Bundler.settings.set_command_option_if_given :shebang, options["shebang"]
installer = Installer.new(Bundler.root, Bundler.definition)
Expand Down
2 changes: 1 addition & 1 deletion bundler/lib/bundler/cli/info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def spec_for_gem(gem_name)
def default_gem_spec(gem_name)
return unless Gem::Specification.respond_to?(:find_all_by_name)
gem_spec = Gem::Specification.find_all_by_name(gem_name).last
return gem_spec if gem_spec && gem_spec.respond_to?(:default_gem?) && gem_spec.default_gem?
return gem_spec if gem_spec&.default_gem?
end

def spec_not_found(gem_name)
Expand Down
2 changes: 1 addition & 1 deletion bundler/lib/bundler/cli/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def normalize_settings
end

bin_option = options["binstubs"]
bin_option = nil if bin_option && bin_option.empty?
bin_option = nil if bin_option&.empty?
Bundler.settings.set_command_option :bin, bin_option if options["binstubs"]

Bundler.settings.set_command_option_if_given :shebang, options["shebang"]
Expand Down
2 changes: 1 addition & 1 deletion bundler/lib/bundler/cli/outdated.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def print_gem(current_spec, active_spec, dependency, groups)
end
current_version = "#{current_spec.version}#{current_spec.git_version}"

if dependency && dependency.specific?
if dependency&.specific?
dependency_version = %(, requested #{dependency.requirement})
end

Expand Down
12 changes: 7 additions & 5 deletions bundler/lib/bundler/cli/platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ def initialize(options)
end

def run
platforms, ruby_version = Bundler.ui.silence do
locked_ruby_version = Bundler.locked_gems && Bundler.locked_gems.ruby_version&.gsub(/p\d+\Z/, "")
gemfile_ruby_version = Bundler.definition.ruby_version && Bundler.definition.ruby_version.single_version_string
[Bundler.definition.platforms.map {|p| "* #{p}" },
locked_ruby_version || gemfile_ruby_version]
ruby_version = if Bundler.locked_gems
Bundler.locked_gems.ruby_version&.gsub(/p\d+\Z/, "")
else
Bundler.definition.ruby_version&.single_version_string
end

output = []

if options[:ruby]
Expand All @@ -23,6 +23,8 @@ def run
output << "No ruby version specified"
end
else
platforms = Bundler.definition.platforms.map {|p| "* #{p}" }

output << "Your platform is: #{Gem::Platform.local}"
output << "Your app has gems that work on these platforms:\n#{platforms.join("\n")}"

Expand Down
6 changes: 3 additions & 3 deletions bundler/lib/bundler/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -647,8 +647,8 @@ def converge_locals

Bundler.settings.local_overrides.map do |k, v|
spec = @dependencies.find {|s| s.name == k }
source = spec && spec.source
if source && source.respond_to?(:local_override!)
source = spec&.source
if source&.respond_to?(:local_override!)
source.unlock! if @unlock[:gems].include?(spec.name)
locals << [source, source.local_override!(v)]
end
Expand Down Expand Up @@ -778,7 +778,7 @@ def converge_specs(specs)
dep = @dependencies.find {|d| s.satisfies?(d) }

# Replace the locked dependency's source with the equivalent source from the Gemfile
s.source = if dep && dep.source
s.source = if dep&.source
gemfile_source = dep.source
lockfile_source = s.source

Expand Down
2 changes: 1 addition & 1 deletion bundler/lib/bundler/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def initialize
end

def eval_gemfile(gemfile, contents = nil)
expanded_gemfile_path = Pathname.new(gemfile).expand_path(@gemfile && @gemfile.parent)
expanded_gemfile_path = Pathname.new(gemfile).expand_path(@gemfile&.parent)
original_gemfile = @gemfile
@gemfile = expanded_gemfile_path
@gemfiles << expanded_gemfile_path
Expand Down
2 changes: 1 addition & 1 deletion bundler/lib/bundler/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def self.environment
specs = Bundler.rubygems.find_name(name)
out << [" #{name}", "(#{specs.map(&:version).join(",")})"] unless specs.empty?
end
if (exe = caller.last.split(":").first) && exe =~ %r{(exe|bin)/bundler?\z}
if (exe = caller.last.split(":").first)&.match? %r{(exe|bin)/bundler?\z}
shebang = File.read(exe).lines.first
shebang.sub!(/^#!\s*/, "")
unless shebang.start_with?(Gem.ruby, "/usr/bin/env ruby")
Expand Down
4 changes: 2 additions & 2 deletions bundler/lib/bundler/fetcher/compact_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def specs_for_names(gem_names)
deps = begin
parallel_compact_index_client.dependencies(remaining_gems)
rescue TooManyRequestsError
@bundle_worker.stop if @bundle_worker
@bundle_worker&.stop
@bundle_worker = nil # reset it. Not sure if necessary
serial_compact_index_client.dependencies(remaining_gems)
end
Expand All @@ -49,7 +49,7 @@ def specs_for_names(gem_names)
complete_gems.concat(deps.map(&:first)).uniq!
remaining_gems = next_gems - complete_gems
end
@bundle_worker.stop if @bundle_worker
@bundle_worker&.stop
@bundle_worker = nil # reset it. Not sure if necessary

gem_info
Expand Down
2 changes: 1 addition & 1 deletion bundler/lib/bundler/friendly_errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def exception_message(error)
def serialized_exception_for(e)
<<-EOS.gsub(/^ {8}/, "")
#{e.class}: #{e.message}
#{e.backtrace && e.backtrace.join("\n ").chomp}
#{e.backtrace&.join("\n ")&.chomp}
EOS
end

Expand Down
7 changes: 3 additions & 4 deletions bundler/lib/bundler/gem_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def tag_prefix=(prefix)

def gemspec(&block)
gemspec = instance.gemspec
block.call(gemspec) if block
block&.call(gemspec)
gemspec
end
end
Expand Down Expand Up @@ -152,8 +152,7 @@ def allowed_push_host

def gem_push_host
env_rubygems_host = ENV["RUBYGEMS_HOST"]
env_rubygems_host = nil if
env_rubygems_host && env_rubygems_host.empty?
env_rubygems_host = nil if env_rubygems_host&.empty?

allowed_push_host || env_rubygems_host || "rubygems.org"
end
Expand Down Expand Up @@ -218,7 +217,7 @@ def sh_with_status(cmd, &block)
SharedHelpers.chdir(base) do
outbuf = IO.popen(cmd, :err => [:child, :out], &:read)
status = $?
block.call(outbuf) if status.success? && block
block&.call(outbuf) if status.success?
[outbuf, status]
end
end
Expand Down
2 changes: 1 addition & 1 deletion bundler/lib/bundler/installer/parallel_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def call
handle_error if failed_specs.any?
@specs
ensure
worker_pool && worker_pool.stop
worker_pool&.stop
end

def check_for_unmet_dependencies
Expand Down
2 changes: 1 addition & 1 deletion bundler/lib/bundler/plugin/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def hook_plugins(event)
# @param [Boolean] is the index file global index
def load_index(index_file, global = false)
SharedHelpers.filesystem_access(index_file, :read) do |index_f|
valid_file = index_f && index_f.exist? && !index_f.size.zero?
valid_file = index_f&.exist? && !index_f.size.zero?
break unless valid_file

data = index_f.read
Expand Down
4 changes: 2 additions & 2 deletions bundler/lib/bundler/ruby_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def initialize(versions, patchlevel, engine, engine_version)
end

@gem_version = Gem::Requirement.create(@versions.first).requirements.first.last
@input_engine = engine && engine.to_s
@engine = engine && engine.to_s || "ruby"
@input_engine = engine&.to_s
@engine = engine&.to_s || "ruby"
@engine_versions = (engine_version && Array(engine_version)) || @versions
@engine_gem_version = Gem::Requirement.create(@engine_versions.first).requirements.first.last
@patchlevel = patchlevel || (@gem_version.prerelease? ? "-1" : nil)
Expand Down
2 changes: 1 addition & 1 deletion bundler/lib/bundler/rubygems_integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def replace_gem(specs, specs_by_name)
kernel = (class << ::Kernel; self; end)
[kernel, ::Kernel].each do |kernel_class|
redefine_method(kernel_class, :gem) do |dep, *reqs|
if executables && executables.include?(File.basename(caller.first.split(":").first))
if executables&.include?(File.basename(caller.first.split(":").first))
break
end

Expand Down
2 changes: 1 addition & 1 deletion bundler/lib/bundler/ui/rg_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize(ui)
end

def say(message)
@ui && @ui.debug(message)
@ui&.debug(message)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion bundler/spec/support/artifice/vcr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def recorded_response
response.uri = request.uri

response.reading_body(response_io, request.response_body_permitted?) do
response_block.call(response) if response_block
response_block&.call(response)
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions lib/rubygems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -857,8 +857,7 @@ def self.latest_rubygems_version
# Returns the version of the latest release-version of gem +name+

def self.latest_version_for(name)
spec = latest_spec_for name
spec && spec.version
latest_spec_for(name)&.version
end

##
Expand Down
2 changes: 1 addition & 1 deletion lib/rubygems/bundler_version_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def self.lockfile_version

def self.lockfile_contents
gemfile = ENV["BUNDLE_GEMFILE"]
gemfile = nil if gemfile && gemfile.empty?
gemfile = nil if gemfile&.empty?

unless gemfile
begin
Expand Down
3 changes: 1 addition & 2 deletions lib/rubygems/gemcutter_utilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ def host
@host ||=
begin
env_rubygems_host = ENV["RUBYGEMS_HOST"]
env_rubygems_host = nil if
env_rubygems_host && env_rubygems_host.empty?
env_rubygems_host = nil if env_rubygems_host&.empty?

env_rubygems_host || configured_host
end
Expand Down
3 changes: 1 addition & 2 deletions lib/rubygems/package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,7 @@ def verify

verify_checksums @digests, @checksums

@security_policy.verify_signatures @spec, @digests, @signatures if
@security_policy
@security_policy&.verify_signatures @spec, @digests, @signatures

true
rescue Gem::Security::Exception
Expand Down
2 changes: 1 addition & 1 deletion lib/rubygems/request_set/lockfile/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def pinned_requirement(name) # :nodoc:
set.find_all(requirement)
end.compact.first

specification && specification.version
specification&.version
end

##
Expand Down
4 changes: 2 additions & 2 deletions lib/rubygems/specification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ def self.find_inactive_by_path(path)
next if s.activated?
s.contains_requirable_file? path
end
stub && stub.to_spec
stub&.to_spec
end

def self.find_active_stub_by_path(path)
Expand Down Expand Up @@ -1626,7 +1626,7 @@ def build_extensions # :nodoc:
builder.build_extensions
end
ensure
ui.close if ui
ui&.close
Gem::Specification.unresolved_deps.replace unresolved_deps
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rubygems/user_interaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ def ask(question)
@outs.flush

result = @ins.gets
result.chomp! if result
result&.chomp!
result
end

Expand All @@ -305,7 +305,7 @@ def ask_for_password(question)

password = _gets_noecho
@outs.puts
password.chomp! if password
password&.chomp!
password
end

Expand Down
2 changes: 1 addition & 1 deletion test/rubygems/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1612,7 +1612,7 @@ def stub(name, val_or_callable, *block_args)
if val_or_callable.respond_to? :call
val_or_callable.call(*args, &blk)
else
blk.call(*block_args) if blk
blk&.call(*block_args)
val_or_callable
end
end
Expand Down
2 changes: 1 addition & 1 deletion util/automatiek/gem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Gem
def initialize(gem_name, &block)
@gem_name = gem_name
@dependencies = []
block.call(self) if block
block&.call(self)
end

def vendor!(version = nil)
Expand Down

0 comments on commit 72fd3dd

Please sign in to comment.