Skip to content

Commit

Permalink
[Fix #532] Fix a false positive for Rails/HttpPositionalArguments
Browse files Browse the repository at this point in the history
Fixes #532.

This PR fixes a false positive for `Rails/HttpPositionalArguments`
when defining `get` in `Rails.application.routes.draw` block.
  • Loading branch information
koic committed Sep 2, 2021
1 parent 7e62ce2 commit b72c896
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
### Bug fixes

* [#528](https://github.com/rubocop/rubocop-rails/issues/528): Fix a false positive for `Rails/HasManyOrHasOneDependent` when specifying `:dependent` strategy with double splat. ([@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

0 comments on commit b72c896

Please sign in to comment.