/
closing_parenthesis_indentation.rb
201 lines (175 loc) · 5.46 KB
/
closing_parenthesis_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
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
# frozen_string_literal: true
module RuboCop
module Cop
module Layout
# This cop checks the indentation of hanging closing parentheses in
# method calls, method definitions, and grouped expressions. A hanging
# closing parenthesis means `)` preceded by a line break.
#
# @example
#
# # bad
# some_method(
# a,
# b
# )
#
# some_method(
# a, b
# )
#
# some_method(a, b, c
# )
#
# some_method(a,
# b,
# c
# )
#
# some_method(a,
# x: 1,
# y: 2
# )
#
# # Scenario 1: When First Parameter Is On Its Own Line
#
# # good: when first param is on a new line, right paren is *always*
# # outdented by IndentationWidth
# some_method(
# a,
# b
# )
#
# # good
# some_method(
# a, b
# )
#
# # Scenario 2: When First Parameter Is On The Same Line
#
# # good: when all other params are also on the same line, outdent
# # right paren by IndentationWidth
# some_method(a, b, c
# )
#
# # good: when all other params are on multiple lines, but are lined
# # up, align right paren with left paren
# some_method(a,
# b,
# c
# )
#
# # good: when other params are not lined up on multiple lines, outdent
# # right paren by IndentationWidth
# some_method(a,
# x: 1,
# y: 2
# )
#
#
class ClosingParenthesisIndentation < Cop
include Alignment
MSG_INDENT = 'Indent `)` to column %<expected>d (not %<actual>d)'
.freeze
MSG_ALIGN = 'Align `)` with `(`.'.freeze
def on_send(node)
check(node, node.arguments)
end
def on_begin(node)
check(node, node.children)
end
def on_def(node)
check(node.arguments, node.arguments)
end
alias on_defs on_def
def autocorrect(node)
AlignmentCorrector.correct(processed_source, node, @column_delta)
end
private
def check(node, elements)
if elements.empty?
check_for_no_elements(node)
else
check_for_elements(node, elements)
end
end
def check_for_elements(node, elements)
left_paren = node.loc.begin
right_paren = node.loc.end
return unless right_paren && begins_its_line?(right_paren)
correct_column = expected_column(left_paren, elements)
@column_delta = correct_column - right_paren.column
return if @column_delta.zero?
add_offense(right_paren,
location: right_paren,
message: message(correct_column,
left_paren,
right_paren))
end
def check_for_no_elements(node)
left_paren = node.loc.begin
right_paren = node.loc.end
return unless right_paren && begins_its_line?(right_paren)
candidates = correct_column_candidates(node, left_paren)
return if candidates.include?(right_paren.column)
# Although there are multiple choices for a correct column,
# select the first one of candidates to determine a specification.
correct_column = candidates.first
@column_delta = correct_column - right_paren.column
add_offense(right_paren,
location: right_paren,
message: message(correct_column,
left_paren,
right_paren))
end
def expected_column(left_paren, elements)
if !line_break_after_left_paren?(left_paren, elements) &&
all_elements_aligned?(elements)
left_paren.column
else
source_indent = processed_source
.line_indentation(first_argument_line(elements))
new_indent = source_indent - indentation_width
new_indent < 0 ? 0 : new_indent
end
end
def all_elements_aligned?(elements)
elements
.map { |e| e.loc.column }
.uniq
.count == 1
end
def first_argument_line(elements)
elements
.first
.loc
.first_line
end
def correct_column_candidates(node, left_paren)
[
processed_source.line_indentation(left_paren.line),
left_paren.column,
node.loc.column
]
end
def message(correct_column, left_paren, right_paren)
if correct_column == left_paren.column
MSG_ALIGN
else
format(
MSG_INDENT,
expected: correct_column,
actual: right_paren.column
)
end
end
def indentation_width
@config.for_cop('IndentationWidth')['Width'] || 2
end
def line_break_after_left_paren?(left_paren, elements)
elements.first && elements.first.loc.line > left_paren.line
end
end
end
end
end