Skip to content

Commit

Permalink
Improve Naming/RescuedExceptionsVariableName autocorrection
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim Krizhanovski authored and bbatsov committed Apr 29, 2019
1 parent b0262a5 commit ab3f564
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
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

0 comments on commit ab3f564

Please sign in to comment.