Sha256: 4da962d6cb9843cdd6b11787657a564153a3beaf12647a058837eb5815045592

Contents?: true

Size: 1.56 KB

Versions: 6791

Compression:

Stored size: 1.56 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 < Cop
        MSG = 'Use `:%<keyword>s` as the location argument to ' \
              '`#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))
            end
          end
        end

        def autocorrect(node)
          (*, keyword) = offending_location_argument(node.parent)

          ->(corrector) { corrector.replace(node.source_range, ":#{keyword}") }
        end

        private

        def_node_matcher :node_type_check, <<-PATTERN
          (send nil? :add_offense $_node $hash)
        PATTERN

        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

6,791 entries across 6,785 versions & 25 rubygems

Version Path
ory-client-0.0.1.alpha74 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
ory-client-0.0.1.alpha73 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
ory-client-0.0.1.alpha72 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
ory-client-0.0.1.alpha71 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
ory-client-0.0.1.alpha70 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
ory-client-0.0.1.alpha69 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
ory-client-0.0.1.alpha68 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
ory-client-0.0.1.alpha67 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
ory-client-0.0.1.alpha66 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
ory-oathkeeper-client-0.38.19.beta1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
ory-oathkeeper-client-0.38.18.beta1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
ory-oathkeeper-client-0.38.17.beta1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
avalara_sdk-2.4.7 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
ory-client-0.0.1.alpha58 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
ory-client-0.0.1.alpha57 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
ory-client-0.0.1.alpha56 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
ory-client-0.0.1.alpha55 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
ory-client-0.0.1.alpha54 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
ory-client-0.0.1.alpha53 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
ory-client-0.0.1.alpha52 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb