Class: RuboCop::Cop::Gemspec::RequiredRubyVersion

Inherits:
Base
  • Object
show all
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/gemspec/required_ruby_version.rb

Overview

Checks that required_ruby_version in a gemspec file is set to a valid value (non-blank) and matches TargetRubyVersion as set in RuboCop’s configuration for the gem.

This ensures that RuboCop is using the same Ruby version as the gem.

Examples:

# When `TargetRubyVersion` of .rubocop.yml is `2.5`.

# bad
Gem::Specification.new do |spec|
  # no `required_ruby_version` specified
end

# bad
Gem::Specification.new do |spec|
  spec.required_ruby_version = '>= 2.4.0'
end

# bad
Gem::Specification.new do |spec|
  spec.required_ruby_version = '>= 2.6.0'
end

# bad
Gem::Specification.new do |spec|
  spec.required_ruby_version = ''
end

# good
Gem::Specification.new do |spec|
  spec.required_ruby_version = '>= 2.5.0'
end

# good
Gem::Specification.new do |spec|
  spec.required_ruby_version = '>= 2.5'
end

# accepted but not recommended
Gem::Specification.new do |spec|
  spec.required_ruby_version = ['>= 2.5.0', '< 2.7.0']
end

# accepted but not recommended, since
# Ruby does not really follow semantic versioning
Gem::Specification.new do |spec|
  spec.required_ruby_version = '~> 2.5'
end

Constant Summary collapse

RESTRICT_ON_SEND =
%i[required_ruby_version=].freeze
NOT_EQUAL_MSG =
'`required_ruby_version` and `TargetRubyVersion` ' \
'(%<target_ruby_version>s, which may be specified in ' \
'.rubocop.yml) should be equal.'
MISSING_MSG =
'`required_ruby_version` should be specified.'

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_other_file, #parse, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#defined_ruby_version(node) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/rubocop/cop/gemspec/required_ruby_version.rb', line 70

def_node_matcher :defined_ruby_version, <<~PATTERN
  {
    $(str _)
    $(array (str _) (str _))
    (send (const (const nil? :Gem) :Requirement) :new $str+)
  }
PATTERN

#on_new_investigationObject



78
79
80
# File 'lib/rubocop/cop/gemspec/required_ruby_version.rb', line 78

def on_new_investigation
  add_global_offense(MISSING_MSG) unless required_ruby_version?(processed_source.ast)
end

#on_send(node) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/rubocop/cop/gemspec/required_ruby_version.rb', line 82

def on_send(node)
  version_def = node.first_argument
  return if dynamic_version?(version_def)

  ruby_version = extract_ruby_version(defined_ruby_version(version_def))
  return if ruby_version == target_ruby_version.to_s

  add_offense(version_def, message: not_equal_message(ruby_version, target_ruby_version))
end

#required_ruby_version?(node) ⇒ Object



65
66
67
# File 'lib/rubocop/cop/gemspec/required_ruby_version.rb', line 65

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