Skip to content

Commit

Permalink
Make Lint/DeprecatedConstants aware of Net::HTTPServerException
Browse files Browse the repository at this point in the history
This PR makes `Lint/DeprecatedConstants` aware of `Net::HTTPServerException`.

The following warning was introduced since Ruby 2.6.

```console
% ruby -rnet/http -vwe 'p Net::HTTPServerException'
ruby 2.6.8p205 (2021-07-07 revision 67951) [x86_64-darwin19]
-e:1: warning: constant Net::HTTPServerException is deprecated
Net::HTTPServerException
```

And `HTTPClientException` (alias) is used as an alternative to `HTTPServerException`.

The following is the quote from Ruby 2.6.0 release news.

> Add `Net::HTTPClientException` to deprecate `Net::HTTPServerException`,
> whose name is misleading. [Bug #14688]

https://github.com/ruby/ruby/blob/master/doc/NEWS-2.6.0

```console
% ruby -rnet/http -vwe 'p Net::HTTPClientException'
ruby 2.6.8p205 (2021-07-07 revision 67951) [x86_64-darwin19]
Net::HTTPServerException
```

`HTTPClientException` does not yet exist in Ruby 2.5, so the following error occurs.

```console
% ruby -rnet/http -vwe 'p Net::HTTPClientException'
ruby 2.5.9p229 (2021-04-05 revision 67939) [x86_64-darwin19]
Traceback (most recent call last):
-e:1:in `<main>': uninitialized constant
Net::HTTPClientException (NameError)
Did you mean?  Net::HTTPServerException
```

To prevent such errors, the cop's behavior is changed to detect only `DeprecatedVersion` or higher.
  • Loading branch information
koic committed Oct 23, 2021
1 parent bf18dd3 commit 932375f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 21 deletions.
@@ -0,0 +1 @@
* [#10209](https://github.com/rubocop/rubocop/pull/10209): Make `Lint/DeprecatedConstants` aware of `Net::HTTPServerException`. ([@koic][])
4 changes: 4 additions & 0 deletions config/default.yml
Expand Up @@ -1568,6 +1568,7 @@ Lint/DeprecatedConstants:
Description: 'Checks for deprecated constants.'
Enabled: pending
VersionAdded: '1.8'
VersionChanged: '<<next>>'
# You can configure deprecated constants.
# If there is an alternative method, you can set alternative value as `Alternative`.
# And you can set the deprecated version as `DeprecatedVersion`.
Expand All @@ -1588,6 +1589,9 @@ Lint/DeprecatedConstants:
'FALSE':
Alternative: 'false'
DeprecatedVersion: '2.4'
'Net::HTTPServerException':
Alternative: 'Net::HTTPClientException'
DeprecatedVersion: '2.6'
'Random::DEFAULT':
Alternative: 'Random.new'
DeprecatedVersion: '3.0'
Expand Down
5 changes: 3 additions & 2 deletions lib/rubocop/cop/lint/deprecated_constants.rb
Expand Up @@ -42,11 +42,12 @@ def on_const(node)
# Maybe further investigation of RuboCop AST will lead to an essential solution.
return unless node.loc

constant = node.absolute? ? constant_name(node, node.short_name.to_s) : node.source
constant = node.absolute? ? constant_name(node, node.short_name) : node.source
return unless (deprecated_constant = deprecated_constants[constant])

alternative = deprecated_constant['Alternative']
version = deprecated_constant['DeprecatedVersion']
return if target_ruby_version < version.to_f

add_offense(node, message: message(alternative, node.source, version)) do |corrector|
corrector.replace(node, alternative)
Expand All @@ -56,7 +57,7 @@ def on_const(node)
private

def constant_name(node, nested_constant_name)
return nested_constant_name unless node.namespace.const_type?
return nested_constant_name.to_s unless node.namespace.const_type?

constant_name(node.namespace, "#{node.namespace.short_name}::#{nested_constant_name}")
end
Expand Down
72 changes: 53 additions & 19 deletions spec/rubocop/cop/lint/deprecated_constants_spec.rb
Expand Up @@ -7,6 +7,9 @@
'NIL' => { 'Alternative' => 'nil', 'DeprecatedVersion' => '2.4' },
'TRUE' => { 'Alternative' => 'true', 'DeprecatedVersion' => '2.4' },
'FALSE' => { 'Alternative' => 'false', 'DeprecatedVersion' => '2.4' },
'Net::HTTPServerException' => {
'Alternative' => 'Net::HTTPClientException', 'DeprecatedVersion' => '2.6'
},
'Random::DEFAULT' => { 'Alternative' => 'Random.new', 'DeprecatedVersion' => '3.0' },
'Triple::Nested::Constant' => { 'Alternative' => 'Value', 'DeprecatedVersion' => '2.4' },
'Have::No::Alternative' => { 'DeprecatedVersion' => '2.4' },
Expand Down Expand Up @@ -48,15 +51,57 @@
RUBY
end

it 'registers and corrects an offense when using `Random::DEFAULT`' do
expect_offense(<<~RUBY)
Random::DEFAULT
^^^^^^^^^^^^^^^ Use `Random.new` instead of `Random::DEFAULT`, deprecated since Ruby 3.0.
RUBY
context 'Ruby <= 2.5', :ruby25 do
it 'does not register an offense when using `Net::HTTPServerException`' do
expect_no_offenses(<<~RUBY)
Net::HTTPServerException
RUBY
end
end

expect_correction(<<~RUBY)
Random.new
RUBY
context 'Ruby >= 2.6', :ruby26 do
it 'registers and corrects an offense when using `Net::HTTPServerException`' do
expect_offense(<<~RUBY)
Net::HTTPServerException
^^^^^^^^^^^^^^^^^^^^^^^^ Use `Net::HTTPClientException` instead of `Net::HTTPServerException`, deprecated since Ruby 2.6.
RUBY

expect_correction(<<~RUBY)
Net::HTTPClientException
RUBY
end
end

context 'Ruby <= 2.7', :ruby27 do
it 'does not register an offense when using `Random::DEFAULT`' do
expect_no_offenses(<<~RUBY)
Random::DEFAULT
RUBY
end
end

context 'Ruby >= 3.0', :ruby30 do
it 'registers and corrects an offense when using `Random::DEFAULT`' do
expect_offense(<<~RUBY)
Random::DEFAULT
^^^^^^^^^^^^^^^ Use `Random.new` instead of `Random::DEFAULT`, deprecated since Ruby 3.0.
RUBY

expect_correction(<<~RUBY)
Random.new
RUBY
end

it 'registers and corrects an offense when using `::Random::DEFAULT`' do
expect_offense(<<~RUBY)
::Random::DEFAULT
^^^^^^^^^^^^^^^^^ Use `Random.new` instead of `::Random::DEFAULT`, deprecated since Ruby 3.0.
RUBY

expect_correction(<<~RUBY)
Random.new
RUBY
end
end

it 'registers and corrects an offense when using `::NIL`' do
Expand Down Expand Up @@ -92,17 +137,6 @@
RUBY
end

it 'registers and corrects an offense when using `::Random::DEFAULT`' do
expect_offense(<<~RUBY)
::Random::DEFAULT
^^^^^^^^^^^^^^^^^ Use `Random.new` instead of `::Random::DEFAULT`, deprecated since Ruby 3.0.
RUBY

expect_correction(<<~RUBY)
Random.new
RUBY
end

it 'registers and corrects an offense when using `::Triple::Nested::Constant`' do
expect_offense(<<~RUBY)
::Triple::Nested::Constant
Expand Down

0 comments on commit 932375f

Please sign in to comment.