class RuboCop::Cop::Style::NumericLiteralPrefix

Checks for octal, hex, binary, and decimal literals using uppercase prefixes and corrects them to lowercase prefix or no prefix (in case of decimals).

@example EnforcedOctalStyle: zero_with_o (default)

# bad - missing octal prefix
num = 01234

# bad - uppercase prefix
num = 0O1234
num = 0X12AB
num = 0B10101

# bad - redundant decimal prefix
num = 0D1234
num = 0d1234

# good
num = 0o1234
num = 0x12AB
num = 0b10101
num = 1234

@example EnforcedOctalStyle: zero_only

# bad
num = 0o1234
num = 0O1234

# good
num = 01234

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 52
def on_int(node)
  type = literal_type(node)

  return unless type

  add_offense(node) do |corrector|
    corrector.replace(node, send(:"format_#{type}", node.source))
  end
end

Private Instance Methods

format_binary(source) click to toggle source
# File lib/rubocop/cop/style/numeric_literal_prefix.rb, line 109
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 113
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 105
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 97
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 101
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 82
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 68
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 64
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 74
def octal_literal_type(literal)
  if OCTAL_ZERO_ONLY_REGEX.match?(literal) && octal_zero_only?
    :octal_zero_only
  elsif OCTAL_REGEX.match?(literal) && !octal_zero_only?
    :octal
  end
end
octal_zero_only?() click to toggle source
# File lib/rubocop/cop/style/numeric_literal_prefix.rb, line 93
def octal_zero_only?
  cop_config['EnforcedOctalStyle'] == 'zero_only'
end