Skip to content

Commit

Permalink
Merge pull request #6130 from tatsuyafw/fix-error-for-layout-closing-…
Browse files Browse the repository at this point in the history
…parenthesis-indentation

[Fix #6127] Fix an error for `Layout/ClosingParenthesisIndentation` cop
  • Loading branch information
koic committed Aug 6, 2018
2 parents 31e686a + b276d13 commit 63cdaa2
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

* [#6132](https://github.com/rubocop-hq/rubocop/issues/6132): Fix a false negative for `Naming/FileName` when `Include` of `AllCops` is the default setting. ([@koic][])
* [#4115](https://github.com/rubocop-hq/rubocop/issues/4115): Fix false positive for unary operations in `Layout/MultilineOperationIndentation`. ([@jonas054][])
* [#6127](https://github.com/rubocop-hq/rubocop/issues/6127): Fix an error for `Layout/ClosingParenthesisIndentation` when method arguments are empty with newlines. ([@tatsuyafw][])

### Changes

Expand Down
35 changes: 35 additions & 0 deletions lib/rubocop/cop/layout/closing_parenthesis_indentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ def autocorrect(node)
private

def check(node, elements)
if elements.empty?
check_for_no_elements(node)
else
check_for_elements(node, elements)
end
end

def check_for_elements(node, elements)
left_paren = node.loc.begin
right_paren = node.loc.end

Expand All @@ -113,6 +121,25 @@ def check(node, elements)
right_paren))
end

def check_for_no_elements(node)
left_paren = node.loc.begin
right_paren = node.loc.end
return unless right_paren && begins_its_line?(right_paren)

candidates = correct_column_candidates(node, left_paren)

return if candidates.include?(right_paren.column)

# Although there are multiple choices for a correct column,
# select the first one of candidates to determine a specification.
correct_column = candidates.first
add_offense(right_paren,
location: right_paren,
message: message(correct_column,
left_paren,
right_paren))
end

def expected_column(left_paren, elements)
if !line_break_after_left_paren?(left_paren, elements) &&
all_elements_aligned?(elements)
Expand Down Expand Up @@ -140,6 +167,14 @@ def last_argument_line(elements)
.first_line
end

def correct_column_candidates(node, left_paren)
[
processed_source.line_indentation(left_paren.line),
left_paren.column,
node.loc.column
]
end

def message(correct_column, left_paren, right_paren)
if correct_column == left_paren.column
MSG_ALIGN
Expand Down
64 changes: 55 additions & 9 deletions spec/rubocop/cop/layout/closing_parenthesis_indentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@
RUBY
end

it 'accepts empty ()' do
expect_no_offenses('some_method()')
end

it 'accepts a correctly indented )' do
expect_no_offenses(<<-RUBY.strip_indent)
some_method(a,
Expand Down Expand Up @@ -100,6 +96,26 @@
RUBY
end
end

context 'without arguments' do
it 'accepts empty ()' do
expect_no_offenses('some_method()')
end

it 'can handle indentation up against the left edge' do
expect_no_offenses(<<-RUBY.strip_indent)
some_method(
)
RUBY
end

it 'accepts a correctly aligned ) against (' do
expect_no_offenses(<<-RUBY.strip_indent)
some_method(
)
RUBY
end
end
end

context 'for method assignments with indented parameters' do
Expand Down Expand Up @@ -197,11 +213,6 @@
)
RUBY
end

it 'accepts empty ()' do
expect_no_offenses('foo = some_method()')
end

it 'accepts a correctly indented )' do
expect_no_offenses(<<-RUBY.strip_indent)
foo = some_method(a,
Expand Down Expand Up @@ -235,6 +246,41 @@
RUBY
end
end

context 'without arguments' do
it 'accepts empty ()' do
expect_no_offenses('foo = some_method()')
end

it 'accepts a correctly aligned ) against (' do
expect_no_offenses(<<-RUBY.strip_indent)
foo = some_method(
)
RUBY
end

it 'can handle indentation up against the left edge' do
expect_no_offenses(<<-RUBY.strip_indent)
foo = some_method(
)
RUBY
end

it 'can handle indentation up against the method' do
expect_no_offenses(<<-RUBY.strip_indent)
foo = some_method(
)
RUBY
end

it 'registers an offense for misaligned )' do
expect_offense(<<-RUBY.strip_indent)
foo = some_method(
)
^ Indent `)` to column 0 (not 2)
RUBY
end
end
end

context 'for method chains' do
Expand Down

0 comments on commit 63cdaa2

Please sign in to comment.