Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/new_add_new_allowed_key_to.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1491](https://github.com/rubocop/rubocop-rails/pull/1491): Add New AllowedKeys to `Rails/HttpPositionalArguments`. ([@steiley][])
1 change: 1 addition & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@ Rails/HttpPositionalArguments:
Description: 'Use keyword arguments instead of positional arguments in http method calls.'
Enabled: true
VersionAdded: '0.44'
AllowedKeys: []
Include:
- 'spec/**/*'
- 'test/**/*'
Expand Down
13 changes: 13 additions & 0 deletions docs/modules/ROOT/pages/cops_rails.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2931,6 +2931,15 @@ get :new, params: { user_id: 1 }
get :new, **options
----

[#allowedkeys_-__foo__-railshttppositionalarguments]
==== AllowedMethods: ['foo']

[source,ruby]
----
# good
get :new, foo: 'bar'
----

[#configurable-attributes-railshttppositionalarguments]
=== Configurable attributes

Expand All @@ -2940,6 +2949,10 @@ get :new, **options
| Include
| `+spec/**/*+`, `+test/**/*+`
| Array

|AllowedKeys
|`[]`
|Array
|===

[#railshttpstatus]
Expand Down
18 changes: 16 additions & 2 deletions lib/rubocop/cop/rails/http_positional_arguments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ module Rails
# # good
# get :new, params: { user_id: 1 }
# get :new, **options
#
# @example AllowedKeys: ['foo']
# # good
# get :new, foo: 'bar'
#
class HttpPositionalArguments < Base
include RangeHelp
extend AutoCorrector
Expand Down Expand Up @@ -94,13 +99,22 @@ def needs_conversion?(data)
return false if kwsplat_hash?(data)

data.each_pair.none? do |pair|
special_keyword_arg?(pair.key) || (format_arg?(pair.key) && data.pairs.one?)
not_applicable_arg?(pair.key) ||
(format_arg?(pair.key) && data.pairs.one?)
end
end
# rubocop:enable Metrics/CyclomaticComplexity

def not_applicable_arg?(node)
node.sym_type? && (special_keyword_arg?(node) || allowed_arg?(node))
end

def special_keyword_arg?(node)
node.sym_type? && KEYWORD_ARGS.include?(node.value)
KEYWORD_ARGS.include?(node.value)
end

def allowed_arg?(node)
cop_config['AllowedKeys'].include?(node.value.to_s)
end

def format_arg?(node)
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/rails/http_positional_arguments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,14 @@
end
end

describe 'when using AllowedKeys option value' do
let(:cop_config) { { 'AllowedKeys' => %w[user_id foo] } }

it 'does not register an offense' do
expect_no_offenses("get :new, user_id: @user.id, foo: 'bar'")
end
end

describe '.get' do
it 'registers an offense' do
expect_offense(<<~RUBY)
Expand Down