From 16531120c0a05d824c2ff1bc6ccb2bf070ad3f89 Mon Sep 17 00:00:00 2001 From: Anton Zaytsev Date: Wed, 3 Oct 2018 14:49:30 +0300 Subject: [PATCH] Fix a false positive for Layout/ClosingParenthesisIndentation This PR fixes a false negative for `Layout/ClosingParenthesisIndentation` when first argument is multiline. The following is a reproduction step. ```bash % rubocop -V 0.59.2 (using Parser 2.5.1.2, running on ruby 2.5.0 x86_64-darwin17) % cat app/models/users.rb class User < ApplicationRecord def self.complex_find where( "users.approved_at < ? OR users.approved_at IS NULL", 3.days.ago ) end end % rubocop app/models/users.rb --only Layout/ClosingParenthesisIndentation -d or /Users/anton/code/rubocop: configuration from /Users/anton/code/rubocop/.rubocop.yml configuration from /Users/anton/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/rubocop-rspec-1.29.1/config/default.yml configuration from /Users/anton/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/rubocop-rspec-1.29.1/config/default.yml Default configuration from /Users/anton/code/rubocop/config/default.yml Inheriting configuration from /Users/anton/code/rubocop/.rubocop_todo.yml Inspecting 1 file Scanning /Users/anton/code/rubocop/users.rb C Offenses: users.rb:6:5: C: Layout/ClosingParenthesisIndentation: Indent ) to column 5 (not 4) ) ^ 1 file inspected, 1 offense detected Finished in 0.5669199999974808 seconds ``` --- CHANGELOG.md | 2 ++ .../layout/closing_parenthesis_indentation.rb | 6 +++--- .../closing_parenthesis_indentation_spec.rb | 21 +++++++++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71ae23a51ff..48894360278 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ * [#6389](https://github.com/rubocop-hq/rubocop/pull/6389): Fix false negative for `Style/TrailingCommaInHashLitera`/`Style/TrailingCommaInArrayLiteral` when there is a comment in the last line. ([@bayandin][]) * [#6566](https://github.com/rubocop-hq/rubocop/issues/6566): Fix false positive for `Layout/EmptyLinesAroundAccessModifier` when at the end of specifying a superclass is missing blank line. ([@koic][]) * [#6571](https://github.com/rubocop-hq/rubocop/issues/6571): Fix a false positive for `Layout/TrailingCommaInArguments` when a line break before a method call and `EnforcedStyleForMultiline` is set to `consistent_comma`. ([@koic][]) +* [#6351](https://github.com/rubocop-hq/rubocop/pull/6351): Fix a false positive for `Layout/ClosingParenthesisIndentation` when first argument is multiline. ([@antonzaytsev][]) ## 0.61.1 (2018-12-06) @@ -3696,3 +3697,4 @@ [@tom-lord]: https://github.com/tom-lord [@bayandin]: https://github.com/bayandin [@nadiyaka]: https://github.com/nadiyaka +[@antonzaytsev]: https://github.com/antonzaytsev diff --git a/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb b/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb index e7e2442c83e..6aa33ebc35b 100644 --- a/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +++ b/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb @@ -146,7 +146,7 @@ def expected_column(left_paren, elements) left_paren.column else source_indent = processed_source - .line_indentation(last_argument_line(elements)) + .line_indentation(first_argument_line(elements)) new_indent = source_indent - indentation_width new_indent < 0 ? 0 : new_indent @@ -160,9 +160,9 @@ def all_elements_aligned?(elements) .count == 1 end - def last_argument_line(elements) + def first_argument_line(elements) elements - .last + .first .loc .first_line end diff --git a/spec/rubocop/cop/layout/closing_parenthesis_indentation_spec.rb b/spec/rubocop/cop/layout/closing_parenthesis_indentation_spec.rb index 3bcc563ab35..83cd03636f1 100644 --- a/spec/rubocop/cop/layout/closing_parenthesis_indentation_spec.rb +++ b/spec/rubocop/cop/layout/closing_parenthesis_indentation_spec.rb @@ -495,6 +495,27 @@ def some_method() end end + context 'method call with first multiline arg on new line' do + it 'accepts ) on the same level as ( with args on same line' do + expect_no_offenses(<<-RUBY.strip_indent) + where( + "multiline + condition", second_arg + ) + RUBY + end + + it 'accepts ) on the same level as ( with second arg on new line' do + expect_no_offenses(<<-RUBY.strip_indent) + where( + "multiline + condition", + second_arg + ) + RUBY + end + end + it 'accepts begin nodes that are not grouped expressions' do expect_no_offenses(<<-RUBY.strip_indent) def a