diff --git a/config/rbi.yml b/config/rbi.yml index 63398c7c..6779920c 100644 --- a/config/rbi.yml +++ b/config/rbi.yml @@ -242,6 +242,9 @@ Style/ClassAndModuleChildren: Style/DefWithParentheses: Enabled: true +Sorbet/EmptyLineAfterSig: + Enabled: true + Style/EmptyMethod: Enabled: true EnforcedStyle: compact diff --git a/lib/rubocop/cop/sorbet/signatures/empty_line_after_sig.rb b/lib/rubocop/cop/sorbet/signatures/empty_line_after_sig.rb index 38560a89..048c53d8 100644 --- a/lib/rubocop/cop/sorbet/signatures/empty_line_after_sig.rb +++ b/lib/rubocop/cop/sorbet/signatures/empty_line_after_sig.rb @@ -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? diff --git a/spec/rubocop/cop/sorbet/signatures/empty_line_after_sig_spec.rb b/spec/rubocop/cop/sorbet/signatures/empty_line_after_sig_spec.rb index ed0ed811..683e8e39 100644 --- a/spec/rubocop/cop/sorbet/signatures/empty_line_after_sig_spec.rb +++ b/spec/rubocop/cop/sorbet/signatures/empty_line_after_sig_spec.rb @@ -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 @@ -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