Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
Layout/EmptyLineAroundAttributeAccessor
cop
This PR adds new `Layout/EmptyLineAroundAttributeAccessor` cop that checks for a newline after attribute accessor. ```ruby # 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 ``` This PR first checks an empty line after an attribute accessor to implement the example shown in The Ruby Style Guide's rule. rubocop/ruby-style-guide#817 In the future this cop will provide an `EnforcedStyle: around` that checks empty lines before and after an attribute accessor, and this `EnforcedStyle: only_after` option could be users selectable option.
- Loading branch information
Showing
14 changed files
with
194 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Layout | ||
# Checks for a newline after attribute accessor. | ||
# | ||
# @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 | ||
# | ||
class EmptyLinesAroundAttributeAccessor < Cop | ||
include RangeHelp | ||
|
||
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) | ||
|
||
next_line_node = next_line_node(node) | ||
return if next_line_node.nil? || attribute_accessor?(next_line_node) | ||
|
||
add_offense(node) | ||
end | ||
|
||
def autocorrect(node) | ||
lambda do |corrector| | ||
range = range_by_whole_lines(node.source_range) | ||
|
||
corrector.insert_after(range, "\n") | ||
end | ||
end | ||
|
||
private | ||
|
||
def next_line_empty?(line) | ||
processed_source[line].blank? | ||
end | ||
|
||
def next_line_node(node) | ||
node.parent.children[node.sibling_index + 1] | ||
end | ||
|
||
def attribute_accessor?(node) | ||
node.send_type? && node.attribute_accessor? | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
spec/rubocop/cop/layout/empty_lines_around_attribute_accessor_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Layout::EmptyLinesAroundAttributeAccessor do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
let(:config) { RuboCop::Config.new } | ||
|
||
it 'registers an offense and corrects for code ' \ | ||
'that immediately follows accessor' do | ||
expect_offense(<<~RUBY) | ||
attr_accessor :foo | ||
^^^^^^^^^^^^^^^^^^ Add an empty line after attribute accessor. | ||
def do_something | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
attr_accessor :foo | ||
def do_something | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects for code ' \ | ||
'that immediately follows accessor with comment' do | ||
expect_offense(<<~RUBY) | ||
attr_accessor :foo # comment | ||
^^^^^^^^^^^^^^^^^^ Add an empty line after attribute accessor. | ||
def do_something | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
attr_accessor :foo # comment | ||
def do_something | ||
end | ||
RUBY | ||
end | ||
|
||
it 'accepts code that separates a attribute accessor from the code ' \ | ||
'with a newline' do | ||
expect_no_offenses(<<~RUBY) | ||
attr_accessor :foo | ||
def do_something | ||
end | ||
RUBY | ||
end | ||
|
||
it 'accepts code that separates attribute accessors from the code ' \ | ||
'with a newline' do | ||
expect_no_offenses(<<~RUBY) | ||
attr_accessor :foo | ||
attr_reader :bar | ||
attr_writer :baz | ||
def do_something | ||
end | ||
RUBY | ||
end | ||
|
||
it 'accepts code when used in class definition' do | ||
expect_no_offenses(<<~RUBY) | ||
class Foo | ||
attr_accessor :foo | ||
end | ||
RUBY | ||
end | ||
end |