Skip to content

Commit

Permalink
[Fix #6903] Handle variables with _ for `Naming/RescuedExceptionsVa…
Browse files Browse the repository at this point in the history
…riableName` cop (#6906)

Currently, this cop do not play nicely with variables that are prefixed by an `_`.

The message should take into consideration if the variable is going to be
used or not when showing the error.
  • Loading branch information
anthony-robin authored and bbatsov committed Apr 10, 2019
1 parent adbb722 commit 954580e
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

* [#5977](https://github.com/rubocop-hq/rubocop/issues/5977): Remove Performance cops. ([@koic][])
* Add auto-correction to `Naming/RescuedExceptionsVariableName`. ([@anthony-robin][])
* [#6903](https://github.com/rubocop-hq/rubocop/issues/6903): Handle variables prefixed with `_` in `Naming/RescuedExceptionsVariableName` cop. ([@anthony-robin][])

## 0.67.2 (2019-04-05)

Expand Down
20 changes: 19 additions & 1 deletion lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb
Expand Up @@ -24,6 +24,13 @@ module Naming
# # do something
# end
#
# # good
# begin
# # do something
# rescue MyException => _e
# # do something
# end
#
# @example PreferredName: exception
# # bad
# begin
Expand All @@ -39,6 +46,13 @@ module Naming
# # do something
# end
#
# # good
# begin
# # do something
# rescue MyException => _exception
# # do something
# end
#
class RescuedExceptionsVariableName < Cop
MSG = 'Use `%<preferred>s` instead of `%<bad>s`.'.freeze

Expand All @@ -62,7 +76,11 @@ def autocorrect(_node)
private

def preferred_name
@preferred_name ||= cop_config.fetch('PreferredName', 'e')
@preferred_name ||= begin
name = cop_config.fetch('PreferredName', 'e')
name = "_#{name}" if variable_name.to_s.start_with?('_')
name
end
end

def variable_name
Expand Down
14 changes: 14 additions & 0 deletions manual/cops_naming.md
Expand Up @@ -507,6 +507,13 @@ begin
rescue MyException => e
# do something
end

# good
begin
# do something
rescue MyException => _e
# do something
end
```
#### PreferredName: exception
Expand All @@ -524,6 +531,13 @@ begin
rescue MyException => exception
# do something
end

# good
begin
# do something
rescue MyException => _exception
# do something
end
```
### Configurable attributes
Expand Down
87 changes: 87 additions & 0 deletions spec/rubocop/cop/naming/rescued_exceptions_variable_name_spec.rb
Expand Up @@ -25,6 +25,25 @@
RUBY
end

it 'registers an offense when using `_exc`' do
expect_offense(<<-RUBY.strip_indent)
begin
something
rescue MyException => _exc
^^^^ Use `_e` instead of `_exc`.
# do something
end
RUBY

expect_correction(<<-RUBY.strip_indent)
begin
something
rescue MyException => _e
# do something
end
RUBY
end

it 'does not register an offense when using `e`' do
expect_no_offenses(<<-RUBY.strip_indent)
begin
Expand All @@ -34,6 +53,16 @@
end
RUBY
end

it 'does not register an offense when using `_e`' do
expect_no_offenses(<<-RUBY.strip_indent)
begin
something
rescue MyException => _e
# do something
end
RUBY
end
end

context 'without `Exception` variable' do
Expand Down Expand Up @@ -70,6 +99,25 @@
RUBY
end

it 'registers an offense when using `_exc`' do
expect_offense(<<-RUBY.strip_indent)
begin
something
rescue _exc
^^^^ Use `_e` instead of `_exc`.
# do something
end
RUBY

expect_correction(<<-RUBY.strip_indent)
begin
something
rescue _e
# do something
end
RUBY
end

it 'does not register an offense when using `e`' do
expect_no_offenses(<<-RUBY.strip_indent)
begin
Expand All @@ -79,6 +127,16 @@
end
RUBY
end

it 'does not register an offense when using `_e`' do
expect_no_offenses(<<-RUBY.strip_indent)
begin
something
rescue _e
# do something
end
RUBY
end
end

context 'without `Exception` variable' do
Expand Down Expand Up @@ -121,6 +179,25 @@
RUBY
end

it 'registers an offense when using `_e`' do
expect_offense(<<-RUBY.strip_indent)
begin
something
rescue MyException => _e
^^ Use `_exception` instead of `_e`.
# do something
end
RUBY

expect_correction(<<-RUBY.strip_indent)
begin
something
rescue MyException => _exception
# do something
end
RUBY
end

it 'does not register an offense when using `exception`' do
expect_no_offenses(<<-RUBY.strip_indent)
begin
Expand All @@ -130,5 +207,15 @@
end
RUBY
end

it 'does not register an offense when using `_exception`' do
expect_no_offenses(<<-RUBY.strip_indent)
begin
something
rescue MyException => _exception
# do something
end
RUBY
end
end
end

0 comments on commit 954580e

Please sign in to comment.