Sha256: 5a7736e86c0653b26233eefd1d072d06905472684c52e547f09e9768e423e964

Contents?: true

Size: 1.83 KB

Versions: 30

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for accessing the first element of `String#unpack`
      # which can be replaced with the shorter method `unpack1`.
      #
      # @example
      #
      #   # bad
      #   'foo'.unpack('h*').first
      #   'foo'.unpack('h*')[0]
      #   'foo'.unpack('h*').slice(0)
      #   'foo'.unpack('h*').at(0)
      #
      #   # good
      #   'foo'.unpack1('h*')
      #
      class UnpackFirst < Base
        extend AutoCorrector

        MSG = 'Use `%<receiver>s.unpack1(%<format>s)` instead of '\
          '`%<receiver>s.unpack(%<format>s)%<method>s`.'
        RESTRICT_ON_SEND = %i[first [] slice at].freeze

        def_node_matcher :unpack_and_first_element?, <<~PATTERN
          {
            (send $(send (...) :unpack $(...)) :first)
            (send $(send (...) :unpack $(...)) {:[] :slice :at} (int 0))
          }
        PATTERN

        def on_send(node)
          unpack_and_first_element?(node) do |unpack_call, unpack_arg|
            range = first_element_range(node, unpack_call)
            message = format(MSG,
                             receiver: unpack_call.receiver.source,
                             format: unpack_arg.source,
                             method: range.source)
            add_offense(node, message: message) do |corrector|
              corrector.remove(first_element_range(node, unpack_call))
              corrector.replace(unpack_call.loc.selector, 'unpack1')
            end
          end
        end

        private

        def first_element_range(node, unpack_call)
          Parser::Source::Range.new(node.loc.expression.source_buffer,
                                    unpack_call.loc.expression.end_pos,
                                    node.loc.expression.end_pos)
        end
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 2 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/unpack_first.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/unpack_first.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/unpack_first.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/unpack_first.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/unpack_first.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/unpack_first.rb
rubocop-1.10.0 lib/rubocop/cop/style/unpack_first.rb
rubocop-1.9.1 lib/rubocop/cop/style/unpack_first.rb
rubocop-1.9.0 lib/rubocop/cop/style/unpack_first.rb
rubocop-1.8.1 lib/rubocop/cop/style/unpack_first.rb
rubocop-1.8.0 lib/rubocop/cop/style/unpack_first.rb
rubocop-1.7.0 lib/rubocop/cop/style/unpack_first.rb
rubocop-1.6.1 lib/rubocop/cop/style/unpack_first.rb
rubocop-1.6.0 lib/rubocop/cop/style/unpack_first.rb
rubocop-1.5.2 lib/rubocop/cop/style/unpack_first.rb
rubocop-1.5.1 lib/rubocop/cop/style/unpack_first.rb
rubocop-1.5.0 lib/rubocop/cop/style/unpack_first.rb
rubocop-1.4.2 lib/rubocop/cop/style/unpack_first.rb
rubocop-1.4.1 lib/rubocop/cop/style/unpack_first.rb
rubocop-1.4.0 lib/rubocop/cop/style/unpack_first.rb