lib/rubocop/cop/rspec/rails/http_status.rb in rubocop-rspec-1.23.0 vs lib/rubocop/cop/rspec/rails/http_status.rb in rubocop-rspec-1.24.0
- old
+ new
@@ -1,7 +1,9 @@
# frozen_string_literal: true
+require 'rack/utils'
+
module RuboCop
module Cop
module RSpec
module Rails
# Enforces use of symbolic or numeric value to describe HTTP status.
@@ -27,17 +29,10 @@
# it { is_expected.to have_http_status 404 }
# it { is_expected.to have_http_status :success }
# it { is_expected.to have_http_status :error }
#
class HttpStatus < Cop
- begin
- require 'rack/utils'
- RACK_LOADED = true
- rescue LoadError
- RACK_LOADED = false
- end
-
include ConfigurableEnforcedStyle
def_node_matcher :http_status, <<-PATTERN
(send nil? :have_http_status ${int sym})
PATTERN
@@ -48,14 +43,10 @@
return unless checker.offensive?
add_offense(checker.node, message: checker.message)
end
end
- def support_autocorrect?
- RACK_LOADED
- end
-
def autocorrect(node)
lambda do |corrector|
checker = checker_class.new(node)
corrector.replace(node.loc.expression, checker.preferred_style)
end
@@ -74,28 +65,22 @@
# :nodoc:
class SymbolicStyleChecker
MSG = 'Prefer `%<prefer>s` over `%<current>s` ' \
'to describe HTTP status code.'.freeze
- DEFAULT_MSG = 'Prefer `symbolic` over `numeric` ' \
- 'to describe HTTP status code.'.freeze
attr_reader :node
def initialize(node)
@node = node
end
def offensive?
- !node.sym_type?
+ !node.sym_type? && !custom_http_status_code?
end
def message
- if RACK_LOADED
- format(MSG, prefer: preferred_style, current: number.to_s)
- else
- DEFAULT_MSG
- end
+ format(MSG, prefer: preferred_style, current: number.to_s)
end
def preferred_style
symbol.inspect
end
@@ -107,18 +92,21 @@
end
def number
node.source.to_i
end
+
+ def custom_http_status_code?
+ node.int_type? &&
+ !::Rack::Utils::SYMBOL_TO_STATUS_CODE.value?(node.source.to_i)
+ end
end
# :nodoc:
class NumericStyleChecker
MSG = 'Prefer `%<prefer>s` over `%<current>s` ' \
'to describe HTTP status code.'.freeze
- DEFAULT_MSG = 'Prefer `numeric` over `symbolic` ' \
- 'to describe HTTP status code.'.freeze
WHITELIST_STATUS = %i[error success missing redirect].freeze
attr_reader :node
def initialize(node)
@@ -128,14 +116,10 @@
def offensive?
!node.int_type? && !whitelisted_symbol?
end
def message
- if RACK_LOADED
- format(MSG, prefer: preferred_style, current: symbol.inspect)
- else
- DEFAULT_MSG
- end
+ format(MSG, prefer: preferred_style, current: symbol.inspect)
end
def preferred_style
number.to_s
end