From 903e9c14efae0131a7b1aa2901cd71a926d252ee Mon Sep 17 00:00:00 2001 From: Konstantin Kazarin Date: Mon, 27 Jun 2022 13:43:07 +0600 Subject: [PATCH] [Fix #731] Fix an incorrect autocorrect for Rails/StripHeredoc --- lib/rubocop/cop/rails/strip_heredoc.rb | 2 +- spec/rubocop/cop/rails/strip_heredoc_spec.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/rubocop/cop/rails/strip_heredoc.rb b/lib/rubocop/cop/rails/strip_heredoc.rb index 1c26b83839..201ad0463b 100644 --- a/lib/rubocop/cop/rails/strip_heredoc.rb +++ b/lib/rubocop/cop/rails/strip_heredoc.rb @@ -43,7 +43,7 @@ def on_send(node) def register_offense(node, heredoc) add_offense(node) do |corrector| - squiggly_heredoc = heredoc.source.sub(/\A<<-?/, '<<~') + squiggly_heredoc = heredoc.source.sub(/\A<<(-|~)?/, '<<~') corrector.replace(heredoc, squiggly_heredoc) corrector.remove(node.loc.dot) diff --git a/spec/rubocop/cop/rails/strip_heredoc_spec.rb b/spec/rubocop/cop/rails/strip_heredoc_spec.rb index 40f4db0afd..b31eb8c4d1 100644 --- a/spec/rubocop/cop/rails/strip_heredoc_spec.rb +++ b/spec/rubocop/cop/rails/strip_heredoc_spec.rb @@ -74,6 +74,21 @@ RUBY end + it 'registers an offence when squiggly already present' do + expect_offense(<<~RUBY) + <<~EOS.strip_heredoc + ^^^^^^^^^^^^^^^^^^^^ Use squiggly heredoc (`<<~`) instead of `strip_heredoc`. + some text + EOS + RUBY + + expect_correction(<<~RUBY) + <<~EOS + some text + EOS + RUBY + end + it 'does not register an offense when using squiggly heredoc' do expect_no_offenses(<<~RUBY) <<~EOS