Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Rails/HttpPositionalArguments cop false positives with arguments forwarding #1414

Merged
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
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
Loading