Skip to content

Commit

Permalink
Do not register an offense for TrailingUnderscoreVariable preceeded b…
Browse files Browse the repository at this point in the history
…y a splat variable
  • Loading branch information
rrosenblum committed Oct 1, 2015
1 parent 1ba23d5 commit a7706fa
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* [#2275](https://github.com/bbatsov/rubocop/pull/2275): Copy default `Exclude` into `Exclude` lists in `.rubocop_todo.yml`. ([@jonas054][])
* `Style/IfUnlessModifier` accepts blocks followed by a chained call. ([@lumeet][])
* [#2261](https://github.com/bbatsov/rubocop/issues/2261): Make relative `Exclude` paths in `$HOME/.rubocop_todo.yml` be relative to current directory. ([@jonas054][])
* [#2246](https://github.com/bbatsov/rubocop/pull/2246): Do not register an offense for `Style/TrailingUnderscoreVariable` when the underscore variable is preceeded by a splat variable. ([@rrosenblum][])

## 0.34.2 (21/09/2015)

Expand Down
9 changes: 9 additions & 0 deletions lib/rubocop/cop/style/trailing_underscore_variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module Style
# #good
# a, b, = foo()
# a, = foo()
# *a, b, _ = foo() => We need to know to not include 2 variables in a
# a, *b, _ = foo() => The correction `a, *b, = foo()` is a syntax error
class TrailingUnderscoreVariable < Cop
include SurroundingSpace

Expand Down Expand Up @@ -71,6 +73,13 @@ def find_first_offense(variables)
first_offense = variable
end

return nil if first_offense.nil?

first_offense_index = variables.index(first_offense)
0.upto(first_offense_index - 1).each do |index|
return nil if variables[index].splat_type?
end

first_offense
end

Expand Down
108 changes: 108 additions & 0 deletions spec/rubocop/cop/style/trailing_underscore_variable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,56 @@
expect(cop.messages).to be_empty
end

it 'does not register an offense for an underscore preceeded by a ' \
'splat variable anywhere in the argument chain' do
inspect_source(cop, '*a, b, _ = foo()')

expect(cop.messages).to be_empty
end

it 'does not register an offense for an underscore preceeded by a ' \
'splat variable' do
inspect_source(cop, 'a, *b, _ = foo()')

expect(cop.messages).to be_empty
end

it 'does not register an offense for multiple underscores preceeded by a ' \
'splat variable' do
inspect_source(cop, 'a, *b, _, _ = foo()')

expect(cop.messages).to be_empty
end

it 'does not register an offense for multiple named underscores ' \
'preceeded by a splat variable' do
inspect_source(cop, 'a, *b, _c, _d = foo()')

expect(cop.messages).to be_empty
end

it 'registers an offense for multiple underscore variables preceeded by ' \
'a splat underscore variable' do
inspect_source(cop, 'a, *_, _, _ = foo()')

expect(cop.messages)
.to eq(['Do not use trailing `_`s in parallel assignment.'])
end

it 'does not register an offense for a named underscore variable ' \
'preceeded by a splat variable' do
inspect_source(cop, 'a, *b, _c = foo()')

expect(cop.messages).to be_empty
end

it 'does not register an offense for a named variable preceeded by a ' \
'names splat underscore variable' do
inspect_source(cop, 'a, *b, _c = foo()')

expect(cop.messages).to be_empty
end

describe 'autocorrect' do
it 'removes trailing underscores automatically' do
new_source = autocorrect_source(cop, 'a, b, _ = foo()')
Expand Down Expand Up @@ -120,6 +170,13 @@
})
end

it 'does not register an offense for an underscore variable preceeded ' \
'by a named splat underscore variable' do
inspect_source(cop, 'a, *_b, _ = foo()')

expect(cop.messages).to be_empty
end

it 'does not register an offense for named variables ' \
'that start with an underscore' do
inspect_source(cop, 'a, b, _c = foo()')
Expand All @@ -133,6 +190,20 @@

expect(cop.messages).to be_empty
end

it 'does not register an offense for an underscore preceeded by ' \
'a named splat underscore' do
inspect_source(cop, 'a, *_b, _ = foo()')

expect(cop.messages).to be_empty
end

it 'does not register an offense for multiple underscore variables ' \
'preceeded by a named splat underscore variable' do
inspect_source(cop, 'a, *_b, _, _ = foo()')

expect(cop.messages).to be_empty
end
end

context 'configured to not allow named underscore variables' do
Expand Down Expand Up @@ -161,6 +232,37 @@
.to eq(['Do not use trailing `_`s in parallel assignment.'])
end

it 'does not register an offense for a named underscore preceeded by a ' \
'splat variable' do
inspect_source(cop, 'a, *b, _c = foo()')

expect(cop.messages).to be_empty
end

it 'registers an offense for an underscore variable preceeded ' \
'by a named splat underscore variable' do
inspect_source(cop, 'a, *_b, _ = foo()')

expect(cop.messages)
.to eq(['Do not use trailing `_`s in parallel assignment.'])
end

it 'registers an offense for an underscore preceeded by ' \
'a named splat underscore' do
inspect_source(cop, 'a, b, *_c, _ = foo()')

expect(cop.messages)
.to eq(['Do not use trailing `_`s in parallel assignment.'])
end

it 'registers an offense for multiple underscore variables ' \
'preceeded by a named splat underscore variable' do
inspect_source(cop, 'a, *_b, _, _ = foo()')

expect(cop.messages)
.to eq(['Do not use trailing `_`s in parallel assignment.'])
end

context 'autocorrect' do
it 'removes named underscore variables' do
new_source = autocorrect_source(cop, 'a, _b = foo()')
Expand All @@ -173,6 +275,12 @@

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

it 'removes named splat underscore and named underscore variables' do
new_source = autocorrect_source(cop, 'a, *_b, _c = foo()')

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

0 comments on commit a7706fa

Please sign in to comment.