/
omit_parentheses.rb
179 lines (148 loc) · 5.9 KB
/
omit_parentheses.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# frozen_string_literal: true
module RuboCop
module Cop
module Style
class MethodCallWithArgsParentheses
# Style omit_parentheses
module OmitParentheses
TRAILING_WHITESPACE_REGEX = /\s+\Z/.freeze
OMIT_MSG = 'Omit parentheses for method calls with arguments.'
private_constant :OMIT_MSG
private
def omit_parentheses(node)
return unless node.parenthesized?
return if inside_endless_method_def?(node)
return if node.implicit_call?
return if super_call_without_arguments?(node)
return if allowed_camel_case_method_call?(node)
return if legitimate_call_with_parentheses?(node)
add_offense(offense_range(node), message: OMIT_MSG) do |corrector|
auto_correct(corrector, node)
end
end
def auto_correct(corrector, node)
if parentheses_at_the_end_of_multiline_call?(node)
corrector.replace(args_begin(node), ' \\')
else
corrector.replace(args_begin(node), ' ')
end
corrector.remove(node.loc.end)
end
def offense_range(node)
node.loc.begin.join(node.loc.end)
end
def inside_endless_method_def?(node)
# parens are required around arguments inside an endless method
node.each_ancestor(:def).any?(&:endless?) && node.arguments.any?
end
def super_call_without_arguments?(node)
node.super_type? && node.arguments.none?
end
def allowed_camel_case_method_call?(node)
node.camel_case_method? &&
(node.arguments.none? ||
cop_config['AllowParenthesesInCamelCaseMethod'])
end
def parentheses_at_the_end_of_multiline_call?(node)
node.multiline? &&
node.loc.begin.source_line
.gsub(TRAILING_WHITESPACE_REGEX, '')
.end_with?('(')
end
def legitimate_call_with_parentheses?(node)
call_in_literals?(node) ||
call_with_ambiguous_arguments?(node) ||
call_in_logical_operators?(node) ||
call_in_optional_arguments?(node) ||
call_in_single_line_inheritance?(node) ||
allowed_multiline_call_with_parentheses?(node) ||
allowed_chained_call_with_parentheses?(node)
end
def call_in_literals?(node)
node.parent &&
(node.parent.pair_type? ||
node.parent.array_type? ||
node.parent.range_type? ||
splat?(node.parent) ||
ternary_if?(node.parent))
end
def call_in_logical_operators?(node)
parent = node.parent&.block_type? ? node.parent.parent : node.parent
parent &&
(logical_operator?(parent) ||
parent.send_type? &&
parent.arguments.any? { |argument| logical_operator?(argument) })
end
def call_in_optional_arguments?(node)
node.parent &&
(node.parent.optarg_type? || node.parent.kwoptarg_type?)
end
def call_in_single_line_inheritance?(node)
node.parent&.class_type? && node.parent&.single_line?
end
def call_with_ambiguous_arguments?(node)
call_with_braced_block?(node) ||
call_as_argument_or_chain?(node) ||
hash_literal_in_arguments?(node) ||
node.descendants.any? do |n|
ambigious_literal?(n) || logical_operator?(n) ||
call_with_braced_block?(n)
end
end
def call_with_braced_block?(node)
(node.send_type? || node.super_type?) &&
node.block_node && node.block_node.braces?
end
def call_as_argument_or_chain?(node)
node.parent &&
(node.parent.send_type? && !assigned_before?(node.parent, node) ||
node.parent.csend_type? || node.parent.super_type? || node.parent.yield_type?)
end
def hash_literal_in_arguments?(node)
node.arguments.any? do |n|
hash_literal?(n) ||
n.send_type? && node.descendants.any? { |descendant| hash_literal?(descendant) }
end
end
def allowed_multiline_call_with_parentheses?(node)
cop_config['AllowParenthesesInMultilineCall'] && node.multiline?
end
def allowed_chained_call_with_parentheses?(node)
return false unless cop_config['AllowParenthesesInChaining']
previous = node.descendants.first
return false unless previous&.send_type?
previous.parenthesized? ||
allowed_chained_call_with_parentheses?(previous)
end
def ambigious_literal?(node)
splat?(node) || ternary_if?(node) || regexp_slash_literal?(node) ||
unary_literal?(node)
end
def splat?(node)
node.splat_type? || node.kwsplat_type? || node.block_pass_type?
end
def ternary_if?(node)
node.if_type? && node.ternary?
end
def logical_operator?(node)
(node.and_type? || node.or_type?) && node.logical_operator?
end
def hash_literal?(node)
node.hash_type? && node.braces?
end
def regexp_slash_literal?(node)
node.regexp_type? && node.loc.begin.source == '/'
end
def unary_literal?(node)
node.numeric_type? && node.sign? ||
node.parent&.send_type? && node.parent&.unary_operation?
end
def assigned_before?(node, target)
node.assignment? &&
node.loc.operator.begin < target.loc.begin
end
end
end
end
end
end