forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
first_element_line_break.rb
54 lines (39 loc) · 1.3 KB
/
first_element_line_break.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
# frozen_string_literal: true
module RuboCop
module Cop
# Common functionality for checking for a line break before the first
# element in a multi-line collection.
module FirstElementLineBreak
private
def check_method_line_break(node, children)
return if children.empty?
return unless method_uses_parens?(node, children.first)
check_children_line_break(node, children)
end
def method_uses_parens?(node, limit)
source = node.source_range.source_line[0...limit.loc.column]
source =~ /\s*\(\s*$/
end
def check_children_line_break(node, children, start = node)
return if children.empty?
return add_offense(children.first) if children.first.multiline?
check_multiple_children_line_break(children, start)
end
def check_multiple_children_line_break(children, start)
return if children.size < 2
line = start.first_line
min = first_by_line(children)
return if line != min.first_line
max = last_by_line(children)
return if line == max.last_line
add_offense(min)
end
def first_by_line(nodes)
nodes.min_by(&:first_line)
end
def last_by_line(nodes)
nodes.max_by(&:last_line)
end
end
end
end