Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #10693] Add ignore case for Style/EmptyLinesAroundAttributeAccessor when there is a comment line on the next line. #10694

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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
ydah marked this conversation as resolved.
Show resolved Hide resolved
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