Skip to content

Commit

Permalink
Detect empty lines between multiple signatures
Browse files Browse the repository at this point in the history
This can occur in RBI files.
  • Loading branch information
sambostock committed Oct 16, 2023
1 parent 5499b1c commit 065ac0c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/rubocop/cop/sorbet/signatures/empty_line_after_sig.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@ class EmptyLineAfterSig < ::RuboCop::Cop::Base

MSG = "Extra empty line or comment detected"

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

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

Expand Down
40 changes: 40 additions & 0 deletions spec/rubocop/cop/sorbet/signatures/empty_line_after_sig_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ def foo; end
sig { void }; def foo; end
RUBY
end

it("does not register an offense or fail if a method definition has multiple sigs (e.g. RBI files)") do
expect_no_offenses(<<~RUBY)
sig { void }
sig { params(foo: String).void }
def bar(foo); end
RUBY
end
end

context("with an empty line between sig and method definition") do
Expand Down Expand Up @@ -191,6 +199,38 @@ def m; end
def m; end
RUBY
end

it("registers an offense for empty line following multiple sigs") do
expect_offense(<<~RUBY)
sig { void }
sig { params(foo: String).void }
^{} Extra empty line or comment detected
def bar(foo); end
RUBY

expect_correction(<<~RUBY)
sig { void }
sig { params(foo: String).void }
def bar(foo); end
RUBY
end

it("registers an offense for empty line in between multiple sigs") do
expect_offense(<<~RUBY)
sig { void }
^{} Extra empty line or comment detected
sig { params(foo: String).void }
def bar(foo); end
RUBY

expect_correction(<<~RUBY)
sig { void }
sig { params(foo: String).void }
def bar(foo); end
RUBY
end
end

it "registers no offense when there is only a sig" do
Expand Down

0 comments on commit 065ac0c

Please sign in to comment.