/
case_indentation.rb
203 lines (180 loc) · 5.01 KB
/
case_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
202
203
# frozen_string_literal: true
module RuboCop
module Cop
module Layout
# This cop checks how the `when` and ``in``s of a `case` expression
# are indented in relation to its `case` or `end` keyword.
#
# It will register a separate offense for each misaligned `when` and `in`.
#
# @example
# # If Layout/EndAlignment is set to keyword style (default)
# # *case* and *end* should always be aligned to same depth,
# # and therefore *when* should always be aligned to both -
# # regardless of configuration.
#
# # bad for all styles
# case n
# when 0
# x * 2
# else
# y / 3
# end
#
# case n
# in pattern
# x * 2
# else
# y / 3
# end
#
# # good for all styles
# case n
# when 0
# x * 2
# else
# y / 3
# end
#
# case n
# in pattern
# x * 2
# else
# y / 3
# end
#
# @example EnforcedStyle: case (default)
# # if EndAlignment is set to other style such as
# # start_of_line (as shown below), then *when* alignment
# # configuration does have an effect.
#
# # bad
# a = case n
# when 0
# x * 2
# else
# y / 3
# end
#
# a = case n
# in pattern
# x * 2
# else
# y / 3
# end
#
# # good
# a = case n
# when 0
# x * 2
# else
# y / 3
# end
#
# a = case n
# in pattern
# x * 2
# else
# y / 3
# end
#
# @example EnforcedStyle: end
# # bad
# a = case n
# when 0
# x * 2
# else
# y / 3
# end
#
# a = case n
# in pattern
# x * 2
# else
# y / 3
# end
#
# # good
# a = case n
# when 0
# x * 2
# else
# y / 3
# end
#
# a = case n
# in pattern
# x * 2
# else
# y / 3
# end
class CaseIndentation < Base
include Alignment
include ConfigurableEnforcedStyle
include RangeHelp
extend AutoCorrector
MSG = 'Indent `%<branch_type>s` %<depth>s `%<base>s`.'
def on_case(case_node)
return if case_node.single_line?
case_node.each_when { |when_node| check_when(when_node, 'when') }
end
def on_case_match(case_match_node)
return if case_match_node.single_line?
case_match_node.each_in_pattern { |in_pattern_node| check_when(in_pattern_node, 'in') }
end
private
def check_when(when_node, branch_type)
when_column = when_node.loc.keyword.column
base_column = base_column(when_node.parent, style)
if when_column == base_column + indentation_width
correct_style_detected
else
incorrect_style(when_node, branch_type)
end
end
def indent_one_step?
cop_config['IndentOneStep']
end
def indentation_width
indent_one_step? ? configured_indentation_width : 0
end
def incorrect_style(when_node, branch_type)
depth = indent_one_step? ? 'one step more than' : 'as deep as'
message = format(MSG, branch_type: branch_type, depth: depth, base: style)
add_offense(when_node.loc.keyword, message: message) do |corrector|
detect_incorrect_style(when_node)
whitespace = whitespace_range(when_node)
corrector.replace(whitespace, replacement(when_node)) if whitespace.source.strip.empty?
end
end
def detect_incorrect_style(when_node)
when_column = when_node.loc.keyword.column
base_column = base_column(when_node.parent, alternative_style)
if when_column == base_column
opposite_style_detected
else
unrecognized_style_detected
end
end
def base_column(case_node, base)
case base
when :case then case_node.location.keyword.column
when :end then case_node.location.end.column
end
end
def whitespace_range(node)
when_column = node.location.keyword.column
begin_pos = node.loc.keyword.begin_pos
range_between(begin_pos - when_column, begin_pos)
end
def replacement(node)
case_node = node.each_ancestor(:case, :case_match).first
base_type = cop_config[style_parameter_name] == 'end' ? :end : :case
column = base_column(case_node, base_type)
column += indentation_width
' ' * column
end
end
end
end
end