123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::TargetRuby::GemspecFile Private

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Macros, Source
Instance Chain:
self, Source
Inherits: RuboCop::TargetRuby::Source
Defined in: lib/rubocop/target_ruby.rb

Overview

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

Class Method Summary

Source - Inherited

Instance Attribute Summary

Source - Inherited

Instance Method Summary

Source - Inherited

Instance Method Details

#find_minimal_known_ruby(right_hand_side) (private)

[ GitHub ]

  
# 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 (private)

[ GitHub ]

  
# 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

#gem_requirement_versions(node)

[ GitHub ]

  
# File 'lib/rubocop/target_ruby.rb', line 62

def_node_matcher :gem_requirement_versions, <<~PATTERN
  (send (const(const _ :Gem):Requirement) :new
    {$str+ | (send $str :freeze)+ | (array $str+) | (array (send $str :freeze)+)}
  )
PATTERN

#gemspec_filepath (private)

[ GitHub ]

  
# 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

#name

[ GitHub ]

  
# File 'lib/rubocop/target_ruby.rb', line 68

def name
  "`required_ruby_version` parameter (in #{gemspec_filepath})"
end

#required_ruby_version(node)

[ GitHub ]

  
# File 'lib/rubocop/target_ruby.rb', line 57

def_node_search :required_ruby_version, <<~PATTERN
  (send _ :required_ruby_version= $_)
PATTERN

#version_from_array(array) (private)

[ GitHub ]

  
# File 'lib/rubocop/target_ruby.rb', line 119

def version_from_array(array)
  array.children.map(&:value)
end

#version_from_gemspec_file(file) (private)

[ GitHub ]

  
# 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) (private)

[ GitHub ]

  
# 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