Class: RuboCop::Cop::Pixelforce::EmptyLineBwteenCategories

Inherits:
RuboCop::Cop
  • Object
show all
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
}.freeze
VISIBILITY_SCOPES =
%i[private protected public].freeze
MSG =
'Use empty lines between categories.'

Instance Method Summary collapse

Instance Method Details

#autocorrect(node) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rubocop/cop/pixelforce/empty_line_between_categories.rb', line 42

def autocorrect(node)
  prev_category = prev_node(node)
  # finds position of first newline
  end_pos = end_position_for(prev_category)
  newline_pos = buffer.source.index("\n", end_pos)

  count = blank_lines_count_between(prev_category, node)

  if count > 1
    autocorrect_remove_lines(newline_pos, count)
  else
    autocorrect_insert_lines(newline_pos, count)
  end
end

#on_class(class_node) ⇒ Object

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rubocop/cop/pixelforce/empty_line_between_categories.rb', line 23

def on_class(class_node)
  previous_category = nil
  previous_node_end_line = -1
  previous_method_name = nil
  walk_over_nested_class_definition(class_node) do |node, category|
    next unless node.respond_to?(:method_name)
    if previous_category && category != previous_category && node.loc.first_line - previous_node_end_line < 2
      add_offense(node, message: MSG)
    end

    if previous_method_name && previous_method_name == node.method_name && node.loc.first_line - previous_node_end_line > 1
      add_offense(node, message: "Don't Use empty lines between same categories.")
    end
    previous_category = category
    previous_node_end_line = node.loc.last_line
    previous_method_name = node.method_name
  end
end