Skip to content

Commit

Permalink
[Fix rubocop#4128] Prevent Style/CaseIndentation from registering off…
Browse files Browse the repository at this point in the history
…enses on single line case statements

This cop would register indentation offenses on single-line case
statements. This change fixes that.
  • Loading branch information
Drenmi committed Mar 22, 2017
1 parent 07ec7f4 commit 5362129
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
* [#4102](https://github.com/bbatsov/rubocop/issues/4102): Fix `Security/JSONLoad`, `Security/MarshalLoad` and `Security/YAMLLoad` cops patterns not matching ::Const. ([@musialik][])
* [#3580](https://github.com/bbatsov/rubocop/issues/3580): Handle combinations of `# rubocop:disable all` and `# rubocop:disable SomeCop`. ([@jonas054][])
* [#4124](https://github.com/bbatsov/rubocop/issues/4124): Fix auto correction bugs in `Style/SymbolArray` cop. ([@pocke][])
* [#4128](https://github.com/bbatsov/rubocop/issues/4128): Prevent `Style/CaseIndentation` cop from registering offenses on single-line case statements. ([@drenmi][])

## 0.47.1 (2017-01-18)

Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/style/case_indentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class CaseIndentation < Cop
MSG = 'Indent `when` %s `%s`.'.freeze

def on_case(case_node)
return if case_node.single_line?

case_node.each_when do |when_node|
check_when(when_node)
end
Expand Down
36 changes: 36 additions & 0 deletions spec/rubocop/cop/style/case_indentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@
{ 'EnforcedStyle' => 'case', 'IndentOneStep' => false }
end

context 'with everything on a single line' do
let(:source) { 'case foo; when :bar then 1; else 0; end' }

it 'does not register an offense' do
inspect_source(cop, source)
expect(cop.offenses).to be_empty
end
end

context 'regarding assignment where the right hand side is a case' do
let(:correct_source) do
['output = case variable',
Expand Down Expand Up @@ -235,6 +244,15 @@
{ 'EnforcedStyle' => 'case', 'IndentOneStep' => true }
end

context 'with everything on a single line' do
let(:source) { 'case foo; when :bar then 1; else 0; end' }

it 'does not register an offense' do
inspect_source(cop, source)
expect(cop.offenses).to be_empty
end
end

let(:correct_source) do
['output = case variable',
" when 'value1'",
Expand Down Expand Up @@ -365,6 +383,15 @@
{ 'EnforcedStyle' => 'end', 'IndentOneStep' => false }
end

context 'with everything on a single line' do
let(:source) { 'case foo; when :bar then 1; else 0; end' }

it 'does not register an offense' do
inspect_source(cop, source)
expect(cop.offenses).to be_empty
end
end

let(:correct_source) do
['output = case variable',
"when 'value1'",
Expand Down Expand Up @@ -417,6 +444,15 @@
{ 'EnforcedStyle' => 'end', 'IndentOneStep' => true }
end

context 'with everything on a single line' do
let(:source) { 'case foo; when :bar then 1; else 0; end' }

it 'does not register an offense' do
inspect_source(cop, source)
expect(cop.offenses).to be_empty
end
end

let(:correct_source) do
['output = case variable',
" when 'value1'",
Expand Down

0 comments on commit 5362129

Please sign in to comment.