Skip to content

Commit

Permalink
[Fix rubocop#9161] Fix a false positive for `Layout/HeredocArgumentCl…
Browse files Browse the repository at this point in the history
…osingParenthesis`

Fixes rubocop#9161.

This PR fixes a false positive for `Layout/HeredocArgumentClosingParenthesis`
when using subsequence closing parentheses in the same line.
  • Loading branch information
koic committed Dec 4, 2020
1 parent 4336f72 commit e268348
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#9161](https://github.com/rubocop-hq/rubocop/issues/9161): Fix a false positive for `Layout/HeredocArgumentClosingParenthesis` when using subsequence closing parentheses in the same line. ([@koic][])
12 changes: 12 additions & 0 deletions lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def on_send(node)
return unless outermost_send
return unless outermost_send.loc.end
return unless heredoc_arg.first_line != outermost_send.loc.end.line
return if subsequent_closing_parentheses_in_same_line?(outermost_send)

add_offense(outermost_send.loc.end) do |corrector|
autocorrect(corrector, outermost_send)
Expand Down Expand Up @@ -160,6 +161,17 @@ def single_line_send_with_heredoc_receiver?(node)

# Closing parenthesis helpers.

def subsequent_closing_parentheses_in_same_line?(outermost_send)
last_arg_of_outer_send = outermost_send.last_argument
return false unless last_arg_of_outer_send&.loc.respond_to?(:end) &&
(end_of_last_arg_of_outer_send = last_arg_of_outer_send.loc.end)

end_of_outer_send = outermost_send.loc.end

end_of_outer_send.line == end_of_last_arg_of_outer_send.line &&
end_of_outer_send.column == end_of_last_arg_of_outer_send.column + 1
end

def fix_closing_parenthesis(node, corrector)
remove_incorrect_closing_paren(node, corrector)
add_correct_closing_paren(node, corrector)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@
RUBY
end

it 'accepts method chain with heredoc argument correct case' do
expect_no_offenses(<<~RUBY)
do_something(
Model
.foo(<<~CODE)
code
CODE
.bar(<<~CODE))
code
CODE
RUBY
end

it 'accepts method with heredoc argument of proc correct case' do
expect_no_offenses(<<~RUBY)
outer_method(-> {
inner_method(<<~CODE)
code
CODE
})
RUBY
end

it 'accepts double correct case nested' do
expect_no_offenses(<<~RUBY)
baz(bar(foo(<<-SQL, <<-NOSQL)))
Expand Down

0 comments on commit e268348

Please sign in to comment.