class RuboCop::Cop::Style::NumericLiterals


3000 # You can specify allowed numbers. (e.g. port number)
# good
@example AllowedNumbers: [3000]
10_000_00 # typical representation of $10,000 in cents
# bad
@example Strict: true
10_000_00 # typical representation of $10,000 in cents
# good
@example Strict: false (default)
1000
1_000_000
# good
1_0000
1_00_000
1000000
# bad
@example
only correct to the standard pattern of an ‘_` every 3 digits.
NOTE: Even if `AllowedPatterns` are given, autocorrection will
`d{4}_d{4}` will allow `1234_5678` but not `1234_5678_9012`).
as anchored even if the patterns do not contain anchors (so
the `AllowedPatterns` configuration. All regexps are treated
Additional allowed patterns can be added by adding regexps to
of digits in them.
Checks for big numeric literals without `_` between groups

def allowed_numbers

def allowed_numbers
  cop_config.fetch('AllowedNumbers', []).map(&:to_s)
end

def allowed_patterns

def allowed_patterns
  # Convert the patterns to be anchored
  super.map { |regexp| /\A#{regexp}\z/ }
end

def check(node)

def check(node)
  int = integer_part(node)
  # TODO: handle non-decimal literals as well
  return if int.start_with?('0')
  return if allowed_numbers.include?(int)
  return if matches_allowed_pattern?(int)
  return unless int.size >= min_digits
  case int
  when /^\d+$/
    register_offense(node) { self.min_digits = int.size + 1 }
  when /\d{4}/, short_group_regex
    register_offense(node) { self.config_to_allow_offenses = { 'Enabled' => false } }
  end
end

def format_int_part(int_part)

Parameters:
  • int_part (String) --
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

def format_number(node)

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

def min_digits

def min_digits
  cop_config['MinDigits']
end

def on_float(node)

def on_float(node)
  check(node)
end

def on_int(node)

def on_int(node)
  check(node)
end

def register_offense(node, &_block)

def register_offense(node, &_block)
  add_offense(node) do |corrector|
    yield
    corrector.replace(node, format_number(node))
  end
end

def short_group_regex

def short_group_regex
  cop_config['Strict'] ? /_\d{1,2}(_|$)/ : /_\d{1,2}_/
end