Skip to content

Commit

Permalink
Merge pull request #533 from koic/fix_false_positive_for_rails_http_p…
Browse files Browse the repository at this point in the history
…osisional_arguments

[Fix #532] Fix a false positive for `Rails/HttpPositionalArguments`
  • Loading branch information
koic authored Sep 4, 2021
2 parents 1f40f4b + 4a81d3a commit 732a0db
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 @@ -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

0 comments on commit 732a0db

Please sign in to comment.