Sha256: d3ed6568fe77b67afa19a63fba9b310c04c71c2daaaadf4eed7d2a4d00265218

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 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
      #   # bad
      #   request.referrer
      #
      #   # good
      #   request.referer
      #
      #   # EnforcedStyle: referrer
      #   # bad
      #   request.referer
      #
      #   # good
      #   request.referrer
      class RequestReferer < Cop
        include ConfigurableEnforcedStyle

        MSG = 'Use `request.%s` instead of `request.%s`.'.freeze

        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, node.source_range, message)
          end
        end

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

        private

        def message
          format(MSG, style, wrong_method_name)
        end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-0.49.1 lib/rubocop/cop/rails/request_referer.rb
rubocop-0.49.0 lib/rubocop/cop/rails/request_referer.rb
rubocop-0.48.1 lib/rubocop/cop/rails/request_referer.rb