Sha256: d7a4593b5ffc4cede5c63c4a9589dea7d8f0683158b21385884fea1a852f04e6

Contents?: true

Size: 1.63 KB

Versions: 92

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # Identifies places where numeric argument to BigDecimal should be
      # converted to string. Initializing from String is faster
      # than from Numeric for BigDecimal.
      #
      # @example
      #   # bad
      #   BigDecimal(1, 2)
      #   4.to_d(6)
      #   BigDecimal(1.2, 3, exception: true)
      #   4.5.to_d(6, exception: true)
      #
      #   # good
      #   BigDecimal('1', 2)
      #   BigDecimal('4', 6)
      #   BigDecimal('1.2', 3, exception: true)
      #   BigDecimal('4.5', 6, exception: true)
      #
      class BigDecimalWithNumericArgument < Base
        extend AutoCorrector

        MSG = 'Convert numeric literal to string and pass it to `BigDecimal`.'
        RESTRICT_ON_SEND = %i[BigDecimal to_d].freeze

        def_node_matcher :big_decimal_with_numeric_argument?, <<~PATTERN
          (send nil? :BigDecimal $numeric_type? ...)
        PATTERN

        def_node_matcher :to_d?, <<~PATTERN
          (send [!nil? $numeric_type?] :to_d ...)
        PATTERN

        def on_send(node)
          if (numeric = big_decimal_with_numeric_argument?(node))
            add_offense(numeric.source_range) do |corrector|
              corrector.wrap(numeric, "'", "'")
            end
          elsif (numeric_to_d = to_d?(node))
            add_offense(numeric_to_d.source_range) do |corrector|
              big_decimal_args = node.arguments.map(&:source).unshift("'#{numeric_to_d.source}'").join(', ')

              corrector.replace(node, "BigDecimal(#{big_decimal_args})")
            end
          end
        end
      end
    end
  end
end

Version data entries

92 entries across 92 versions & 6 rubygems

Version Path
harbr-0.1.96 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb
harbr-0.1.95 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb
harbr-0.1.94 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb
harbr-0.1.93 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb
harbr-0.1.91 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb
harbr-0.1.90 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb
harbr-0.1.89 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb
harbr-0.1.88 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb
harbr-0.1.87 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb
harbr-0.1.86 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb
harbr-0.1.85 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb
harbr-0.1.84 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb
harbr-0.1.83 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb
harbr-0.1.82 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb
harbr-0.1.81 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb
harbr-0.1.80 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb
harbr-0.1.79 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb
harbr-0.1.78 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb
harbr-0.1.77 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb
harbr-0.1.76 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/big_decimal_with_numeric_argument.rb