-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
case_like_if.rb
236 lines (201 loc) · 6.63 KB
/
case_like_if.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
232
233
234
235
236
# frozen_string_literal: true
module RuboCop
module Cop
module Style
# This cop identifies places where `if-elsif` constructions
# can be replaced with `case-when`.
#
# @example
# # bad
# if status == :active
# perform_action
# elsif status == :inactive || status == :hibernating
# check_timeout
# else
# final_action
# end
#
# # good
# case status
# when :active
# perform_action
# when :inactive, :hibernating
# check_timeout
# else
# final_action
# end
#
class CaseLikeIf < Base
include RangeHelp
extend AutoCorrector
MSG = 'Convert `if-elsif` to `case-when`.'
def on_if(node)
return unless should_check?(node)
target = find_target(node.condition)
return unless target
conditions = []
convertible = true
branch_conditions(node).each do |branch_condition|
conditions << []
convertible = collect_conditions(branch_condition, target, conditions.last)
break unless convertible
end
return unless convertible
add_offense(node) do |corrector|
autocorrect(corrector, node)
end
end
private
def autocorrect(corrector, node)
target = find_target(node.condition)
corrector.insert_before(node, "case #{target.source}\n#{indent(node)}")
branch_conditions(node).each do |branch_condition|
conditions = []
collect_conditions(branch_condition, target, conditions)
range = correction_range(branch_condition)
branch_replacement = "when #{conditions.map(&:source).join(', ')}"
corrector.replace(range, branch_replacement)
end
end
def should_check?(node)
!node.unless? && !node.elsif? && !node.modifier_form? && !node.ternary? &&
node.elsif_conditional?
end
# rubocop:disable Metrics/MethodLength
def find_target(node)
case node.type
when :begin
find_target(node.children.first)
when :or
find_target(node.lhs)
when :match_with_lvasgn
lhs, rhs = *node
if lhs.regexp_type?
rhs
elsif rhs.regexp_type?
lhs
end
when :send
find_target_in_send_node(node)
end
end
# rubocop:enable Metrics/MethodLength
def find_target_in_send_node(node)
case node.method_name
when :is_a?
node.receiver
when :==, :eql?, :equal?
find_target_in_equality_node(node)
when :===
node.arguments.first
when :include?, :cover?
receiver = deparenthesize(node.receiver)
node.arguments.first if receiver.range_type?
when :match, :match?
find_target_in_match_node(node)
end
end
def find_target_in_equality_node(node)
argument = node.arguments.first
receiver = node.receiver
return unless receiver
if argument.literal? || const_reference?(argument)
receiver
elsif receiver.literal? || const_reference?(receiver)
argument
end
end
def find_target_in_match_node(node)
argument = node.arguments.first
receiver = node.receiver
return unless receiver
if receiver.regexp_type?
argument
elsif argument.regexp_type?
receiver
end
end
def collect_conditions(node, target, conditions)
condition =
case node.type
when :begin
return collect_conditions(node.children.first, target, conditions)
when :or
return collect_conditions(node.lhs, target, conditions) &&
collect_conditions(node.rhs, target, conditions)
when :match_with_lvasgn
lhs, rhs = *node
condition_from_binary_op(lhs, rhs, target)
when :send
condition_from_send_node(node, target)
end
conditions << condition if condition
end
# rubocop:disable Metrics/CyclomaticComplexity
def condition_from_send_node(node, target)
case node.method_name
when :is_a?
node.arguments.first if node.receiver == target
when :==, :eql?, :equal?
condition_from_equality_node(node, target)
when :=~, :match, :match?
condition_from_match_node(node, target)
when :===
lhs, _method, rhs = *node
lhs if rhs == target
when :include?, :cover?
receiver = deparenthesize(node.receiver)
receiver if receiver.range_type? && node.arguments.first == target
end
end
# rubocop:enable Metrics/CyclomaticComplexity
def condition_from_equality_node(node, target)
lhs, _method, rhs = *node
condition = condition_from_binary_op(lhs, rhs, target)
condition if condition && !class_reference?(condition)
end
def condition_from_match_node(node, target)
lhs, _method, rhs = *node
condition_from_binary_op(lhs, rhs, target)
end
def condition_from_binary_op(lhs, rhs, target)
lhs = deparenthesize(lhs)
rhs = deparenthesize(rhs)
if lhs == target
rhs
elsif rhs == target
lhs
end
end
def branch_conditions(node)
conditions = []
while node&.if_type?
conditions << node.condition
node = node.else_branch
end
conditions
end
def const_reference?(node)
return false unless node.const_type?
name = node.children[1].to_s
# We can no be sure if, e.g. `C`, represents a constant or a class reference
name.length > 1 &&
name == name.upcase
end
def class_reference?(node)
node.const_type? && node.children[1].match?(/[[:lower:]]/)
end
def deparenthesize(node)
node = node.children.last while node.begin_type?
node
end
def correction_range(node)
range_between(node.parent.loc.keyword.begin_pos, node.loc.expression.end_pos)
end
def indent(node)
' ' * node.loc.column
end
end
end
end
end