Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support block syntax in AllowIncompatibleOverride #173

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,53 @@ class AllowIncompatibleOverride < RuboCop::Cop::Base
"Instead, strive to write interfaces which respect subtyping principles and remove `allow_incompatible`"
RESTRICT_ON_SEND = [:override].freeze

# @!method allow_incompatible_override?(node)
def_node_matcher(:allow_incompatible_override?, <<~PATTERN)
# @!method sig_dot_override?(node)
def_node_matcher(:sig_dot_override?, <<~PATTERN)
(send
#sig?
[!nil? #sig?]
:override
(hash <$(pair (sym :allow_incompatible) true) ...>)
)
PATTERN

# @!method sig?(node)
def_node_search :sig?, <<~PATTERN
def_node_search(:sig?, <<~PATTERN)
(send _ :sig ...)
PATTERN

# @!method override?(node)
def_node_matcher(:override?, <<~PATTERN)
(send
_
:override
(hash <$(pair (sym :allow_incompatible) true) ...>)
)
PATTERN

def on_send(node)
allow_incompatible_override?(node) do |allow_incompatible_pair|
sig_dot_override?(node) do |allow_incompatible_pair|
add_offense(allow_incompatible_pair)
end
end

def on_block(node)
return unless sig?(node.send_node)

block = node.children.last
return unless block.send_type?

receiver = block.receiver
while receiver
allow_incompatible_pair = override?(receiver)
if allow_incompatible_pair
add_offense(allow_incompatible_pair)
break
end
receiver = receiver.receiver
end
end

alias_method :on_numblock, :on_block
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
RSpec.describe(RuboCop::Cop::Sorbet::AllowIncompatibleOverride, :config) do
let(:message) { RuboCop::Cop::Sorbet::AllowIncompatibleOverride::MSG }

it("allows using override(allow_incompatible: true) outside of sig") do
expect_no_offenses(<<~RUBY)
class Foo
override(allow_incompatible: true)
end
RUBY
end

it("disallows using override(allow_incompatible: true)") do
expect_offense(<<~RUBY)
class Foo
Expand All @@ -14,6 +22,33 @@ class Foo
RUBY
end

it("disallows using override(allow_incompatible: true) with block syntax") do
expect_offense(<<~RUBY)
class Foo
sig { override(allow_incompatible: true).void }
^^^^^^^^^^^^^^^^^^^^^^^^ #{message}
end
RUBY
end

it("disallows using receiver and override(allow_incompatible: true) with block syntax") do
expect_offense(<<~RUBY)
class Foo
sig { recv.override(allow_incompatible: true).void }
^^^^^^^^^^^^^^^^^^^^^^^^ #{message}
end
RUBY
end

it("disallows using override(allow_incompatible: true) with block syntax and params") do
expect_offense(<<~RUBY)
class Foo
sig { override(allow_incompatible: true).params(a: Integer).void }
^^^^^^^^^^^^^^^^^^^^^^^^ #{message}
end
RUBY
end

it("disallows using override(allow_incompatible: true) even when other keywords are present") do
expect_offense(<<~RUBY)
class Foo
Expand Down