Class: CustomCops::VersionComparison

Inherits:
RuboCop::Cop::Base
  • Object
show all
Defined in:
lib/simplycop/custom_cops/version_comparison.rb

Overview

Detects incorrect semantic version comparisons. Triggers on variables/methods containing ‘version’.

If this cop flags a false positive, disable it with an inline rubocop comment.

Examples:

Bad - version.to_f >= 2.0, version >= “2”


Good - Gem::Version.new(version) >= Gem::Version.new(‘2.0’)


Constant Summary collapse

DISABLE_HINT =
'Disable if not a semantic version string.'
MSG_TO_F =
"Avoid `.to_f` on version strings; use `Gem::Version.new()`. #{DISABLE_HINT}"
MSG_TO_I =
"Avoid `.to_i` on version strings; use `Gem::Version.new()`. #{DISABLE_HINT}"
MSG_STRING =
"Avoid ordering operators on version strings; use `Gem::Version.new()`. #{DISABLE_HINT}"
ORDERING_OPERATORS =
[:>=, :>, :<=, :<].freeze
REVERSED_OPERATORS =
{ :>= => :<=, :> => :<, :<= => :>=, :< => :> }.freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



28
29
30
31
32
33
# File 'lib/simplycop/custom_cops/version_comparison.rb', line 28

def on_send(node)
  check_numeric_conversion(node, :to_f_call?, MSG_TO_F)
  check_numeric_conversion(node, :to_i_call?, MSG_TO_I)
  check_ordering_comparison(node)
  check_reversed_ordering_comparison(node)
end