From 32de26447ce4233854f4a1eefae980ce2fa3562e Mon Sep 17 00:00:00 2001 From: Jonas Arvidsson Date: Sat, 6 Jun 2020 08:28:26 +0200 Subject: [PATCH] [Fix #8108] Disregard whitespace lines in HeredocIndentation 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. --- CHANGELOG.md | 1 + lib/rubocop/cop/layout/heredoc_indentation.rb | 2 +- .../cop/layout/heredoc_indentation_spec.rb | 62 +++++++++++++------ 3 files changed, 44 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93b077e24c8..73f478c19c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/rubocop/cop/layout/heredoc_indentation.rb b/lib/rubocop/cop/layout/heredoc_indentation.rb index 6725994457a..e2c617567ac 100644 --- a/lib/rubocop/cop/layout/heredoc_indentation.rb +++ b/lib/rubocop/cop/layout/heredoc_indentation.rb @@ -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 diff --git a/spec/rubocop/cop/layout/heredoc_indentation_spec.rb b/spec/rubocop/cop/layout/heredoc_indentation_spec.rb index 491ca3fecc2..f0b37939b45 100644 --- a/spec/rubocop/cop/layout/heredoc_indentation_spec.rb +++ b/spec/rubocop/cop/layout/heredoc_indentation_spec.rb @@ -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