Class: RuboCop::Cop::Layout::LineBreakAfterFinalMixin

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/layout/line_break_after_final_mixin.rb

Overview

# good

class Hello
  include Something1
  include Something2

  def world
  end
end

Constant Summary collapse

MSG =
'Add an empty line after the last `%<mixin>s`.'
MIXIN_METHODS =
%i[include extend prepend].to_set.freeze

Instance Method Summary collapse

Instance Method Details

#mixin?(node) ⇒ Object



33
34
35
# File 'lib/rubocop/cop/layout/line_break_after_final_mixin.rb', line 33

def_node_matcher :mixin?, <<~PATTERN
  (send {nil? | self} MIXIN_METHODS ...)
PATTERN

#on_class(node) ⇒ Object



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

def on_class(node)
  return unless node.body

  mixins = node.body.child_nodes.select { |child| mixin?(child) }

  return if mixins.empty?

  last_mixin = mixins.last

  return if next_line_valid?(last_mixin)

  add_offense(last_mixin, message: format(MSG, mixin: last_mixin.method_name)) do |corrector|
    corrector.insert_after(last_mixin, "\n")
  end
end