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

handle autocorrect exception in Style/BlockComments #6856

Merged
merged 1 commit into from Mar 23, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

### Bug fixes
* [#6855](https://github.com/rubocop-hq/rubocop/pull/6855): Fix an exception in `Rails/RedundantReceiverInWithOptions` when the body is empty. ([@ericsullivan][])
* [#6856](https://github.com/rubocop-hq/rubocop/pull/6856): Fix auto-correction for `Style/BlockComments` when the file is missing a trailing blank line. ([@ericsullivan][])

### Changes

Expand Down
10 changes: 9 additions & 1 deletion lib/rubocop/cop/style/block_comments.rb
Expand Up @@ -52,10 +52,18 @@ def autocorrect(comment)
def parts(comment)
expr = comment.loc.expression
eq_begin = expr.resize(BEGIN_LENGTH)
eq_end = range_between(expr.end_pos - END_LENGTH, expr.end_pos)
eq_end = eq_end_part(comment, expr)
contents = range_between(eq_begin.end_pos, eq_end.begin_pos)
[eq_begin, eq_end, contents]
end

def eq_end_part(comment, expr)
if comment.text.chomp == comment.text
range_between(expr.end_pos - END_LENGTH - 1, expr.end_pos - 2)
else
range_between(expr.end_pos - END_LENGTH, expr.end_pos)
end
end
end
end
end
Expand Down
21 changes: 21 additions & 0 deletions spec/rubocop/cop/style/block_comments_spec.rb
Expand Up @@ -47,4 +47,25 @@ def foo
end
RUBY
end

it 'auto-corrects a block comment into a regular comment (without trailing' \
'newline)' do
source = <<-RUBY.strip_indent
=begin
comment line 1

comment line 2
=end
RUBY

new_source = autocorrect_source(source.chomp)

expected_source = <<-RUBY.strip_indent
# comment line 1
#
# comment line 2
RUBY

expect(new_source).to eq(expected_source)
end
end