Skip to content

Commit

Permalink
[Fix #6653] IndentHeredoc: Do not run into next line
Browse files Browse the repository at this point in the history
When finding the indentation whitespace to be replaced in a multiline
string, the regexp `/\s*/` could run into the next line (because it
matches `\n`).
  • Loading branch information
buehmann authored and bbatsov committed Jul 15, 2019
1 parent e360c5d commit d3a9e2f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,7 @@
* [#5088](https://github.com/rubocop-hq/rubocop/issues/5088): Fix an error of `Layout/MultilineMethodCallIndentation` on method chains inside an argument. ([@buehmann][])
* [#4719](https://github.com/rubocop-hq/rubocop/issues/4719): Make `Layout/Tab` detect tabs between string literals. ([@buehmann][])
* [#7203](https://github.com/rubocop-hq/rubocop/pull/7203): Fix an infinite loop error for `Layout/SpaceInsideBlockBraces` when `EnforcedStyle: no_space` with `SpaceBeforeBlockParameters: false` are set in multiline block. ([@koic][])
* [#6653](https://github.com/rubocop-hq/rubocop/issues/6653): Fix a bug where `Layout/IndentHeredoc` would remove empty lines when autocorrecting heredocs. ([@buehmann][])

### Changes

Expand Down
5 changes: 3 additions & 2 deletions lib/rubocop/cop/layout/indent_heredoc.rb
Expand Up @@ -37,7 +37,7 @@ module Layout
# # good
# # When EnforcedStyle is powerpack, bad code is auto-corrected to
# # the following code.
# <<~RUBY
# <<-RUBY.strip_indent
# something
# RUBY
#
Expand Down Expand Up @@ -206,7 +206,8 @@ def indented_body(node)
body = heredoc_body(node)
body_indent_level = indent_level(body)
correct_indent_level = base_indent_level(node) + indentation_width
body.gsub(/^\s{#{body_indent_level}}/, ' ' * correct_indent_level)
body.gsub(/^[^\S\r\n]{#{body_indent_level}}/,
' ' * correct_indent_level)
end

def indented_end(node)
Expand Down
2 changes: 1 addition & 1 deletion manual/cops_layout.md
Expand Up @@ -2621,7 +2621,7 @@ RUBY
# good
# When EnforcedStyle is powerpack, bad code is auto-corrected to
# the following code.
<<~RUBY
<<-RUBY.strip_indent
something
RUBY
```
Expand Down
19 changes: 19 additions & 0 deletions spec/rubocop/cop/layout/indent_heredoc_spec.rb
Expand Up @@ -274,6 +274,25 @@ def foo
MSG
RUBY

include_examples 'offense', 'not indented enough with empty lines',
<<-RUBY, <<-CORRECTION
def baz
<<~#{quote}MSG#{quote}
foo
bar
MSG
end
RUBY
def baz
<<~#{quote}MSG#{quote}
foo
bar
MSG
end
CORRECTION

it 'displays message to use `<<~` instead of `<<`' do
expect_offense(<<~RUBY)
<<RUBY2
Expand Down

0 comments on commit d3a9e2f

Please sign in to comment.