Class: RuboCop::Cop::Grape::HelpersIncludeModule

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/grape/helpers_include_module.rb

Overview

Prevent usage of Grape ‘helpers` with a block to include module. Using a bloc will create a new unnecessary module.

# bad
helpers do
  include MyModule
end

# bad
helpers do
  include MyModule
  include MyOtherModule
end

# good
helpers MyModule

# good
helpers MyModule, MyOtherModule

Constant Summary collapse

MSG =
'Use `helpers %<module_name>s` instead of `helpers` with a block.'

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rubocop/cop/grape/helpers_include_module.rb', line 36

def on_send(node)
  return unless helpers?(node)

  helpers_block_node = node.block_node.children.last
  block_nodes = block_nodes_in_helpers(helpers_block_node)

  block_nodes.each do |block_node|
    if (module_name = called_include?(block_node))
      add_offense(block_node, message: format(MSG, module_name: module_name))
    end
  end
end