Sha256: 0ee7f2d9ab53e3a34889f6d193a0736df735e992f15ff82226a5422c67cf39b9

Contents?: true

Size: 1.34 KB

Versions: 10

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # This cop checks for consistent uses of `request.referer` or
      # `request.referrer`, depending on the cop's configuration.
      #
      # @example EnforcedStyle: referer (default)
      #   # bad
      #   request.referrer
      #
      #   # good
      #   request.referer
      #
      # @example EnforcedStyle: referrer
      #   # bad
      #   request.referer
      #
      #   # good
      #   request.referrer
      class RequestReferer < Cop
        include ConfigurableEnforcedStyle

        MSG = 'Use `request.%<prefer>s` instead of ' \
              '`request.%<current>s`.'

        def_node_matcher :referer?, <<~PATTERN
          (send (send nil? :request) {:referer :referrer})
        PATTERN

        def on_send(node)
          referer?(node) do
            return unless node.method?(wrong_method_name)

            add_offense(node.source_range, location: node.source_range)
          end
        end

        def autocorrect(node)
          ->(corrector) { corrector.replace(node, "request.#{style}") }
        end

        private

        def message(_node)
          format(MSG, prefer: style, current: wrong_method_name)
        end

        def wrong_method_name
          style == :referer ? :referrer : :referer
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rubocop-rails-2.8.1 lib/rubocop/cop/rails/request_referer.rb
rubocop-rails-2.8.0 lib/rubocop/cop/rails/request_referer.rb
rubocop-rails-2.7.1 lib/rubocop/cop/rails/request_referer.rb
rubocop-rails-2.7.0 lib/rubocop/cop/rails/request_referer.rb
rubocop-rails-2.6.0 lib/rubocop/cop/rails/request_referer.rb
rubocop-rails-2.5.2 lib/rubocop/cop/rails/request_referer.rb
rubocop-rails-2.5.1 lib/rubocop/cop/rails/request_referer.rb
rubocop-rails-2.5.0 lib/rubocop/cop/rails/request_referer.rb
rubocop-rails-2.4.2 lib/rubocop/cop/rails/request_referer.rb
rubocop-rails-2.4.1 lib/rubocop/cop/rails/request_referer.rb