Skip to content

Commit

Permalink
Merge pull request #188 from Shopify/empty-lines-in-rbis
Browse files Browse the repository at this point in the history
  • Loading branch information
sambostock authored Oct 16, 2023
2 parents 5499b1c + 5183f23 commit d94f631
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
3 changes: 3 additions & 0 deletions config/rbi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ Style/ClassAndModuleChildren:
Style/DefWithParentheses:
Enabled: true

Sorbet/EmptyLineAfterSig:
Enabled: true

Style/EmptyMethod:
Enabled: true
EnforcedStyle: compact
Expand Down
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 d94f631

Please sign in to comment.