forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
empty_lines_around_attribute_accessor.rb
139 lines (117 loc) · 3.56 KB
/
empty_lines_around_attribute_accessor.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
# frozen_string_literal: true
module RuboCop
module Cop
module Layout
# Checks for a newline after an attribute accessor or a group of them.
# `alias` syntax and `alias_method`, `public`, `protected`, and `private` methods are allowed
# by default. These are customizable with `AllowAliasSyntax` and `AllowedMethods` options.
#
# @example
# # bad
# attr_accessor :foo
# def do_something
# end
#
# # good
# attr_accessor :foo
#
# def do_something
# end
#
# # good
# attr_accessor :foo
# attr_reader :bar
# attr_writer :baz
# attr :qux
#
# def do_something
# end
#
# @example AllowAliasSyntax: true (default)
# # good
# attr_accessor :foo
# alias :foo? :foo
#
# def do_something
# end
#
# @example AllowAliasSyntax: false
# # bad
# attr_accessor :foo
# alias :foo? :foo
#
# def do_something
# end
#
# # good
# attr_accessor :foo
#
# alias :foo? :foo
#
# def do_something
# end
#
# @example AllowedMethods: ['private']
# # good
# attr_accessor :foo
# private :foo
#
# def do_something
# end
#
class EmptyLinesAroundAttributeAccessor < Base
include RangeHelp
include AllowedMethods
extend AutoCorrector
MSG = 'Add an empty line after attribute accessor.'
def on_send(node)
return unless node.attribute_accessor?
return if next_line_empty?(node.last_line)
return if next_line_empty_or_enable_directive_comment?(node.last_line)
next_line_node = next_line_node(node)
return unless require_empty_line?(next_line_node)
add_offense(node) { |corrector| autocorrect(corrector, node) }
end
private
def autocorrect(corrector, node)
node_range = range_by_whole_lines(node.source_range)
next_line = node_range.last_line + 1
if next_line_enable_directive_comment?(next_line)
node_range = processed_source.comment_at_line(next_line)
end
corrector.insert_after(node_range, "\n")
end
def next_line_empty_or_enable_directive_comment?(line)
return true if next_line_empty?(line)
next_line = line + 1
next_line_enable_directive_comment?(next_line) && next_line_empty?(next_line)
end
def next_line_enable_directive_comment?(line)
return false unless (comment = processed_source.comment_at_line(line))
DirectiveComment.new(comment).enabled?
end
def next_line_empty?(line)
processed_source[line].nil? || processed_source[line].blank?
end
def require_empty_line?(node)
return false unless node.respond_to?(:type)
!allow_alias?(node) && !attribute_or_allowed_method?(node)
end
def next_line_node(node)
return if node.parent.if_type?
node.right_sibling
end
def allow_alias?(node)
allow_alias_syntax? && node.alias_type?
end
def attribute_or_allowed_method?(node)
return false unless node.send_type?
node.attribute_accessor? || allowed_method?(node.method_name)
end
def allow_alias_syntax?
cop_config.fetch('AllowAliasSyntax', true)
end
end
end
end
end