Skip to content

Commit 732a0db

Browse files
authored
Merge pull request rubocop#533 from koic/fix_false_positive_for_rails_http_posisional_arguments
[Fix rubocop#532] Fix a false positive for `Rails/HttpPositionalArguments`
2 parents 1f40f4b + 4a81d3a commit 732a0db

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

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

1617
## Changes
1718

lib/rubocop/cop/rails/http_positional_arguments.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class HttpPositionalArguments < Base
2727
KEYWORD_ARGS = %i[
2828
method params session body flash xhr as headers env to
2929
].freeze
30+
ROUTING_METHODS = %i[draw routes].freeze
3031
RESTRICT_ON_SEND = %i[get post put patch delete head].freeze
3132

3233
minimum_target_rails_version 5.0
@@ -40,6 +41,8 @@ class HttpPositionalArguments < Base
4041
PATTERN
4142

4243
def on_send(node)
44+
return if in_routing_block?(node)
45+
4346
http_request?(node) do |data|
4447
return unless needs_conversion?(data)
4548

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

6467
private
6568

69+
def in_routing_block?(node)
70+
!!node.each_ancestor(:block).detect { |block| ROUTING_METHODS.include?(block.send_node.method_name) }
71+
end
72+
6673
def needs_conversion?(data)
6774
return true unless data.hash_type?
6875
return false if kwsplat_hash?(data)

spec/rubocop/cop/rails/http_positional_arguments_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,5 +415,21 @@
415415
RUBY
416416
end
417417
end
418+
419+
it 'does not register an offense when defining `get` in `routes` block' do
420+
expect_no_offenses(<<~RUBY)
421+
routes do
422+
get :list, on: :collection
423+
end
424+
RUBY
425+
end
426+
427+
it 'does not register an offense when defining `get` in `routes.draw` block' do
428+
expect_no_offenses(<<~RUBY)
429+
Rails.application.routes.draw do
430+
get :list, on: :collection
431+
end
432+
RUBY
433+
end
418434
end
419435
end

0 commit comments

Comments
 (0)