Class: RuboCop::Cop::Iotventure::DuplicateResponseCode

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/iotventure/duplicate_response_code.rb

Overview

This cop checks for duplicated response code example definitions.

Examples:


# bad

response 400, 'response description 1' {}
response 400, 'response description 2' {}

# bad

context 'context 1' do
  response 400, 'response description 1' {}
end
context 'context 2' do
  response 400, 'response description 2' {}
end

# bad

2.times do
  response 400, 'response description' {}
end

# good

response 400, 'response description' do
  context 'context 1' {}
  context 'context 2' {}
end

# good

response 400, 'response description 1' {}
response 401, 'response description 2' {}

Constant Summary collapse

DUPLICATE_MSG =
'Response Code `%<response_code>s` is defined at both %<other>s and %<current>s.'
IN_LOOP_MSG =
'Response definition at %<current>s is defined inside loop at %<loop>s'

Instance Method Summary collapse

Constructor Details

#initialize(config = nil, options = nil) ⇒ DuplicateResponseCode

Returns a new instance of DuplicateResponseCode.



75
76
77
78
# File 'lib/rubocop/cop/iotventure/duplicate_response_code.rb', line 75

def initialize(config = nil, options = nil)
  super
  @definitions = {}
end

Instance Method Details

#on_block(node) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/rubocop/cop/iotventure/duplicate_response_code.rb', line 80

def on_block(node)
  code = response_code_definition(node)
  return unless code

  check_for_duplicate_response_code(node, code)
  check_for_definition_inside_loop(node)
end

#response_code_definition?(node) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/rubocop/cop/iotventure/duplicate_response_code.rb', line 58

def_node_matcher :response_code_definition, <<~PATTERN
  (block
    (send nil? :response
      (int $_)
      ...)
    ...
  )
PATTERN

#route_definition?(node) ⇒ Object



68
69
70
71
72
73
# File 'lib/rubocop/cop/iotventure/duplicate_response_code.rb', line 68

def_node_matcher :route_definition, <<~PATTERN
  (block
    (send nil? #route_verb? (str _))
    ...
  )
PATTERN