Skip to content

Commit 047dee3

Browse files
authored
Merge pull request #1414 from viralpraxis/fix-rails-http-positional-arguments-cop-false-positives-on-forwarded-args
Fix `Rails/HttpPositionalArguments` cop false positives with arguments forwarding
2 parents 1c4c37e + d47284e commit 047dee3

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1414](https://github.com/rubocop/rubocop-rails/pull/1414): Fix `Rails/HttpPositionalArguments` cop false positives with arguments forwarding. ([@viralpraxis][])

lib/rubocop/cop/rails/http_positional_arguments.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ class HttpPositionalArguments < Base
4040
(hash (kwsplat _))
4141
PATTERN
4242

43+
def_node_matcher :forwarded_kwrestarg?, <<~PATTERN
44+
(hash (forwarded-kwrestarg))
45+
PATTERN
46+
4347
def_node_matcher :include_rack_test_methods?, <<~PATTERN
4448
(send nil? :include
4549
(const
@@ -83,14 +87,17 @@ def use_rack_test_methods?
8387
end
8488
end
8589

90+
# rubocop:disable Metrics/CyclomaticComplexity
8691
def needs_conversion?(data)
92+
return false if data.forwarded_args_type? || forwarded_kwrestarg?(data)
8793
return true unless data.hash_type?
8894
return false if kwsplat_hash?(data)
8995

9096
data.each_pair.none? do |pair|
9197
special_keyword_arg?(pair.key) || (format_arg?(pair.key) && data.pairs.one?)
9298
end
9399
end
100+
# rubocop:enable Metrics/CyclomaticComplexity
94101

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

spec/rubocop/cop/rails/http_positional_arguments_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,30 @@
432432
RUBY
433433
end
434434

435+
it 'does not register an offese for arguments forwaring' do
436+
expect_no_offenses(<<~RUBY)
437+
def perform_request(...)
438+
get(:list, ...)
439+
end
440+
RUBY
441+
end
442+
443+
it 'does not register an offese for keyword arguments forwaring' do
444+
expect_no_offenses(<<~RUBY)
445+
def perform_request(**options)
446+
get(:list, **options)
447+
end
448+
RUBY
449+
end
450+
451+
it 'does not register an offese for anonymous keyword arguments forwaring', :ruby32 do
452+
expect_no_offenses(<<~RUBY)
453+
def perform_request(**)
454+
get(:list, **)
455+
end
456+
RUBY
457+
end
458+
435459
context 'when using `include Rack::Test::Methods`' do
436460
it 'does not register an offense for get method' do
437461
expect_no_offenses(<<~RUBY)

0 commit comments

Comments
 (0)