Skip to content

Commit

Permalink
Fix a false positive for Layout/ClosingParenthesisIndentation
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
antonzaytsev committed Dec 17, 2018
1 parent f2ec4a6 commit 1653112
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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)

Expand Down Expand Up @@ -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
6 changes: 3 additions & 3 deletions lib/rubocop/cop/layout/closing_parenthesis_indentation.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions spec/rubocop/cop/layout/closing_parenthesis_indentation_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit 1653112

Please sign in to comment.