From 074d408c005a9fe35e739b6b4c8335d18e94cb9a Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Wed, 19 Jun 2024 16:37:36 -0400 Subject: [PATCH] Pass SymbolDefs to plugin `on_constant` Signed-off-by: Alexandre Terrasa --- lib/spoom/deadcode/plugins/base.rb | 16 ++++++------- lib/spoom/deadcode/plugins/rubocop.rb | 17 ++++++------- lib/spoom/deadcode/plugins/sorbet.rb | 28 +++++++++++----------- test/spoom/deadcode/plugins/base_test.rb | 4 ++-- test/spoom/deadcode/plugins/sorbet_test.rb | 18 +++++++------- 5 files changed, 42 insertions(+), 41 deletions(-) diff --git a/lib/spoom/deadcode/plugins/base.rb b/lib/spoom/deadcode/plugins/base.rb index cfcda2ba..38e9f26b 100644 --- a/lib/spoom/deadcode/plugins/base.rb +++ b/lib/spoom/deadcode/plugins/base.rb @@ -189,22 +189,22 @@ def internal_on_define_class(symbol_def, definition) # # ~~~rb # class MyPlugin < Spoom::Deadcode::Plugins::Base - # def on_define_constant(indexer, definition) - # definition.ignored! if definition.name == "FOO" + # def on_define_constant(symbol_def, definition) + # definition.ignored! if symbol_def.name == "FOO" # end # end # ~~~ - sig { params(indexer: Indexer, definition: Definition).void } - def on_define_constant(indexer, definition) + sig { params(symbol_def: Model::Constant, definition: Definition).void } + def on_define_constant(symbol_def, definition) # no-op end # Do not override this method, use `on_define_constant` instead. - sig { params(indexer: Indexer, definition: Definition).void } - def internal_on_define_constant(indexer, definition) - definition.ignored! if ignored_constant_name?(definition.name) + sig { params(symbol_def: Model::Constant, definition: Definition).void } + def internal_on_define_constant(symbol_def, definition) + definition.ignored! if ignored_constant_name?(symbol_def.name) - on_define_constant(indexer, definition) + on_define_constant(symbol_def, definition) end # Called when a method is defined. diff --git a/lib/spoom/deadcode/plugins/rubocop.rb b/lib/spoom/deadcode/plugins/rubocop.rb index d1bcc592..444b307e 100644 --- a/lib/spoom/deadcode/plugins/rubocop.rb +++ b/lib/spoom/deadcode/plugins/rubocop.rb @@ -14,9 +14,15 @@ class Rubocop < Base /^(::)?RuboCop::Cop::Base$/, ) - sig { override.params(indexer: Indexer, definition: Definition).void } - def on_define_constant(indexer, definition) - definition.ignored! if rubocop_constant?(indexer, definition) + sig { override.params(symbol_def: Model::Constant, definition: Definition).void } + def on_define_constant(symbol_def, definition) + owner = symbol_def.owner + return false unless owner.is_a?(Model::Class) + + superclass_name = owner.superclass_name + return false unless superclass_name + + definition.ignored! if ignored_subclass?(superclass_name) && RUBOCOP_CONSTANTS.include?(symbol_def.name) end sig { override.params(indexer: Indexer, definition: Definition).void } @@ -26,11 +32,6 @@ def on_define_method(indexer, definition) private - sig { params(indexer: Indexer, definition: Definition).returns(T::Boolean) } - def rubocop_constant?(indexer, definition) - ignored_subclass?(indexer.nesting_class_superclass_name) && RUBOCOP_CONSTANTS.include?(definition.name) - end - sig { params(indexer: Indexer, definition: Definition).returns(T::Boolean) } def rubocop_method?(indexer, definition) ignored_subclass?(indexer.nesting_class_superclass_name) && definition.name == "on_send" diff --git a/lib/spoom/deadcode/plugins/sorbet.rb b/lib/spoom/deadcode/plugins/sorbet.rb index 6c10a18f..2b538546 100644 --- a/lib/spoom/deadcode/plugins/sorbet.rb +++ b/lib/spoom/deadcode/plugins/sorbet.rb @@ -7,9 +7,9 @@ module Plugins class Sorbet < Base extend T::Sig - sig { override.params(indexer: Indexer, definition: Definition).void } - def on_define_constant(indexer, definition) - definition.ignored! if sorbet_type_member?(indexer, definition) || sorbet_enum_constant?(indexer, definition) + sig { override.params(symbol_def: Model::Constant, definition: Definition).void } + def on_define_constant(symbol_def, definition) + definition.ignored! if sorbet_type_member?(symbol_def) || sorbet_enum_constant?(symbol_def) end sig { override.params(indexer: Indexer, definition: Definition).void } @@ -19,20 +19,20 @@ def on_define_method(indexer, definition) private - sig { params(indexer: Indexer, definition: Definition).returns(T::Boolean) } - def sorbet_type_member?(indexer, definition) - assign = indexer.nesting_node(Prism::ConstantWriteNode) - return false unless assign + sig { params(symbol_def: Model::Constant).returns(T::Boolean) } + def sorbet_type_member?(symbol_def) + symbol_def.value.match?(/^(type_member|type_template)/) + end - value = assign.value - return false unless value.is_a?(Prism::CallNode) + sig { params(symbol_def: Model::Constant).returns(T::Boolean) } + def sorbet_enum_constant?(symbol_def) + owner = symbol_def.owner + return false unless owner.is_a?(Model::Class) - value.name == :type_member || value.name == :type_template - end + superclass_name = owner.superclass_name + return false unless superclass_name - sig { params(indexer: Indexer, definition: Definition).returns(T::Boolean) } - def sorbet_enum_constant?(indexer, definition) - /^(::)?T::Enum$/.match?(indexer.nesting_class_superclass_name) && indexer.nesting_call&.name == :enums + superclass_name.match?(/^(::)?T::Enum$/) end end end diff --git a/test/spoom/deadcode/plugins/base_test.rb b/test/spoom/deadcode/plugins/base_test.rb index a4998ba5..d3d473c6 100644 --- a/test/spoom/deadcode/plugins/base_test.rb +++ b/test/spoom/deadcode/plugins/base_test.rb @@ -48,8 +48,8 @@ class Class2; end def test_on_define_constant plugin = Class.new(Base) do - def on_define_constant(indexer, definition) - definition.ignored! if definition.name == "CONST1" + def on_define_constant(symbol_def, definition) + definition.ignored! if symbol_def.name == "CONST1" end end diff --git a/test/spoom/deadcode/plugins/sorbet_test.rb b/test/spoom/deadcode/plugins/sorbet_test.rb index fcfeeec0..f0094112 100644 --- a/test/spoom/deadcode/plugins/sorbet_test.rb +++ b/test/spoom/deadcode/plugins/sorbet_test.rb @@ -31,23 +31,17 @@ def test_ignore_sorbet_enum_constants DEAD1 = new end - class BadEnum < T::Enum - something do - DEAD2 = new - end - end - class BadEnum < T::Enum class BadEnum enums do - DEAD3 = new + DEAD2 = new end end end class Foo enums do - DEAD4 = new + DEAD3 = new end end @@ -66,6 +60,12 @@ class OtherEnum < ::T::Enum end end end + + class BadEnum < T::Enum + something do + IGNORED5 = new + end + end RB index = index_with_plugins @@ -73,10 +73,10 @@ class OtherEnum < ::T::Enum assert_ignored(index, "IGNORED2") assert_ignored(index, "IGNORED3") assert_ignored(index, "IGNORED4") + assert_ignored(index, "IGNORED5") refute_ignored(index, "DEAD1") refute_ignored(index, "DEAD2") refute_ignored(index, "DEAD3") - refute_ignored(index, "DEAD4") end def test_ignore_sorbet_overrides