Sha256: 4004ea8f27088e7d9abdb11fdfd4fcf31e7964fc634ecf4c30022273491d4f5b

Contents?: true

Size: 985 Bytes

Versions: 10

Compression:

Stored size: 985 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # This cop checks for consistent uses of request.referrer or
      # request.referrer, depending on configuration.
      class RequestReferer < Cop
        include ConfigurableEnforcedStyle

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

        def on_send(node)
          referer?(node) do |method_name|
            return unless method_name == 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
          "Use `request.#{style}` instead of `request.#{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 & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/rails/request_referer.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/rails/request_referer.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/rails/request_referer.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/rails/request_referer.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/rails/request_referer.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/rails/request_referer.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/rails/request_referer.rb
rubocop-0.47.1 lib/rubocop/cop/rails/request_referer.rb
rubocop-0.47.0 lib/rubocop/cop/rails/request_referer.rb
rubocop-0.46.0 lib/rubocop/cop/rails/request_referer.rb