From d47284e4a45459e060881a46339861f4225dea62 Mon Sep 17 00:00:00 2001 From: viralpraxis Date: Fri, 17 Jan 2025 16:48:01 +0300 Subject: [PATCH] Fix `Rails/HttpPositionalArguments` cop false positives with arguments forwarding --- ...se_positivies_with_arguments_forwarding.md | 1 + .../cop/rails/http_positional_arguments.rb | 7 ++++++ .../rails/http_positional_arguments_spec.rb | 24 +++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 changelog/fix_rails_http_positional_arguments_cop_false_positivies_with_arguments_forwarding.md diff --git a/changelog/fix_rails_http_positional_arguments_cop_false_positivies_with_arguments_forwarding.md b/changelog/fix_rails_http_positional_arguments_cop_false_positivies_with_arguments_forwarding.md new file mode 100644 index 0000000000..9ab5c4f1af --- /dev/null +++ b/changelog/fix_rails_http_positional_arguments_cop_false_positivies_with_arguments_forwarding.md @@ -0,0 +1 @@ +* [#1414](https://github.com/rubocop/rubocop-rails/pull/1414): Fix `Rails/HttpPositionalArguments` cop false positives with arguments forwarding. ([@viralpraxis][]) diff --git a/lib/rubocop/cop/rails/http_positional_arguments.rb b/lib/rubocop/cop/rails/http_positional_arguments.rb index 221fca3487..bad0054eac 100644 --- a/lib/rubocop/cop/rails/http_positional_arguments.rb +++ b/lib/rubocop/cop/rails/http_positional_arguments.rb @@ -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 @@ -83,7 +87,9 @@ 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) @@ -91,6 +97,7 @@ def needs_conversion?(data) 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) diff --git a/spec/rubocop/cop/rails/http_positional_arguments_spec.rb b/spec/rubocop/cop/rails/http_positional_arguments_spec.rb index f41b0b4e40..ea61c487b0 100644 --- a/spec/rubocop/cop/rails/http_positional_arguments_spec.rb +++ b/spec/rubocop/cop/rails/http_positional_arguments_spec.rb @@ -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)