forked from rubocop/rubocop
/
parentheses_as_grouped_expression.rb
64 lines (52 loc) · 1.66 KB
/
parentheses_as_grouped_expression.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
61
62
63
64
# frozen_string_literal: true
module RuboCop
module Cop
module Lint
# Checks for space between the name of a called method and a left
# parenthesis.
#
# @example
#
# # bad
#
# puts (x + y)
#
# @example
#
# # good
#
# puts(x + y)
class ParenthesesAsGroupedExpression < Cop
include RangeHelp
MSG = '`(...)` interpreted as grouped expression.'.freeze
def on_send(node)
return unless node.arguments.one?
return if node.operator_method? || node.setter_method?
return unless node.first_argument.source.start_with?('(')
space_length = spaces_before_left_parenthesis(node)
return unless space_length > 0
range = space_range(node.first_argument.source_range, space_length)
add_offense(nil, location: range)
end
alias on_csend on_send
private
def spaces_before_left_parenthesis(node)
receiver = node.receiver
receiver_length = if receiver
receiver.source.length
else
0
end
without_receiver = node.source[receiver_length..-1]
# Escape question mark if any.
method_regexp = Regexp.escape(node.method_name)
match = without_receiver.match(/^\s*&?\.?\s*#{method_regexp}(\s+)\(/)
match ? match.captures[0].length : 0
end
def space_range(expr, space_length)
range_between(expr.begin_pos - space_length, expr.begin_pos)
end
end
end
end
end