diff --git a/changelog/fix_an_error_for_style_string_concatenation.md b/changelog/fix_an_error_for_style_string_concatenation.md new file mode 100644 index 00000000000..6ba9825592e --- /dev/null +++ b/changelog/fix_an_error_for_style_string_concatenation.md @@ -0,0 +1 @@ +* [#10403](https://github.com/rubocop/rubocop/issues/10403): Fix an error for `Style/StringConcatenation` when string concatenation with multiline heredoc text. ([@koic][]) diff --git a/lib/rubocop/cop/style/string_concatenation.rb b/lib/rubocop/cop/style/string_concatenation.rb index 4b302534799..d9ff5b4b104 100644 --- a/lib/rubocop/cop/style/string_concatenation.rb +++ b/lib/rubocop/cop/style/string_concatenation.rb @@ -134,7 +134,13 @@ def plus_node?(node) end def uncorrectable?(part) - part.multiline? || (part.str_type? && part.heredoc?) || part.each_descendant(:block).any? + part.multiline? || heredoc?(part) || part.each_descendant(:block).any? + end + + def heredoc?(node) + return false unless node.str_type? || node.dstr_type? + + node.heredoc? end def corrected_ancestor?(node) diff --git a/spec/rubocop/cop/style/string_concatenation_spec.rb b/spec/rubocop/cop/style/string_concatenation_spec.rb index 2242d7b0baa..86cead9b419 100644 --- a/spec/rubocop/cop/style/string_concatenation_spec.rb +++ b/spec/rubocop/cop/style/string_concatenation_spec.rb @@ -143,6 +143,18 @@ expect_no_corrections end + + it 'registers an offense but does not correct when string concatenation with multiline heredoc text' do + expect_offense(<<~RUBY) + "foo" + <<~TEXT + ^^^^^^^^^^^^^^^ Prefer string interpolation to string concatenation. + bar + baz + TEXT + RUBY + + expect_no_corrections + end end context 'double quotes inside string' do