Skip to content

Commit

Permalink
Merge pull request #10694 from ydah/add_ignore_case_for_empty_lines_a…
Browse files Browse the repository at this point in the history
…round_attribute_accessor

[Fix #10693] Add ignore case for `Style/EmptyLinesAroundAttributeAccessor` when there is a comment line on the next line.
  • Loading branch information
koic committed Jun 8, 2022
2 parents c258452 + 71076f5 commit 40e83ef
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 5 deletions.
@@ -0,0 +1 @@
* [#10693](https://github.com/rubocop/rubocop/issues/10693): Add ignore case for `Style/EmptyLinesAroundAttributeAccessor` when there is a comment line on the next line. ([@ydah][])
29 changes: 25 additions & 4 deletions lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb
Expand Up @@ -70,18 +70,39 @@ class EmptyLinesAroundAttributeAccessor < Base
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) do |corrector|
range = range_by_whole_lines(node.source_range)
add_offense(node) { |corrector| autocorrect(corrector, node) }
end

private

corrector.insert_after(range, "\n")
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

private
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?
Expand Down
149 changes: 148 additions & 1 deletion spec/rubocop/cop/layout/empty_lines_around_attribute_accessor_spec.rb
Expand Up @@ -33,7 +33,130 @@ def do_something
RUBY
end

it 'accepts code that separates a attribute accessor from the code with a newline' do
it 'registers an offense and corrects for an attribute accessor and comment line' do
expect_offense(<<~RUBY)
attr_accessor :foo
^^^^^^^^^^^^^^^^^^ Add an empty line after attribute accessor.
# comment
def do_something
end
RUBY

expect_correction(<<~RUBY)
attr_accessor :foo
# comment
def do_something
end
RUBY
end

it 'registers an offense and corrects for some attribute accessors and comment line' do
expect_offense(<<~RUBY)
attr_accessor :foo
attr_reader :bar
attr_writer :baz
^^^^^^^^^^^^^^^^ Add an empty line after attribute accessor.
# comment
def do_something
end
RUBY

expect_correction(<<~RUBY)
attr_accessor :foo
attr_reader :bar
attr_writer :baz
# comment
def do_something
end
RUBY
end

it 'registers an offense and corrects for an attribute accessor and some comment line' do
expect_offense(<<~RUBY)
attr_accessor :foo
^^^^^^^^^^^^^^^^^^ Add an empty line after attribute accessor.
# comment
# comment
def do_something
end
RUBY

expect_correction(<<~RUBY)
attr_accessor :foo
# comment
# comment
def do_something
end
RUBY
end

it 'registers an offense and corrects for an attribute accessor and `rubocop:disable` ' \
'comment line' do
expect_offense(<<~RUBY)
attr_accessor :foo
^^^^^^^^^^^^^^^^^^ Add an empty line after attribute accessor.
# rubocop:disable Department/Cop
def do_something
end
RUBY

expect_correction(<<~RUBY)
attr_accessor :foo
# rubocop:disable Department/Cop
def do_something
end
RUBY
end

it 'registers an offense and corrects for an attribute accessor and `rubocop:enable` ' \
'comment line' do
expect_offense(<<~RUBY)
# rubocop:disable Department/Cop
attr_accessor :foo
^^^^^^^^^^^^^^^^^^ Add an empty line after attribute accessor.
# rubocop:enable Department/Cop
def do_something
end
RUBY

expect_correction(<<~RUBY)
# rubocop:disable Department/Cop
attr_accessor :foo
# rubocop:enable Department/Cop
def do_something
end
RUBY
end

it 'registers an offense and corrects for an attribute accessor and `rubocop:enable` ' \
'comment line and other comment' do
expect_offense(<<~RUBY)
# rubocop:disable Department/Cop
attr_accessor :foo
^^^^^^^^^^^^^^^^^^ Add an empty line after attribute accessor.
# rubocop:enable Department/Cop
# comment
def do_something
end
RUBY

expect_correction(<<~RUBY)
# rubocop:disable Department/Cop
attr_accessor :foo
# rubocop:enable Department/Cop
# comment
def do_something
end
RUBY
end

it 'accepts code that separates an attribute accessor from the code with a newline' do
expect_no_offenses(<<~RUBY)
attr_accessor :foo
Expand All @@ -42,6 +165,18 @@ def do_something
RUBY
end

it 'accepts code that separates an attribute accessor from the code and `rubocop:enable` ' \
'comment line with a newline' do
expect_no_offenses(<<~RUBY)
# rubocop:disable Department/Cop
attr_accessor :foo
# rubocop:enable Department/Cop
def do_something
end
RUBY
end

it 'accepts code that where the attr_accessor is the last line' do
expect_no_offenses('attr_accessor :foo')
end
Expand All @@ -57,6 +192,18 @@ def do_something
RUBY
end

it 'accepts code that separates attribute accessors from the code and comment line with a newline' do
expect_no_offenses(<<~RUBY)
attr_accessor :foo
attr_reader :bar
attr_writer :baz
# comment
def do_something
end
RUBY
end

it 'accepts code when used in class definition' do
expect_no_offenses(<<~RUBY)
class Foo
Expand Down

0 comments on commit 40e83ef

Please sign in to comment.