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

Improve Naming/RescuedExceptionsVariableName autocorrection #6998

Merged
merged 1 commit into from Apr 29, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
### Bug fixes

* [#6995](https://github.com/rubocop-hq/rubocop/pull/6995): Fix an incorrect auto-correct for `Style/RedundantParentheses` when enclosed in parentheses at `while-post` or `until-post`. ([@koic][])
* [#6998](https://github.com/rubocop-hq/rubocop/pull/6998): Fix autocorrect of `Naming/RescuedExceptionsVariableName` to also rename all references to the variable. ([@Darhazer][])

## 0.68.0 (2019-04-29)

Expand Down
9 changes: 9 additions & 0 deletions lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb
Expand Up @@ -69,7 +69,16 @@ def on_resbody(node)

def autocorrect(node)
lambda do |corrector|
offending_name = node.exception_variable.children.first
corrector.replace(offense_range(node), preferred_name)

return unless node.body

node.body.each_descendant(:lvar) do |var|
next unless var.children.first == offending_name

corrector.replace(var.loc.expression, preferred_name)
end
end
end

Expand Down
21 changes: 21 additions & 0 deletions spec/rubocop/cop/naming/rescued_exceptions_variable_name_spec.rb
Expand Up @@ -176,6 +176,27 @@
end
end
end

context 'with variable being referenced' do
it 'renames the variable references when auto-correcting' do
expect_offense(<<-RUBY.strip_indent)
begin
get something
rescue ActiveResource::Redirection => redirection
^^^^^^^^^^^ Use `e` instead of `redirection`.
redirect_to redirection.response['Location']
end
RUBY

expect_correction(<<-RUBY.strip_indent)
begin
get something
rescue ActiveResource::Redirection => e
redirect_to e.response['Location']
end
RUBY
end
end
end

context 'with the `PreferredName` setup' do
Expand Down