From 932375f77f8c7b3b0601008674fa247038ca2539 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sun, 24 Oct 2021 02:19:26 +0900 Subject: [PATCH] Make `Lint/DeprecatedConstants` aware of `Net::HTTPServerException` 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 `
': 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. --- ...ants_aware_of_net_http_server_exception.md | 1 + config/default.yml | 4 ++ lib/rubocop/cop/lint/deprecated_constants.rb | 5 +- .../cop/lint/deprecated_constants_spec.rb | 72 ++++++++++++++----- 4 files changed, 61 insertions(+), 21 deletions(-) create mode 100644 changelog/change_make_deprecated_constants_aware_of_net_http_server_exception.md diff --git a/changelog/change_make_deprecated_constants_aware_of_net_http_server_exception.md b/changelog/change_make_deprecated_constants_aware_of_net_http_server_exception.md new file mode 100644 index 00000000000..07422a95cc2 --- /dev/null +++ b/changelog/change_make_deprecated_constants_aware_of_net_http_server_exception.md @@ -0,0 +1 @@ +* [#10209](https://github.com/rubocop/rubocop/pull/10209): Make `Lint/DeprecatedConstants` aware of `Net::HTTPServerException`. ([@koic][]) diff --git a/config/default.yml b/config/default.yml index 84b6c8556da..572710c6aea 100644 --- a/config/default.yml +++ b/config/default.yml @@ -1568,6 +1568,7 @@ Lint/DeprecatedConstants: Description: 'Checks for deprecated constants.' Enabled: pending VersionAdded: '1.8' + VersionChanged: '<>' # 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`. @@ -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' diff --git a/lib/rubocop/cop/lint/deprecated_constants.rb b/lib/rubocop/cop/lint/deprecated_constants.rb index 546ed6acbeb..181b6f2be4b 100644 --- a/lib/rubocop/cop/lint/deprecated_constants.rb +++ b/lib/rubocop/cop/lint/deprecated_constants.rb @@ -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) @@ -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 diff --git a/spec/rubocop/cop/lint/deprecated_constants_spec.rb b/spec/rubocop/cop/lint/deprecated_constants_spec.rb index ad4b23a9043..1787bafa75e 100644 --- a/spec/rubocop/cop/lint/deprecated_constants_spec.rb +++ b/spec/rubocop/cop/lint/deprecated_constants_spec.rb @@ -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' }, @@ -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 @@ -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