Skip to content

Commit

Permalink
Merge pull request #11177 from epaew/bugfix/style/object_then
Browse files Browse the repository at this point in the history
Fix a false positive for `Style/ObjectThen` cop with TargetRubyVersion < 2.6
  • Loading branch information
koic committed Nov 13, 2022
2 parents 3f3c829 + b13c21a commit d6345ac
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
1 change: 1 addition & 0 deletions changelog/fix_a_false_positive_for_style_object_then.md
@@ -0,0 +1 @@
* [#11177](https://github.com/rubocop/rubocop/pull/11177): Fix a false positive for `Style/ObjectThen` cop with TargetRubyVersion < 2.6. ([@epaew][])
3 changes: 3 additions & 0 deletions lib/rubocop/cop/style/object_then.rb
Expand Up @@ -25,6 +25,9 @@ module Style
class ObjectThen < Base
include ConfigurableEnforcedStyle
extend AutoCorrector
extend TargetRubyVersion

minimum_target_ruby_version 2.6

MSG = 'Prefer `%<prefer>s` over `%<current>s`.'

Expand Down
26 changes: 18 additions & 8 deletions spec/rubocop/cop/style/object_then_spec.rb
Expand Up @@ -4,15 +4,25 @@
context 'EnforcedStyle: then' do
let(:cop_config) { { 'EnforcedStyle' => 'then' } }

it 'registers an offense for yield_self with block' do
expect_offense(<<~RUBY)
obj.yield_self { |e| e.test }
^^^^^^^^^^ Prefer `then` over `yield_self`.
RUBY
context 'Ruby 2.5', :ruby25 do
it 'accepts yield_self with block' do
expect_no_offenses(<<~RUBY)
obj.yield_self { |e| e.test }
RUBY
end
end

expect_correction(<<~RUBY)
obj.then { |e| e.test }
RUBY
context 'Ruby 2.6', :ruby26 do
it 'registers an offense for yield_self with block' do
expect_offense(<<~RUBY)
obj.yield_self { |e| e.test }
^^^^^^^^^^ Prefer `then` over `yield_self`.
RUBY

expect_correction(<<~RUBY)
obj.then { |e| e.test }
RUBY
end
end

context 'Ruby 2.7', :ruby27 do
Expand Down

0 comments on commit d6345ac

Please sign in to comment.