diff --git a/CHANGELOG.md b/CHANGELOG.md index 944cb23068f9..da886c9b4c78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/rubocop/cop/style/case_indentation.rb b/lib/rubocop/cop/style/case_indentation.rb index 8900a986b701..8188575755ed 100644 --- a/lib/rubocop/cop/style/case_indentation.rb +++ b/lib/rubocop/cop/style/case_indentation.rb @@ -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 diff --git a/spec/rubocop/cop/style/case_indentation_spec.rb b/spec/rubocop/cop/style/case_indentation_spec.rb index b8737090097a..7d7f0328c90b 100644 --- a/spec/rubocop/cop/style/case_indentation_spec.rb +++ b/spec/rubocop/cop/style/case_indentation_spec.rb @@ -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', @@ -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'", @@ -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'", @@ -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'",