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 #8108] Disregard whitespace lines in HeredocIndentation #8109

Merged
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 @@ -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