Class: RuboCop::Cop::Grape::PresentWith

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
EndpointHelper
Defined in:
lib/rubocop/cop/grape/present_with.rb

Overview

Examples:


# bad
get do
  present users: users, with: API::Entities::User
end

# good
get do
  present :users, users, with: API::Entities::User
end

Constant Summary collapse

MSG_TEMPLATE =
'Maybe you mistyped "`%<current>s`", it should be "`%<correct>s`"'

Instance Method Summary collapse

Instance Method Details

#message(key, val) ⇒ Object



43
44
45
# File 'lib/rubocop/cop/grape/present_with.rb', line 43

def message(key, val)
  format(MSG_TEMPLATE, current: "#{key}: #{val}", correct: ":#{key}, #{val}")
end

#on_send(present_node) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/rubocop/cop/grape/present_with.rb', line 32

def on_send(present_node)
  one_hash_arg_with_key_name_with?(present_node) do |key, val|
    kwarg = present_node.children.last

    add_offense(kwarg.children.first, message: message(key, val)) do |corrector|
      corrector.remove(kwarg.children.first)
      corrector.insert_before(kwarg, ":#{key}, #{val}")
    end
  end
end