Skip to content

Commit

Permalink
Merge pull request #1856 from rubocop/expect-actual-autocorrect-with-…
Browse files Browse the repository at this point in the history
…hash

Fix autocorrect error for RSpec/ExpectActual
  • Loading branch information
ydah authored Mar 31, 2024
2 parents 954b29e + dde398e commit 6add066
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Master (Unreleased)

- Fix an autocorrect error for `RSpec/ExpectActual`. ([@bquorning])

## 2.28.0 (2024-03-30)

- Extract RSpec Rails cops to a separate repository, [`rubocop-rspec_rails`](/~https://github.com/rubocop/rubocop-rspec_rails). The `rubocop-rspec_rails` repository is a dependency of `rubocop-rspec` and the cops related to rspec-rails are aliased (`RSpec/Rails/Foo` == `RSpecRails/Foo`) until v3.0 is released, so the change will be invisible to users until then. ([@ydah])
Expand Down
18 changes: 9 additions & 9 deletions lib/rubocop/cop/rspec/expect_actual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,27 @@ class ExpectActual < Base
(send
(send nil? :expect $#literal?)
#Runners.all
{
${
(send (send nil? $:be) :== $_)
(send nil? $_ $_ ...)
}
)
PATTERN

def on_send(node)
expect_literal(node) do |actual, matcher, expected|
def on_send(node) # rubocop:disable Metrics/MethodLength
expect_literal(node) do |actual, send_node, matcher, expected|
next if SKIPPED_MATCHERS.include?(matcher)

add_offense(actual.source_range) do |corrector|
next unless CORRECTABLE_MATCHERS.include?(matcher)
next if literal?(expected)

swap(corrector, actual, expected)
corrector.replace(actual, expected.source)
if matcher == :be
corrector.replace(expected, actual.source)
else
corrector.replace(send_node, "#{matcher}(#{actual.source})")
end
end
end
end
Expand All @@ -94,11 +99,6 @@ def complex_literal?(node)
COMPLEX_LITERALS.include?(node.type) &&
node.each_child_node.all?(&method(:literal?))
end

def swap(corrector, actual, expected)
corrector.replace(actual, expected.source)
corrector.replace(expected, actual.source)
end
end
end
end
Expand Down
22 changes: 22 additions & 0 deletions spec/rubocop/cop/rspec/expect_actual_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,28 @@
RUBY
end

it 'autocorrects literal hash when expected is without parentheses' do
expect_offense(<<~RUBY)
describe Foo do
it 'uses expect incorrectly' do
expect(foo: 1, bar: 2).to eq bar
^^^^^^^^^^^^^^ Provide the actual value you are testing to `expect(...)`.
expect({ foo: 1, bar: 2 }).to eq bar
^^^^^^^^^^^^^^^^^^ Provide the actual value you are testing to `expect(...)`.
end
end
RUBY

expect_correction(<<~RUBY)
describe Foo do
it 'uses expect incorrectly' do
expect(bar).to eq(foo: 1, bar: 2)
expect(bar).to eq({ foo: 1, bar: 2 })
end
end
RUBY
end

it 'flags ranges containing only literal values within expect(...)' do
expect_offense(<<~RUBY)
describe Foo do
Expand Down

0 comments on commit 6add066

Please sign in to comment.