class RuboCop::Cop::Style::NumericLiteralPrefix

This cop checks for octal, hex, binary and decimal literals using uppercase prefixes and corrects them to lowercase prefix or no prefix (in case of decimals). eg. for octal use `0o` instead of `0` or `0O`.

Can be configured to use `0` only for octal literals using `EnforcedOctalStyle` => `zero_only`

Constants

BINARY_MSG
BINARY_REGEX
DECIMAL_MSG
DECIMAL_REGEX
HEX_MSG
HEX_REGEX
OCTAL_MSG
OCTAL_REGEX
OCTAL_ZERO_ONLY_MSG
OCTAL_ZERO_ONLY_REGEX

Public Instance Methods

on_int(node) click to toggle source
# File lib/rubocop/cop/style/numeric_literal_prefix.rb, line 27
def on_int(node)
  type = literal_type(node)

  return unless type

  add_offense(node)
end

Private Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/style/numeric_literal_prefix.rb, line 41
def autocorrect(node)
  lambda do |corrector|
    type = literal_type(node)
    corrector.replace(node.source_range,
                      send(:"format_#{type}", node.source))
  end
end
format_binary(source) click to toggle source
# File lib/rubocop/cop/style/numeric_literal_prefix.rb, line 90
def format_binary(source)
  source.sub(/^0B/, '0b')
end
format_decimal(source) click to toggle source
# File lib/rubocop/cop/style/numeric_literal_prefix.rb, line 94
def format_decimal(source)
  source.sub(/^0[dD]/, '')
end
format_hex(source) click to toggle source
# File lib/rubocop/cop/style/numeric_literal_prefix.rb, line 86
def format_hex(source)
  source.sub(/^0X/, '0x')
end
format_octal(source) click to toggle source
# File lib/rubocop/cop/style/numeric_literal_prefix.rb, line 78
def format_octal(source)
  source.sub(/^0O?/, '0o')
end
format_octal_zero_only(source) click to toggle source
# File lib/rubocop/cop/style/numeric_literal_prefix.rb, line 82
def format_octal_zero_only(source)
  source.sub(/^0[Oo]?/, '0')
end
hex_bin_dec_literal_type(literal) click to toggle source
# File lib/rubocop/cop/style/numeric_literal_prefix.rb, line 63
def hex_bin_dec_literal_type(literal)
  case literal
  when HEX_REGEX
    :hex
  when BINARY_REGEX
    :binary
  when DECIMAL_REGEX
    :decimal
  end
end
literal_type(node) click to toggle source
# File lib/rubocop/cop/style/numeric_literal_prefix.rb, line 49
def literal_type(node)
  literal = integer_part(node)

  octal_literal_type(literal) || hex_bin_dec_literal_type(literal)
end
message(node) click to toggle source
# File lib/rubocop/cop/style/numeric_literal_prefix.rb, line 37
def message(node)
  self.class.const_get("#{literal_type(node).upcase}_MSG")
end
octal_literal_type(literal) click to toggle source
# File lib/rubocop/cop/style/numeric_literal_prefix.rb, line 55
def octal_literal_type(literal)
  if literal =~ OCTAL_ZERO_ONLY_REGEX && octal_zero_only?
    :octal_zero_only
  elsif literal =~ OCTAL_REGEX && !octal_zero_only?
    :octal
  end
end
octal_zero_only?() click to toggle source
# File lib/rubocop/cop/style/numeric_literal_prefix.rb, line 74
def octal_zero_only?
  cop_config['EnforcedOctalStyle'] == 'zero_only'
end