-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
redundant_location_argument.rb
51 lines (43 loc) · 1.3 KB
/
redundant_location_argument.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
# frozen_string_literal: true
module RuboCop
module Cop
module InternalAffairs
# Checks for redundant `location` argument to `#add_offense`. `location`
# argument has a default value of `:expression` and this method will
# automatically use it.
#
# @example
#
# # bad
# add_offense(node, location: :expression)
#
# # good
# add_offense(node)
# add_offense(node, location: :selector)
#
class RedundantLocationArgument < Base
include RangeHelp
extend AutoCorrector
MSG = 'Redundant location argument to `#add_offense`.'
def_node_matcher :redundant_location_argument, <<~PATTERN
(send nil? :add_offense _
(hash <$(pair (sym :location) (sym :expression)) ...>)
)
PATTERN
def on_send(node)
redundant_location_argument(node) do |argument|
add_offense(argument) do |corrector|
range = offending_range(argument)
corrector.remove(range)
end
end
end
private
def offending_range(node)
with_space = range_with_surrounding_space(range: node.loc.expression)
range_with_surrounding_comma(with_space, :left)
end
end
end
end
end