Skip to content

Commit

Permalink
Recover Ruby 2.0 code analysis using TargetRubyVersion: 2.0
Browse files Browse the repository at this point in the history
Follow up rubocop#10632 (comment).

Reverts part of rubocop#4787.

Only the Ruby version (2.0) to runtime should have been dropped, not code analysis.
This PR makes Ruby 2.0 code analysis with `TargetRubyVersion: 2.0`.
It aims to solve essentially the same problem as rubocop#10626, rubocop#10632, rubocop#10640, rubocop#10644, and rubocop#10662.
  • Loading branch information
koic committed May 25, 2022
1 parent bfe86b1 commit 0d092f3
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 10 deletions.
1 change: 1 addition & 0 deletions changelog/fix_recover_ruby_20_code_analysis.md
@@ -0,0 +1 @@
* [#10668](https://github.com/rubocop/rubocop/pull/10668): Recover Ruby 2.0 code analysis using `TargetRubyVersion: 2.0`. ([@koic][])
3 changes: 3 additions & 0 deletions lib/rubocop/cop/style/dir.rb
Expand Up @@ -18,6 +18,9 @@ module Style
# path = __dir__
class Dir < Base
extend AutoCorrector
extend TargetRubyVersion

minimum_target_ruby_version 2.0

MSG = "Use `__dir__` to get an absolute path to the current file's directory."
RESTRICT_ON_SEND = %i[expand_path dirname].freeze
Expand Down
6 changes: 5 additions & 1 deletion lib/rubocop/cop/style/symbol_array.rb
Expand Up @@ -7,7 +7,8 @@ module Style
# using the %i() syntax.
#
# Alternatively, it checks for symbol arrays using the %i() syntax on
# projects which do not want to use that syntax.
# projects which do not want to use that syntax, perhaps because they
# support a version of Ruby lower than 2.0.
#
# Configuration option: MinSize
# If set, arrays with fewer elements than this value will not trigger the
Expand All @@ -33,6 +34,9 @@ class SymbolArray < Base
include ConfigurableEnforcedStyle
include PercentArray
extend AutoCorrector
extend TargetRubyVersion

minimum_target_ruby_version 2.0

PERCENT_MSG = 'Use `%i` or `%I` for an array of symbols.'
ARRAY_MSG = 'Use `%<prefer>s` for an array of symbols.'
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.0', :ruby20 do
let(:ruby_version) { 2.0 }
end

RSpec.shared_context 'ruby 2.1', :ruby21 do
let(:ruby_version) { 2.1 }
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.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2].freeze
KNOWN_RUBIES = [2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2].freeze
DEFAULT_VERSION = 2.6

OBSOLETE_RUBIES = {
Expand Down
10 changes: 5 additions & 5 deletions spec/rubocop/cli_spec.rb
Expand Up @@ -1724,7 +1724,7 @@ def method(foo, bar, qux, fred, arg5, f) end #{'#' * 85}
'Error: RuboCop found unknown Ruby version 4.0 in `TargetRubyVersion`'
)
expect($stderr.string.strip).to match(
/Supported versions: 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2/
/Supported versions: 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2/
)
end
end
Expand All @@ -1733,20 +1733,20 @@ def method(foo, bar, qux, fred, arg5, f) end #{'#' * 85}
it 'fails with an error message' do
create_file('.rubocop.yml', <<~YAML)
AllCops:
TargetRubyVersion: 2.0
TargetRubyVersion: 1.9
YAML

expect(cli.run([])).to eq(2)
expect($stderr.string.strip).to start_with(
'Error: RuboCop found unsupported Ruby version 2.0 in '\
'Error: RuboCop found unsupported Ruby version 1.9 in '\
'`TargetRubyVersion`'
)

expect($stderr.string.strip).to match(
/2\.0-compatible analysis was dropped after version 0\.50/
/1\.9-compatible analysis was dropped after version 0\.41/
)

expect($stderr.string.strip).to match(/Supported versions: 2.1/)
expect($stderr.string.strip).to match(/Supported versions: 2.0/)
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.0' }
let(:inherited_version) { '1.9' }

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.0')
expect(error.message).to start_with('RuboCop found unsupported Ruby version 1.9')
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/cop/style/optional_arguments_spec.rb
Expand Up @@ -71,7 +71,7 @@ def foo(a = 1, b: 2)
end
end

context 'required params' do
context 'required params', :ruby21 do
it 'registers an offense for optional arguments that come before ' \
'required arguments where there are name arguments' do
expect_offense(<<~RUBY)
Expand Down

0 comments on commit 0d092f3

Please sign in to comment.