forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 2
/
empty_line_between_defs.rb
273 lines (237 loc) · 7.56 KB
/
empty_line_between_defs.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
# frozen_string_literal: true
module RuboCop
module Cop
module Layout
# Checks whether class/module/method definitions are
# separated by one or more empty lines.
#
# `NumberOfEmptyLines` can be an integer (default is 1) or
# an array (e.g. [1, 2]) to specify a minimum and maximum
# number of empty lines permitted.
#
# `AllowAdjacentOneLineDefs` configures whether adjacent
# one-line definitions are considered an offense.
#
# @example EmptyLineBetweenMethodDefs: true (default)
# # checks for empty lines between method definitions.
#
# # bad
# def a
# end
# def b
# end
#
# @example
#
# # good
# def a
# end
#
# def b
# end
#
# @example EmptyLineBetweenClassDefs: true (default)
# # checks for empty lines between class definitions.
#
# # bad
# class A
# end
# class B
# end
# def b
# end
#
# @example
#
# # good
# class A
# end
#
# class B
# end
#
# def b
# end
#
# @example EmptyLineBetweenModuleDefs: true (default)
# # checks for empty lines between module definitions.
#
# # bad
# module A
# end
# module B
# end
# def b
# end
#
# @example
#
# # good
# module A
# end
#
# module B
# end
#
# def b
# end
#
# @example AllowAdjacentOneLineDefs: true (default)
#
# # good
# class ErrorA < BaseError; end
# class ErrorB < BaseError; end
# class ErrorC < BaseError; end
#
# # good
# class ErrorA < BaseError; end
#
# class ErrorB < BaseError; end
#
# class ErrorC < BaseError; end
#
# @example AllowAdjacentOneLineDefs: false
#
# # bad
# class ErrorA < BaseError; end
# class ErrorB < BaseError; end
# class ErrorC < BaseError; end
#
# # good
# class ErrorA < BaseError; end
#
# class ErrorB < BaseError; end
#
# class ErrorC < BaseError; end
#
class EmptyLineBetweenDefs < Base
include RangeHelp
extend AutoCorrector
MSG = 'Expected %<expected>s between %<type>s definitions; found %<actual>d.'
def self.autocorrect_incompatible_with
[Layout::EmptyLines]
end
# We operate on `begin` nodes, instead of using `OnMethodDef`,
# so that we can walk over pairs of consecutive nodes and
# efficiently access a node's predecessor; #prev_node ends up
# doing a linear scan over siblings, so we don't want to call
# it on each def.
def on_begin(node)
node.children.each_cons(2) do |prev, n|
nodes = [prev, n]
check_defs(nodes) if nodes.all? { |def_candidate| candidate?(def_candidate) }
end
end
def check_defs(nodes)
count = blank_lines_count_between(*nodes)
return if line_count_allowed?(count)
return if multiple_blank_lines_groups?(*nodes)
return if nodes.all?(&:single_line?) && cop_config['AllowAdjacentOneLineDefs']
correction_node = nodes.last
location = correction_node.loc.keyword.join(correction_node.loc.name)
add_offense(location, message: message(correction_node, count: count)) do |corrector|
autocorrect(corrector, *nodes, count)
end
end
def autocorrect(corrector, prev_def, node, count)
# finds position of first newline
end_pos = end_loc(prev_def).end_pos
source_buffer = end_loc(prev_def).source_buffer
newline_pos = source_buffer.source.index("\n", end_pos)
# Handle the case when multiple one-liners are on the same line.
newline_pos = end_pos + 1 if newline_pos > node.source_range.begin_pos
if count > maximum_empty_lines
autocorrect_remove_lines(corrector, newline_pos, count)
else
autocorrect_insert_lines(corrector, newline_pos, count)
end
end
private
def candidate?(node)
return unless node
method_candidate?(node) || class_candidate?(node) || module_candidate?(node)
end
def method_candidate?(node)
cop_config['EmptyLineBetweenMethodDefs'] && (node.def_type? || node.defs_type?)
end
def class_candidate?(node)
cop_config['EmptyLineBetweenClassDefs'] && node.class_type?
end
def module_candidate?(node)
cop_config['EmptyLineBetweenModuleDefs'] && node.module_type?
end
def message(node, count: nil)
type = node_type(node)
format(MSG, type: type, expected: expected_lines, actual: count)
end
def expected_lines
if allowance_range?
"#{minimum_empty_lines..maximum_empty_lines} empty lines"
else
lines = maximum_empty_lines == 1 ? 'line' : 'lines'
"#{maximum_empty_lines} empty #{lines}"
end
end
def multiple_blank_lines_groups?(first_def_node, second_def_node)
lines = lines_between_defs(first_def_node, second_def_node)
blank_start = lines.each_index.select { |i| lines[i].blank? }.max
non_blank_end = lines.each_index.reject { |i| lines[i].blank? }.min
return false if blank_start.nil? || non_blank_end.nil?
blank_start > non_blank_end
end
def line_count_allowed?(count)
(minimum_empty_lines..maximum_empty_lines).cover?(count)
end
def blank_lines_count_between(first_def_node, second_def_node)
lines_between_defs(first_def_node, second_def_node).count(&:blank?)
end
def minimum_empty_lines
Array(cop_config['NumberOfEmptyLines']).first
end
def maximum_empty_lines
Array(cop_config['NumberOfEmptyLines']).last
end
def lines_between_defs(first_def_node, second_def_node)
begin_line_num = def_end(first_def_node)
end_line_num = def_start(second_def_node) - 2
return [] if end_line_num.negative?
processed_source.lines[begin_line_num..end_line_num]
end
def def_start(node)
node.loc.keyword.line
end
def def_end(node)
end_loc(node).line
end
def end_loc(node)
if (node.def_type? || node.defs_type?) && node.endless?
node.loc.expression.end
else
node.loc.end
end
end
def autocorrect_remove_lines(corrector, newline_pos, count)
difference = count - maximum_empty_lines
range_to_remove = range_between(newline_pos, newline_pos + difference)
corrector.remove(range_to_remove)
end
def autocorrect_insert_lines(corrector, newline_pos, count)
difference = minimum_empty_lines - count
where_to_insert = range_between(newline_pos, newline_pos + 1)
corrector.insert_after(where_to_insert, "\n" * difference)
end
def node_type(node)
case node.type
when :def, :defs
:method
else
node.type
end
end
def allowance_range?
minimum_empty_lines != maximum_empty_lines
end
end
end
end
end