Class: RuboCop::Cop::Layout::SingleLineStatementSpacing

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

Overview

Forbids a blank line between two consecutive single-line statements — they are one run, so the blank reveals nothing. A blank that separates code from an adjacent comment is kept, as is one owned by another cop (a guard clause, a flow-control statement, an access modifier) or one beside a multi-line or heredoc neighbour. Autocorrects by removing it.

Constant Summary collapse

ACCESS_MODIFIERS =
%i[private protected public module_function].freeze
FLOW_CONTROL_METHODS =
%i[raise fail throw].freeze
FLOW_CONTROL_TYPES =
%i[return next break redo retry].freeze
MSG =
'Remove the blank line between consecutive single-line statements.'

Instance Method Summary collapse

Instance Method Details

#on_begin(node) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rubocop/cop/layout/single_line_statement_spacing.rb', line 20

def on_begin(node)
  node.children.each_cons(2) do |first, second|
    next unless first.is_a?(::RuboCop::AST::Node) && second.is_a?(::RuboCop::AST::Node)
    next if structural?(first) || structural?(second)

    blanks = removable_blank_lines(first, second)

    next if blanks.empty?

    add_offense(second) do |corrector|
      blanks.each { |line| corrector.remove(line_range_with_newline(line)) }
    end
  end
end