class RuboCop::Cop::Bundler::GemVersion
Enforce that Gem version specifications or a commit reference (branch, ref, or tag) are either required or forbidden.
@example EnforcedStyle: required (default)
# bad gem 'rubocop' # good gem 'rubocop', '~> 1.12' # good gem 'rubocop', '>= 1.10.0' # good gem 'rubocop', '>= 1.5.0', '< 1.10.0' # good gem 'rubocop', branch: 'feature-branch' # good gem 'rubocop', ref: '74b5bfbb2c4b6fd6cdbbc7254bd7084b36e0c85b' # good gem 'rubocop', tag: 'v1.17.0'
@example EnforcedStyle: forbidden
# good gem 'rubocop' # bad gem 'rubocop', '~> 1.12' # bad gem 'rubocop', '>= 1.10.0' # bad gem 'rubocop', '>= 1.5.0', '< 1.10.0' # bad gem 'rubocop', branch: 'feature-branch' # bad gem 'rubocop', ref: '74b5bfbb2c4b6fd6cdbbc7254bd7084b36e0c85b' # bad gem 'rubocop', tag: 'v1.17.0'
Constants
- FORBIDDEN_MSG
- REQUIRED_MSG
- RESTRICT_ON_SEND
- VERSION_SPECIFICATION_REGEX
Public Instance Methods
Source
# File lib/rubocop/cop/bundler/gem_version.rb, line 72 def on_send(node) return unless gem_declaration?(node) return if allowed_gem?(node) if offense?(node) add_offense(node) opposite_style_detected else correct_style_detected end end
Private Instance Methods
Source
# File lib/rubocop/cop/bundler/gem_version.rb, line 86 def allowed_gem?(node) allowed_gems.include?(node.first_argument.value) end
Source
# File lib/rubocop/cop/bundler/gem_version.rb, line 90 def allowed_gems Array(cop_config['AllowedGems']) end
Source
# File lib/rubocop/cop/bundler/gem_version.rb, line 112 def forbidden_offense?(node) return false unless forbidden_style? includes_version_specification?(node) || includes_commit_reference?(node) end
Source
# File lib/rubocop/cop/bundler/gem_version.rb, line 118 def forbidden_style? style == :forbidden end
Source
# File lib/rubocop/cop/bundler/gem_version.rb, line 94 def message(_range) if required_style? REQUIRED_MSG elsif forbidden_style? FORBIDDEN_MSG end end
Source
# File lib/rubocop/cop/bundler/gem_version.rb, line 102 def offense?(node) required_offense?(node) || forbidden_offense?(node) end
Source
# File lib/rubocop/cop/bundler/gem_version.rb, line 106 def required_offense?(node) return false unless required_style? !includes_version_specification?(node) && !includes_commit_reference?(node) end
Source
# File lib/rubocop/cop/bundler/gem_version.rb, line 122 def required_style? style == :required end
Source
# File lib/rubocop/cop/bundler/gem_version.rb, line 126 def version_specification?(expression) expression.match?(VERSION_SPECIFICATION_REGEX) end