/
hash_alignment.rb
366 lines (330 loc) · 9.75 KB
/
hash_alignment.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
# frozen_string_literal: true
module RuboCop
module Cop
module Layout
# Check that the keys, separators, and values of a multi-line hash
# literal are aligned according to configuration. The configuration
# options are:
#
# * key (left align keys, one space before hash rockets and values)
# * separator (align hash rockets and colons, right align keys)
# * table (left align keys, hash rockets, and values)
#
# The treatment of hashes passed as the last argument to a method call
# can also be configured. The options are:
#
# * always_inspect
# * always_ignore
# * ignore_implicit (without curly braces)
#
# Alternatively you can specify multiple allowed styles. That's done by
# passing a list of styles to EnforcedStyles.
#
# @example EnforcedHashRocketStyle: key (default)
# # bad
# {
# :foo => bar,
# :ba => baz
# }
# {
# :foo => bar,
# :ba => baz
# }
#
# # good
# {
# :foo => bar,
# :ba => baz
# }
#
# @example EnforcedHashRocketStyle: separator
# # bad
# {
# :foo => bar,
# :ba => baz
# }
# {
# :foo => bar,
# :ba => baz
# }
#
# # good
# {
# :foo => bar,
# :ba => baz
# }
#
# @example EnforcedHashRocketStyle: table
# # bad
# {
# :foo => bar,
# :ba => baz
# }
#
# # good
# {
# :foo => bar,
# :ba => baz
# }
#
# @example EnforcedColonStyle: key (default)
# # bad
# {
# foo: bar,
# ba: baz
# }
# {
# foo: bar,
# ba: baz
# }
#
# # good
# {
# foo: bar,
# ba: baz
# }
#
# @example EnforcedColonStyle: separator
# # bad
# {
# foo: bar,
# ba: baz
# }
#
# # good
# {
# foo: bar,
# ba: baz
# }
#
# @example EnforcedColonStyle: table
# # bad
# {
# foo: bar,
# ba: baz
# }
#
# # good
# {
# foo: bar,
# ba: baz
# }
#
# @example EnforcedLastArgumentHashStyle: always_inspect (default)
# # Inspect both implicit and explicit hashes.
#
# # bad
# do_something(foo: 1,
# bar: 2)
#
# # bad
# do_something({foo: 1,
# bar: 2})
#
# # good
# do_something(foo: 1,
# bar: 2)
#
# # good
# do_something(
# foo: 1,
# bar: 2
# )
#
# # good
# do_something({foo: 1,
# bar: 2})
#
# # good
# do_something({
# foo: 1,
# bar: 2
# })
#
# @example EnforcedLastArgumentHashStyle: always_ignore
# # Ignore both implicit and explicit hashes.
#
# # good
# do_something(foo: 1,
# bar: 2)
#
# # good
# do_something({foo: 1,
# bar: 2})
#
# @example EnforcedLastArgumentHashStyle: ignore_implicit
# # Ignore only implicit hashes.
#
# # bad
# do_something({foo: 1,
# bar: 2})
#
# # good
# do_something(foo: 1,
# bar: 2)
#
# @example EnforcedLastArgumentHashStyle: ignore_explicit
# # Ignore only explicit hashes.
#
# # bad
# do_something(foo: 1,
# bar: 2)
#
# # good
# do_something({foo: 1,
# bar: 2})
#
class HashAlignment < Cop
include HashAlignmentStyles
include RangeHelp
MESSAGES = { KeyAlignment => 'Align the keys of a hash literal if ' \
'they span more than one line.',
SeparatorAlignment => 'Align the separators of a hash ' \
'literal if they span more than one line.',
TableAlignment => 'Align the keys and values of a hash ' \
'literal if they span more than one line.' }.freeze
def on_send(node)
return if double_splat?(node)
return unless node.arguments?
last_argument = node.last_argument
return unless last_argument.hash_type? &&
ignore_hash_argument?(last_argument)
ignore_node(last_argument)
end
alias on_super on_send
alias on_yield on_send
def on_hash(node) # rubocop:todo Metrics/CyclomaticComplexity
return if ignored_node?(node)
return if node.pairs.empty? || node.single_line?
return unless alignment_for_hash_rockets
.any? { |a| a.checkable_layout?(node) } &&
alignment_for_colons
.any? { |a| a.checkable_layout?(node) }
check_pairs(node)
end
def autocorrect(node)
delta = column_deltas[alignment_for(node).first.class][node]
return if delta.nil?
correct_node(node, delta)
end
attr_accessor :offences_by
attr_accessor :column_deltas
private
def reset!
self.offences_by = {}
self.column_deltas = Hash.new { |hash, key| hash[key] = {} }
end
def double_splat?(node)
node.children.last.is_a?(Symbol)
end
def check_pairs(node)
first_pair = node.pairs.first
reset!
alignment_for(first_pair).each do |alignment|
delta = alignment.deltas_for_first_pair(first_pair, node)
check_delta delta, node: first_pair, alignment: alignment
end
node.children.each do |current|
alignment_for(current).each do |alignment|
delta = alignment.deltas(first_pair, current)
check_delta delta, node: current, alignment: alignment
end
end
add_offences
end
def add_offences
format, offences = offences_by.min_by { |_, v| v.length }
(offences || []).each do |offence|
add_offense(offence, message: MESSAGES[format])
end
end
def check_delta(delta, node:, alignment:)
offences_by[alignment.class] ||= []
return if good_alignment? delta
column_deltas[alignment.class][node] = delta
offences_by[alignment.class].push(node)
end
def ignore_hash_argument?(node)
case cop_config['EnforcedLastArgumentHashStyle']
when 'always_inspect' then false
when 'always_ignore' then true
when 'ignore_explicit' then node.braces?
when 'ignore_implicit' then !node.braces?
end
end
def alignment_for(pair)
if pair.hash_rocket?
alignment_for_hash_rockets
else
alignment_for_colons
end
end
def alignment_for_hash_rockets
@alignment_for_hash_rockets ||=
new_alignment('EnforcedHashRocketStyle')
end
def alignment_for_colons
@alignment_for_colons ||=
new_alignment('EnforcedColonStyle')
end
def correct_node(node, delta)
# We can't use the instance variable inside the lambda. That would
# just give each lambda the same reference and they would all get the
# last value of each. A local variable fixes the problem.
if !node.value
correct_no_value(delta[:key] || 0, node.source_range)
else
correct_key_value(delta, node.key.source_range,
node.value.source_range,
node.loc.operator)
end
end
def correct_no_value(key_delta, key)
->(corrector) { adjust(corrector, key_delta, key) }
end
def correct_key_value(delta, key, value, separator)
# We can't use the instance variable inside the lambda. That would
# just give each lambda the same reference and they would all get the
# last value of each. Some local variables fix the problem.
separator_delta = delta[:separator] || 0
value_delta = delta[:value] || 0
key_delta = delta[:key] || 0
key_column = key.column
key_delta = -key_column if key_delta < -key_column
lambda do |corrector|
adjust(corrector, key_delta, key)
adjust(corrector, separator_delta, separator)
adjust(corrector, value_delta, value)
end
end
def new_alignment(key)
formats = cop_config[key]
formats = [formats] if formats.is_a? String
formats.uniq.map do |format|
case format
when 'key'
KeyAlignment.new
when 'table'
TableAlignment.new
when 'separator'
SeparatorAlignment.new
else
raise "Unknown #{key}: #{formats}"
end
end
end
def adjust(corrector, delta, range)
if delta.positive?
corrector.insert_before(range, ' ' * delta)
elsif delta.negative?
range = range_between(range.begin_pos - delta.abs, range.begin_pos)
corrector.remove(range)
end
end
def good_alignment?(column_deltas)
column_deltas.values.all?(&:zero?)
end
end
end
end
end