-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
Style/RedundantDoubleSplatBrackets
cop
This cop checks for redundant uses of double splat hash braces. ```ruby # bad do_something(**{foo: bar, baz: qux}) # good do_something(foo: bar, baz: qux) ``` Both are keyword arguments after all.
- Loading branch information
Showing
5 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#11305](https://github.com/rubocop/rubocop/pull/11305): Add new `Style/RedundantDoubleSplatHashBraces` cop. ([@koic][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# Checks for redundant uses of double splat hash braces. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# do_something(**{foo: bar, baz: qux}) | ||
# | ||
# # good | ||
# do_something(foo: bar, baz: qux) | ||
# | ||
class RedundantDoubleSplatHashBraces < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Remove the redundant double splat and braces, use keyword arguments directly.' | ||
|
||
# @!method double_splat_hash_braces?(node) | ||
def_node_matcher :double_splat_hash_braces?, <<~PATTERN | ||
(hash (kwsplat (hash ...))) | ||
PATTERN | ||
|
||
def on_hash(node) | ||
return if node.pairs.empty? || node.pairs.any?(&:hash_rocket?) | ||
|
||
grandparent = node.parent&.parent | ||
return unless double_splat_hash_braces?(grandparent) | ||
|
||
add_offense(grandparent) do |corrector| | ||
corrector.replace(grandparent, node.pairs.map(&:source).join(', ')) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
56 changes: 56 additions & 0 deletions
56
spec/rubocop/cop/style/redundant_double_splat_hash_braces_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Style::RedundantDoubleSplatHashBraces, :config do | ||
it 'registers an offense when using double splat hash braces' do | ||
expect_offense(<<~RUBY) | ||
do_something(**{foo: bar, baz: qux}) | ||
^^^^^^^^^^^^^^^^^^^^^^ Remove the redundant double splat and braces, use keyword arguments directly. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
do_something(foo: bar, baz: qux) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using keyword arguments' do | ||
expect_no_offenses(<<~RUBY) | ||
do_something(foo: bar, baz: qux) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using empty double splat hash braces arguments' do | ||
expect_no_offenses(<<~RUBY) | ||
do_something(**{}) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using hash rocket double splat hash braces arguments' do | ||
expect_no_offenses(<<~RUBY) | ||
do_something(**{foo => bar}) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using method call for double splat hash braces arguments' do | ||
expect_no_offenses(<<~RUBY) | ||
do_something(**{foo: bar}.merge(options)) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using hash braces arguments' do | ||
expect_no_offenses(<<~RUBY) | ||
do_something({foo: bar, baz: qux}) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using double splat variable' do | ||
expect_no_offenses(<<~RUBY) | ||
do_something(**h) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using hash literal' do | ||
expect_no_offenses(<<~RUBY) | ||
{ a: a } | ||
RUBY | ||
end | ||
end |