Sha256: 1af762454abd452fb16564280cda3775cd97278ed62edbbff35eee8c1347d86d

Contents?: true

Size: 1.89 KB

Versions: 16

Compression:

Stored size: 1.89 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 < Cop
        MSG = 'Use `%<receiver>s.unpack1(%<format>s)` instead of '\
          '`%<receiver>s.unpack(%<format>s)%<method>s`.'

        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)
          end
        end

        def autocorrect(node)
          unpack_and_first_element?(node) do |unpack_call, _unpack_arg|
            lambda 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

16 entries across 16 versions & 3 rubygems

Version Path
rubocop-0.89.1 lib/rubocop/cop/style/unpack_first.rb
rubocop-0.89.0 lib/rubocop/cop/style/unpack_first.rb
rubocop-0.88.0 lib/rubocop/cop/style/unpack_first.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/style/unpack_first.rb
rubocop-0.87.1 lib/rubocop/cop/style/unpack_first.rb
rubocop-0.87.0 lib/rubocop/cop/style/unpack_first.rb
rubocop-0.86.0 lib/rubocop/cop/style/unpack_first.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/style/unpack_first.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/style/unpack_first.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/style/unpack_first.rb
rubocop-0.85.1 lib/rubocop/cop/style/unpack_first.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/style/unpack_first.rb
rubocop-0.85.0 lib/rubocop/cop/style/unpack_first.rb
rubocop-0.84.0 lib/rubocop/cop/style/unpack_first.rb
rubocop-0.83.0 lib/rubocop/cop/style/unpack_first.rb
rubocop-0.82.0 lib/rubocop/cop/style/unpack_first.rb