diff --git a/CHANGELOG.md b/CHANGELOG.md index fc8edc2ac3e3..51a3d843e2b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ * [#743](https://github.com/bbatsov/rubocop/issues/743): `Semicolon` cop does auto-correction. ([@jonas054][]) * [#743](https://github.com/bbatsov/rubocop/issues/743): `EmptyLineBetweenDefs` cop does auto-correction. ([@jonas054][]) * [#743](https://github.com/bbatsov/rubocop/issues/743): `IndentationWidth` cop does auto-correction. ([@jonas054][]) +* [#743](https://github.com/bbatsov/rubocop/issues/743): `IndentationConsistency` cop does auto-correction. ([@jonas054][]) * [#809](https://github.com/bbatsov/rubocop/issues/809): New formatter `fuubar` displays a progress bar and shows details of offenses as soon as they are detected. ([@yujinakayama][]) * [#797](https://github.com/bbatsov/rubocop/issues/797): New cop `IndentHash` checks the indentation of the first key in multi-line hash literals. ([@jonas054][]) * [#797](https://github.com/bbatsov/rubocop/issues/797): New cop `IndentArray` checks the indentation of the first element in multi-line array literals. ([@jonas054][]) diff --git a/lib/rubocop/cop/style/indentation_consistency.rb b/lib/rubocop/cop/style/indentation_consistency.rb index 04a76022836e..29d429738eb1 100644 --- a/lib/rubocop/cop/style/indentation_consistency.rb +++ b/lib/rubocop/cop/style/indentation_consistency.rb @@ -14,6 +14,8 @@ module Style # end # end class IndentationConsistency < Cop + include AutocorrectAlignment + MSG = 'Inconsistent indentation detected.' def on_begin(node) @@ -33,17 +35,7 @@ def check(node) AccessModifierIndentation.modifier_node?(child) end - children_to_check.map(&:loc).each_cons(2) do |child1, child2| - if child2.line > child1.line && child2.column != child1.column - expr = child2.expression - indentation = expr.source_line =~ /\S/ - end_pos = expr.begin_pos - begin_pos = end_pos - indentation - add_offense(nil, - Parser::Source::Range.new(expr.source_buffer, - begin_pos, end_pos)) - end - end + check_alignment(children_to_check) end end end diff --git a/spec/rubocop/cli_spec.rb b/spec/rubocop/cli_spec.rb index 73845a8eeef8..a7586c9c448b 100644 --- a/spec/rubocop/cli_spec.rb +++ b/spec/rubocop/cli_spec.rb @@ -365,6 +365,7 @@ def abs(path) ' Enabled: false', '', '# Offense count: 1', + '# Cop supports --auto-correct.', 'IndentationConsistency:', ' Enabled: false', '', @@ -648,7 +649,7 @@ def full_description_of_cop(cop) 'example2.rb:3:1: C: Inconsistent indentation ' \ 'detected.', 'def a', - '', + '^^^^^', 'example2.rb:4:1: C: Use 2 (not 3) spaces for ' \ 'indentation.', ' puts', diff --git a/spec/rubocop/cop/style/indentation_consistency_spec.rb b/spec/rubocop/cop/style/indentation_consistency_spec.rb index 21f21fb6e53d..647cac2ac6a8 100644 --- a/spec/rubocop/cop/style/indentation_consistency_spec.rb +++ b/spec/rubocop/cop/style/indentation_consistency_spec.rb @@ -39,6 +39,26 @@ expect(cop.messages).to eq(['Inconsistent indentation detected.']) end + it 'autocorrects bad indentation' do + corrected = autocorrect_source(cop, + ['if a1', + ' b1', + 'elsif a2', + ' b2', + ' b3', + 'else', + ' c', + 'end']) + expect(corrected).to eq ['if a1', + ' b1', + 'elsif a2', + ' b2', + ' b3', + 'else', + ' c', + 'end'].join("\n") + end + it 'accepts a one line if statement' do inspect_source(cop, ['if cond then func1 else func2 end']) @@ -428,7 +448,7 @@ ' end', 'end']) expect(cop.messages).to eq(['Inconsistent indentation detected.']) - expect(cop.highlights).to eq([' ']) + expect(cop.highlights).to eq(["def g\n end"]) end end