From a4c197ceeb7e3c14ca9f6d207bb8344ac478b87f Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Wed, 18 May 2022 01:39:06 +0900 Subject: [PATCH] Recover Ruby 2.0 code analysis using `TargetRubyVersion: 2.0` Follow up https://github.com/rubocop/rubocop/pull/10632#issuecomment-1125909580. Reverts part of https://github.com/rubocop/rubocop/pull/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 #10626, #10632, #10640, #10644, and #10662. --- changelog/fix_recover_ruby_20_code_analysis.md | 1 + lib/rubocop/cop/style/dir.rb | 3 +++ lib/rubocop/cop/style/symbol_array.rb | 6 +++++- lib/rubocop/rspec/shared_contexts.rb | 4 ++++ lib/rubocop/target_ruby.rb | 2 +- spec/rubocop/cli_spec.rb | 10 +++++----- spec/rubocop/config_loader_spec.rb | 4 ++-- spec/rubocop/cop/style/optional_arguments_spec.rb | 2 +- 8 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 changelog/fix_recover_ruby_20_code_analysis.md diff --git a/changelog/fix_recover_ruby_20_code_analysis.md b/changelog/fix_recover_ruby_20_code_analysis.md new file mode 100644 index 00000000000..adacfac03b7 --- /dev/null +++ b/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][]) diff --git a/lib/rubocop/cop/style/dir.rb b/lib/rubocop/cop/style/dir.rb index 0432d8af4f2..92c00aceb4b 100644 --- a/lib/rubocop/cop/style/dir.rb +++ b/lib/rubocop/cop/style/dir.rb @@ -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 diff --git a/lib/rubocop/cop/style/symbol_array.rb b/lib/rubocop/cop/style/symbol_array.rb index 140bc46cab3..3d57985306d 100644 --- a/lib/rubocop/cop/style/symbol_array.rb +++ b/lib/rubocop/cop/style/symbol_array.rb @@ -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 @@ -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 `%s` for an array of symbols.' diff --git a/lib/rubocop/rspec/shared_contexts.rb b/lib/rubocop/rspec/shared_contexts.rb index 5046de6b058..a2ec95126ca 100644 --- a/lib/rubocop/rspec/shared_contexts.rb +++ b/lib/rubocop/rspec/shared_contexts.rb @@ -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 diff --git a/lib/rubocop/target_ruby.rb b/lib/rubocop/target_ruby.rb index 0ae7a8f52ea..174d448e1b4 100644 --- a/lib/rubocop/target_ruby.rb +++ b/lib/rubocop/target_ruby.rb @@ -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 = { diff --git a/spec/rubocop/cli_spec.rb b/spec/rubocop/cli_spec.rb index af36786db94..b72b755ca1b 100644 --- a/spec/rubocop/cli_spec.rb +++ b/spec/rubocop/cli_spec.rb @@ -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 @@ -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 diff --git a/spec/rubocop/config_loader_spec.rb b/spec/rubocop/config_loader_spec.rb index 77e7fe77a37..737086038cb 100644 --- a/spec/rubocop/config_loader_spec.rb +++ b/spec/rubocop/config_loader_spec.rb @@ -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 @@ -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 diff --git a/spec/rubocop/cop/style/optional_arguments_spec.rb b/spec/rubocop/cop/style/optional_arguments_spec.rb index 82b825b28a6..c9a41983c6e 100644 --- a/spec/rubocop/cop/style/optional_arguments_spec.rb +++ b/spec/rubocop/cop/style/optional_arguments_spec.rb @@ -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)