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 #9882

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 @@
* [#9882](https://github.com/rubocop/rubocop/pull/9882): Fix an incorrect auto-correct for `Layout/LineLength` when using heredoc as the first method argument and omitting parentheses. ([@koic][])
11 changes: 10 additions & 1 deletion lib/rubocop/cop/mixin/check_line_breakable.rb
Expand Up @@ -72,7 +72,9 @@ def extract_first_element_over_column_limit(node, elements, max)

# If a `send` node is not parenthesized, don't move the first element, because it
# can result in changed behavior or a syntax error.
elements = elements.drop(1) if node.send_type? && !node.parenthesized?
if node.send_type? && !node.parenthesized? && !first_argument_is_heredoc?(node)
elements = elements.drop(1)
end

i = 0
i += 1 while within_column_limit?(elements[i], max, line)
Expand All @@ -84,6 +86,13 @@ def extract_first_element_over_column_limit(node, elements, max)
elements[i - 1]
end

# @api private
def first_argument_is_heredoc?(node)
first_argument = node.first_argument

first_argument.respond_to?(:heredoc?) && first_argument.heredoc?
end

# @api private
# If a send node contains a heredoc argument, splitting cannot happen
# after the heredoc or else it will cause a syntax error.
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/layout/line_length_spec.rb
Expand Up @@ -768,6 +768,17 @@ def baz(bar)
expect_no_corrections
end

it 'does not break up the line when parentheses are omitted' do
args = 'x' * 25
expect_offense(<<~RUBY, args: args)
foo <<~STRING, #{args}xxx
_{args}^^^ 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