From c1149044f1c5854f7b5055dfb7b4e31ff47d5c54 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Thu, 20 Jun 2024 10:26:14 -0400 Subject: [PATCH] Use proper inheritance checks in plugins Signed-off-by: Alexandre Terrasa --- lib/spoom/deadcode/plugins/actionpack.rb | 6 +++--- lib/spoom/deadcode/plugins/active_model.rb | 2 +- lib/spoom/deadcode/plugins/active_support.rb | 2 +- lib/spoom/deadcode/plugins/graphql.rb | 8 ++++---- lib/spoom/deadcode/plugins/rubocop.rb | 14 ++++---------- lib/spoom/deadcode/plugins/sorbet.rb | 5 +---- lib/spoom/deadcode/plugins/thor.rb | 5 +---- .../deadcode/plugins/action_mailer_preview_test.rb | 2 +- test/spoom/deadcode/plugins/actionpack_test.rb | 2 +- 9 files changed, 17 insertions(+), 29 deletions(-) diff --git a/lib/spoom/deadcode/plugins/actionpack.rb b/lib/spoom/deadcode/plugins/actionpack.rb index 6f4eb595..f2e8c0a3 100644 --- a/lib/spoom/deadcode/plugins/actionpack.rb +++ b/lib/spoom/deadcode/plugins/actionpack.rb @@ -7,6 +7,8 @@ module Plugins class ActionPack < Base extend T::Sig + ignore_classes_inheriting_from("ApplicationController") + CALLBACKS = T.let( [ "after_action", @@ -25,14 +27,12 @@ class ActionPack < Base T::Array[String], ) - ignore_classes_named(/Controller$/) - sig { override.params(definition: Model::Method).void } def on_define_method(definition) owner = definition.owner return unless owner.is_a?(Model::Class) - @index.ignore(definition) if ignored_class_name?(owner.name) + @index.ignore(definition) if ignored_subclass?(owner) end sig { override.params(send: Send).void } diff --git a/lib/spoom/deadcode/plugins/active_model.rb b/lib/spoom/deadcode/plugins/active_model.rb index de42f59b..2ab028a2 100644 --- a/lib/spoom/deadcode/plugins/active_model.rb +++ b/lib/spoom/deadcode/plugins/active_model.rb @@ -7,7 +7,7 @@ module Plugins class ActiveModel < Base extend T::Sig - ignore_classes_inheriting_from(/^(::)?ActiveModel::EachValidator$/) + ignore_classes_inheriting_from("ActiveModel::EachValidator") ignore_methods_named("validate_each") sig { override.params(send: Send).void } diff --git a/lib/spoom/deadcode/plugins/active_support.rb b/lib/spoom/deadcode/plugins/active_support.rb index 477032f8..4acf3050 100644 --- a/lib/spoom/deadcode/plugins/active_support.rb +++ b/lib/spoom/deadcode/plugins/active_support.rb @@ -5,7 +5,7 @@ module Spoom module Deadcode module Plugins class ActiveSupport < Base - ignore_classes_inheriting_from(/^(::)?ActiveSupport::TestCase$/) + ignore_classes_inheriting_from("ActiveSupport::TestCase") ignore_methods_named( "after_all", diff --git a/lib/spoom/deadcode/plugins/graphql.rb b/lib/spoom/deadcode/plugins/graphql.rb index f331da9f..9d0dd6b1 100644 --- a/lib/spoom/deadcode/plugins/graphql.rb +++ b/lib/spoom/deadcode/plugins/graphql.rb @@ -8,10 +8,10 @@ class GraphQL < Base extend T::Sig ignore_classes_inheriting_from( - /^(::)?GraphQL::Schema::Enum$/, - /^(::)?GraphQL::Schema::Object$/, - /^(::)?GraphQL::Schema::Scalar$/, - /^(::)?GraphQL::Schema::Union$/, + "GraphQL::Schema::Enum", + "GraphQL::Schema::Object", + "GraphQL::Schema::Scalar", + "GraphQL::Schema::Union", ) ignore_methods_named( diff --git a/lib/spoom/deadcode/plugins/rubocop.rb b/lib/spoom/deadcode/plugins/rubocop.rb index 7686180a..6e855d58 100644 --- a/lib/spoom/deadcode/plugins/rubocop.rb +++ b/lib/spoom/deadcode/plugins/rubocop.rb @@ -10,8 +10,8 @@ class Rubocop < Base RUBOCOP_CONSTANTS = T.let(["MSG", "RESTRICT_ON_SEND"].to_set.freeze, T::Set[String]) ignore_classes_inheriting_from( - /^(::)?RuboCop::Cop::Cop$/, - /^(::)?RuboCop::Cop::Base$/, + "RuboCop::Cop::Cop", + "RuboCop::Cop::Base", ) sig { override.params(definition: Model::Constant).void } @@ -19,10 +19,7 @@ def on_define_constant(definition) owner = definition.owner return false unless owner.is_a?(Model::Class) - superclass_name = owner.superclass_name - return false unless superclass_name - - @index.ignore(definition) if ignored_subclass?(superclass_name) && RUBOCOP_CONSTANTS.include?(definition.name) + @index.ignore(definition) if ignored_subclass?(owner) && RUBOCOP_CONSTANTS.include?(definition.name) end sig { override.params(definition: Model::Method).void } @@ -32,10 +29,7 @@ def on_define_method(definition) owner = definition.owner return unless owner.is_a?(Model::Class) - superclass_name = owner.superclass_name - return unless superclass_name - - @index.ignore(definition) if ignored_subclass?(superclass_name) + @index.ignore(definition) if ignored_subclass?(owner) end end end diff --git a/lib/spoom/deadcode/plugins/sorbet.rb b/lib/spoom/deadcode/plugins/sorbet.rb index 694ac66c..4ede9549 100644 --- a/lib/spoom/deadcode/plugins/sorbet.rb +++ b/lib/spoom/deadcode/plugins/sorbet.rb @@ -29,10 +29,7 @@ def sorbet_enum_constant?(definition) owner = definition.owner return false unless owner.is_a?(Model::Class) - superclass_name = owner.superclass_name - return false unless superclass_name - - superclass_name.match?(/^(::)?T::Enum$/) + subclass_of?(owner, "T::Enum") end end end diff --git a/lib/spoom/deadcode/plugins/thor.rb b/lib/spoom/deadcode/plugins/thor.rb index a9faa9c3..b17a46dc 100644 --- a/lib/spoom/deadcode/plugins/thor.rb +++ b/lib/spoom/deadcode/plugins/thor.rb @@ -14,10 +14,7 @@ def on_define_method(definition) owner = definition.owner return unless owner.is_a?(Model::Class) - superclass_name = owner.superclass_name - return unless superclass_name - - @index.ignore(definition) if superclass_name =~ /^(::)?Thor$/ + @index.ignore(definition) if subclass_of?(owner, "Thor") end end end diff --git a/test/spoom/deadcode/plugins/action_mailer_preview_test.rb b/test/spoom/deadcode/plugins/action_mailer_preview_test.rb index 476a9287..17ef3a8d 100644 --- a/test/spoom/deadcode/plugins/action_mailer_preview_test.rb +++ b/test/spoom/deadcode/plugins/action_mailer_preview_test.rb @@ -41,7 +41,7 @@ def bar; end # Document current less then ideal behavior: assert_ignored(index, "some_unused_private_method") - refute_ignored(index, "MyIndirectInheritanceMailerPreview") + assert_ignored(index, "MyIndirectInheritanceMailerPreview") end private diff --git a/test/spoom/deadcode/plugins/actionpack_test.rb b/test/spoom/deadcode/plugins/actionpack_test.rb index 0cdc3568..e6c2cf76 100644 --- a/test/spoom/deadcode/plugins/actionpack_test.rb +++ b/test/spoom/deadcode/plugins/actionpack_test.rb @@ -12,7 +12,7 @@ class ActionPackTest < TestWithProject def test_ignore_actionpack_controllers @project.write!("app/controllers/foo.rb", <<~RB) - class FooController + class FooController < ApplicationController def foo; end end