Sha256: 58d8dcba4ed206445798e20c725e15c647e398d2a9eca95e25c0b5da20bea429

Contents?: true

Size: 1.4 KB

Versions: 34

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module InternalAffairs
      # Checks for redundant `location` argument to `#add_offense`. `location`
      # argument has a default value of `:expression` and this method will
      # automatically use it.
      #
      # @example
      #
      #   # bad
      #   add_offense(node, location: :expression)
      #
      #   # good
      #   add_offense(node)
      #   add_offense(node, location: :selector)
      #
      class RedundantLocationArgument < Base
        include RangeHelp
        extend AutoCorrector

        MSG = 'Redundant location argument to `#add_offense`.'
        RESTRICT_ON_SEND = %i[add_offense].freeze

        # @!method redundant_location_argument(node)
        def_node_matcher :redundant_location_argument, <<~PATTERN
          (send nil? :add_offense _
            (hash <$(pair (sym :location) (sym :expression)) ...>)
          )
        PATTERN

        def on_send(node)
          redundant_location_argument(node) do |argument|
            add_offense(argument) do |corrector|
              range = offending_range(argument)

              corrector.remove(range)
            end
          end
        end

        private

        def offending_range(node)
          with_space = range_with_surrounding_space(node.loc.expression)

          range_with_surrounding_comma(with_space, :left)
        end
      end
    end
  end
end

Version data entries

34 entries across 30 versions & 4 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb
zilla-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.46.0/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb
rubocop-1.46.0 lib/rubocop/cop/internal_affairs/redundant_location_argument.rb
rubocop-1.45.1 lib/rubocop/cop/internal_affairs/redundant_location_argument.rb
rubocop-1.45.0 lib/rubocop/cop/internal_affairs/redundant_location_argument.rb
rubocop-1.44.1 lib/rubocop/cop/internal_affairs/redundant_location_argument.rb
rubocop-1.44.0 lib/rubocop/cop/internal_affairs/redundant_location_argument.rb
rubocop-1.43.0 lib/rubocop/cop/internal_affairs/redundant_location_argument.rb
rubocop-1.42.0 lib/rubocop/cop/internal_affairs/redundant_location_argument.rb
rubocop-1.41.1 lib/rubocop/cop/internal_affairs/redundant_location_argument.rb
rubocop-1.41.0 lib/rubocop/cop/internal_affairs/redundant_location_argument.rb
rubocop-1.40.0 lib/rubocop/cop/internal_affairs/redundant_location_argument.rb