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

Refactor ForbidExtendTSigHelpersInShims #146

Merged
merged 1 commit into from
Feb 21, 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 @@ -22,32 +22,24 @@ module Sorbet
# sig { returns(String) }
# def foo; end
# end
class ForbidExtendTSigHelpersInShims < RuboCop::Cop::Cop # rubocop:todo InternalAffairs/InheritDeprecatedCopClass
class ForbidExtendTSigHelpersInShims < RuboCop::Cop::Base
extend AutoCorrector
include RangeHelp

MSG = "Extending T::Sig or T::Helpers in a shim is unnecessary"
RESTRICT_ON_SEND = [:extend]
RESTRICT_ON_SEND = [:extend].freeze

# @!method extend_t_sig?(node)
def_node_matcher :extend_t_sig?, <<~PATTERN
(send nil? :extend (const (const nil? :T) :Sig))
# @!method extend_t_sig_or_helpers?(node)
def_node_matcher :extend_t_sig_or_helpers?, <<~PATTERN
(send nil? :extend (const (const nil? :T) {:Sig :Helpers}))
PATTERN

# @!method extend_t_helpers?(node)
def_node_matcher :extend_t_helpers?, <<~PATTERN
(send nil? :extend (const (const nil? :T) :Helpers))
PATTERN

def autocorrect(node)
-> (corrector) do
corrector.remove(
range_by_whole_lines(node.source_range, include_final_newline: true)
)
end
end

def on_send(node)
add_offense(node) if extend_t_helpers?(node) || extend_t_sig?(node)
extend_t_sig_or_helpers?(node) do
add_offense(node) do |corrector|
corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
end
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ module MyModule
def foo; end
end
RBI

expect_correction(<<~RBI)
module MyModule

sig { returns(String) }
def foo; end
end
RBI
end

it "adds an offence when an extend T::Sig or extend T::Helpers call uses parenthesis syntax" do
Expand All @@ -30,77 +38,48 @@ module MyModule
def foo; end
end
RBI
end
end

describe("no offences") do
it "does not add an offence to uses of extend that are not T::Sig or T::Helpers" do
expect_no_offenses(<<~RBI)
expect_correction(<<~RBI)
module MyModule
extend ActiveSupport::Concern

sig { returns(String) }
def foo; end
end
RBI
end
end

describe("autocorrect") do
it "autocorrects usages of extend T::Sig and extend T::Helpers by removing them" do
source = <<~RBI
it "adds an offense when extend T::Sig or extend T::Helpers are extended in otherwise empty classes or modules" do
expect_offense(<<~RBI)
module MyModule
extend T::Sig
extend T::Helpers
extend ActiveSupport::Concern
extend(T::Sig)
^^^^^^^^^^^^^^ Extending T::Sig or T::Helpers in a shim is unnecessary
end

sig { returns(String) }
def foo; end
class MyClass
extend(T::Helpers)
^^^^^^^^^^^^^^^^^^ Extending T::Sig or T::Helpers in a shim is unnecessary
end
RBI
expect(autocorrect_source(source))
.to(eq(<<~RBI))
module MyModule
extend ActiveSupport::Concern

sig { returns(String) }
def foo; end
end
RBI
end

it "autocorrects usages of extend(T::Sig) and extend(T::Helpers) by removing them" do
source = <<~RBI
expect_correction(<<~RBI)
module MyModule
extend(T::Sig)
extend(T::Helpers)
extend ActiveSupport::Concern
end

sig { returns(String) }
def foo; end
class MyClass
end
RBI
expect(autocorrect_source(source))
.to(eq(<<~RBI))
module MyModule
extend ActiveSupport::Concern

sig { returns(String) }
def foo; end
end
RBI
end
end

it "autocorrects by removing extend T::Sig or T::Helpers from an otherwise empty class" do
source = <<~RBI
describe("no offences") do
it "does not add an offence to uses of extend that are not T::Sig or T::Helpers" do
expect_no_offenses(<<~RBI)
module MyModule
extend(T::Sig)
extend ActiveSupport::Concern

def foo; end
end
RBI
expect(autocorrect_source(source))
.to(eq(<<~RBI))
module MyModule
end
RBI
end
end
end