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

Recover Ruby 2.0 code analysis using TargetRubyVersion: 2.0 #10668

Merged
merged 1 commit into from
May 25, 2022
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
1 change: 1 addition & 0 deletions changelog/fix_recover_ruby_20_code_analysis.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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