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 an incorrect auto-correct for Layout/LineLength #9938

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
@@ -0,0 +1 @@
* [#9938](https://github.com/rubocop/rubocop/pull/9938): Fix an incorrect auto-correct for `Layout/LineLength` when a heredoc is used as the first element of an array. ([@koic][])
4 changes: 2 additions & 2 deletions lib/rubocop/cop/mixin/check_line_breakable.rb
Expand Up @@ -97,9 +97,9 @@ def first_argument_is_heredoc?(node)
# If a send node contains a heredoc argument, splitting cannot happen
# after the heredoc or else it will cause a syntax error.
def shift_elements_for_heredoc_arg(node, elements, index)
return index unless node.send_type?
return index unless node.send_type? || node.array_type?

heredoc_index = elements.index { |arg| (arg.str_type? || arg.dstr_type?) && arg.heredoc? }
heredoc_index = elements.index { |arg| arg.respond_to?(:heredoc?) && arg.heredoc? }
return index unless heredoc_index
return nil if heredoc_index.zero?

Expand Down
10 changes: 10 additions & 0 deletions spec/rubocop/cop/layout/line_length_spec.rb
Expand Up @@ -779,6 +779,16 @@ def baz(bar)
expect_no_corrections
end

it 'does not break up the line when a heredoc is used as the first element of an array' do
expect_offense(<<~RUBY)
[<<~STRING, { key1: value1, key2: value2 }]
^^^ Line is too long. [43/40]
STRING
RUBY

expect_no_corrections
end

context 'and other arguments before the heredoc' do
it 'can break up the line before the heredoc argument' do
args = 'x' * 20
Expand Down