/
ambiguous_operator.rb
60 lines (53 loc) · 1.91 KB
/
ambiguous_operator.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
57
58
59
60
# frozen_string_literal: true
module RuboCop
module Cop
module Lint
# This cop checks for ambiguous operators in the first argument of a
# method invocation without parentheses.
#
# @example
#
# # bad
#
# # The `*` is interpreted as a splat operator but it could possibly be
# # a `*` method invocation (i.e. `do_something.*(some_array)`).
# do_something *some_array
#
# @example
#
# # good
#
# # With parentheses, there's no ambiguity.
# do_something(*some_array)
class AmbiguousOperator < Cop
include ParserDiagnostic
AMBIGUITIES = {
'+' => { actual: 'positive number', possible: 'addition' },
'-' => { actual: 'negative number', possible: 'subtraction' },
'*' => { actual: 'splat', possible: 'multiplication' },
'&' => { actual: 'block', possible: 'binary AND' },
'**' => { actual: 'keyword splat', possible: 'exponent' }
}.each do |key, hash|
hash[:operator] = key
end
MSG_FORMAT = 'Ambiguous %<actual>s operator. Parenthesize the method ' \
"arguments if it's surely a %<actual>s operator, or add " \
'a whitespace to the right of the `%<operator>s` if it ' \
'should be a %<possible>s.'
private
def relevant_diagnostic?(diagnostic)
diagnostic.reason == :ambiguous_prefix
end
def find_offense_node_by(diagnostic)
# TODO: When implementing auto-correction, this method should return
# an offense node passed as first argument of `add_offense` method.
end
def alternative_message(diagnostic)
operator = diagnostic.location.source
hash = AMBIGUITIES[operator]
format(MSG_FORMAT, hash)
end
end
end
end
end