Skip to content

Commit

Permalink
[Fix rubocop#352] Do not register an offense for `Rails/HttpPositiona…
Browse files Browse the repository at this point in the history
…lArguments` when given a splatted hash.
  • Loading branch information
dvandersluis committed Sep 11, 2020
1 parent 119bbaa commit 73a2797
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* [#349](https://github.com/rubocop-hq/rubocop-rails/pull/349): Fix errors of `Rails/UniqueValidationWithoutIndex`. ([@Tietew][])
* [#338](https://github.com/rubocop-hq/rubocop-rails/issues/338): Fix a false positive for `Rails/IndexBy` and `Rails/IndexWith` when the `each_with_object` hash is used in the transformed key or value. ([@eugeneius][])
* [#351](https://github.com/rubocop-hq/rubocop-rails/pull/351): Add `<>` operator to `Rails/WhereNot` cop. ([@Tietew][])
* [#352](https://github.com/rubocop-hq/rubocop-rails/pull/352): Do not register offense if given a splatted hash. ([@dvandersluis][])

## 2.8.0 (2020-09-04)

Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/cops_rails.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,7 @@ get :new, { user_id: 1}
# good
get :new, params: { user_id: 1 }
get :new, **args
----

=== Configurable attributes
Expand Down
6 changes: 6 additions & 0 deletions lib/rubocop/cop/rails/http_positional_arguments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module Rails
#
# # good
# get :new, params: { user_id: 1 }
# get :new, **args
class HttpPositionalArguments < Cop
extend TargetRailsVersion

Expand All @@ -32,6 +33,10 @@ class HttpPositionalArguments < Cop
(send nil? {#{HTTP_METHODS.map(&:inspect).join(' ')}} !nil? $_ ...)
PATTERN

def_node_matcher :kwsplat_hash?, <<~PATTERN
(hash (kwsplat _))
PATTERN

def on_send(node)
http_request?(node) do |data|
return unless needs_conversion?(data)
Expand Down Expand Up @@ -61,6 +66,7 @@ def autocorrect(node)

def needs_conversion?(data)
return true unless data.hash_type?
return false if kwsplat_hash?(data)

data.each_pair.none? do |pair|
special_keyword_arg?(pair.key) ||
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/rails/http_positional_arguments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -411,5 +411,16 @@
post user_attrs, params: params.merge(foo: bar)
RUBY
end

context 'when given a kwsplat hash' do
# See https://github.com/rubocop-hq/rubocop-rails/issues/352
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
[{ format: :json }, { format: :html }].each do |args|
get :nothing, **args
end
RUBY
end
end
end
end

0 comments on commit 73a2797

Please sign in to comment.