Skip to content

Commit

Permalink
Use proper inheritance checks in plugins
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Terrasa <[email protected]>
  • Loading branch information
Morriar committed Jun 20, 2024
1 parent 8701aa7 commit c114904
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 29 deletions.
6 changes: 3 additions & 3 deletions lib/spoom/deadcode/plugins/actionpack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module Plugins
class ActionPack < Base
extend T::Sig

ignore_classes_inheriting_from("ApplicationController")

CALLBACKS = T.let(
[
"after_action",
Expand All @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion lib/spoom/deadcode/plugins/active_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion lib/spoom/deadcode/plugins/active_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions lib/spoom/deadcode/plugins/graphql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
14 changes: 4 additions & 10 deletions lib/spoom/deadcode/plugins/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,16 @@ 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 }
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 }
Expand All @@ -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
Expand Down
5 changes: 1 addition & 4 deletions lib/spoom/deadcode/plugins/sorbet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions lib/spoom/deadcode/plugins/thor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/spoom/deadcode/plugins/action_mailer_preview_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/spoom/deadcode/plugins/actionpack_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c114904

Please sign in to comment.