Skip to content

Commit

Permalink
[Fix rubocop#3114] Fix alignment of end in EmptyElse
Browse files Browse the repository at this point in the history
  • Loading branch information
rrosenblum committed May 10, 2016
1 parent 28b0f63 commit 76f9e85
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#3114](https://github.com/bbatsov/rubocop/issues/3114): Fix alignment `end` when auto-correcting `Style/EmptyElse`. ([@rrosenblum][])

## 0.40.0 (2016-05-09)

### New features
Expand Down
3 changes: 2 additions & 1 deletion lib/rubocop/cop/style/empty_else.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ def autocorrect(node)
end_pos = if node.loc.end
node.loc.end.begin_pos
else
node.source_range.end_pos + 1
node.parent.loc.end.begin_pos
end

range = Parser::Source::Range.new(node.source_range.source_buffer,
node.loc.else.begin_pos,
end_pos)
Expand Down
135 changes: 123 additions & 12 deletions spec/rubocop/cop/style/empty_else_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,38 @@

context 'given an if-statement' do
context 'with a completely empty else-clause' do
let(:source) { 'if a; foo else end' }
let(:corrected_source) { 'if a; foo end' }
context 'using semicolons' do
let(:source) { 'if a; foo else end' }
let(:corrected_source) { 'if a; foo end' }

it 'registers an offense' do
inspect_source(cop, source)
expect(cop.messages).to eq(['Redundant `else`-clause.'])
it 'registers an offense' do
inspect_source(cop, source)
expect(cop.messages).to eq(['Redundant `else`-clause.'])
end

it_behaves_like 'auto-correct', 'if'
end

it_behaves_like 'auto-correct', 'if'
context 'when the result is assigned to a variable' do
let(:source) do
['if a',
' foo',
'else',
'end'].join("\n")
end
let(:corrected_source) do
['if a',
' foo',
'end'].join("\n")
end

it 'registers an offense' do
inspect_source(cop, source)
expect(cop.messages).to eq(['Redundant `else`-clause.'])
end

it_behaves_like 'auto-correct', 'if'
end
end

context 'with an else-clause containing only the literal nil' do
Expand Down Expand Up @@ -168,6 +191,63 @@
end

context 'with an else-clause containing only the literal nil' do
context 'when standalone' do
let(:source) do
['if a',
' foo',
'elsif b',
' bar',
'else',
' nil',
'end'].join("\n")
end

let(:corrected_source) do
['if a',
' foo',
'elsif b',
' bar',
'end'].join("\n")
end

it 'registers an offense' do
inspect_source(cop, source)
expect(cop.messages).to eq(['Redundant `else`-clause.'])
end

it_behaves_like 'auto-correct', 'if'
end

context 'when the result is assigned to a variable' do
let(:source) do
['foobar = if a',
' foo',
' elsif b',
' bar',
' else',
' nil',
' end'].join("\n")
end

let(:corrected_source) do
['foobar = if a',
' foo',
' elsif b',
' bar',
' end'].join("\n")
end

it 'registers an offense' do
inspect_source(cop, source)
expect(cop.messages).to eq(['Redundant `else`-clause.'])
end

it_behaves_like 'auto-correct', 'if'
end
end

context 'with an else-clause containing only the literal nil ' \
'using semicolons' do
let(:source) { 'if a; foo elsif b; bar else nil end' }
let(:corrected_source) { 'if a; foo elsif b; bar end' }

Expand Down Expand Up @@ -238,15 +318,46 @@
end

context 'with an else-clause containing only the literal nil' do
let(:source) { 'case v; when a; foo; when b; bar; else nil end' }
let(:corrected_source) { 'case v; when a; foo; when b; bar; end' }
context 'using semicolons' do
let(:source) { 'case v; when a; foo; when b; bar; else nil end' }
let(:corrected_source) { 'case v; when a; foo; when b; bar; end' }

it 'registers an offense' do
inspect_source(cop, source)
expect(cop.messages).to eq(['Redundant `else`-clause.'])
it 'registers an offense' do
inspect_source(cop, source)
expect(cop.messages).to eq(['Redundant `else`-clause.'])
end

it_behaves_like 'auto-correct', 'case'
end

it_behaves_like 'auto-correct', 'case'
context 'when the result is assigned to a variable' do
let(:source) do
['foobar = case v',
' when a',
' foo',
' when b',
' bar',
' else',
' nil',
' end'].join("\n")
end

let(:corrected_source) do
['foobar = case v',
' when a',
' foo',
' when b',
' bar',
' end'].join("\n")
end

it 'registers an offense' do
inspect_source(cop, source)
expect(cop.messages).to eq(['Redundant `else`-clause.'])
end

it_behaves_like 'auto-correct', 'case'
end
end

context 'with an else-clause with side-effects' do
Expand Down

0 comments on commit 76f9e85

Please sign in to comment.