Skip to content

Commit

Permalink
Merge pull request #1414 from viralpraxis/fix-rails-http-positional-a…
Browse files Browse the repository at this point in the history
…rguments-cop-false-positives-on-forwarded-args

Fix `Rails/HttpPositionalArguments` cop false positives with arguments forwarding
  • Loading branch information
koic authored Jan 18, 2025
2 parents 1c4c37e + d47284e commit 047dee3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1414](https://github.com/rubocop/rubocop-rails/pull/1414): Fix `Rails/HttpPositionalArguments` cop false positives with arguments forwarding. ([@viralpraxis][])
7 changes: 7 additions & 0 deletions lib/rubocop/cop/rails/http_positional_arguments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class HttpPositionalArguments < Base
(hash (kwsplat _))
PATTERN

def_node_matcher :forwarded_kwrestarg?, <<~PATTERN
(hash (forwarded-kwrestarg))
PATTERN

def_node_matcher :include_rack_test_methods?, <<~PATTERN
(send nil? :include
(const
Expand Down Expand Up @@ -83,14 +87,17 @@ def use_rack_test_methods?
end
end

# rubocop:disable Metrics/CyclomaticComplexity
def needs_conversion?(data)
return false if data.forwarded_args_type? || forwarded_kwrestarg?(data)
return true unless data.hash_type?
return false if kwsplat_hash?(data)

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

def special_keyword_arg?(node)
node.sym_type? && KEYWORD_ARGS.include?(node.value)
Expand Down
24 changes: 24 additions & 0 deletions spec/rubocop/cop/rails/http_positional_arguments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,30 @@
RUBY
end

it 'does not register an offese for arguments forwaring' do
expect_no_offenses(<<~RUBY)
def perform_request(...)
get(:list, ...)
end
RUBY
end

it 'does not register an offese for keyword arguments forwaring' do
expect_no_offenses(<<~RUBY)
def perform_request(**options)
get(:list, **options)
end
RUBY
end

it 'does not register an offese for anonymous keyword arguments forwaring', :ruby32 do
expect_no_offenses(<<~RUBY)
def perform_request(**)
get(:list, **)
end
RUBY
end

context 'when using `include Rack::Test::Methods`' do
it 'does not register an offense for get method' do
expect_no_offenses(<<~RUBY)
Expand Down

0 comments on commit 047dee3

Please sign in to comment.