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/rubocop#10644.

Reverts part of rubocop/rubocop#7026.

This PR requires rubocop/rubocop#10644 to pass the build.
  • Loading branch information
koic committed Jun 2, 2022
1 parent 3a93706 commit 3290f7c
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 92 deletions.
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 @@
* [#708](https://github.com/rubocop/rubocop-rails/pull/708): Recover Ruby 2.2 code analysis using `TargetRubyVersion: 2.2`. ([@koic][])
6 changes: 5 additions & 1 deletion lib/rubocop/cop/rails/safe_navigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ module RuboCop
module Cop
module Rails
# Converts usages of `try!` to `&.`. It can also be configured
# to convert `try`. It will convert code to use safe navigation.
# to convert `try`. It will convert code to use safe navigation
# if the target Ruby version is set to 2.3+
#
# @example ConvertTry: false (default)
# # bad
Expand Down Expand Up @@ -39,6 +40,9 @@ module Rails
class SafeNavigation < Base
include RangeHelp
extend AutoCorrector
extend TargetRubyVersion

minimum_target_ruby_version 2.3

MSG = 'Use safe navigation (`&.`) instead of `%<try>s`.'
RESTRICT_ON_SEND = %i[try try!].freeze
Expand Down
210 changes: 119 additions & 91 deletions spec/rubocop/cop/rails/safe_navigation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,43 @@

it_behaves_like 'accepts', 'non try! method calls', 'join'

context 'try!' do
it_behaves_like 'offense', 'try! with a single parameter', 'try!',
'(:join)'
it_behaves_like 'offense', 'try! with a multiple parameters', 'try!',
'(:join, ",")'
it_behaves_like 'offense', 'try! with a block', 'try!',
'(:map) { |e| e.some_method }'
it_behaves_like 'offense', 'try! with params and a block', 'try!',
['(:each_with_object, []) do |e, acc|',
context 'target_ruby_version < 2.3', :ruby22 do
it_behaves_like 'accepts', 'try! with a single parameter', 'try!(:join)'
it_behaves_like 'accepts', 'try! with a multiple parameters',
'try!(:join, ",")'
it_behaves_like 'accepts', 'try! with a block',
'try!(:map) { |e| e.some_method }'
it_behaves_like 'accepts', 'try! with params and a block',
['try!(:each_with_object, []) do |e, acc|',
' acc << e.some_method',
'end'].join("\n")
it_behaves_like 'offense', 'try! with a question method', 'try!',
'(:something?)'
it_behaves_like 'offense', 'try! with a bang method', 'try!',
'(:something!)'

it_behaves_like 'accepts', 'try! used to call an enumerable accessor',
'foo.try!(:[], :bar)'
it_behaves_like 'accepts', 'try! with ==', 'foo.try!(:==, bar)'
it_behaves_like 'accepts', 'try! with an operator', 'foo.try!(:+, bar)'
it_behaves_like 'accepts', 'try! with a method stored as a variable',
['bar = :==',
'foo.try!(baz, bar)'].join("\n")
end

context 'target_ruby_version > 2.3', :ruby23 do
context 'try!' do
it_behaves_like 'offense', 'try! with a single parameter', 'try!',
'(:join)'
it_behaves_like 'offense', 'try! with a multiple parameters', 'try!',
'(:join, ",")'
it_behaves_like 'offense', 'try! with a block', 'try!',
'(:map) { |e| e.some_method }'
it_behaves_like 'offense', 'try! with params and a block', 'try!',
['(:each_with_object, []) do |e, acc|',
' acc << e.some_method',
'end'].join("\n")
it_behaves_like 'offense', 'try! with a question method', 'try!',
'(:something?)'
it_behaves_like 'offense', 'try! with a bang method', 'try!',
'(:something!)'

it_behaves_like 'accepts', 'try! used to call an enumerable accessor',
'foo.try!(:[], :bar)'
it_behaves_like 'accepts', 'try! with ==', 'foo.try!(:==, bar)'
it_behaves_like 'accepts', 'try! with an operator', 'foo.try!(:+, bar)'
it_behaves_like 'accepts', 'try! with a method stored as a variable',
['bar = :==',
'foo.try!(baz, bar)'].join("\n")
end
end

context 'try' do
Expand Down Expand Up @@ -93,80 +107,94 @@
context 'convert try and try!' do
let(:cop_config) { { 'ConvertTry' => true } }

context 'try!' do
it_behaves_like 'offense', 'try! with a single parameter', 'try!',
'(:join)'
it_behaves_like 'offense', 'try! with a multiple parameters', 'try!',
'(:join, ",")'
it_behaves_like 'offense', 'try! with a block', 'try!',
'(:map) { |e| e.some_method }'
it_behaves_like 'offense', 'try! with params and a block', 'try!',
['(:each_with_object, []) do |e, acc|',
' acc << e.some_method',
'end'].join("\n")

it_behaves_like 'accepts', 'try! used to call an enumerable accessor',
'foo.try!(:[], :bar)'

it_behaves_like 'autocorrect', 'try! a single parameter',
'[1, 2].try!(:join)', '[1, 2]&.join'
it_behaves_like 'autocorrect', 'try! with 2 parameters',
'[1, 2].try!(:join, ",")', '[1, 2]&.join(",")'
it_behaves_like 'autocorrect', 'try! with multiple parameters',
'[1, 2].try!(:join, bar, baz)', '[1, 2]&.join(bar, baz)'
it_behaves_like 'autocorrect', 'try! without receiver',
'try!(:join)', 'self&.join'
it_behaves_like 'autocorrect', 'try! with a block',
['[foo, bar].try!(:map) do |e|',
' e.some_method',
'end'].join("\n"),
['[foo, bar]&.map do |e|',
' e.some_method',
'end'].join("\n")
it_behaves_like 'autocorrect', 'try! with params and a block',
['[foo, bar].try!(:each_with_object, []) do |e, acc|',
' acc << e.some_method',
'end'].join("\n"),
['[foo, bar]&.each_with_object([]) do |e, acc|',
context 'target_ruby_version < 2.3', :ruby22 do
it_behaves_like 'accepts', 'try! with a single parameter', 'try!(:join)'
it_behaves_like 'accepts', 'try! with a multiple parameters',
'try!(:join, ",")'
it_behaves_like 'accepts', 'try! with a block',
'try!(:map) { |e| e.some_method }'
it_behaves_like 'accepts', 'try! with params and a block',
['try!(:each_with_object, []) do |e, acc|',
' acc << e.some_method',
'end'].join("\n")
end

context 'try' do
it_behaves_like 'offense', 'try with a single parameter', 'try',
'(:join)'
it_behaves_like 'offense', 'try with a multiple parameters', 'try',
'(:join, ",")'
it_behaves_like 'offense', 'try with a block', 'try',
'(:map) { |e| e.some_method }'
it_behaves_like 'offense', 'try with params and a block', 'try',
['(:each_with_object, []) do |e, acc|',
' acc << e.some_method',
'end'].join("\n")

it_behaves_like 'accepts', 'try! used to call an enumerable accessor',
'foo.try!(:[], :bar)'

it_behaves_like 'autocorrect', 'try a single parameter',
'[1, 2].try(:join)', '[1, 2]&.join'
it_behaves_like 'autocorrect', 'try with 2 parameters',
'[1, 2].try(:join, ",")', '[1, 2]&.join(",")'
it_behaves_like 'autocorrect', 'try with multiple parameters',
'[1, 2].try(:join, bar, baz)', '[1, 2]&.join(bar, baz)'
it_behaves_like 'autocorrect', 'try with a block',
['[foo, bar].try(:map) do |e|',
' e.some_method',
'end'].join("\n"),
['[foo, bar]&.map do |e|',
' e.some_method',
'end'].join("\n")
it_behaves_like 'autocorrect', 'try with params and a block',
['[foo, bar].try(:each_with_object, []) do |e, acc|',
' acc << e.some_method',
'end'].join("\n"),
['[foo, bar]&.each_with_object([]) do |e, acc|',
' acc << e.some_method',
'end'].join("\n")
context 'target_ruby_version > 2.3', :ruby23 do
context 'try!' do
it_behaves_like 'offense', 'try! with a single parameter', 'try!',
'(:join)'
it_behaves_like 'offense', 'try! with a multiple parameters', 'try!',
'(:join, ",")'
it_behaves_like 'offense', 'try! with a block', 'try!',
'(:map) { |e| e.some_method }'
it_behaves_like 'offense', 'try! with params and a block', 'try!',
['(:each_with_object, []) do |e, acc|',
' acc << e.some_method',
'end'].join("\n")

it_behaves_like 'accepts', 'try! used to call an enumerable accessor',
'foo.try!(:[], :bar)'

it_behaves_like 'autocorrect', 'try! a single parameter',
'[1, 2].try!(:join)', '[1, 2]&.join'
it_behaves_like 'autocorrect', 'try! with 2 parameters',
'[1, 2].try!(:join, ",")', '[1, 2]&.join(",")'
it_behaves_like 'autocorrect', 'try! with multiple parameters',
'[1, 2].try!(:join, bar, baz)', '[1, 2]&.join(bar, baz)'
it_behaves_like 'autocorrect', 'try! without receiver',
'try!(:join)', 'self&.join'
it_behaves_like 'autocorrect', 'try! with a block',
['[foo, bar].try!(:map) do |e|',
' e.some_method',
'end'].join("\n"),
['[foo, bar]&.map do |e|',
' e.some_method',
'end'].join("\n")
it_behaves_like 'autocorrect', 'try! with params and a block',
['[foo, bar].try!(:each_with_object, []) do |e, acc|',
' acc << e.some_method',
'end'].join("\n"),
['[foo, bar]&.each_with_object([]) do |e, acc|',
' acc << e.some_method',
'end'].join("\n")
end

context 'try' do
it_behaves_like 'offense', 'try with a single parameter', 'try',
'(:join)'
it_behaves_like 'offense', 'try with a multiple parameters', 'try',
'(:join, ",")'
it_behaves_like 'offense', 'try with a block', 'try',
'(:map) { |e| e.some_method }'
it_behaves_like 'offense', 'try with params and a block', 'try',
['(:each_with_object, []) do |e, acc|',
' acc << e.some_method',
'end'].join("\n")

it_behaves_like 'accepts', 'try! used to call an enumerable accessor',
'foo.try!(:[], :bar)'

it_behaves_like 'autocorrect', 'try a single parameter',
'[1, 2].try(:join)', '[1, 2]&.join'
it_behaves_like 'autocorrect', 'try with 2 parameters',
'[1, 2].try(:join, ",")', '[1, 2]&.join(",")'
it_behaves_like 'autocorrect', 'try with multiple parameters',
'[1, 2].try(:join, bar, baz)', '[1, 2]&.join(bar, baz)'
it_behaves_like 'autocorrect', 'try with a block',
['[foo, bar].try(:map) do |e|',
' e.some_method',
'end'].join("\n"),
['[foo, bar]&.map do |e|',
' e.some_method',
'end'].join("\n")
it_behaves_like 'autocorrect', 'try with params and a block',
['[foo, bar].try(:each_with_object, []) do |e, acc|',
' acc << e.some_method',
'end'].join("\n"),
['[foo, bar]&.each_with_object([]) do |e, acc|',
' acc << e.some_method',
'end'].join("\n")
end
end
end
end

0 comments on commit 3290f7c

Please sign in to comment.