forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_referer.rb
56 lines (47 loc) · 1.34 KB
/
request_referer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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