Skip to content

Commit

Permalink
[rubocop#743] Support autocorrect in IndentationConsistency.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas054 committed Feb 26, 2014
1 parent 2de4da9 commit 09ba991
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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][])
Expand Down
14 changes: 3 additions & 11 deletions lib/rubocop/cop/style/indentation_consistency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ module Style
# end
# end
class IndentationConsistency < Cop
include AutocorrectAlignment

MSG = 'Inconsistent indentation detected.'

def on_begin(node)
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion spec/rubocop/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ def abs(path)
' Enabled: false',
'',
'# Offense count: 1',
'# Cop supports --auto-correct.',
'IndentationConsistency:',
' Enabled: false',
'',
Expand Down Expand Up @@ -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',
Expand Down
22 changes: 21 additions & 1 deletion spec/rubocop/cop/style/indentation_consistency_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 09ba991

Please sign in to comment.