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

#bare_sig?, #on_block, #sig_with_runtime?, #sig_without_runtime?, #signature?

Instance Method Details

#on_signature(sig) ⇒ Object



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

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? || contains_only_rubocop_directives?(range)

    add_offense(range) do |corrector|
      lines = range.source.lines
      rubocop_lines, other_lines = lines.partition { |line| line.strip.start_with?("# rubocop:") }

      unless other_lines.empty?
        corrector.insert_before(
          range_by_whole_lines(sig.source_range),
          other_lines.join
            .sub(/\A\n+/, "") # remove initial newline(s)
            .gsub(/\n{2,}/, "\n"), # remove empty line(s)
        )
      end

      corrector.replace(range, rubocop_lines.empty? ? "" : rubocop_lines.join)
    end
  end
end

#sig_or_signable_method_definition?(node) ⇒ Object



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

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