Class: RuboCop::Cop::Sorbet::EmptyLineAfterSig

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp, SignatureHelp
Defined in:
lib/rubocop/cop/sorbet/signatures/empty_line_after_sig.rb

Overview

Checks for blank lines after signatures.

Examples:

# bad
sig { void }

def foo; end

# good
sig { void }
def foo; end

Constant Summary collapse

MSG =
"Extra empty line or comment detected"

Instance Method Summary collapse

Methods included from SignatureHelp

#on_block, #signature?, #with_runtime?, #without_runtime?

Instance Method Details

#on_signature(sig) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rubocop/cop/sorbet/signatures/empty_line_after_sig.rb', line 34

def on_signature(sig)
  sig_or_signable_method_definition?(next_sibling(sig)) do |definition|
    range = lines_between(sig, definition)
    next if range.empty? || range.single_line?

    add_offense(range) do |corrector|
      corrector.insert_before(
        range_by_whole_lines(sig.source_range),
        range.source
          .sub(/\A\n+/, "") # remove initial newline(s)
          .gsub(/\n{2,}/, "\n"), # remove empty line(s)
      )
      corrector.remove(range)
    end
  end
end

#sig_or_signable_method_definition?(node) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/rubocop/cop/sorbet/signatures/empty_line_after_sig.rb', line 25

def_node_matcher :sig_or_signable_method_definition?, <<~PATTERN
  ${
    def
    defs
    (send nil? {:attr_reader :attr_writer :attr_accessor} ...)
    #signature?
  }
PATTERN