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` of gemspec is specified and equal to `TargetRubyVersion` of .rubocop.yml. Thereby, RuboCop to perform static analysis working on the version required by gemspec.

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

# 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 versionning
Gem::Specification.new do |spec|
  spec.required_ruby_version = '~> 2.5'
end

Constant Summary collapse

NOT_EQUAL_MSG =
'`required_ruby_version` (%<required_ruby_version>s, ' \
'declared in %<gemspec_filename>s) and `TargetRubyVersion` ' \
'(%<target_ruby_version>s, which may be specified in ' \
'.rubocop.yml) should be equal.'
MISSING_MSG =
'`required_ruby_version` should be specified.'

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods inherited from Base

#add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #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, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_other_file, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #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

#on_new_investigationObject

rubocop:disable Metrics/AbcSize


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rubocop/cop/gemspec/required_ruby_version.rb', line 68

def on_new_investigation
  version_def = required_ruby_version(processed_source.ast).first

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

    add_offense(
      version_def.loc.expression,
      message: not_equal_message(ruby_version, target_ruby_version)
    )
  else
    range = source_range(processed_source.buffer, 1, 0)
    add_offense(range, message: MISSING_MSG)
  end
end