class RuboCop::Cop::Layout::EmptyLinesAroundAttributeAccessor
end
def do_something
private :foo
attr_accessor :foo
# good
@example AllowedMethods: [‘private’]
end
def do_something
alias :foo? :foo
attr_accessor :foo
# good
end
def do_something
alias :foo? :foo
attr_accessor :foo
# bad
@example AllowAliasSyntax: false
end
def do_something
alias :foo? :foo
attr_accessor :foo
# good
@example AllowAliasSyntax: true (default)
end
def do_something
attr :qux
attr_writer :baz
attr_reader :bar
attr_accessor :foo
# good
end
def do_something
attr_accessor :foo
# good
end
def do_something
attr_accessor :foo
# bad
@example
by default. These are customizable with ‘AllowAliasSyntax` and `AllowedMethods` options.
`alias` syntax and `alias_method`, `public`, `protected`, and `private` methods are allowed
Checks for a newline after an attribute accessor or a group of them.
def allow_alias?(node)
def allow_alias?(node) allow_alias_syntax? && node.alias_type? end
def allow_alias_syntax?
def allow_alias_syntax? cop_config.fetch('AllowAliasSyntax', true) end
def attribute_or_allowed_method?(node)
def attribute_or_allowed_method?(node) return false unless node.send_type? node.attribute_accessor? || allowed_method?(node.method_name) end
def autocorrect(corrector, node)
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?(line)
def next_line_empty?(line) processed_source[line].nil? || processed_source[line].blank? end
def next_line_empty_or_enable_directive_comment?(line)
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)
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_node(node)
def next_line_node(node) return if node.parent.if_type? node.right_sibling end
def on_send(node)
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
def require_empty_line?(node)
def require_empty_line?(node) return false unless node.respond_to?(:type) !allow_alias?(node) && !attribute_or_allowed_method?(node) end