Sha256: 21b6c3741aed0bdeaea74ee3f3cddacff9f3c726604ab8efc8b6fd51a8557ab6
Contents?: true
Size: 1.9 KB
Versions: 3
Compression:
Stored size: 1.9 KB
Contents
# encoding: utf-8 module Rubocop module Cop module Style # This cop checks for big numeric literals without _ between groups # of digits in them. class NumericLiterals < Cop # The parameter is called MinDigits (meaning the minimum number of # digits for which an offense can be registered), but essentially it's # a Max parameter (the maximum number of something that's allowed). include ConfigurableMax MSG = 'Separate every 3 digits in the integer portion of a number ' \ 'with underscores(_).' def on_int(node) check(node) end def on_float(node) check(node) end private def parameter_name 'MinDigits' end def check(node) int = integer_part(node) # TODO: handle non-decimal literals as well return if int.start_with?('0') if int.size >= min_digits case int when /^\d+$/ add_offense(node, :expression) { self.max = int.size + 1 } when /\d{4}/, /_\d{1,2}_/ add_offense(node, :expression) do self.config_to_allow_offenses = { 'Enabled' => false } end end end end def autocorrect(node) @corrections << lambda do |corrector| int = node.loc.expression.source.to_i formatted_int = int .abs .to_s .reverse .gsub(/...(?=.)/, '\&_') .reverse formatted_int.insert(0, '-') if int < 0 corrector.replace(node.loc.expression, formatted_int) end end def integer_part(node) node.loc.expression.source.sub(/^[+-]/, '').split('.').first end def min_digits cop_config['MinDigits'] end end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
rubocop-0.21.0 | lib/rubocop/cop/style/numeric_literals.rb |
rubocop-0.20.1 | lib/rubocop/cop/style/numeric_literals.rb |
rubocop-0.20.0 | lib/rubocop/cop/style/numeric_literals.rb |