/
conditional_assignment.rb
675 lines (551 loc) · 20.5 KB
/
conditional_assignment.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
# frozen_string_literal: true
module RuboCop
module Cop
module Style
# Helper module to provide common methods to classes needed for the
# ConditionalAssignment Cop.
module ConditionalAssignmentHelper
extend NodePattern::Macros
EQUAL = '='
END_ALIGNMENT = 'Layout/EndAlignment'
ALIGN_WITH = 'EnforcedStyleAlignWith'
KEYWORD = 'keyword'
# `elsif` branches show up in the `node` as an `else`. We need
# to recursively iterate over all `else` branches and consider all
# but the last `node` an `elsif` branch and consider the last `node`
# the actual `else` branch.
def expand_elses(branch)
elsif_branches = expand_elsif(branch)
else_branch = elsif_branches.any? ? elsif_branches.pop : branch
[elsif_branches, else_branch]
end
# `when` nodes contain the entire branch including the condition.
# We only need the contents of the branch, not the condition.
def expand_when_branches(when_branches)
when_branches.map { |branch| branch.children[1] }
end
def tail(branch)
branch.begin_type? ? Array(branch).last : branch
end
# rubocop:disable Metrics/AbcSize
def lhs(node)
case node.type
when :send
lhs_for_send(node)
when :op_asgn
"#{node.children[0].source} #{node.children[1]}= "
when :and_asgn, :or_asgn
"#{node.children[0].source} #{node.loc.operator.source} "
when :casgn
lhs_for_casgn(node)
when *ConditionalAssignment::VARIABLE_ASSIGNMENT_TYPES
"#{node.children[0]} = "
else
node.source
end
end
# rubocop:enable Metrics/AbcSize
def indent(cop, source)
conf = cop.config.for_cop(END_ALIGNMENT)
if conf[ALIGN_WITH] == KEYWORD
' ' * source.length
else
''
end
end
def end_with_eq?(sym)
sym.to_s.end_with?(EQUAL)
end
private
def expand_elsif(node, elsif_branches = []) # rubocop:todo Metrics/CyclomaticComplexity
return [] if node.nil? || !node.if_type? || !node.elsif?
elsif_branches << node.if_branch
else_branch = node.else_branch
if else_branch&.if_type? && else_branch&.elsif?
expand_elsif(else_branch, elsif_branches)
else
elsif_branches << else_branch
end
end
def lhs_for_send(node)
receiver = node.receiver ? node.receiver.source : ''
if node.method?(:[]=)
indices = node.arguments[0...-1].map(&:source).join(', ')
"#{receiver}[#{indices}] = "
elsif node.setter_method?
"#{receiver}.#{node.method_name[0...-1]} = "
else
"#{receiver} #{node.method_name} "
end
end
def lhs_for_casgn(node)
namespace = node.children[0]
if namespace.nil? || namespace.cbase_type?
"#{namespace&.source}#{node.children[1]} = "
else
"#{namespace.source}::#{node.children[1]} = "
end
end
def setter_method?(method_name)
method_name.to_s.end_with?(EQUAL) &&
!%i[!= == === >= <=].include?(method_name)
end
def assignment_rhs_exist?(node)
parent = node.parent
return true unless parent
!(parent.mlhs_type? || parent.resbody_type?)
end
end
# Check for `if` and `case` statements where each branch is used for
# assignment to the same variable when using the return of the
# condition can be used instead.
#
# @example EnforcedStyle: assign_to_condition (default)
# # bad
# if foo
# bar = 1
# else
# bar = 2
# end
#
# case foo
# when 'a'
# bar += 1
# else
# bar += 2
# end
#
# if foo
# some_method
# bar = 1
# else
# some_other_method
# bar = 2
# end
#
# # good
# bar = if foo
# 1
# else
# 2
# end
#
# bar += case foo
# when 'a'
# 1
# else
# 2
# end
#
# bar << if foo
# some_method
# 1
# else
# some_other_method
# 2
# end
#
# @example EnforcedStyle: assign_inside_condition
# # bad
# bar = if foo
# 1
# else
# 2
# end
#
# bar += case foo
# when 'a'
# 1
# else
# 2
# end
#
# bar << if foo
# some_method
# 1
# else
# some_other_method
# 2
# end
#
# # good
# if foo
# bar = 1
# else
# bar = 2
# end
#
# case foo
# when 'a'
# bar += 1
# else
# bar += 2
# end
#
# if foo
# some_method
# bar = 1
# else
# some_other_method
# bar = 2
# end
class ConditionalAssignment < Cop
include ConditionalAssignmentHelper
include ConfigurableEnforcedStyle
include IgnoredNode
MSG = 'Use the return of the conditional for variable assignment ' \
'and comparison.'
ASSIGN_TO_CONDITION_MSG =
'Assign variables inside of conditionals'
VARIABLE_ASSIGNMENT_TYPES =
%i[casgn cvasgn gvasgn ivasgn lvasgn].freeze
ASSIGNMENT_TYPES = VARIABLE_ASSIGNMENT_TYPES +
%i[and_asgn or_asgn op_asgn masgn].freeze
LINE_LENGTH = 'Layout/LineLength'
INDENTATION_WIDTH = 'Layout/IndentationWidth'
ENABLED = 'Enabled'
MAX = 'Max'
SINGLE_LINE_CONDITIONS_ONLY = 'SingleLineConditionsOnly'
WIDTH = 'Width'
# The shovel operator `<<` does not have its own type. It is a `send`
# type.
def_node_matcher :assignment_type?, <<~PATTERN
{
#{ASSIGNMENT_TYPES.join(' ')}
(send _recv {:[]= :<< :=~ :!~ :<=> #end_with_eq?} ...)
}
PATTERN
ASSIGNMENT_TYPES.each do |type|
define_method "on_#{type}" do |node|
return if part_of_ignored_node?(node)
check_assignment_to_condition(node)
end
end
def on_send(node)
return unless assignment_type?(node)
check_assignment_to_condition(node)
end
def on_if(node)
return unless style == :assign_to_condition
return if node.elsif?
else_branch = node.else_branch
elsif_branches, else_branch = expand_elses(else_branch)
return unless else_branch
branches = [node.if_branch, *elsif_branches, else_branch]
check_node(node, branches)
end
def on_case(node)
return unless style == :assign_to_condition
return unless node.else_branch
when_branches = expand_when_branches(node.when_branches)
branches = [*when_branches, node.else_branch]
check_node(node, branches)
end
def autocorrect(node)
if assignment_type?(node)
move_assignment_inside_condition(node)
else
move_assignment_outside_condition(node)
end
end
private
def check_assignment_to_condition(node)
return unless candidate_node?(node)
ignore_node(node)
assignment = assignment_node(node)
return unless candidate_condition?(assignment)
_condition, *branches, else_branch = *assignment
return unless else_branch
return if allowed_single_line?([*branches, else_branch])
add_offense(node, message: ASSIGN_TO_CONDITION_MSG)
end
def candidate_node?(node)
style == :assign_inside_condition && assignment_rhs_exist?(node)
end
def_node_matcher :candidate_condition?, '[{if case} !#allowed_ternary?]'
def allowed_ternary?(assignment)
assignment.if_type? && assignment.ternary? && !include_ternary?
end
def allowed_single_line?(branches)
single_line_conditions_only? && branches.any?(&:begin_type?)
end
def assignment_node(node)
*_variable, assignment = *node
# ignore pseudo-assignments without rhs in for nodes
return if node.parent&.for_type?
assignment, = *assignment if assignment.begin_type? && assignment.children.one?
assignment
end
def move_assignment_outside_condition(node)
if node.case_type?
CaseCorrector.correct(self, node)
elsif node.ternary?
TernaryCorrector.correct(node)
elsif node.if? || node.unless?
IfCorrector.correct(self, node)
end
end
def move_assignment_inside_condition(node)
*_assignment, condition = *node
if ternary_condition?(condition)
TernaryCorrector.move_assignment_inside_condition(node)
elsif condition.case_type?
CaseCorrector.move_assignment_inside_condition(node)
elsif condition.if_type?
IfCorrector.move_assignment_inside_condition(node)
end
end
def ternary_condition?(node)
[node, node.children.first].any? { |n| n.if_type? && n.ternary? }
end
def lhs_all_match?(branches)
return true if branches.empty?
first_lhs = lhs(branches.first)
branches.all? { |branch| lhs(branch) == first_lhs }
end
def assignment_types_match?(*nodes)
return unless assignment_type?(nodes.first)
nodes.map(&:type).uniq.one?
end
def check_node(node, branches)
return if allowed_ternary?(node)
return unless allowed_statements?(branches)
return if allowed_single_line?(branches)
return if correction_exceeds_line_limit?(node, branches)
add_offense(node)
end
def allowed_statements?(branches)
return false unless branches.all?
statements = branches.map { |branch| tail(branch) }.compact
lhs_all_match?(statements) && statements.none?(&:masgn_type?) &&
assignment_types_match?(*statements)
end
# If `Layout/LineLength` is enabled, we do not want to introduce an
# offense by auto-correcting this cop. Find the max configured line
# length. Find the longest line of condition. Remove the assignment
# from lines that contain the offending assignment because after
# correcting, this will not be on the line anymore. Check if the length
# of the longest line + the length of the corrected assignment is
# greater than the max configured line length
def correction_exceeds_line_limit?(node, branches)
return false unless line_length_cop_enabled?
assignment = lhs(tail(branches[0]))
longest_line_exceeds_line_limit?(node, assignment)
end
def longest_line_exceeds_line_limit?(node, assignment)
longest_line(node, assignment).length > max_line_length
end
def longest_line(node, assignment)
assignment_regex = /\s*#{Regexp.escape(assignment).gsub('\ ', '\s*')}/
lines = node.source.lines.map do |line|
line.chomp.sub(assignment_regex, '')
end
longest_line = lines.max_by(&:length)
assignment + longest_line
end
def line_length_cop_enabled?
config.for_cop(LINE_LENGTH)[ENABLED]
end
def max_line_length
config.for_cop(LINE_LENGTH)[MAX]
end
def indentation_width
config.for_cop(INDENTATION_WIDTH)[WIDTH] || 2
end
def single_line_conditions_only?
cop_config[SINGLE_LINE_CONDITIONS_ONLY]
end
def include_ternary?
cop_config['IncludeTernaryExpressions']
end
end
# Helper module to provide common methods to ConditionalAssignment
# correctors
module ConditionalCorrectorHelper
def remove_whitespace_in_branches(corrector, branch, condition, column)
branch.each_node do |child|
white_space = white_space_range(child, column)
corrector.remove(white_space) if white_space.source.strip.empty?
end
[condition.loc.else, condition.loc.end].each do |loc|
corrector.remove_preceding(loc, loc.column - column)
end
end
def white_space_range(node, column)
expression = node.loc.expression
begin_pos = expression.begin_pos - (expression.column - column - 2)
Parser::Source::Range.new(expression.source_buffer,
begin_pos,
expression.begin_pos)
end
def assignment(node)
*_, condition = *node
Parser::Source::Range.new(node.loc.expression.source_buffer,
node.loc.expression.begin_pos,
condition.loc.expression.begin_pos)
end
def correct_if_branches(corrector, cop, node)
if_branch, elsif_branches, else_branch = extract_tail_branches(node)
corrector.insert_before(node, lhs(if_branch))
replace_branch_assignment(corrector, if_branch)
correct_branches(corrector, elsif_branches)
replace_branch_assignment(corrector, else_branch)
corrector.insert_before(node.loc.end, indent(cop, lhs(if_branch)))
end
def replace_branch_assignment(corrector, branch)
_variable, *_operator, assignment = *branch
source = assignment.source
replacement = if assignment.array_type? && !assignment.bracketed?
"[#{source}]"
else
source
end
corrector.replace(branch, replacement)
end
def correct_branches(corrector, branches)
branches.each do |branch|
*_, assignment = *branch
corrector.replace(branch, assignment.source)
end
end
end
# Corrector to correct conditional assignment in ternary conditions.
class TernaryCorrector
class << self
include ConditionalAssignmentHelper
include ConditionalCorrectorHelper
def correct(node)
lambda do |corrector|
corrector.replace(node, correction(node))
end
end
def move_assignment_inside_condition(node)
*_var, rhs = *node
if_branch, else_branch = extract_branches(node)
assignment = assignment(node)
lambda do |corrector|
remove_parentheses(corrector, rhs) if Util.parentheses?(rhs)
corrector.remove(assignment)
move_branch_inside_condition(corrector, if_branch, assignment)
move_branch_inside_condition(corrector, else_branch, assignment)
end
end
private
def correction(node)
"#{lhs(node.if_branch)}#{ternary(node)}"
end
def ternary(node)
_variable, *_operator, if_rhs = *node.if_branch
_else_variable, *_operator, else_rhs = *node.else_branch
expr = "#{node.condition.source} ? " \
"#{if_rhs.source} : " \
"#{else_rhs.source}"
element_assignment?(node.if_branch) ? "(#{expr})" : expr
end
def element_assignment?(node)
node.send_type? && node.method_name != :[]=
end
def extract_branches(node)
*_var, rhs = *node
condition, = *rhs if rhs.begin_type? && rhs.children.one?
_condition, if_branch, else_branch = *(condition || rhs)
[if_branch, else_branch]
end
def remove_parentheses(corrector, node)
corrector.remove(node.loc.begin)
corrector.remove(node.loc.end)
end
def move_branch_inside_condition(corrector, branch, assignment)
corrector.insert_before(branch, assignment.source)
end
end
end
# Corrector to correct conditional assignment in `if` statements.
class IfCorrector
class << self
include ConditionalAssignmentHelper
include ConditionalCorrectorHelper
def correct(cop, node)
->(corrector) { correct_if_branches(corrector, cop, node) }
end
def move_assignment_inside_condition(node)
column = node.loc.expression.column
*_var, condition = *node
assignment = assignment(node)
lambda do |corrector|
corrector.remove(assignment)
condition.branches.flatten.each do |branch|
move_branch_inside_condition(corrector, branch, condition,
assignment, column)
end
end
end
private
def extract_tail_branches(node)
if_branch, *elsif_branches, else_branch = *node.branches
elsif_branches.map! { |branch| tail(branch) }
[tail(if_branch), elsif_branches, tail(else_branch)]
end
def move_branch_inside_condition(corrector, branch, condition,
assignment, column)
branch_assignment = tail(branch)
corrector.insert_before(branch_assignment,
assignment.source)
remove_whitespace_in_branches(corrector, branch, condition, column)
return unless (branch_else = branch.parent.loc.else)
corrector.remove_preceding(branch_else, branch_else.column - column)
end
end
end
# Corrector to correct conditional assignment in `case` statements.
class CaseCorrector
class << self
include ConditionalAssignmentHelper
include ConditionalCorrectorHelper
def correct(cop, node)
when_branches, else_branch = extract_tail_branches(node)
lambda do |corrector|
corrector.insert_before(node, lhs(else_branch))
correct_branches(corrector, when_branches)
replace_branch_assignment(corrector, else_branch)
corrector.insert_before(node.loc.end,
indent(cop, lhs(else_branch)))
end
end
def move_assignment_inside_condition(node)
column = node.loc.expression.column
*_var, condition = *node
assignment = assignment(node)
lambda do |corrector|
corrector.remove(assignment)
extract_branches(condition).flatten.each do |branch|
move_branch_inside_condition(corrector, branch, condition,
assignment, column)
end
end
end
private
def extract_tail_branches(node)
when_branches, else_branch = extract_branches(node)
when_branches.map! { |branch| tail(branch) }
[when_branches, tail(else_branch)]
end
def extract_branches(case_node)
when_branches = expand_when_branches(case_node.when_branches)
[when_branches, case_node.else_branch]
end
def move_branch_inside_condition(corrector, branch, condition,
assignment, column)
branch_assignment = tail(branch)
corrector.insert_before(branch_assignment,
assignment.source)
remove_whitespace_in_branches(corrector, branch, condition, column)
parent_keyword = branch.parent.loc.keyword
corrector.remove_preceding(parent_keyword,
parent_keyword.column - column)
end
end
end
end
end
end