Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rescue exception autocorrect #966

Merged
merged 1 commit into from
Apr 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### New features

* [#966](https://github.com/bbatsov/rubocop/issues/966): `RescueException` cop does auto-correction. ([@tamird][])
* [#967](https://github.com/bbatsov/rubocop/issues/967): `TrivialAccessors` cop does auto-correction. ([@tamird][])
* [#963](https://github.com/bbatsov/rubocop/issues/963): Add `AllowDSLWriters` options to `TrivialAccessors`. ([@tamird][])
* [#969](https://github.com/bbatsov/rubocop/issues/969): Let the `Debugger` cop check for forgotten calls to byebug. ([@bquorning][])
Expand Down
11 changes: 11 additions & 0 deletions lib/rubocop/cop/lint/rescue_exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ def on_resbody(node)
def targets_exception?(rescue_arg_node)
Util.const_name(rescue_arg_node) == 'Exception'
end

def autocorrect(node)
@corrections << lambda do |corrector|
corrector.remove(
range_with_surrounding_space(
node.children.first.children.first.loc.expression,
:left
)
)
end
end
end
end
end
Expand Down
36 changes: 36 additions & 0 deletions spec/rubocop/cop/lint/rescue_exception_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,40 @@
'end'])
expect(cop.offenses).to be_empty
end

context 'without exception capture' do
let(:source) do
['begin',
'rescue Exception',
'end']
end

let(:corrected_source) do
['begin', # rubocop:disable WordArray
'rescue',
'end'].join("\n")
end

it 'autocorrects by unspecifying the exception class' do
expect(autocorrect_source(cop, source)).to eq(corrected_source)
end
end

context 'with exception capture' do
let(:source) do
['begin',
'rescue Exception => e',
'end']
end

let(:corrected_source) do
['begin',
'rescue => e',
'end'].join("\n")
end

it 'autocorrects by unspecifying the exception class' do
expect(autocorrect_source(cop, source)).to eq(corrected_source)
end
end
end