class RuboCop::Cop::Style::NumericLiterals

This cop checks for big numeric literals without _ between groups of digits in them.

@example

# bad

1000000
1_00_000
1_0000

# good

1_000_000
1000

# good unless Strict is set

10_000_00 # typical representation of $10,000 in cents

Constants

DELIMITER_REGEXP
MSG

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/style/numeric_literals.rb, line 45
def autocorrect(node)
  lambda do |corrector|
    corrector.replace(node.source_range, format_number(node))
  end
end
on_float(node) click to toggle source
# File lib/rubocop/cop/style/numeric_literals.rb, line 41
def on_float(node)
  check(node)
end
on_int(node) click to toggle source
# File lib/rubocop/cop/style/numeric_literals.rb, line 37
def on_int(node)
  check(node)
end

Private Instance Methods

check(node) click to toggle source
# File lib/rubocop/cop/style/numeric_literals.rb, line 57
def check(node)
  int = integer_part(node)

  # TODO: handle non-decimal literals as well
  return if int.start_with?('0')
  return unless int.size >= min_digits

  case int
  when /^\d+$/
    add_offense(node) { self.max = int.size + 1 }
  when /\d{4}/, short_group_regex
    add_offense(node) do
      self.config_to_allow_offenses = { 'Enabled' => false }
    end
  end
end
format_int_part(int_part) click to toggle source

@param int_part [String]

# File lib/rubocop/cop/style/numeric_literals.rb, line 92
def format_int_part(int_part)
  int_part = Integer(int_part)
  formatted_int = int_part
                  .abs
                  .to_s
                  .reverse
                  .gsub(/...(?=.)/, '\&_')
                  .reverse
  formatted_int.insert(0, '-') if int_part.negative?
  formatted_int
end
format_number(node) click to toggle source
# File lib/rubocop/cop/style/numeric_literals.rb, line 78
def format_number(node)
  source = node.source.gsub(/\s+/, '')
  int_part, additional_part = source.split(DELIMITER_REGEXP, 2)
  formatted_int = format_int_part(int_part)
  delimiter = source[DELIMITER_REGEXP]

  if additional_part
    formatted_int + delimiter + additional_part
  else
    formatted_int
  end
end
max_parameter_name() click to toggle source
# File lib/rubocop/cop/style/numeric_literals.rb, line 53
def max_parameter_name
  'MinDigits'
end
min_digits() click to toggle source
# File lib/rubocop/cop/style/numeric_literals.rb, line 104
def min_digits
  cop_config['MinDigits']
end
short_group_regex() click to toggle source
# File lib/rubocop/cop/style/numeric_literals.rb, line 74
def short_group_regex
  cop_config['Strict'] ? /_\d{1,2}(_|$)/ : /_\d{1,2}_/
end