forked from rubocop/rubocop
/
redundant_parentheses.rb
231 lines (181 loc) · 6.76 KB
/
redundant_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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# frozen_string_literal: true
module RuboCop
module Cop
module Style
# This cop checks for redundant parentheses.
#
# @example
#
# # bad
# (x) if ((y.z).nil?)
#
# # good
# x if y.z.nil?
#
class RedundantParentheses < Cop
include Parentheses
def_node_matcher :square_brackets?,
'(send {(send _recv _msg) str array hash} :[] ...)'
def_node_matcher :range_end?, '^^{irange erange}'
def_node_matcher :method_node_and_args, '$(call _recv _msg $...)'
def_node_matcher :rescue?, '{^resbody ^^resbody}'
def_node_matcher :arg_in_call_with_block?,
'^^(block (send _ _ equal?(%0) ...) ...)'
def on_begin(node)
return if !parentheses?(node) || parens_allowed?(node)
return if node.parent && (node.parent.while_post_type? ||
node.parent.until_post_type?)
check(node)
end
def autocorrect(node)
ParenthesesCorrector.correct(node)
end
private
def parens_allowed?(node)
empty_parentheses?(node) ||
first_arg_begins_with_hash_literal?(node) ||
rescue?(node) ||
allowed_expression?(node)
end
def allowed_expression?(node)
allowed_ancestor?(node) ||
allowed_method_call?(node) ||
allowed_array_or_hash_element?(node) ||
allowed_multiple_expression?(node)
end
def allowed_ancestor?(node)
# Don't flag `break(1)`, etc
keyword_ancestor?(node) && parens_required?(node)
end
def allowed_method_call?(node)
# Don't flag `method (arg) { }`
arg_in_call_with_block?(node) && !parentheses?(node.parent)
end
def allowed_multiple_expression?(node)
return false if node.children.one?
ancestor = node.ancestors.first
return false unless ancestor
!ancestor.begin_type? && !ancestor.def_type? && !ancestor.block_type?
end
def empty_parentheses?(node)
# Don't flag `()`
node.children.empty?
end
def first_arg_begins_with_hash_literal?(node)
# Don't flag `method ({key: value})` or `method ({key: value}.method)`
method_chain_begins_with_hash_literal?(node.children.first) &&
first_argument?(node) &&
!parentheses?(node.parent)
end
def method_chain_begins_with_hash_literal?(node)
return false if node.nil?
return true if node.hash_type?
return false unless node.send_type?
method_chain_begins_with_hash_literal?(node.children.first)
end
def check(begin_node)
node = begin_node.children.first
if keyword_with_redundant_parentheses?(node)
return offense(begin_node, 'a keyword')
end
if disallowed_literal?(begin_node, node)
return offense(begin_node, 'a literal')
end
return offense(begin_node, 'a variable') if node.variable?
return offense(begin_node, 'a constant') if node.const_type?
check_send(begin_node, node) if node.call_type?
end
def check_send(begin_node, node)
return check_unary(begin_node, node) if node.unary_operation?
return unless method_call_with_redundant_parentheses?(node)
return if call_chain_starts_with_int?(begin_node, node)
offense(begin_node, 'a method call')
end
def check_unary(begin_node, node)
return if begin_node.chained?
node = node.children.first while suspect_unary?(node)
if node.send_type?
return unless method_call_with_redundant_parentheses?(node)
end
offense(begin_node, 'an unary operation')
end
def offense(node, msg)
add_offense(node, message: "Don't use parentheses around #{msg}.")
end
def suspect_unary?(node)
node.send_type? && node.unary_operation? && !node.prefix_not?
end
def keyword_ancestor?(node)
node.parent&.keyword?
end
def allowed_array_or_hash_element?(node)
# Don't flag
# ```
# { a: (1
# ), }
# ```
(hash_element?(node) || array_element?(node)) &&
only_closing_paren_before_comma?(node)
end
def hash_element?(node)
node.parent&.pair_type?
end
def array_element?(node)
node.parent&.array_type?
end
def only_closing_paren_before_comma?(node)
source_buffer = node.source_range.source_buffer
line_range = source_buffer.line_range(node.loc.end.line)
line_range.source =~ /^\s*\)\s*,/
end
def disallowed_literal?(begin_node, node)
node.literal? &&
!node.range_type? &&
!raised_to_power_negative_numeric?(begin_node, node)
end
def raised_to_power_negative_numeric?(begin_node, node)
return false unless node.numeric_type?
siblings = begin_node.parent&.children
return false if siblings.nil?
next_sibling = siblings[begin_node.sibling_index + 1]
base_value = node.children.first
base_value.negative? && next_sibling == :**
end
def keyword_with_redundant_parentheses?(node)
return false unless node.keyword?
return true if node.special_keyword?
args = *node
if only_begin_arg?(args)
parentheses?(args.first)
else
args.empty? || parentheses?(node)
end
end
def method_call_with_redundant_parentheses?(node)
return false unless node.call_type?
return false if node.prefix_not?
return false if range_end?(node)
send_node, args = method_node_and_args(node)
args.empty? || parentheses?(send_node) || square_brackets?(send_node)
end
def only_begin_arg?(args)
args.one? && args.first.begin_type?
end
def first_argument?(node)
first_send_argument?(node) || first_super_argument?(node)
end
def_node_matcher :first_send_argument?, <<~PATTERN
^(send _ _ equal?(%0) ...)
PATTERN
def_node_matcher :first_super_argument?, <<~PATTERN
^(super equal?(%0) ...)
PATTERN
def call_chain_starts_with_int?(begin_node, send_node)
recv = first_part_of_call_chain(send_node)
recv&.int_type? && (parent = begin_node.parent) &&
parent.send_type? && (parent.method?(:-@) || parent.method?(:+@))
end
end
end
end
end