Class: RuboCop::Cop::Gemspec::RequiredRubyVersion
- 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.
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
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #defined_ruby_version(node) ⇒ Object
-
#on_new_investigation ⇒ Object
rubocop:disable Metrics/AbcSize.
- #required_ruby_version(node) ⇒ Object
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 ExcludeLimit
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
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Method Details
#defined_ruby_version(node) ⇒ Object
64 65 66 67 |
# File 'lib/rubocop/cop/gemspec/required_ruby_version.rb', line 64 def_node_matcher :defined_ruby_version, <<~PATTERN {$(str _) $(array (str _) (str _)) (send (const (const nil? :Gem) :Requirement) :new $(str _))} PATTERN |
#on_new_investigation ⇒ Object
rubocop:disable Metrics/AbcSize
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/rubocop/cop/gemspec/required_ruby_version.rb', line 70 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: (ruby_version, target_ruby_version) ) else range = source_range(processed_source.buffer, 1, 0) add_offense(range, message: MISSING_MSG) end end |
#required_ruby_version(node) ⇒ Object
59 60 61 |
# File 'lib/rubocop/cop/gemspec/required_ruby_version.rb', line 59 def_node_search :required_ruby_version, <<~PATTERN (send _ :required_ruby_version= $_) PATTERN |