forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
first_hash_element_indentation.rb
200 lines (176 loc) · 6.93 KB
/
first_hash_element_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
# frozen_string_literal: true
module RuboCop
module Cop
module Layout
# This cop checks the indentation of the first key in a hash literal
# where the opening brace and the first key are on separate lines. The
# other keys' indentations are handled by the HashAlignment cop.
#
# By default, Hash literals that are arguments in a method call with
# parentheses, and where the opening curly brace of the hash is on the
# same line as the opening parenthesis of the method call, shall have
# their first key indented one step (two spaces) more than the position
# inside the opening parenthesis.
#
# Other hash literals shall have their first key indented one step more
# than the start of the line where the opening curly brace is.
#
# This default style is called 'special_inside_parentheses'. Alternative
# styles are 'consistent' and 'align_braces'. Here are examples:
#
# @example EnforcedStyle: special_inside_parentheses (default)
# # The `special_inside_parentheses` style enforces that the first key
# # in a hash literal where the opening brace and the first key are on
# # separate lines is indented one step (two spaces) more than the
# # position inside the opening parentheses.
#
# # bad
# hash = {
# key: :value
# }
# and_in_a_method_call({
# no: :difference
# })
#
# # good
# special_inside_parentheses
# hash = {
# key: :value
# }
# but_in_a_method_call({
# its_like: :this
# })
#
# @example EnforcedStyle: consistent
# # The `consistent` style enforces that the first key in a hash
# # literal where the opening brace and the first key are on
# # separate lines is indented the same as a hash literal which is not
# # defined inside a method call.
#
# # bad
# hash = {
# key: :value
# }
# but_in_a_method_call({
# its_like: :this
# })
#
# # good
# hash = {
# key: :value
# }
# and_in_a_method_call({
# no: :difference
# })
#
# @example EnforcedStyle: align_braces
# # The `align_brackets` style enforces that the opening and closing
# # braces are indented to the same position.
#
# # bad
# and_now_for_something = {
# completely: :different
# }
#
# # good
# and_now_for_something = {
# completely: :different
# }
class FirstHashElementIndentation < Base
include Alignment
include ConfigurableEnforcedStyle
include MultilineElementIndentation
extend AutoCorrector
MSG = 'Use %<configured_indentation_width>d spaces for indentation ' \
'in a hash, relative to %<base_description>s.'
def on_hash(node)
check(node, nil) if node.loc.begin
end
def on_send(node)
return if enforce_first_argument_with_fixed_indentation?
each_argument_node(node, :hash) do |hash_node, left_parenthesis|
check(hash_node, left_parenthesis)
end
end
alias on_csend on_send
private
def autocorrect(corrector, node)
AlignmentCorrector.correct(corrector, processed_source, node, @column_delta)
end
def brace_alignment_style
:align_braces
end
def check(hash_node, left_parenthesis)
return if ignored_node?(hash_node)
left_brace = hash_node.loc.begin
first_pair = hash_node.pairs.first
if first_pair
return if first_pair.first_line == left_brace.line
if separator_style?(first_pair)
check_based_on_longest_key(hash_node, left_brace, left_parenthesis)
else
check_first(first_pair, left_brace, left_parenthesis, 0)
end
end
check_right_brace(hash_node.loc.end, left_brace, left_parenthesis)
end
def check_right_brace(right_brace, left_brace, left_parenthesis)
# if the right brace is on the same line as the last value, accept
return if /\S/.match?(right_brace.source_line[0...right_brace.column])
expected_column = base_column(left_brace, left_parenthesis)
@column_delta = expected_column - right_brace.column
return if @column_delta.zero?
message = message_for_right_brace(left_parenthesis)
add_offense(right_brace, message: message) do |corrector|
autocorrect(corrector, right_brace)
end
end
def separator_style?(first_pair)
separator = first_pair.loc.operator
key = "Enforced#{separator.is?(':') ? 'Colon' : 'HashRocket'}Style"
config.for_cop('Layout/HashAlignment')[key] == 'separator'
end
def check_based_on_longest_key(hash_node, left_brace, left_parenthesis)
key_lengths = hash_node.keys.map { |key| key.source_range.length }
check_first(hash_node.pairs.first, left_brace, left_parenthesis,
key_lengths.max - key_lengths.first)
end
# Returns the description of what the correct indentation is based on.
def base_description(left_parenthesis)
if style == :align_braces
'the position of the opening brace'
elsif left_parenthesis && style == :special_inside_parentheses
'the first position after the preceding left parenthesis'
else
'the start of the line where the left curly brace is'
end
end
def message(base_description)
format(
MSG,
configured_indentation_width: configured_indentation_width,
base_description: base_description
)
end
def message_for_right_brace(left_parenthesis)
if style == :align_braces
'Indent the right brace the same as the left brace.'
elsif style == :special_inside_parentheses && left_parenthesis
'Indent the right brace the same as the first position ' \
'after the preceding left parenthesis.'
else
'Indent the right brace the same as the start of the line ' \
'where the left brace is.'
end
end
def enforce_first_argument_with_fixed_indentation?
return false unless argument_alignment_config['Enabled']
argument_alignment_config['EnforcedStyle'] == 'with_fixed_indentation'
end
def argument_alignment_config
config.for_cop('Layout/ArgumentAlignment')
end
end
end
end
end