Skip to content

Commit

Permalink
Merge pull request #8109 from jonas054/8108_heredoc_trailing_ws_infin…
Browse files Browse the repository at this point in the history
…ite_loop

[Fix #8108] Disregard whitespace lines in HeredocIndentation
  • Loading branch information
koic committed Jun 7, 2020
2 parents 20f1231 + 32de264 commit dc757dd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
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
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
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 dc757dd

Please sign in to comment.