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).
@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
autocorrect(node)
click to toggle source
# File lib/rubocop/cop/style/numeric_literal_prefix.rb, line 59 def autocorrect(node) lambda do |corrector| type = literal_type(node) corrector.replace(node.source_range, send(:"format_#{type}", node.source)) end end
on_int(node)
click to toggle source
# File lib/rubocop/cop/style/numeric_literal_prefix.rb, line 51 def on_int(node) type = literal_type(node) return unless type add_offense(node) end
Private Instance Methods
format_binary(source)
click to toggle source
# File lib/rubocop/cop/style/numeric_literal_prefix.rb, line 114 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 118 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 110 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 102 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 106 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 87 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 73 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 69 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 79 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 98 def octal_zero_only? cop_config['EnforcedOctalStyle'] == 'zero_only' end