Skip to content

Commit

Permalink
Merge pull request #1929 from jonas054/1928_autocorrect_indentation_a…
Browse files Browse the repository at this point in the history
…nd_tabs

[Fix #1928] Auto-correct one offense at a time if there are tabs
  • Loading branch information
bbatsov committed Jun 5, 2015
2 parents b4c8123 + e0fce7e commit 59ba2bb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* Show reference links when displaying style guide links. ([@rrosenblum][])
* `Debugger` cop now checks for the Capybara debug method `save_screenshot`. ([@crazydog115][])
* [#1282](https://github.com/bbatsov/rubocop/issues/1282): `CaseIndentation` cop does auto-correction. ([@lumeet][])
* [#1928](https://github.com/bbatsov/rubocop/issues/1928): Do auto-correction one offense at a time (rather than one cop at a time) if there are tabs in the code. ([@jonas054][])

### Changes

Expand Down
9 changes: 8 additions & 1 deletion lib/rubocop/cop/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,14 @@ def autocorrect_one_cop(buffer, cops)
cop.relevant_file?(buffer.name) && cop.corrections.any?
end
if cop_with_corrections
corrector = Corrector.new(buffer, cop_with_corrections.corrections)
corrections = cop_with_corrections.corrections
# Be extra careful if there are tabs in the source and just correct
# one offense, because inserting or removing space next to a tab has
# special implications, and existing ranges can't be used after such
# a change.
corrections = [corrections.first] if buffer.source =~ /\t/

corrector = Corrector.new(buffer, corrections)
corrector.rewrite
else
buffer.source
Expand Down
26 changes: 26 additions & 0 deletions spec/rubocop/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,32 @@ def abs(path)
end

describe '--auto-correct' do
it 'corrects Tab and IndentationConsistency offenses' do
source = [' render_views',
" describe 'GET index' do",
"\t it 'returns http success' do",
"\t end",
"\tdescribe 'admin user' do",
' before(:each) do',
"\t end",
"\tend",
' end',
'']
create_file('example.rb', source)
expect(cli.run(['--auto-correct'])).to eq(0)
corrected = [' render_views',
" describe 'GET index' do",
" it 'returns http success' do",
' end',
" describe 'admin user' do",
' before(:each) do',
' end',
' end',
' end',
'']
expect(IO.read('example.rb')).to eq(corrected.join("\n"))
end

it 'corrects SymbolProc and SpaceBeforeBlockBraces offenses' do
source = ['foo.map{ |a| a.nil? }']
create_file('example.rb', source)
Expand Down

0 comments on commit 59ba2bb

Please sign in to comment.