Class: RuboCop::Cop::Sevencop::MethodDefinitionInIncluded

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp, VisibilityHelp
Defined in:
lib/rubocop/cop/sevencop/method_definition_in_included.rb

Overview

Do not define methods in ‘included` blocks.

Examples:

# bad
module A
  extend ::ActiveSupport::Concern

  included do
    def foo
    end
  end
end

# good
module A
  extend ::ActiveSupport::Concern

  def foo
  end
end

Constant Summary collapse

MSG =
'Do not define methods in `included` blocks.'

Instance Method Summary collapse

Instance Method Details

#on_def(node) ⇒ void

This method returns an undefined value.

Parameters:

  • node (RuboCop::AST::DefNode)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rubocop/cop/sevencop/method_definition_in_included.rb', line 36

def on_def(node)
  block = find_ancestor_included_block(node)
  return unless block

  add_offense(node) do |corrector|
    corrector.insert_before(
      range_with_comments_and_lines(block),
      "#{range_with_comments_and_lines(node).source}\n".sub(
        /\bdef /, "#{node_visibility(node)} def "
      )
    )
    corrector.remove(
      range_with_comments_and_lines(node)
    )
  end
end