Skip to content

Commit

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

Reverts part of rubocop#6766, rubocop#7026, and rubocop#7030.

Only the Ruby version (2.2) to runtime should have been dropped, not code analysis.
This PR makes Ruby 2.2 code analysis with `TargetRubyVersion: 2.2`.
It aims to solve essentially the same problem as rubocop#10626, rubocop#10632, and rubocop#10640.

Previously, there was the following default enforced style `when_needed` for
`Style/FrozenStringLiteralComment` cop.

```ruby
# @example EnforcedStyle: when_needed (default)
#   # The `when_needed` style will add the frozen string literal
#   # to files only when the `TargetRubyVersion` is set to 2.3+.
#   # bad
#   module Foo
#     # ...
#   end
#
#   # good
#   # frozen_string_literal: true
#
#   module Foo
#     # ...
#   end
```

This PR does not restore that option, but sets the `minimum_target_ruby_version 2.3`
to make `always (default)` apply by default. It is a simple solution that does not
handle frozen literal magic comment added in Ruby 2.3 when `TargetRubyVersion` is
Ruby 2.2 or lower.
  • Loading branch information
koic committed May 18, 2022
1 parent 8ee7a42 commit a14ff0f
Show file tree
Hide file tree
Showing 15 changed files with 299 additions and 224 deletions.
1 change: 1 addition & 0 deletions changelog/changelog/fix_recover_ruby_22_code_analysis.md
@@ -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
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
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
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
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
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
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
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
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

0 comments on commit a14ff0f

Please sign in to comment.