Class: RuboCop::Cop::Boxt::ApiTypeParameters

Inherits:
RuboCop::Cop::Base
  • Object
show all
Defined in:
lib/rubocop/cop/boxt/api_type_parameters.rb

Overview

This cop ensures that each parameter in a Grape API has a type specified.

Examples:

# bad
requires :name
optional :age

# good
requires :name, type: String
optional :age, type: Integer

Constant Summary collapse

API_MESSAGE =
"Ensure each parameter has a type specified, e.g., `type: String`."
ENTITY_MESSAGE =
"Ensure each parameter has a type specified, e.g., `documentation: { type: String }`."

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubocop/cop/boxt/api_type_parameters.rb', line 33

def on_send(node)
  param_declaration(node) do
    next unless grape_api_class?(node) || grape_entity_class?(node)

    if grape_api_class?(node) && param_with_type(node).none?
      add_offense(node, message: API_MESSAGE)
    elsif grape_entity_class?(node) && entity_with_type_documentation(node).none?
      add_offense(node, message: ENTITY_MESSAGE)
    end
  end
end