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.2 code analysis using TargetRubyVersion: 2.2 #10644

Merged
merged 1 commit into from
May 23, 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/changelog/fix_recover_ruby_22_code_analysis.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#10644](https://github.com/rubocop/rubocop/pull/10644): Recover Ruby 2.2 code analysis using `TargetRubyVersion: 2.2`. ([@koic][])
3 changes: 3 additions & 0 deletions lib/rubocop/cop/lint/safe_navigation_chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ module Lint
# x&.foo || bar
class SafeNavigationChain < Base
include NilMethods
extend TargetRubyVersion

minimum_target_ruby_version 2.3

MSG = 'Do not chain ordinary method call after safe navigation operator.'

Expand Down
5 changes: 4 additions & 1 deletion lib/rubocop/cop/style/frozen_string_literal_comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Style
# It will add the `# frozen_string_literal: true` magic comment to the top
# of files to enable frozen string literals. Frozen string literals may be
# default in future Ruby. The comment will be added below a shebang and
# encoding comment.
# encoding comment. The frozen string literal comment is only valid in Ruby 2.3+.
#
# Note that the cop will accept files where the comment exists but is set
# to `false` instead of `true`.
Expand Down Expand Up @@ -86,6 +86,9 @@ class FrozenStringLiteralComment < Base
include FrozenStringLiteral
include RangeHelp
extend AutoCorrector
extend TargetRubyVersion

minimum_target_ruby_version 2.3

MSG_MISSING_TRUE = 'Missing magic comment `# frozen_string_literal: true`.'
MSG_MISSING = 'Missing frozen string literal comment.'
Expand Down
10 changes: 9 additions & 1 deletion lib/rubocop/cop/style/numeric_predicate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def check(node)
predicate(node)
end

return unless numeric && operator
return unless numeric && operator && replacement_supported?(operator)

[numeric, replacement(numeric, operator)]
end
Expand All @@ -107,6 +107,14 @@ def require_parentheses?(node)
node.send_type? && node.binary_operation? && !node.parenthesized?
end

def replacement_supported?(operator)
if %i[> <].include?(operator)
target_ruby_version >= 2.3
else
true
end
end

def invert
lambda do |comparison, numeric|
comparison = { :> => :<, :< => :> }[comparison] || comparison
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def to_supported_styles(enforced_style)
private

def compatible_external_encoding_for?(src)
src = src.dup if RUBY_ENGINE == 'jruby'
src = src.dup if RUBY_VERSION < '2.3' || RUBY_ENGINE == 'jruby'
src.force_encoding(Encoding.default_external).valid_encoding?
end
end
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.2', :ruby22 do
let(:ruby_version) { 2.2 }
end

RSpec.shared_context 'ruby 2.3', :ruby23 do
let(:ruby_version) { 2.3 }
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.3, 2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2].freeze
KNOWN_RUBIES = [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
4 changes: 2 additions & 2 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.3, 2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2/
/Supported versions: 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2/
)
end
end
Expand All @@ -1746,7 +1746,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.3/)
expect($stderr.string.strip).to match(/Supported versions: 2.2/)
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.2' }
let(:inherited_version) { '2.1' }

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.2')
expect(error.message).to start_with('RuboCop found unsupported Ruby version 2.1')
end
end
end
Expand Down