From 68e27a90304a8ffda3b2e10cb57d8c22767f0a5d Mon Sep 17 00:00:00 2001 From: Daniel Vandersluis Date: Thu, 10 Sep 2020 13:28:16 -0400 Subject: [PATCH] [Fix #352] Do not register an offense for `Rails/HttpPositionalArguments` when given a splatted hash. --- CHANGELOG.md | 1 + docs/modules/ROOT/pages/cops_rails.adoc | 1 + lib/rubocop/cop/rails/http_positional_arguments.rb | 6 ++++++ .../cop/rails/http_positional_arguments_spec.rb | 11 +++++++++++ 4 files changed, 19 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ea105ada4..ee41ba5e9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/docs/modules/ROOT/pages/cops_rails.adoc b/docs/modules/ROOT/pages/cops_rails.adoc index ef45ef39ff..276b07f550 100644 --- a/docs/modules/ROOT/pages/cops_rails.adoc +++ b/docs/modules/ROOT/pages/cops_rails.adoc @@ -1550,6 +1550,7 @@ get :new, { user_id: 1} # good get :new, params: { user_id: 1 } +get :new, **options ---- === Configurable attributes diff --git a/lib/rubocop/cop/rails/http_positional_arguments.rb b/lib/rubocop/cop/rails/http_positional_arguments.rb index 07330ed440..03ab9c4e26 100644 --- a/lib/rubocop/cop/rails/http_positional_arguments.rb +++ b/lib/rubocop/cop/rails/http_positional_arguments.rb @@ -16,6 +16,7 @@ module Rails # # # good # get :new, params: { user_id: 1 } + # get :new, **options class HttpPositionalArguments < Cop extend TargetRailsVersion @@ -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) @@ -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) || diff --git a/spec/rubocop/cop/rails/http_positional_arguments_spec.rb b/spec/rubocop/cop/rails/http_positional_arguments_spec.rb index e9383d5a30..7644bbfd27 100644 --- a/spec/rubocop/cop/rails/http_positional_arguments_spec.rb +++ b/spec/rubocop/cop/rails/http_positional_arguments_spec.rb @@ -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