Sha256: 9702e5919699341ceabc85812808c26c8a1ac4a6ce1c3bfe8704da1b765d2cc6

Contents?: true

Size: 1.65 KB

Versions: 38

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module InternalAffairs
      # Checks for the use of `node.arguments.first` or `node.arguments.last` and
      # suggests the use of `node.first_argument` or `node.last_argument` instead.
      #
      # @example
      #   # bad
      #   node.arguments.first
      #   node.arguments[0]
      #   node.arguments.last
      #   node.arguments[-1]
      #
      #   # good
      #   node.first_argument
      #   node.last_argument
      #
      class NodeFirstOrLastArgument < Base
        extend AutoCorrector
        include RangeHelp

        MSG = 'Use `#%<correct>s` instead of `#%<incorrect>s`.'
        RESTRICT_ON_SEND = %i[arguments].freeze

        # @!method arguments_first_or_last?(node)
        def_node_matcher :arguments_first_or_last?, <<~PATTERN
          {
            (send (send !nil? :arguments) ${:first :last})
            (send (send !nil? :arguments) :[] (int ${0 -1}))
          }
        PATTERN

        def on_send(node)
          arguments_first_or_last?(node.parent) do |end_or_index|
            range = range_between(node.loc.selector.begin_pos, node.parent.source_range.end_pos)
            correct = case end_or_index
                      when :first, 0 then 'first_argument'
                      when :last, -1 then 'last_argument'
                      else raise "Unknown end_or_index: #{end_or_index}"
                      end
            message = format(MSG, correct: correct, incorrect: range.source)

            add_offense(range, message: message) do |corrector|
              corrector.replace(range, correct)
            end
          end
        end
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 8 rubygems

Version Path
rubocop-1.70.0 lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb
rubocop-1.69.2 lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb
rubocop-1.69.1 lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb
rubocop-1.69.0 lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb
rubocop-1.68.0 lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb
rubocop-1.67.0 lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb
rubocop-1.66.1 lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb
rubocop-1.66.0 lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb
rubocop-1.65.1 lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb
rubocop-1.65.0 lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb
rubocop-1.64.1 lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb
rubocop-1.63.4 lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb
rubocop-1.63.3 lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb
rubocop-1.63.2 lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb
rubocop-1.63.1 lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb
rubocop-1.63.0 lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb