diff --git a/changelog/new_add_new_allowed_key_to.md b/changelog/new_add_new_allowed_key_to.md new file mode 100644 index 0000000000..52b8376acd --- /dev/null +++ b/changelog/new_add_new_allowed_key_to.md @@ -0,0 +1 @@ +* [#1491](https://github.com/rubocop/rubocop-rails/pull/1491): Add New AllowedKeys to `Rails/HttpPositionalArguments`. ([@steiley][]) diff --git a/config/default.yml b/config/default.yml index 26a00ce501..743211e8f2 100644 --- a/config/default.yml +++ b/config/default.yml @@ -581,6 +581,7 @@ Rails/HttpPositionalArguments: Description: 'Use keyword arguments instead of positional arguments in http method calls.' Enabled: true VersionAdded: '0.44' + AllowedKeys: [] Include: - 'spec/**/*' - 'test/**/*' diff --git a/docs/modules/ROOT/pages/cops_rails.adoc b/docs/modules/ROOT/pages/cops_rails.adoc index 21de7cc8c1..58ba0ae722 100644 --- a/docs/modules/ROOT/pages/cops_rails.adoc +++ b/docs/modules/ROOT/pages/cops_rails.adoc @@ -2931,6 +2931,15 @@ get :new, params: { user_id: 1 } get :new, **options ---- +[#allowedkeys_-__foo__-railshttppositionalarguments] +==== AllowedMethods: ['foo'] + +[source,ruby] +---- +# good +get :new, foo: 'bar' +---- + [#configurable-attributes-railshttppositionalarguments] === Configurable attributes @@ -2940,6 +2949,10 @@ get :new, **options | Include | `+spec/**/*+`, `+test/**/*+` | Array + +|AllowedKeys +|`[]` +|Array |=== [#railshttpstatus] diff --git a/lib/rubocop/cop/rails/http_positional_arguments.rb b/lib/rubocop/cop/rails/http_positional_arguments.rb index bad0054eac..ae0d324bc2 100644 --- a/lib/rubocop/cop/rails/http_positional_arguments.rb +++ b/lib/rubocop/cop/rails/http_positional_arguments.rb @@ -20,6 +20,11 @@ module Rails # # good # get :new, params: { user_id: 1 } # get :new, **options + # + # @example AllowedKeys: ['foo'] + # # good + # get :new, foo: 'bar' + # class HttpPositionalArguments < Base include RangeHelp extend AutoCorrector @@ -94,13 +99,22 @@ def needs_conversion?(data) return false if kwsplat_hash?(data) data.each_pair.none? do |pair| - special_keyword_arg?(pair.key) || (format_arg?(pair.key) && data.pairs.one?) + not_applicable_arg?(pair.key) || + (format_arg?(pair.key) && data.pairs.one?) end end # rubocop:enable Metrics/CyclomaticComplexity + def not_applicable_arg?(node) + node.sym_type? && (special_keyword_arg?(node) || allowed_arg?(node)) + end + def special_keyword_arg?(node) - node.sym_type? && KEYWORD_ARGS.include?(node.value) + KEYWORD_ARGS.include?(node.value) + end + + def allowed_arg?(node) + cop_config['AllowedKeys'].include?(node.value.to_s) end def format_arg?(node) diff --git a/spec/rubocop/cop/rails/http_positional_arguments_spec.rb b/spec/rubocop/cop/rails/http_positional_arguments_spec.rb index ea61c487b0..1f39e55cc7 100644 --- a/spec/rubocop/cop/rails/http_positional_arguments_spec.rb +++ b/spec/rubocop/cop/rails/http_positional_arguments_spec.rb @@ -200,6 +200,14 @@ end end + describe 'when using AllowedKeys option value' do + let(:cop_config) { { 'AllowedKeys' => %w[user_id foo] } } + + it 'does not register an offense' do + expect_no_offenses("get :new, user_id: @user.id, foo: 'bar'") + end + end + describe '.get' do it 'registers an offense' do expect_offense(<<~RUBY)