Skip to content

Commit

Permalink
Fix NameError when execute Rails/HttpStatus cop without Rack
Browse files Browse the repository at this point in the history
Rails/HttpStatus cop with symbolic style raise `NameError (uninitialized constant Rack)`.

```ruby
class FooController < ApplicationController
  def bar
    render :foo, status: 418
  end
end
```

```
$ rubocop --only Rails/HttpStatus app/controllers/foo_controller.rb -d
For /Users/onaka/sample_app: configuration from /Users/onaka/.gem/ruby/2.6.0/gems/rubocop-0.54.0/config/default.yml
Inheriting configuration from /Users/onaka/.gem/ruby/2.6.0/gems/rubocop-0.54.0/config/enabled.yml
Inheriting configuration from /Users/onaka/.gem/ruby/2.6.0/gems/rubocop-0.54.0/config/disabled.yml
Inspecting 1 file
Scanning /Users/onaka/sample_app/app/controllers/foo_controller.rb
An error occurred while Rails/HttpStatus cop was inspecting /Users/onaka/sample_app/app/controllers/foo_controller.rb:3:4.
uninitialized constant Rack
Did you mean?  Racc
/Users/onaka/.gem/ruby/2.6.0/gems/rubocop-0.54.0/lib/rubocop/cop/rails/http_status.rb:131:in `custom_http_status_code?'
/Users/onaka/.gem/ruby/2.6.0/gems/rubocop-0.54.0/lib/rubocop/cop/rails/http_status.rb:104:in `offensive?'
...
```
  • Loading branch information
onk committed Mar 21, 2018
1 parent 3323d94 commit 84383d0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#5707](https://github.com/bbatsov/rubocop/pull/5707): Fix NameError when execute `Rails/HttpStatus` cop without Rack. ([@onk][])

## 0.54.0 (2018-03-21)

### New features
Expand Down
9 changes: 8 additions & 1 deletion lib/rubocop/cop/rails/http_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ class SymbolicStyleChecker
MSG = 'Prefer `%<prefer>s` over `%<current>s` ' \
'to define HTTP status code.'.freeze
DEFAULT_MSG = 'Prefer `symbolic` over `numeric` ' \
'to define HTTP status code.'.freeze
'to define HTTP status code. ' \
'If the status code can not be written as symbol, ' \
'this warning can be silenced ' \
'by running rubocop with rack gem.'.freeze

attr_reader :node
def initialize(node)
Expand Down Expand Up @@ -127,6 +130,10 @@ def number
end

def custom_http_status_code?
# Can not check if it is custom status code without Rack.
# So all int value are offensed (this make false positive)
return false unless RACK_LOADED

node.int_type? &&
!::Rack::Utils::SYMBOL_TO_STATUS_CODE.value?(number)
end
Expand Down
8 changes: 4 additions & 4 deletions spec/rubocop/cop/rails/http_status_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@
it 'registers an offense when using numeric value' do
expect_offense(<<-RUBY)
render :foo, status: 200
^^^ Prefer `symbolic` over `numeric` to define HTTP status code.
^^^ Prefer `symbolic` over `numeric` to define HTTP status code. If the status code can not be written as symbol, this warning can be silenced by running rubocop with rack gem.
render json: { foo: 'bar' }, status: 404
^^^ Prefer `symbolic` over `numeric` to define HTTP status code.
^^^ Prefer `symbolic` over `numeric` to define HTTP status code. If the status code can not be written as symbol, this warning can be silenced by running rubocop with rack gem.
render plain: 'foo/bar', status: 304
^^^ Prefer `symbolic` over `numeric` to define HTTP status code.
^^^ Prefer `symbolic` over `numeric` to define HTTP status code. If the status code can not be written as symbol, this warning can be silenced by running rubocop with rack gem.
redirect_to root_url, status: 301
^^^ Prefer `symbolic` over `numeric` to define HTTP status code.
^^^ Prefer `symbolic` over `numeric` to define HTTP status code. If the status code can not be written as symbol, this warning can be silenced by running rubocop with rack gem.
RUBY
end
end
Expand Down

0 comments on commit 84383d0

Please sign in to comment.