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 #532] Fix a false positive for Rails/HttpPositionalArguments #533

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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

* [#528](https://github.com/rubocop/rubocop-rails/issues/528): Fix a false positive for `Rails/HasManyOrHasOneDependent` when specifying `:dependent` strategy with double splat. ([@koic][])
* [#529](https://github.com/rubocop/rubocop-rails/issues/529): Fix a false positive for `Rails/LexicallyScopedActionFilter` when action method is aliased by `alias_method`. ([@koic][])
* [#532](https://github.com/rubocop/rubocop-rails/issues/532): Fix a false positive for `Rails/HttpPositionalArguments` when defining `get` in `Rails.application.routes.draw` block. ([@koic][])

## Changes

Expand Down
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 @@ -27,6 +27,7 @@ class HttpPositionalArguments < Base
KEYWORD_ARGS = %i[
method params session body flash xhr as headers env to
].freeze
ROUTING_METHODS = %i[draw routes].freeze
RESTRICT_ON_SEND = %i[get post put patch delete head].freeze

minimum_target_rails_version 5.0
Expand All @@ -40,6 +41,8 @@ class HttpPositionalArguments < Base
PATTERN

def on_send(node)
return if in_routing_block?(node)

http_request?(node) do |data|
return unless needs_conversion?(data)

Expand All @@ -63,6 +66,10 @@ def on_send(node)

private

def in_routing_block?(node)
!!node.each_ancestor(:block).detect { |block| ROUTING_METHODS.include?(block.send_node.method_name) }
end

def needs_conversion?(data)
return true unless data.hash_type?
return false if kwsplat_hash?(data)
Expand Down
16 changes: 16 additions & 0 deletions spec/rubocop/cop/rails/http_positional_arguments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -415,5 +415,21 @@
RUBY
end
end

it 'does not register an offense when defining `get` in `routes` block' do
expect_no_offenses(<<~RUBY)
routes do
get :list, on: :collection
end
RUBY
end

it 'does not register an offense when defining `get` in `routes.draw` block' do
expect_no_offenses(<<~RUBY)
Rails.application.routes.draw do
get :list, on: :collection
end
RUBY
end
end
end