Sha256: c9f195425f26f59e5dc2114fb28ccf37d64995b562dd2988630332853f755603

Contents?: true

Size: 1.56 KB

Versions: 32

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`.'

        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

32 entries across 21 versions & 3 rubygems

Version Path
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/rubocop-0.75.1/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
rubocop-0.75.1 lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
rubocop-0.75.0 lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
zuora_connect_ui-0.8.3 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
zuora_connect_ui-0.8.2 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
zuora_connect_ui-0.8.1 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
zuora_connect_ui-0.8.0 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
rubocop-0.74.0 lib/rubocop/cop/internal_affairs/offense_location_keyword.rb
rubocop-0.73.0 lib/rubocop/cop/internal_affairs/offense_location_keyword.rb