From f2356e5ef4e5361ccb5c91e5a006315a60e74d4a Mon Sep 17 00:00:00 2001 From: Sam Bostock Date: Mon, 20 Feb 2023 11:30:24 -0500 Subject: [PATCH] Refactor `ForbidExtendTSigHelpersInShims` - Migrate from deprecated `Cop` parent class to `Base` - Combine node matchers - Use `expect_correction` helper to simplify tests --- .../forbid_extend_t_sig_helpers_in_shims.rb | 30 +++----- ...rbid_extend_t_sig_helpers_in_shims_spec.rb | 77 +++++++------------ 2 files changed, 39 insertions(+), 68 deletions(-) diff --git a/lib/rubocop/cop/sorbet/rbi/forbid_extend_t_sig_helpers_in_shims.rb b/lib/rubocop/cop/sorbet/rbi/forbid_extend_t_sig_helpers_in_shims.rb index 2a0a432f..ee6c91fd 100644 --- a/lib/rubocop/cop/sorbet/rbi/forbid_extend_t_sig_helpers_in_shims.rb +++ b/lib/rubocop/cop/sorbet/rbi/forbid_extend_t_sig_helpers_in_shims.rb @@ -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 diff --git a/spec/rubocop/cop/sorbet/rbi/forbid_extend_t_sig_helpers_in_shims_spec.rb b/spec/rubocop/cop/sorbet/rbi/forbid_extend_t_sig_helpers_in_shims_spec.rb index 1382262d..7b23dae8 100644 --- a/spec/rubocop/cop/sorbet/rbi/forbid_extend_t_sig_helpers_in_shims_spec.rb +++ b/spec/rubocop/cop/sorbet/rbi/forbid_extend_t_sig_helpers_in_shims_spec.rb @@ -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 @@ -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