Skip to content

Commit

Permalink
[Fix rubocop#10625] Restore the specification to `TargetRubyVersion: …
Browse files Browse the repository at this point in the history
…2.5`

Fixes rubocop#10625 and reverts part of rubocop#10577.

Only the Ruby version (2.5) to runtime should have been dropped, not code analysis.
This PR the specification to `TargetRubyVersion: 2.5` So, this keeps it compatible
with `TargetRubyVersion` up to 1.28.2.
  • Loading branch information
koic committed May 12, 2022
1 parent fbe6b7b commit 50e25b3
Show file tree
Hide file tree
Showing 16 changed files with 358 additions and 288 deletions.
1 change: 1 addition & 0 deletions changelog/fix_restore_target_ruby_version_ruby_25.md
@@ -0,0 +1 @@
* [#10625](https://github.com/rubocop/rubocop/issues/10625): Restore the specification to `TargetRubyVersion: 2.5`. ([@koic][])
3 changes: 3 additions & 0 deletions lib/rubocop/cop/lint/erb_new_arguments.rb
Expand Up @@ -62,6 +62,9 @@ module Lint
class ErbNewArguments < Base
include RangeHelp
extend AutoCorrector
extend TargetRubyVersion

minimum_target_ruby_version 2.6

MESSAGES = [
'Passing safe_level with the 2nd argument of `ERB.new` is ' \
Expand Down
3 changes: 3 additions & 0 deletions lib/rubocop/cop/style/map_to_hash.rb
Expand Up @@ -29,8 +29,11 @@ module Style
#
class MapToHash < Base
extend AutoCorrector
extend TargetRubyVersion
include RangeHelp

minimum_target_ruby_version 2.6

MSG = 'Pass a block to `to_h` instead of calling `%<method>s.to_h`.'
RESTRICT_ON_SEND = %i[to_h].freeze

Expand Down
3 changes: 3 additions & 0 deletions lib/rubocop/cop/style/slicing_with_range.rb
Expand Up @@ -27,6 +27,9 @@ module Style
# items[1..]
class SlicingWithRange < Base
extend AutoCorrector
extend TargetRubyVersion

minimum_target_ruby_version 2.6

MSG = 'Prefer ary[n..] over ary[n..-1].'
RESTRICT_ON_SEND = %i[[]].freeze
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.5', :ruby25 do
let(:ruby_version) { 2.5 }
end

RSpec.shared_context 'ruby 2.6', :ruby26 do
let(:ruby_version) { 2.6 }
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.6, 2.7, 3.0, 3.1, 3.2].freeze
KNOWN_RUBIES = [2.5, 2.6, 2.7, 3.0, 3.1, 3.2].freeze
DEFAULT_VERSION = KNOWN_RUBIES.first

OBSOLETE_RUBIES = {
Expand Down
6 changes: 3 additions & 3 deletions spec/rubocop/cli_spec.rb
Expand Up @@ -150,7 +150,7 @@ def and_with_args
expect($stderr.string).to eq ''
expect($stdout.string)
.to eq(["#{abs('example.rb')}:3:1: E: Lint/Syntax: unexpected " \
'token $end (Using Ruby 2.6 parser; configure using ' \
'token $end (Using Ruby 2.5 parser; configure using ' \
'`TargetRubyVersion` parameter, under `AllCops`)',
''].join("\n"))
end
Expand Down Expand Up @@ -1693,7 +1693,7 @@ 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.6, 2.7, 3.0, 3.1, 3.2/)
expect($stderr.string.strip).to match(/Supported versions: 2.5, 2.6, 2.7, 3.0, 3.1, 3.2/)
end
end

Expand All @@ -1714,7 +1714,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.6/)
expect($stderr.string.strip).to match(/Supported versions: 2.5/)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/cop/lint/ambiguous_range_spec.rb
Expand Up @@ -86,7 +86,7 @@
RUBY
end

it 'can handle an endless range' do
it 'can handle an endless range', :ruby26 do
expect_offense(<<~RUBY)
x || 1#{operator}
^^^^^^ Wrap complex range boundaries with parentheses to avoid ambiguity.
Expand Down
26 changes: 18 additions & 8 deletions spec/rubocop/cop/lint/deprecated_constants_spec.rb
Expand Up @@ -51,15 +51,25 @@
RUBY
end

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
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)
Net::HTTPClientException
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
Expand Down
4 changes: 3 additions & 1 deletion spec/rubocop/cop/lint/duplicate_hash_key_spec.rb
Expand Up @@ -63,7 +63,9 @@
it_behaves_like 'duplicated literal key', 'false'
it_behaves_like 'duplicated literal key', 'nil'
it_behaves_like 'duplicated literal key', "'str'"
it_behaves_like 'duplicated literal key', '(42..)'
context 'target ruby version >= 2.6', :ruby26 do
it_behaves_like 'duplicated literal key', '(42..)'
end

shared_examples 'duplicated non literal key' do |key|
it "does not register an offense for duplicated `#{key}` hash keys" do
Expand Down
168 changes: 89 additions & 79 deletions spec/rubocop/cop/lint/erb_new_arguments_spec.rb
@@ -1,101 +1,111 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Lint::ErbNewArguments, :config do
it 'registers an offense when using `ERB.new` with non-keyword 2nd argument' do
expect_offense(<<~RUBY)
ERB.new(str, nil)
^^^ Passing safe_level with the 2nd argument of `ERB.new` is deprecated. Do not use it, and specify other arguments as keyword arguments.
RUBY

expect_correction(<<~RUBY)
ERB.new(str)
RUBY
context '<= Ruby 2.5', :ruby25 do
it 'does not register an offense when using `ERB.new` with non-keyword arguments' do
expect_no_offenses(<<~RUBY)
ERB.new(str, nil, '-', '@output_buffer')
RUBY
end
end

it 'registers an offense when using `ERB.new` with non-keyword 2nd and 3rd arguments' do
expect_offense(<<~RUBY)
ERB.new(str, nil, '-')
^^^ Passing trim_mode with the 3rd argument of `ERB.new` is deprecated. Use keyword argument like `ERB.new(str, trim_mode: '-')` instead.
^^^ Passing safe_level with the 2nd argument of `ERB.new` is deprecated. Do not use it, and specify other arguments as keyword arguments.
RUBY
context '>= Ruby 2.6', :ruby26 do
it 'registers an offense when using `ERB.new` with non-keyword 2nd argument' do
expect_offense(<<~RUBY)
ERB.new(str, nil)
^^^ Passing safe_level with the 2nd argument of `ERB.new` is deprecated. Do not use it, and specify other arguments as keyword arguments.
RUBY

expect_correction(<<~RUBY)
ERB.new(str, trim_mode: '-')
RUBY
end
expect_correction(<<~RUBY)
ERB.new(str)
RUBY
end

it 'registers an offense when using `ERB.new` with non-keyword 2nd, 3rd and 4th arguments' do
expect_offense(<<~RUBY)
ERB.new(str, nil, '-', '@output_buffer')
^^^^^^^^^^^^^^^^ Passing eoutvar with the 4th argument of `ERB.new` is deprecated. Use keyword argument like `ERB.new(str, eoutvar: '@output_buffer')` instead.
^^^ Passing trim_mode with the 3rd argument of `ERB.new` is deprecated. Use keyword argument like `ERB.new(str, trim_mode: '-')` instead.
^^^ Passing safe_level with the 2nd argument of `ERB.new` is deprecated. Do not use it, and specify other arguments as keyword arguments.
RUBY

expect_correction(<<~RUBY)
ERB.new(str, trim_mode: '-', eoutvar: '@output_buffer')
RUBY
end
it 'registers an offense when using `ERB.new` with non-keyword 2nd and 3rd arguments' do
expect_offense(<<~RUBY)
ERB.new(str, nil, '-')
^^^ Passing trim_mode with the 3rd argument of `ERB.new` is deprecated. Use keyword argument like `ERB.new(str, trim_mode: '-')` instead.
^^^ Passing safe_level with the 2nd argument of `ERB.new` is deprecated. Do not use it, and specify other arguments as keyword arguments.
RUBY

it 'registers an offense when using `ERB.new` ' \
'with non-keyword 2nd, 3rd and 4th arguments and' \
'keyword 5th argument' do
expect_offense(<<~RUBY)
ERB.new(str, nil, '-', '@output_buffer', trim_mode: '-', eoutvar: '@output_buffer')
^^^^^^^^^^^^^^^^ Passing eoutvar with the 4th argument of `ERB.new` is deprecated. Use keyword argument like `ERB.new(str, eoutvar: '@output_buffer')` instead.
^^^ Passing trim_mode with the 3rd argument of `ERB.new` is deprecated. Use keyword argument like `ERB.new(str, trim_mode: '-')` instead.
^^^ Passing safe_level with the 2nd argument of `ERB.new` is deprecated. Do not use it, and specify other arguments as keyword arguments.
RUBY

expect_correction(<<~RUBY)
ERB.new(str, trim_mode: '-', eoutvar: '@output_buffer')
RUBY
end
expect_correction(<<~RUBY)
ERB.new(str, trim_mode: '-')
RUBY
end

it 'registers an offense when using `ERB.new` ' \
'with non-keyword 2nd and 3rd arguments and' \
'keyword 4th argument' do
expect_offense(<<~RUBY)
ERB.new(str, nil, '-', trim_mode: '-', eoutvar: '@output_buffer')
^^^ Passing trim_mode with the 3rd argument of `ERB.new` is deprecated. Use keyword argument like `ERB.new(str, trim_mode: '-')` instead.
^^^ Passing safe_level with the 2nd argument of `ERB.new` is deprecated. Do not use it, and specify other arguments as keyword arguments.
RUBY

expect_correction(<<~RUBY)
ERB.new(str, trim_mode: '-', eoutvar: '@output_buffer')
RUBY
end
it 'registers an offense when using `ERB.new` with non-keyword 2nd, 3rd and 4th arguments' do
expect_offense(<<~RUBY)
ERB.new(str, nil, '-', '@output_buffer')
^^^^^^^^^^^^^^^^ Passing eoutvar with the 4th argument of `ERB.new` is deprecated. Use keyword argument like `ERB.new(str, eoutvar: '@output_buffer')` instead.
^^^ Passing trim_mode with the 3rd argument of `ERB.new` is deprecated. Use keyword argument like `ERB.new(str, trim_mode: '-')` instead.
^^^ Passing safe_level with the 2nd argument of `ERB.new` is deprecated. Do not use it, and specify other arguments as keyword arguments.
RUBY

expect_correction(<<~RUBY)
ERB.new(str, trim_mode: '-', eoutvar: '@output_buffer')
RUBY
end

it 'registers an offense when using `::ERB.new` with non-keyword 2nd, 3rd and 4th arguments' do
expect_offense(<<~RUBY)
::ERB.new(str, nil, '-', '@output_buffer')
it 'registers an offense when using `ERB.new` ' \
'with non-keyword 2nd, 3rd and 4th arguments and' \
'keyword 5th argument' do
expect_offense(<<~RUBY)
ERB.new(str, nil, '-', '@output_buffer', trim_mode: '-', eoutvar: '@output_buffer')
^^^^^^^^^^^^^^^^ Passing eoutvar with the 4th argument of `ERB.new` is deprecated. Use keyword argument like `ERB.new(str, eoutvar: '@output_buffer')` instead.
^^^ Passing trim_mode with the 3rd argument of `ERB.new` is deprecated. Use keyword argument like `ERB.new(str, trim_mode: '-')` instead.
^^^ Passing safe_level with the 2nd argument of `ERB.new` is deprecated. Do not use it, and specify other arguments as keyword arguments.
RUBY
RUBY

expect_correction(<<~RUBY)
::ERB.new(str, trim_mode: '-', eoutvar: '@output_buffer')
RUBY
end
expect_correction(<<~RUBY)
ERB.new(str, trim_mode: '-', eoutvar: '@output_buffer')
RUBY
end

it 'does not register an offense when using `ERB.new` with keyword arguments' do
expect_no_offenses(<<~RUBY)
ERB.new(str, trim_mode: '-', eoutvar: '@output_buffer')
RUBY
end
it 'registers an offense when using `ERB.new` ' \
'with non-keyword 2nd and 3rd arguments and' \
'keyword 4th argument' do
expect_offense(<<~RUBY)
ERB.new(str, nil, '-', trim_mode: '-', eoutvar: '@output_buffer')
^^^ Passing trim_mode with the 3rd argument of `ERB.new` is deprecated. Use keyword argument like `ERB.new(str, trim_mode: '-')` instead.
^^^ Passing safe_level with the 2nd argument of `ERB.new` is deprecated. Do not use it, and specify other arguments as keyword arguments.
RUBY

it 'does not register an offense when using `ERB.new` without optional arguments' do
expect_no_offenses(<<~RUBY)
ERB.new(str)
RUBY
end
expect_correction(<<~RUBY)
ERB.new(str, trim_mode: '-', eoutvar: '@output_buffer')
RUBY
end

context 'when using `ActionView::Template::Handlers::ERB.new`' do
it 'does not register an offense when using `ERB.new` without arguments' do
it 'registers an offense when using `::ERB.new` with non-keyword 2nd, 3rd and 4th arguments' do
expect_offense(<<~RUBY)
::ERB.new(str, nil, '-', '@output_buffer')
^^^^^^^^^^^^^^^^ Passing eoutvar with the 4th argument of `ERB.new` is deprecated. Use keyword argument like `ERB.new(str, eoutvar: '@output_buffer')` instead.
^^^ Passing trim_mode with the 3rd argument of `ERB.new` is deprecated. Use keyword argument like `ERB.new(str, trim_mode: '-')` instead.
^^^ Passing safe_level with the 2nd argument of `ERB.new` is deprecated. Do not use it, and specify other arguments as keyword arguments.
RUBY

expect_correction(<<~RUBY)
::ERB.new(str, trim_mode: '-', eoutvar: '@output_buffer')
RUBY
end

it 'does not register an offense when using `ERB.new` with keyword arguments' do
expect_no_offenses(<<~RUBY)
ERB.new
ERB.new(str, trim_mode: '-', eoutvar: '@output_buffer')
RUBY
end

it 'does not register an offense when using `ERB.new` without optional arguments' do
expect_no_offenses(<<~RUBY)
ERB.new(str)
RUBY
end

context 'when using `ActionView::Template::Handlers::ERB.new`' do
it 'does not register an offense when using `ERB.new` without arguments' do
expect_no_offenses(<<~RUBY)
ERB.new
RUBY
end
end
end
end

0 comments on commit 50e25b3

Please sign in to comment.