Skip to content

Commit 12c642f

Browse files
pr4-botsteiley
authored andcommitted
Add New AllowedKeys to Rails/HttpPositionalArguments
1 parent 8f9cf2b commit 12c642f

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1491](https://github.com/rubocop/rubocop-rails/pull/1491): Add New AllowedKeys to Rails/HttpPositionalArguments. ([@steiley][])

config/default.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@ Rails/HttpPositionalArguments:
581581
Description: 'Use keyword arguments instead of positional arguments in http method calls.'
582582
Enabled: true
583583
VersionAdded: '0.44'
584+
AllowedKeys: []
584585
Include:
585586
- 'spec/**/*'
586587
- 'test/**/*'

docs/modules/ROOT/pages/cops_rails.adoc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2931,6 +2931,15 @@ get :new, params: { user_id: 1 }
29312931
get :new, **options
29322932
----
29332933
2934+
[#allowedkeys_-__foo__-railshttppositionalarguments]
2935+
==== AllowedMethods: ['foo']
2936+
2937+
[source,ruby]
2938+
----
2939+
# good
2940+
get :new, foo: 'bar'
2941+
----
2942+
29342943
[#configurable-attributes-railshttppositionalarguments]
29352944
=== Configurable attributes
29362945
@@ -2940,6 +2949,10 @@ get :new, **options
29402949
| Include
29412950
| `+spec/**/*+`, `+test/**/*+`
29422951
| Array
2952+
2953+
|AllowedKeys
2954+
|`[]`
2955+
|Array
29432956
|===
29442957
29452958
[#railshttpstatus]

lib/rubocop/cop/rails/http_positional_arguments.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ module Rails
2020
# # good
2121
# get :new, params: { user_id: 1 }
2222
# get :new, **options
23+
#
24+
# @example AllowedKeys: ['foo']
25+
# # good
26+
# get :new, foo: 'bar'
27+
#
2328
class HttpPositionalArguments < Base
2429
include RangeHelp
2530
extend AutoCorrector
@@ -94,7 +99,8 @@ def needs_conversion?(data)
9499
return false if kwsplat_hash?(data)
95100

96101
data.each_pair.none? do |pair|
97-
special_keyword_arg?(pair.key) || (format_arg?(pair.key) && data.pairs.one?)
102+
special_keyword_arg?(pair.key) || allowed_arg?(pair.key) ||
103+
(format_arg?(pair.key) && data.pairs.one?)
98104
end
99105
end
100106
# rubocop:enable Metrics/CyclomaticComplexity
@@ -103,6 +109,10 @@ def special_keyword_arg?(node)
103109
node.sym_type? && KEYWORD_ARGS.include?(node.value)
104110
end
105111

112+
def allowed_arg?(node)
113+
node.sym_type? && cop_config['AllowedKeys'].include?(node.value.to_s)
114+
end
115+
106116
def format_arg?(node)
107117
node.sym_type? && node.value == :format
108118
end

spec/rubocop/cop/rails/http_positional_arguments_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@
200200
end
201201
end
202202

203+
describe 'when using AllowedKeys option value' do
204+
let(:cop_config) { { 'AllowedKeys' => ['user_id', 'foo'] } }
205+
206+
it 'does not register an offense' do
207+
expect_no_offenses("get :new, user_id: @user.id, foo: 'bar'")
208+
end
209+
end
210+
203211
describe '.get' do
204212
it 'registers an offense' do
205213
expect_offense(<<~RUBY)

0 commit comments

Comments
 (0)