Module: RuboCop::Cop::TrailingComma
Overview
Common methods shared by Style/TrailingCommaInArguments and Style/TrailingCommaInLiteral
Constant Summary
collapse
- MSG =
'%s comma after the last %s'.freeze
Instance Method Summary
collapse
-
#autocorrect(range) ⇒ Object
-
#avoid_autocorrect?(_) ⇒ Boolean
By default, there’s no reason to avoid auto-correct.
-
#avoid_comma(kind, comma_begin_pos, sb, extra_info) ⇒ Object
-
#brackets?(node) ⇒ Boolean
Returns true if the node has round/square/curly brackets.
-
#check(node, items, kind, begin_pos, end_pos) ⇒ Object
-
#check_comma(node, kind, comma_pos) ⇒ Object
-
#elements(node) ⇒ Object
-
#extra_avoid_comma_info ⇒ Object
-
#heredoc?(source_after_last_item) ⇒ Boolean
-
#inside_comment?(range, comma_offset) ⇒ Boolean
-
#multiline?(node) ⇒ Boolean
Returns true if the round/square/curly brackets of the given node are on different lines, and each item within is on its own line, and the closing bracket is on its own line.
-
#no_elements_on_same_line?(node) ⇒ Boolean
-
#on_same_line?(a, b) ⇒ Boolean
-
#parameter_name ⇒ Object
-
#put_comma(node, items, kind, sb) ⇒ Object
-
#should_have_comma?(style, node) ⇒ Boolean
#alternative_style, #ambiguous_style_detected, #correct_style_detected, #detected_style, #detected_style=, #no_acceptable_style!, #no_acceptable_style?, #opposite_style_detected, #style, #style_detected, #supported_styles, #unexpected_style_detected
Instance Method Details
#autocorrect(range) ⇒ Object
149
150
151
152
153
154
155
156
157
158
|
# File 'lib/rubocop/cop/mixin/trailing_comma.rb', line 149
def autocorrect(range)
return unless range
lambda do |corrector|
case range.source
when ',' then corrector.remove(range)
else corrector.insert_after(range, ',')
end
end
end
|
#avoid_autocorrect?(_) ⇒ Boolean
By default, there’s no reason to avoid auto-correct.
145
146
147
|
# File 'lib/rubocop/cop/mixin/trailing_comma.rb', line 145
def avoid_autocorrect?(_)
false
end
|
#avoid_comma(kind, comma_begin_pos, sb, extra_info) ⇒ Object
120
121
122
123
124
125
126
127
|
# File 'lib/rubocop/cop/mixin/trailing_comma.rb', line 120
def avoid_comma(kind, comma_begin_pos, sb, )
range = Parser::Source::Range.new(sb, comma_begin_pos,
comma_begin_pos + 1)
article = kind =~ /array/ ? 'an' : 'a'
add_offense(range, range,
format(MSG, 'Avoid', format(kind, article)) +
"#{}.")
end
|
#brackets?(node) ⇒ Boolean
Returns true if the node has round/square/curly brackets.
73
74
75
|
# File 'lib/rubocop/cop/mixin/trailing_comma.rb', line 73
def brackets?(node)
node.loc.end
end
|
#check(node, items, kind, begin_pos, end_pos) ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/rubocop/cop/mixin/trailing_comma.rb', line 17
def check(node, items, kind, begin_pos, end_pos)
sb = node.source_range.source_buffer
after_last_item = Parser::Source::Range.new(sb, begin_pos, end_pos)
return if heredoc?(after_last_item.source)
comma_offset = after_last_item.source =~ /,/
if comma_offset && !(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, sb)
end
end
|
#check_comma(node, kind, comma_pos) ⇒ Object
32
33
34
35
36
37
|
# File 'lib/rubocop/cop/mixin/trailing_comma.rb', line 32
def check_comma(node, kind, comma_pos)
return if should_have_comma?(style, node)
avoid_comma(kind, comma_pos, node.source_range.source_buffer,
)
end
|
#elements(node) ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/rubocop/cop/mixin/trailing_comma.rb', line 93
def elements(node)
return node.children unless node.send_type?
_receiver, _method_name, *args = *node
args.flat_map do |a|
if a.hash_type? && a.loc.first_line != a.loc.last_line &&
!brackets?(a)
a.children
else
a
end
end
end
|
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/rubocop/cop/mixin/trailing_comma.rb', line 39
def
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
|
#heredoc?(source_after_last_item) ⇒ Boolean
68
69
70
|
# File 'lib/rubocop/cop/mixin/trailing_comma.rb', line 68
def heredoc?(source_after_last_item)
source_after_last_item =~ /\w/
end
|
61
62
63
64
65
66
|
# File 'lib/rubocop/cop/mixin/trailing_comma.rb', line 61
def (range, comma_offset)
processed_source..any? do ||
= .loc.expression.begin_pos - range.begin_pos
>= 0 && < comma_offset
end
end
|
#multiline?(node) ⇒ Boolean
Returns true if the round/square/curly brackets of the given node are on different lines, and each item within is on its own line, and the closing bracket is on its own line.
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/rubocop/cop/mixin/trailing_comma.rb', line 80
def multiline?(node)
return false unless node.multiline?
items = elements(node).map(&:source_range)
return false if items.empty?
items << node.loc.begin << node.loc.end
(items.map(&:first_line) + items.map(&:last_line)).uniq.count > 1
end
|
#no_elements_on_same_line?(node) ⇒ Boolean
110
111
112
113
114
|
# File 'lib/rubocop/cop/mixin/trailing_comma.rb', line 110
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
|
#on_same_line?(a, b) ⇒ Boolean
116
117
118
|
# File 'lib/rubocop/cop/mixin/trailing_comma.rb', line 116
def on_same_line?(a, b)
a.last_line == b.line
end
|
#parameter_name ⇒ Object
13
14
15
|
# File 'lib/rubocop/cop/mixin/trailing_comma.rb', line 13
def parameter_name
'EnforcedStyleForMultiline'
end
|
#put_comma(node, items, kind, sb) ⇒ Object
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/rubocop/cop/mixin/trailing_comma.rb', line 129
def put_comma(node, items, kind, sb)
last_item = items.last
return if last_item.type == :block_pass
last_expr = last_item.source_range
ix = last_expr.source.rindex("\n") || 0
ix += last_expr.source[ix..-1] =~ /\S/
range = Parser::Source::Range.new(sb, last_expr.begin_pos + ix,
last_expr.end_pos)
autocorrect_range = avoid_autocorrect?(elements(node)) ? nil : range
add_offense(autocorrect_range, range,
format(MSG, 'Put a', format(kind, 'a multiline') + '.'))
end
|
#should_have_comma?(style, node) ⇒ Boolean
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/rubocop/cop/mixin/trailing_comma.rb', line 50
def should_have_comma?(style, node)
case style
when :comma
multiline?(node) && no_elements_on_same_line?(node)
when :consistent_comma
multiline?(node)
else
false
end
end
|