Class: RuboCop::Cop::Pixelforce::EmptyLineBetweenCategories

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/pixelforce/empty_line_between_categories.rb

Constant Summary collapse

HUMANIZED_NODE_TYPE =
{
  casgn: :constants,
  defs: :class_methods,
  def: :public_methods,
  sclass: :class_singleton,
  send: :method_calls
}.freeze
VISIBILITY_SCOPES =
%i[private protected public].freeze
MSG_EMPTY_LINE_BETWEEN_CATEGORIES =
'Use empty lines between categories.'.freeze
MSG_EMPTY_LINE_BETWEEN_SAME_CATEGORIES =
"Don't Use empty lines between same categories.".freeze

Instance Method Summary collapse

Instance Method Details

#on_class(class_node) ⇒ Object

Validates code style on class declaration. Add offense when find a node out of expected order.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rubocop/cop/pixelforce/empty_line_between_categories.rb', line 26

def on_class(class_node)
  previous_category = nil

  walk_over_nested_class_definition(class_node) do |node, category|
    # Skip empty line enforcement for method definitions
    next if method_definition?(node)

    if previous_category && previous_category != category
      count = blank_lines_count_between(prev_node(node), node)
      if count < 2
        add_offense(node, message: MSG_EMPTY_LINE_BETWEEN_CATEGORIES) do |corrector|
          prev_category = prev_node(node)
          end_pos = end_position_for(prev_category)
          newline_pos = buffer.source.index("\n", end_pos)
          where_to_insert = range_between(newline_pos, newline_pos + 1)
          corrector.insert_after(where_to_insert, "\n")
        end
      end
    elsif previous_category && previous_category == category
      count = blank_lines_count_between(prev_node(node), node)
      if count > 1
        add_offense(node, message: MSG_EMPTY_LINE_BETWEEN_SAME_CATEGORIES) do |corrector|
          prev_category = prev_node(node)
          end_pos = end_position_for(prev_category)
          newline_pos = buffer.source.index("\n", end_pos)
          range_to_remove = range_between(newline_pos, newline_pos + 1)
          corrector.remove(range_to_remove)
        end
      end
    end

    previous_category = category
  end
end