Skip to content

Commit

Permalink
Fix an incorrect autocorrect for Style/RedundantDoubleSplatHashBraces
Browse files Browse the repository at this point in the history
This PR fixes the following incorrect autocorrect for `Style/RedundantDoubleSplatHashBraces`
when using double splat hash braces with `merge` method call twice.

```console
$ bundle exec rspec  spec/rubocop/cop/style/redundant_double_splat_hash_braces_spec.rb:82
Run options: include {:locations=>{"./spec/rubocop/cop/style/redundant_double_splat_hash_braces_spec.rb"=>[82]}}
(snip)

Failures:

  1) RuboCop::Cop::Style::RedundantDoubleSplatHashBraces registers an offense
     when using double splat hash braces with `merge` method call twice
     Failure/Error: expect(new_source).to eq(correction)

       expected: "do_something(foo: bar, **options)\ndo_something(baz: qux, **options)\n"
            got: "do_something(foo: bar, **options)\ndo_something(baz: qux.merge(options))\n"

       (compared using ==)

       Diff:
       @@ -1,3 +1,3 @@
        do_something(foo: bar, **options)
       -do_something(baz: qux, **options)
       +do_something(baz: qux.merge(options))

     # ./lib/rubocop/rspec/expect_offense.rb:164:in `expect_correction'
     # ./spec/rubocop/cop/style/redundant_double_splat_hash_braces_spec.rb:90:in `block (2 levels) in <top (required)>'

Finished in 0.15475 seconds (files took 1.24 seconds to load)
1 example, 1 failure
```
  • Loading branch information
koic committed Oct 11, 2023
1 parent 215ab03 commit 99e6f85
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12262](https://github.com/rubocop/rubocop/pull/12262): Fix an incorrect autocorrect for `Style/RedundantDoubleSplatHashBraces` when using double splat hash braces with `merge` method call twice. ([@koic][])
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def range_of_merge_methods(merge_methods)
end

def extract_send_methods(kwsplat)
@extract_send_methods ||= kwsplat.each_descendant(:send, :csend)
kwsplat.each_descendant(:send, :csend)
end

def convert_to_new_arguments(node)
Expand Down
14 changes: 14 additions & 0 deletions spec/rubocop/cop/style/redundant_double_splat_hash_braces_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@
RUBY
end

it 'registers an offense when using double splat hash braces with `merge` method call twice' do
expect_offense(<<~RUBY)
do_something(**{ foo: bar }.merge(options))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Remove the redundant double splat and braces, use keyword arguments directly.
do_something(**{ baz: qux }.merge(options))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Remove the redundant double splat and braces, use keyword arguments directly.
RUBY

expect_correction(<<~RUBY)
do_something(foo: bar, **options)
do_something(baz: qux, **options)
RUBY
end

it 'registers an offense when using double splat hash braces with `merge` multiple arguments method call' do
expect_offense(<<~RUBY)
do_something(**{foo: bar, baz: qux}.merge(options1, options2))
Expand Down

0 comments on commit 99e6f85

Please sign in to comment.