-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
assignment_indentation.rb
55 lines (47 loc) · 1.45 KB
/
assignment_indentation.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
# frozen_string_literal: true
module RuboCop
module Cop
module Layout
# This cop checks the indentation of the first line of the
# right-hand-side of a multi-line assignment.
#
# @example
# # bad
# value =
# if foo
# 'bar'
# end
#
# # good
# value =
# if foo
# 'bar'
# end
#
# The indentation of the remaining lines can be corrected with
# other cops such as `IndentationConsistency` and `EndAlignment`.
class AssignmentIndentation < Cop
include CheckAssignment
include Alignment
MSG = 'Indent the first line of the right-hand-side of a ' \
'multi-line assignment.'
def check_assignment(node, rhs)
return unless rhs
return unless node.loc.operator
return if node.loc.operator.line == rhs.first_line
base = display_column(leftmost_multiple_assignment(node).source_range)
check_alignment([rhs], base + configured_indentation_width)
end
def autocorrect(node)
AlignmentCorrector.correct(processed_source, node, column_delta)
end
def leftmost_multiple_assignment(node)
return node unless same_line?(node, node.parent) &&
node.parent.assignment?
leftmost_multiple_assignment(node.parent)
node.parent
end
end
end
end
end