forked from rubocop/rubocop
/
trailing_comma_in_attribute_declaration.rb
57 lines (50 loc) · 1.33 KB
/
trailing_comma_in_attribute_declaration.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
# frozen_string_literal: true
module RuboCop
module Cop
module Lint
# This cop checks for trailing commas in attribute declarations, such as
# `#attr_reader`. Leaving a trailing comma will nullify the next method
# definition by overriding it with a getter method.
#
# @example
#
# # bad
# class Foo
# attr_reader :foo,
#
# def bar
# puts "Unreachable."
# end
# end
#
# # good
# class Foo
# attr_reader :foo
#
# def bar
# puts "No problem!"
# end
# end
#
class TrailingCommaInAttributeDeclaration < Base
extend AutoCorrector
include RangeHelp
MSG = 'Do not use trailing comma in attribute declarations.'
def on_send(node)
return unless node.attribute_accessor? && node.arguments.last.def_type?
trailing_comma = trailing_comma_range(node)
add_offense(trailing_comma) do |corrector|
corrector.remove(trailing_comma)
end
end
private
def trailing_comma_range(node)
range_with_surrounding_space(
range: node.arguments[-2].source_range,
side: :right
).end.resize(1)
end
end
end
end
end