Sha256: f82c9d233faa6768ca9784e0902467bf78e2fa40f86af2e6942be6c3959e4bef

Contents?: true

Size: 1.69 KB

Versions: 229

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module InternalAffairs
      # Checks for potential uses of the location keywords which can be used as
      # shortcut arguments to `#add_offense`.
      #
      # @example
      #
      #   # bad
      #   add_offense(node, location: node.loc.selector)
      #
      #   # good
      #   add_offense(node, location: :selector)
      class OffenseLocationKeyword < Base
        extend AutoCorrector

        MSG = 'Use `:%<keyword>s` as the location argument to `#add_offense`.'
        RESTRICT_ON_SEND = %i[add_offense].freeze

        def on_send(node)
          node_type_check(node) do |node_arg, kwargs|
            find_offending_argument(node_arg, kwargs) do |location, keyword|
              add_offense(location, message: format(MSG, keyword: keyword)) do |corrector|
                (*, keyword) = offending_location_argument(location.parent)

                corrector.replace(location, ":#{keyword}")
              end
            end
          end
        end

        private

        # @!method node_type_check(node)
        def_node_matcher :node_type_check, <<~PATTERN
          (send nil? :add_offense $_node $hash)
        PATTERN

        # @!method offending_location_argument(node)
        def_node_matcher :offending_location_argument, <<~PATTERN
          (pair (sym :location) $(send (send $_node :loc) $_keyword))
        PATTERN

        def find_offending_argument(searched_node, kwargs)
          kwargs.pairs.each do |pair|
            offending_location_argument(pair) do |location, node, keyword|
              yield(location, keyword) if searched_node == node
            end
          end
        end
      end
    end
  end
end

Version data entries

229 entries across 220 versions & 22 rubygems

Version Path
rubocop-1.70.0 lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
rubocop-1.69.2 lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
rubocop-1.69.1 lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
rubocop-1.69.0 lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
rubocop-1.68.0 lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
rubocop-1.67.0 lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
rubocop-1.66.1 lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
rubocop-1.66.0 lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
rubocop-1.65.1 lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
rubocop-1.65.0 lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
rubocop-1.64.1 lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
rubocop-1.63.4 lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
rubocop-1.63.3 lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
rubocop-1.63.2 lib/rubocop/cop/internal_affairs/offense_location_keyword.rb