Skip to content

Commit

Permalink
[Fix #10258] Recover Ruby 2.4 code analysis using `TargetRubyVersion:…
Browse files Browse the repository at this point in the history
… 2.4`

Fixes #10258 and reverts part of #9648.

Only the Ruby version (2.4) to runtime should have been dropped,
not code analysis.
This PR makes Ruby 2.4 code analysis with `TargetRubyVersion: 2.4`.
So, this keeps it compatible with `TargetRubyVersion` up to
RuboCop 1.12.1 as semver.

It aims to solve essentially the same problem as #10626.
  • Loading branch information
koic authored and bbatsov committed May 13, 2022
1 parent c225149 commit a5c1bc4
Show file tree
Hide file tree
Showing 10 changed files with 344 additions and 302 deletions.
1 change: 1 addition & 0 deletions changelog/fix_recover_ruby_24_code_analysis.md
@@ -0,0 +1 @@
* [#10258](https://github.com/rubocop/rubocop/issues/10258): Recover Ruby 2.4 code analysis using `TargetRubyVersion: 2.4`. ([@koic][])
5 changes: 5 additions & 0 deletions lib/rubocop/cop/style/hash_transform_keys.rb
Expand Up @@ -7,6 +7,8 @@ module Style
# `_.map {...}.to_h`, and `Hash[_.map {...}]` that are actually just
# transforming the keys of a hash, and tries to use a simpler & faster
# call to `transform_keys` instead.
# It should only be enabled on Ruby version 2.5 or newer.
# (`transform_keys` was added in Ruby 2.5.)
#
# @safety
# This cop is unsafe, as it can produce false positives if we are
Expand All @@ -26,6 +28,9 @@ module Style
class HashTransformKeys < Base
include HashTransformMethod
extend AutoCorrector
extend TargetRubyVersion

minimum_target_ruby_version 2.5

# @!method on_bad_each_with_object(node)
def_node_matcher :on_bad_each_with_object, <<~PATTERN
Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/style/redundant_begin.rb
Expand Up @@ -36,6 +36,7 @@ module Style
# do_something
#
# # bad
# # When using Ruby 2.5 or later.
# do_something do
# begin
# something
Expand Down Expand Up @@ -75,6 +76,7 @@ def on_def(node)
alias on_defs on_def

def on_block(node)
return if target_ruby_version < 2.5
return if node.send_node.lambda_literal?
return if node.braces?
return unless node.body&.kwbegin_type?
Expand Down
4 changes: 4 additions & 0 deletions lib/rubocop/rspec/shared_contexts.rb
Expand Up @@ -116,6 +116,10 @@ def source_range(range, buffer: source_buffer)
end
end

RSpec.shared_context 'ruby 2.4', :ruby24 do
let(:ruby_version) { 2.4 }
end

RSpec.shared_context 'ruby 2.5', :ruby25 do
let(:ruby_version) { 2.5 }
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/target_ruby.rb
Expand Up @@ -4,7 +4,7 @@ module RuboCop
# The kind of Ruby that code inspected by RuboCop is written in.
# @api private
class TargetRuby
KNOWN_RUBIES = [2.5, 2.6, 2.7, 3.0, 3.1, 3.2].freeze
KNOWN_RUBIES = [2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2].freeze
DEFAULT_VERSION = 2.6

OBSOLETE_RUBIES = {
Expand Down
6 changes: 4 additions & 2 deletions spec/rubocop/cli_spec.rb
Expand Up @@ -1693,7 +1693,9 @@ def method(foo, bar, qux, fred, arg5, f) end #{'#' * 85}
expect($stderr.string.strip).to start_with(
'Error: RuboCop found unknown Ruby version 4.0 in `TargetRubyVersion`'
)
expect($stderr.string.strip).to match(/Supported versions: 2.5, 2.6, 2.7, 3.0, 3.1, 3.2/)
expect($stderr.string.strip).to match(
/Supported versions: 2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2/
)
end
end

Expand All @@ -1714,7 +1716,7 @@ def method(foo, bar, qux, fred, arg5, f) end #{'#' * 85}
/2\.0-compatible analysis was dropped after version 0\.50/
)

expect($stderr.string.strip).to match(/Supported versions: 2.5/)
expect($stderr.string.strip).to match(/Supported versions: 2.4/)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/rubocop/config_loader_spec.rb
Expand Up @@ -1188,7 +1188,7 @@ class Loop < Base
end

context 'when the specified version is obsolete' do
let(:inherited_version) { '2.4' }
let(:inherited_version) { '2.3' }

context 'and it is not overridden' do
before do
Expand All @@ -1199,7 +1199,7 @@ class Loop < Base

it 'raises a validation error' do
expect { configuration_from_file }.to raise_error(RuboCop::ValidationError) do |error|
expect(error.message).to start_with('RuboCop found unsupported Ruby version 2.4')
expect(error.message).to start_with('RuboCop found unsupported Ruby version 2.3')
end
end
end
Expand Down
74 changes: 39 additions & 35 deletions spec/rubocop/cop/lint/suppressed_exception_spec.rb
Expand Up @@ -114,25 +114,27 @@ def self.foo
end
end

context 'when empty rescue for `do` block' do
it 'registers an offense for empty rescue without comment' do
expect_offense(<<~RUBY)
foo do
do_something
rescue
^^^^^^ Do not suppress exceptions.
end
RUBY
end
context 'Ruby 2.5 or higher', :ruby25 do
context 'when empty rescue for `do` block' do
it 'registers an offense for empty rescue without comment' do
expect_offense(<<~RUBY)
foo do
do_something
rescue
^^^^^^ Do not suppress exceptions.
end
RUBY
end

it 'registers an offense for empty rescue with comment' do
expect_offense(<<~RUBY)
foo do
rescue
^^^^^^ Do not suppress exceptions.
# do nothing
end
RUBY
it 'registers an offense for empty rescue with comment' do
expect_offense(<<~RUBY)
foo do
rescue
^^^^^^ Do not suppress exceptions.
# do nothing
end
RUBY
end
end
end
end
Expand Down Expand Up @@ -195,24 +197,26 @@ def self.foo
end
end

context 'when empty rescue for `do` block' do
it 'registers an offense for empty rescue without comment' do
expect_offense(<<~RUBY)
foo do
do_something
rescue
^^^^^^ Do not suppress exceptions.
end
RUBY
end
context 'Ruby 2.5 or higher', :ruby25 do
context 'when empty rescue for `do` block' do
it 'registers an offense for empty rescue without comment' do
expect_offense(<<~RUBY)
foo do
do_something
rescue
^^^^^^ Do not suppress exceptions.
end
RUBY
end

it 'does not register an offense for empty rescue with comment' do
expect_no_offenses(<<~RUBY)
foo do
rescue
# do nothing
end
RUBY
it 'does not register an offense for empty rescue with comment' do
expect_no_offenses(<<~RUBY)
foo do
rescue
# do nothing
end
RUBY
end
end
end

Expand Down

0 comments on commit a5c1bc4

Please sign in to comment.