Skip to content

Commit

Permalink
[Fix rubocop#305] Fix crash in Rails/MatchRoute cop when via opti…
Browse files Browse the repository at this point in the history
…on is a variable

Closes rubocop#305
  • Loading branch information
tejasbubane committed Jul 24, 2020
1 parent 214410f commit 0311fe5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

* [#297](https://github.com/rubocop-hq/rubocop-rails/pull/297): Handle an upstream Ruby issue where the DidYouMean module is not available, which would break the `Rails/UnknownEnv` cop. ([@taylorthurlow][])
* [#300](https://github.com/rubocop-hq/rubocop-rails/issues/300): Fix `Rails/RenderInline` error on variable key in render options. ([@tejasbubane][])
* [#305](https://github.com/rubocop-hq/rubocop-rails/issues/305): Fix crash in `Rails/MatchRoute` cop when via option is a variable. ([@tejasbubane][])

### Changes

Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/rails/match_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ def extract_via(node)
[via.value]
elsif via.array_type?
via.values.map(&:value)
else
[]
end
end

Expand Down
23 changes: 23 additions & 0 deletions spec/rubocop/cop/rails/match_route_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@
RUBY
end

it 'registers an offense when using match with string interpolation' do
expect_offense(<<~'RUBY')
routes.draw do
match "#{resource}/:action/:id", via: [:put]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `put` instead of `match` to define a route.
end
RUBY

expect_correction(<<~'RUBY')
routes.draw do
put "#{resource}/:action/:id"
end
RUBY
end

it 'does not register an offense when not within routes' do
expect_no_offenses(<<~RUBY)
match 'photos/:id', to: 'photos#show', via: :get
Expand Down Expand Up @@ -92,4 +107,12 @@
end
RUBY
end

it 'does not register an offense when via is a variable' do
expect_no_offenses(<<~'RUBY')
routes.draw do
match ':controller/:action/:id', via: method
end
RUBY
end
end

0 comments on commit 0311fe5

Please sign in to comment.