From 76fc82b24a3cab961ad25e5d26e655629bae5c12 Mon Sep 17 00:00:00 2001 From: Andrei Eres Date: Sun, 13 Jun 2021 23:16:31 +0300 Subject: [PATCH] Fix empty line after guard clause with `and return` and heredoc --- ..._fix_empty_line_after_guard_clause_with.md | 1 + .../layout/empty_line_after_guard_clause.rb | 28 +++++++------- .../empty_line_after_guard_clause_spec.rb | 37 +++++++++++++++++++ 3 files changed, 51 insertions(+), 15 deletions(-) create mode 100644 changelog/fix_fix_empty_line_after_guard_clause_with.md diff --git a/changelog/fix_fix_empty_line_after_guard_clause_with.md b/changelog/fix_fix_empty_line_after_guard_clause_with.md new file mode 100644 index 00000000000..4d57a1a1ec1 --- /dev/null +++ b/changelog/fix_fix_empty_line_after_guard_clause_with.md @@ -0,0 +1 @@ +* [#9876](https://github.com/rubocop/rubocop/pull/9876): Fix empty line after guard clause with `and return` and heredoc. ([@AndreiEres][]) diff --git a/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb b/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb index 213180af3e2..6186eea891e 100644 --- a/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +++ b/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb @@ -45,8 +45,7 @@ class EmptyLineAfterGuardClause < Base def on_if(node) return if correct_style?(node) - if node.modifier_form? && last_argument_is_heredoc?(node) - heredoc_node = last_heredoc_argument(node) + if node.modifier_form? && (heredoc_node = last_heredoc_argument(node)) return if next_line_empty_or_enable_directive_comment?(heredoc_line(node, heredoc_node)) add_offense(heredoc_node.loc.heredoc_end) do |corrector| @@ -62,7 +61,7 @@ def on_if(node) private def autocorrect(corrector, node) - node_range = if node.respond_to?(:heredoc?) && node.heredoc? + node_range = if heredoc?(node) range_by_whole_lines(node.loc.heredoc_body) else range_by_whole_lines(node.source_range) @@ -125,19 +124,8 @@ def next_sibling_empty_or_guard_clause?(node) next_sibling.if_type? && contains_guard_clause?(next_sibling) end - def last_argument_is_heredoc?(node) - last_children = node.if_branch - return false unless last_children&.send_type? - - heredoc?(last_heredoc_argument(node)) - end - def last_heredoc_argument(node) - n = if node.respond_to?(:if_branch) - node.if_branch.children.last - else - node - end + n = last_heredoc_argument_node(node) return n if heredoc?(n) return unless n.respond_to?(:arguments) @@ -150,6 +138,16 @@ def last_heredoc_argument(node) return last_heredoc_argument(n.receiver) if n.respond_to?(:receiver) end + def last_heredoc_argument_node(node) + return node unless node.respond_to?(:if_branch) + + if node.if_branch.and_type? + node.if_branch.children.first + else + node.if_branch.children.last + end + end + def heredoc_line(node, heredoc_node) heredoc_body = heredoc_node.loc.heredoc_body num_of_heredoc_lines = heredoc_body.last_line - heredoc_body.first_line diff --git a/spec/rubocop/cop/layout/empty_line_after_guard_clause_spec.rb b/spec/rubocop/cop/layout/empty_line_after_guard_clause_spec.rb index ff6260a3982..db35f3ea559 100644 --- a/spec/rubocop/cop/layout/empty_line_after_guard_clause_spec.rb +++ b/spec/rubocop/cop/layout/empty_line_after_guard_clause_spec.rb @@ -523,4 +523,41 @@ def foo end RUBY end + + it 'registers no offenses using heredoc with `and return` before guard condition with empty line' do + expect_no_offenses(<<~RUBY) + def foo + puts(<<~MSG) and return if bar + A multiline + message + MSG + + baz + end + RUBY + end + + it 'registers an offense and corrects using heredoc with `and return` before guard condition' do + expect_offense(<<~RUBY) + def foo + puts(<<~MSG) and return if bar + A multiline + message + MSG + ^^^^^ Add empty line after guard clause. + baz + end + RUBY + + expect_correction(<<~RUBY) + def foo + puts(<<~MSG) and return if bar + A multiline + message + MSG + + baz + end + RUBY + end end