class RuboCop::TargetRuby::GemspecFile

The target ruby version may be found in a .gemspec file. @api private

Public Instance Methods

name() click to toggle source
# File lib/rubocop/target_ruby.rb, line 68
def name
  "`required_ruby_version` parameter (in #{gemspec_filepath})"
end

Private Instance Methods

find_minimal_known_ruby(right_hand_side) click to toggle source
# File lib/rubocop/target_ruby.rb, line 123
def find_minimal_known_ruby(right_hand_side)
  version = version_from_right_hand_side(right_hand_side)
  return unless version

  requirement = Gem::Requirement.new(version)

  KNOWN_RUBIES.detect do |v|
    requirement.satisfied_by?(Gem::Version.new("#{v}.99"))
  end
end
find_version() click to toggle source
# File lib/rubocop/target_ruby.rb, line 74
def find_version
  file = gemspec_filepath
  return unless file && File.file?(file)

  right_hand_side = version_from_gemspec_file(file)
  return if right_hand_side.nil?

  find_minimal_known_ruby(right_hand_side)
end
gemspec_filepath() click to toggle source
# File lib/rubocop/target_ruby.rb, line 84
def gemspec_filepath
  return @gemspec_filepath if defined?(@gemspec_filepath)

  @gemspec_filepath =
    @config.traverse_directories_upwards(@config.base_dir_for_path_parameters) do |dir|
      # NOTE: Can't use `dir.glob` because of JRuby 9.4.8.0 incompatibility:
      # https://github.com/jruby/jruby/issues/8358
      candidates = Pathname.glob("#{dir}/*.gemspec")
      # Bundler will use a gemspec whatever the filename is, as long as its the only one in
      # the folder.
      break candidates.first if candidates.one?
    end
end
version_from_array(array) click to toggle source
# File lib/rubocop/target_ruby.rb, line 119
def version_from_array(array)
  array.children.map(&:value)
end
version_from_gemspec_file(file) click to toggle source
# File lib/rubocop/target_ruby.rb, line 98
def version_from_gemspec_file(file)
  processed_source = ProcessedSource.from_file(
    file, DEFAULT_VERSION, parser_engine: @config.parser_engine
  )
  return unless processed_source.valid_syntax?

  required_ruby_version(processed_source.ast).first
end
version_from_right_hand_side(right_hand_side) click to toggle source
# File lib/rubocop/target_ruby.rb, line 107
def version_from_right_hand_side(right_hand_side)
  gem_requirement_versions = gem_requirement_versions(right_hand_side)

  if right_hand_side.array_type? && right_hand_side.children.all?(&:str_type?)
    version_from_array(right_hand_side)
  elsif gem_requirement_versions
    gem_requirement_versions.map(&:value)
  elsif right_hand_side.str_type?
    right_hand_side.value
  end
end