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

[Fix #12693] Fix an incorrect autocorrect for Style/ObjectThen #12696

Merged
merged 1 commit into from
Feb 16, 2024
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/fix_an_incorrect_for_style_object_then.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12693](https://github.com/rubocop/rubocop/issues/12693): Fix an incorrect autocorrect for `Style/ObjectThen` when using `yield_self` without receiver. ([@koic][])
8 changes: 5 additions & 3 deletions lib/rubocop/cop/style/object_then.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,17 @@ def on_send(node)
private

def check_method_node(node)
return unless preferred_method(node)
return unless preferred_method?(node)

message = message(node)
add_offense(node.loc.selector, message: message) do |corrector|
corrector.replace(node.loc.selector, style.to_s)
prefer = style == :then && node.receiver.nil? ? 'self.then' : style

corrector.replace(node.loc.selector, prefer)
end
end

def preferred_method(node)
def preferred_method?(node)
case style
when :then
node.method?(:yield_self)
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/style/object_then_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@
obj.then { _1.test }
RUBY
end

it 'registers an offense for `yield_self` without receiver' do
expect_offense(<<~RUBY)
yield_self { |obj| obj.test }
^^^^^^^^^^ Prefer `then` over `yield_self`.
RUBY

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

it 'registers an offense for yield_self with proc param' do
Expand Down