forked from rubocop/rubocop
/
trailing_comma.rb
216 lines (178 loc) · 6.3 KB
/
trailing_comma.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
# frozen_string_literal: true
module RuboCop
module Cop
# Common methods shared by Style/TrailingCommaInArguments and
# Style/TrailingCommaInLiteral
module TrailingComma
include ConfigurableEnforcedStyle
include RangeHelp
MSG = '%<command>s comma after the last %<unit>s.'.freeze
private
def style_parameter_name
'EnforcedStyleForMultiline'
end
def check(node, items, kind, begin_pos, end_pos)
after_last_item = range_between(begin_pos, end_pos)
# If there is any heredoc in items, then match the comma succeeding
# any whitespace (except newlines), otherwise allow for newlines
comma_regex = any_heredoc?(items) ? /\A[^\S\n]*,/ : /\A\s*,/
comma_offset = after_last_item.source =~ comma_regex &&
after_last_item.source.index(',')
if comma_offset && !inside_comment?(after_last_item, comma_offset)
check_comma(node, kind, after_last_item.begin_pos + comma_offset)
elsif should_have_comma?(style, node)
put_comma(node, items, kind)
end
end
def check_comma(node, kind, comma_pos)
return if should_have_comma?(style, node)
avoid_comma(kind, comma_pos, extra_avoid_comma_info)
end
def check_literal(node, kind)
return if node.children.empty?
# A braceless hash is the last parameter of a method call and will be
# checked as such.
return unless brackets?(node)
check(node, node.children, kind,
node.children.last.source_range.end_pos,
node.loc.end.begin_pos)
end
def extra_avoid_comma_info
case style
when :comma
', unless each item is on its own line'
when :consistent_comma
', unless items are split onto multiple lines'
else
''
end
end
def should_have_comma?(style, node)
case style
when :comma
multiline?(node) && no_elements_on_same_line?(node)
when :consistent_comma
multiline?(node) && !method_name_and_arguments_on_same_line?(node)
else
false
end
end
def inside_comment?(range, comma_offset)
processed_source.comments.any? do |comment|
comment_offset = comment.loc.expression.begin_pos - range.begin_pos
comment_offset >= 0 && comment_offset < comma_offset
end
end
# Returns true if the node has round/square/curly brackets.
def brackets?(node)
node.loc.end
end
# Returns true if the round/square/curly brackets of the given node are
# on different lines, each item within is on its own line, and the
# closing bracket is on its own line.
def multiline?(node)
node.multiline? && !allowed_multiline_argument?(node)
end
def method_name_and_arguments_on_same_line?(node)
node.send_type? &&
node.loc.selector.line == node.arguments.last.last_line &&
node.last_line == node.arguments.last.last_line
end
# A single argument with the closing bracket on the same line as the end
# of the argument is not considered multiline, even if the argument
# itself might span multiple lines.
def allowed_multiline_argument?(node)
elements(node).one? && !Util.begins_its_line?(node.loc.end)
end
def elements(node)
return node.children unless node.send_type?
node.arguments.flat_map do |argument|
# For each argument, if it is a multi-line hash without braces,
# then promote the hash elements to method arguments
# for the purpose of determining multi-line-ness.
if argument.hash_type? && argument.multiline? && !argument.braces?
argument.children
else
argument
end
end
end
def no_elements_on_same_line?(node)
items = elements(node).map(&:source_range)
items << node.loc.end
items.each_cons(2).none? { |a, b| on_same_line?(a, b) }
end
def on_same_line?(range1, range2)
range1.last_line == range2.line
end
def avoid_comma(kind, comma_begin_pos, extra_info)
range = range_between(comma_begin_pos, comma_begin_pos + 1)
article = kind =~ /array/ ? 'an' : 'a'
msg = format(
MSG,
command: 'Avoid',
unit: format(kind, article: article) + extra_info.to_s
)
add_offense(range, location: range, message: msg)
end
def put_comma(node, items, kind)
return if avoid_autocorrect?(elements(node))
last_item = items.last
return if last_item.block_pass_type?
range = autocorrect_range(last_item)
msg = format(
MSG,
command: 'Put a',
unit: format(kind, article: 'a multiline')
)
add_offense(range, location: range, message: msg)
end
def autocorrect_range(item)
expr = item.source_range
ix = expr.source.rindex("\n") || 0
ix += expr.source[ix..-1] =~ /\S/
range_between(expr.begin_pos + ix, expr.end_pos)
end
# By default, there's no reason to avoid auto-correct.
def avoid_autocorrect?(_nodes)
false
end
def any_heredoc?(items)
items.any? { |item| heredoc?(item) }
end
def heredoc?(node)
return false unless node.is_a?(RuboCop::AST::Node)
return true if node.loc.respond_to?(:heredoc_body)
return heredoc_send?(node) if node.send_type?
# handle hash values
#
# some_method({
# 'auth' => <<-SOURCE
# ...
# SOURCE
# })
if node.pair_type? || node.hash_type?
return heredoc?(node.children.last)
end
false
end
def heredoc_send?(node)
# handle heredocs with methods
#
# some_method(<<-CODE.strip.chomp)
# ...
# CODE
return heredoc?(node.children.first) if node.children.size == 2
# handle nested methods
#
# some_method(
# another_method(<<-CODE.strip.chomp)
# ...
# CODE
# )
return heredoc?(node.children.last) if node.children.size > 2
false
end
end
end
end