class RuboCop::Cop::Style::MixinUsage

This cop checks that `include`, `extend` and `prepend` exists at the top level. Using these at the top level affects the behavior of `Object`. There will not be using `include`, `extend` and `prepend` at the top level. Let's use it inside `class` or `module`.

@example

# bad
include M

class C
end

# bad
extend M

class C
end

# bad
prepend M

class C
end

# good
class C
  include M
end

# good
class C
  extend M
end

# good
class C
  prepend M
end

Constants

MSG

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/style/mixin_usage.rb, line 54
def on_send(node)
  return unless (statement = include_statement(node))
  return unless top_level_node?(node)

  add_offense(node, message: format(MSG, statement: statement))
end

Private Instance Methods

top_level_node?(node) click to toggle source
# File lib/rubocop/cop/style/mixin_usage.rb, line 63
def top_level_node?(node)
  if node.parent.parent.nil?
    node.sibling_index.zero?
  else
    top_level_node?(node.parent)
  end
end