Skip to content

Commit

Permalink
[rubocop#743] Implement auto-correct for EmptyLineBetweenDefs cop
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas054 committed Feb 8, 2014
1 parent 257b761 commit e47e9ac
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* New cop `FileName` makes sure that source files have snake_case names. ([@bbatsov][])
* [#743](https://github.com/bbatsov/rubocop/issues/743): `SingleLineMethods` cop does auto-correction. ([@jonas054][])
* [#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][])

### Changes

Expand Down
7 changes: 7 additions & 0 deletions lib/rubocop/cop/style/empty_line_between_defs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ def def_start(node)
def def_end(node)
node.loc.end.line
end

def autocorrect(node)
range = range_with_surrounding_space(node.loc.expression, :left)
@corrections << lambda do |corrector|
corrector.insert_before(range, "\n")
end
end
end
end
end
Expand Down
5 changes: 3 additions & 2 deletions spec/rubocop/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def abs(path)
'def func1',
' do_something',
'end',
'',
'def func2',
' do_1',
' do_2',
Expand All @@ -66,11 +67,11 @@ def abs(path)
expect($stdout.string).to eq(['',
'6 TrailingWhitespace',
'4 Semicolon',
'2 EmptyLineBetweenDefs',
'2 SingleLineMethods',
'1 DefWithParentheses',
'1 EmptyLineBetweenDefs',
'--',
'15 Total',
'14 Total',
'',
''].join("\n"))
end
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/style/empty_line_between_defs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@
expect(cop.offences.size).to eq(1)
end

it 'auto-corrects adjacent one-liners by default' do
corrected = autocorrect_source(cop, [' def a; end',
' def b; end'])
expect(corrected).to eq([' def a; end',
'',
' def b; end'].join("\n"))
end

context 'when AllowAdjacentOneLineDefs is enabled' do
let(:cop_config) { { 'AllowAdjacentOneLineDefs' => true } }

Expand Down

0 comments on commit e47e9ac

Please sign in to comment.