Skip to content

Commit

Permalink
Fix an error for Layout/HeredocArgumentClosingParenthesis
Browse files Browse the repository at this point in the history
This PR fixes an error for `Layout/HeredocArgumentClosingParenthesis`
when heredoc is a method argument in a parenthesized block argument:

```ruby
foo(bar do
  baz <<~EOS
  EOS
end)
```

```console
% bundle exec rubocop --only Layout/HeredocArgumentClosingParenthesis
(snip)

undefined method `begin_pos' for nil:NilClass
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/layout/
heredoc_argument_closing_parenthesis.rb:220:in `exist_argument_between_heredoc_end_and_closing_parentheses?'
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/layout/
heredoc_argument_closing_parenthesis.rb:73:in `on_send'
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/commissioner.rb:107:in `public_send'
```
  • Loading branch information
koic authored and bbatsov committed Feb 15, 2023
1 parent 582036c commit 417c729
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#11579](https://github.com/rubocop/rubocop/pull/11579): Fix an error for `Layout/HeredocArgumentClosingParenthesis` when heredoc is a method argument in a parenthesized block argument. ([@koic][])
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ def incorrect_parenthesis_removal_end(node)
end

def exist_argument_between_heredoc_end_and_closing_parentheses?(node)
return true unless node.loc.end
return false unless (heredoc_end = find_most_bottom_of_heredoc_end(node.arguments))

heredoc_end < node.loc.end.begin_pos &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@
RUBY
end

it 'accepts when heredoc is a method argument in a parenthesized block argument' do
expect_no_offenses(<<~RUBY)
foo(bar do
baz <<~EOS
text
EOS
end)
RUBY
end

it 'accepts correct case with other param after' do
expect_no_offenses(<<~RUBY)
foo(<<-SQL, 123)
Expand Down

0 comments on commit 417c729

Please sign in to comment.