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

Make Lint/DeprecatedConstants aware of Net::HTTPServerException #10209

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
@@ -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