Skip to content

Commit

Permalink
[FIX #4563] Style/TrailingUnderscoreVariable cop leaves an unclosed p…
Browse files Browse the repository at this point in the history
…arenthesis
  • Loading branch information
smakagon authored and bbatsov committed Jul 2, 2017
1 parent 1c133cd commit 122b993
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* Add new `Style/Dir` cop. ([@drenmi][])
* Add new `Style/HeredocDelimiterCase` cop. ([@drenmi][])
* [#2943](https://github.com/bbatsov/rubocop/pull/2943): Add new `Lint/RescueWithoutErrorClass` cop. ([@drenmi][])
* [#4568](https://github.com/bbatsov/rubocop/pull/4568): Fix autocorrection for `Style/TrailingUnderscoreVariable`. ([@smakagon][])

### Bug fixes

Expand Down
37 changes: 28 additions & 9 deletions lib/rubocop/cop/style/trailing_underscore_variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,35 @@ def unneeded_range(node)

return unless first_offense

end_position =
if first_offense.source_range == variables.first.source_range
right.source_range.begin_pos
else
node.loc.operator.begin_pos
end
if unused_variables_only?(first_offense, variables)
return left_side_range(left, right)
end

if Util.parentheses?(left)
return range_for_parentheses(first_offense, left)
end

range_between(
first_offense.source_range.begin_pos,
node.loc.operator.begin_pos
)
end

def unused_variables_only?(offense, variables)
offense.source_range == variables.first.source_range
end

def left_side_range(left, right)
range_between(
left.source_range.begin_pos, right.source_range.begin_pos
)
end

range = range_between(first_offense.source_range.begin_pos,
end_position)
range_with_surrounding_space(range, :right)
def range_for_parentheses(offense, left)
range_between(
offense.source_range.begin_pos - 1,
left.loc.expression.end_pos - 1
)
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions spec/rubocop/cop/style/trailing_underscore_variable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,32 @@

expect(new_source).to eq('a, = foo()')
end

context 'with parentheses' do
it 'leaves parentheses but removes trailing underscores' do
new_source = autocorrect_source('(a, b, _) = foo()')

expect(new_source).to eq('(a, b,) = foo()')
end

it 'removes assignment part when every assignment is to `_`' do
new_source = autocorrect_source('(_, _, _,) = foo()')

expect(new_source).to eq('foo()')
end

it 'removes assignment part when it is the only variable' do
new_source = autocorrect_source('(_,) = foo()')

expect(new_source).to eq('foo()')
end

it 'leaves parentheses but removes trailing underscores and commas' do
new_source = autocorrect_source('(a, _, _,) = foo()')

expect(new_source).to eq('(a,) = foo()')
end
end
end
end

Expand Down

0 comments on commit 122b993

Please sign in to comment.