Skip to content

Commit

Permalink
[Fix #8108] Disregard whitespace lines in HeredocIndentation
Browse files Browse the repository at this point in the history
Empty lines were already excluded for calculation of indentation in
Layout/HeredocIndentation. We need to do the same thing for lines
consisting only of whitespace.
  • Loading branch information
jonas054 committed Jun 7, 2020
1 parent 20f1231 commit 32de264
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* [#8083](https://github.com/rubocop-hq/rubocop/issues/8083): Fix an error for `Lint/MixedRegexpCaptureTypes` cop when using a regular expression that cannot be processed by regexp_parser gem. ([@koic][])
* [#8081](https://github.com/rubocop-hq/rubocop/issues/8081): Fix a false positive for `Lint/SuppressedException` when empty rescue block in `do` block. ([@koic][])
* [#8096](https://github.com/rubocop-hq/rubocop/issues/8096): Fix a false positive for `Lint/SuppressedException` when empty rescue block in defs. ([@koic][])
* [#8108](https://github.com/rubocop-hq/rubocop/issues/8108): Fix infinite loop in `Layout/HeredocIndentation` auto-correct. ([@jonas054][])

## 0.85.0 (2020-06-01)

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/heredoc_indentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def base_indent_level(node)
def indent_level(str)
indentations = str.lines
.map { |line| line[/^\s*/] }
.reject { |line| line == "\n" }
.reject { |line| line.end_with?("\n") }
indentations.empty? ? 0 : indentations.min_by(&:size).size
end

Expand Down
62 changes: 42 additions & 20 deletions spec/rubocop/cop/layout/heredoc_indentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,29 +189,51 @@ def foo
RUBY
end

it 'registers an offense for not indented enough with empty lines' do
# rubocop:disable Layout/HeredocIndentation
expect_offense(<<-RUBY)
def baz
<<~#{quote}MSG#{quote}
foo
^^^^^^^^^^^^^^^ Use 2 spaces for indentation in a heredoc.
bar
MSG
end
RUBY
# rubocop:enable Layout/HeredocIndentation

expect_correction(<<-CORRECTION)
def baz
<<~#{quote}MSG#{quote}
{ empty: '', whitespace: ' ' }.each do |description, line|
it "registers an offense for not indented enough with #{description} line" do
# Using <<- in this section makes the code more readable.
# rubocop:disable Layout/HeredocIndentation
expect_offense(<<-RUBY)
def baz
<<~#{quote}MSG#{quote}
foo
^^^^^^^^^^^^^^^^^ Use 2 spaces for indentation in a heredoc.
#{line}
bar
MSG
end
RUBY

expect_correction(<<-CORRECTION)
def baz
<<~#{quote}MSG#{quote}
foo
#{line}
bar
MSG
end
CORRECTION
end

it "registers an offense for too deep indented with #{description} line" do
expect_offense(<<-RUBY)
<<~#{quote}RUBY2#{quote}
foo
^^^^^^^^^^^^^^^^^^^^^ Use 2 spaces for indentation in a heredoc.
#{line}
bar
MSG
end
CORRECTION
RUBY2
RUBY

expect_correction(<<-CORRECTION)
<<~#{quote}RUBY2#{quote}
foo
#{line}
bar
RUBY2
CORRECTION
end
# rubocop:enable Layout/HeredocIndentation
end

it 'displays message to use `<<~` instead of `<<`' do
Expand Down

0 comments on commit 32de264

Please sign in to comment.