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 #10877] Fix crash with Layout/BlockEndNewline heredoc detection. #10878

Merged
merged 1 commit into from Aug 6, 2022
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
1 change: 1 addition & 0 deletions changelog/fix_fix_crash_with_layoutblockendnewline.md
@@ -0,0 +1 @@
* [#10877](https://github.com/rubocop/rubocop/issues/10877): Fix crash with `Layout/BlockEndNewline` heredoc detection. ([@dvandersluis][])
3 changes: 2 additions & 1 deletion lib/rubocop/cop/layout/block_end_newline.rb
Expand Up @@ -60,9 +60,10 @@ def message(node)
end

def last_heredoc_argument(node)
return unless node&.call_type?
return unless (arguments = node&.arguments)

heredoc = arguments.reverse.detect(&:heredoc?)
heredoc = arguments.reverse.detect { |arg| arg.str_type? && arg.heredoc? }
return heredoc if heredoc

last_heredoc_argument(node.children.first)
Expand Down
28 changes: 28 additions & 0 deletions spec/rubocop/cop/layout/block_end_newline_spec.rb
Expand Up @@ -154,4 +154,32 @@
}
RUBY
end

it 'registers an offense and corrects when a multiline block ends with a hash' do
expect_offense(<<~RUBY)
foo {
{ bar: :baz } }
^ Expression at 2, 17 should be on its own line.
RUBY

expect_correction(<<~RUBY)
foo {
{ bar: :baz }
}
RUBY
end

it 'registers an offense and corrects when a multiline block ends with a method call with hash arguments' do
expect_offense(<<~RUBY)
foo {
bar(baz: :quux) }
^ Expression at 2, 19 should be on its own line.
RUBY

expect_correction(<<~RUBY)
foo {
bar(baz: :quux)
}
RUBY
end
end