Skip to content

Commit

Permalink
[Fix rubocop#10877] Fix crash with Layout/BlockEndNewline heredoc d…
Browse files Browse the repository at this point in the history
…etection.
  • Loading branch information
dvandersluis committed Aug 6, 2022
1 parent 4f9cc54 commit e5bd111
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
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

0 comments on commit e5bd111

Please sign in to comment.