Skip to content

Commit

Permalink
[Fix rubocop#142] Minitest/GlobalExpectations: autocorrect when recei…
Browse files Browse the repository at this point in the history
…ver is lambda

This fixes an issue when autocorrecting offenses where the receiver is
a lambda function: `-> { n }`.
The expected correct result is `_ { n }` instead of `_ { -> { n } }`.

Fixes rubocop#142
  • Loading branch information
gi committed Oct 18, 2021
1 parent d0101f6 commit 3c8ff87
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#149](https://github.com/rubocop/rubocop-minitest/pull/149): Fix `Minitest/GlobalExpectations` autocorrect when receiver is lambda. ([@gi][])

## 0.15.2 (2021-10-11)

### Bug fixes
Expand Down Expand Up @@ -246,3 +250,4 @@
[@tsmmark]: https://github.com/tsmmark
[@cstyles]: https://github.com/cstyles
[@ghiculescu]: https://github.com/ghiculescu
[@gi]: https://github.com/gi
28 changes: 17 additions & 11 deletions lib/rubocop/cop/minitest/global_expectations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,36 @@ class GlobalExpectations < Base
)
PATTERN

def_node_matcher :block_lambda?, <<~PATTERN
(block (send nil? :lambda) (args) $_)
PATTERN

def on_send(node)
return unless value_global_expectation?(node) || block_global_expectation?(node)

message = format(MSG, preferred: preferred_receiver(node))

add_offense(node.receiver.source_range, message: message) do |corrector|
receiver = node.receiver.source_range

if BLOCK_MATCHERS.include?(node.method_name)
corrector.wrap(receiver, '_ { ', ' }')
else
corrector.wrap(receiver, '_(', ')')
end
add_offense(node.receiver, message: message) do |corrector|
receiver = node.receiver
replacement = preferred_receiver(node)
corrector.replace(receiver, replacement)
end
end

private

def preferred_method
:_
end

def preferred_receiver(node)
source = node.receiver.source
receiver = node.receiver

if BLOCK_MATCHERS.include?(node.method_name)
"_ { #{source} }"
body = block_lambda?(receiver) || receiver
"#{preferred_method} { #{body.source} }"
else
"_(#{source})"
"#{preferred_method}(#{receiver.source})"
end
end
end
Expand Down
15 changes: 15 additions & 0 deletions test/rubocop/cop/minitest/global_expectations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,21 @@ def test_works_with_chained_method_calls
RUBY
end

define_method(:"test_registers_offense_when_using_global_#{matcher}_and_block") do
assert_offense(<<~RUBY)
it 'does something' do
-> { n }.#{matcher} 42
^^^^^^^^ Use `_ { n }` instead.
end
RUBY

assert_correction(<<~RUBY)
it 'does something' do
_ { n }.#{matcher} 42
end
RUBY
end

define_method(:"test_no_offense_when_using_expect_form_of_#{matcher}") do
assert_no_offenses(<<~RUBY)
it 'does something' do
Expand Down

0 comments on commit 3c8ff87

Please sign in to comment.