forked from rubocop/rubocop
/
branch.rb
317 lines (259 loc) · 8.12 KB
/
branch.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# frozen_string_literal: true
module RuboCop
module Cop
class VariableForce
# Namespace for branch classes for each control structure.
module Branch
def self.of(target_node, scope: nil)
([target_node] + target_node.ancestors).each do |node|
return nil unless node.parent
return nil unless scope.include?(node)
klass = CLASSES_BY_TYPE[node.parent.type]
next unless klass
branch = klass.new(node, scope)
return branch if branch.branched?
end
nil
end
# Abstract base class for branch classes.
# A branch represents a conditional branch in a scope.
#
# @example
# def some_scope
# do_something # no branch
#
# if foo
# do_something # branch A
# do_something # branch A
# else
# do_something # branch B
# if bar
# do_something # branch C (whose parent is branch B)
# end
# end
#
# do_something # no branch
# end
Base = Struct.new(:child_node, :scope) do
def self.classes
@classes ||= []
end
def self.inherited(subclass)
super
classes << subclass
end
def self.type
name.split('::').last.gsub(/(.)([A-Z])/, '\1_\2').downcase.to_sym
end
def self.define_predicate(name, child_index: nil)
define_method(name) do
target_node = control_node.children[child_index]
# We don't use Kernel#Array here
# because it invokes Node#to_a rather than wrapping with an array.
if target_node.is_a?(Array)
target_node.any? { |node| node.equal?(child_node) }
else
target_node.equal?(child_node)
end
end
end
def control_node
child_node.parent
end
def parent
return @parent if instance_variable_defined?(:@parent)
@branch = Branch.of(control_node, scope: scope)
end
def each_ancestor(include_self: false, &block)
return to_enum(__method__, include_self: include_self) unless block_given?
yield self if include_self
scan_ancestors(&block)
self
end
def branched?
!always_run?
end
def always_run?
raise NotImplementedError
end
def may_jump_to_other_branch?
false
end
def may_run_incompletely?
false
end
def exclusive_with?(other)
return false unless other
return false if may_jump_to_other_branch?
other.each_ancestor(include_self: true) do |other_ancestor|
if control_node.equal?(other_ancestor.control_node)
return !child_node.equal?(other_ancestor.child_node)
end
end
if parent
parent.exclusive_with?(other)
else
false
end
end
def ==(other)
return false unless other
control_node.equal?(other.control_node) &&
child_node.equal?(other.child_node)
end
alias_method :eql?, :==
def hash
control_node.object_id.hash ^ child_node.object_id.hash
end
private
def scan_ancestors
branch = self
while (branch = branch.parent)
yield branch
end
end
end
# Mix-in module for simple conditional control structures.
module SimpleConditional
def conditional_clause?
raise NotImplementedError
end
def always_run?
conditional_clause?
end
end
# if conditional_clause
# truthy_body
# else
# falsey_body
# end
#
# unless conditional_clause
# falsey_body
# else
# truthy_body
# end
class If < Base
include SimpleConditional
define_predicate :conditional_clause?, child_index: 0
define_predicate :truthy_body?, child_index: 1
define_predicate :falsey_body?, child_index: 2
end
# while conditional_clause
# loop_body
# end
class While < Base
include SimpleConditional
define_predicate :conditional_clause?, child_index: 0
define_predicate :loop_body?, child_index: 1
end
# until conditional_clause
# loop_body
# end
class Until < Base
include SimpleConditional
define_predicate :conditional_clause?, child_index: 0
define_predicate :loop_body?, child_index: 1
end
# begin
# loop_body
# end while conditional_clause
class WhilePost < Base
include SimpleConditional
define_predicate :conditional_clause?, child_index: 0
define_predicate :loop_body?, child_index: 1
end
# begin
# loop_body
# end until conditional_clause
class UntilPost < Base
include SimpleConditional
define_predicate :conditional_clause?, child_index: 0
define_predicate :loop_body?, child_index: 1
end
# case target
# when /pattern/ # when_clause
# else
# else_body
# end
class Case < Base
define_predicate :target?, child_index: 0
define_predicate :when_clause?, child_index: 1..-2
define_predicate :else_body?, child_index: -1
def always_run?
target?
end
end
# for element in collection
# loop_body
# end
class For < Base
define_predicate :element?, child_index: 0
define_predicate :collection?, child_index: 1
define_predicate :loop_body?, child_index: 2
def always_run?
element? || collection?
end
end
# Mix-in module for logical operator control structures.
module LogicalOperator
def always_run?
left_body?
end
end
# left_body && right_body
class And < Base
include LogicalOperator
define_predicate :left_body?, child_index: 0
define_predicate :right_body?, child_index: 1
end
# left_body || right_body
class Or < Base
include LogicalOperator
define_predicate :left_body?, child_index: 0
define_predicate :right_body?, child_index: 1
end
# Mix-in module for exception handling control structures.
module ExceptionHandler
def may_jump_to_other_branch?
main_body?
end
def may_run_incompletely?
main_body?
end
end
# begin
# main_body
# rescue StandardError => error # rescue_clause
# else
# else_body
# end
class Rescue < Base
include ExceptionHandler
define_predicate :main_body?, child_index: 0
define_predicate :rescue_clause?, child_index: 1..-2
define_predicate :else_body?, child_index: -1
def always_run?
false
end
end
# begin
# main_body
# ensure
# ensure_body
# end
class Ensure < Base
include ExceptionHandler
define_predicate :main_body?, child_index: 0
define_predicate :ensure_body?, child_index: -1
def always_run?
ensure_body?
end
end
CLASSES_BY_TYPE = Base.classes.each_with_object({}) do |klass, classes|
classes[klass.type] = klass
end
end
end
end
end