From 2f5993825a8ff6e39583eda52d650eb0d7ec23ad Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Mon, 30 Sep 2024 14:17:48 -0400 Subject: [PATCH] Add deadcode plugin handling for Object#method Signed-off-by: Alexandre Terrasa --- lib/spoom/deadcode/plugins/ruby.rb | 3 +++ test/spoom/deadcode/plugins/ruby_test.rb | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/spoom/deadcode/plugins/ruby.rb b/lib/spoom/deadcode/plugins/ruby.rb index 4daa2edb..96e4a250 100644 --- a/lib/spoom/deadcode/plugins/ruby.rb +++ b/lib/spoom/deadcode/plugins/ruby.rb @@ -34,6 +34,9 @@ def on_send(send) if last_arg.is_a?(Prism::SymbolNode) || last_arg.is_a?(Prism::StringNode) @index.reference_method(last_arg.unescaped, send.location) end + when "method" + arg = send.args.first + @index.reference_method(arg.unescaped, send.location) if arg.is_a?(Prism::SymbolNode) end end diff --git a/test/spoom/deadcode/plugins/ruby_test.rb b/test/spoom/deadcode/plugins/ruby_test.rb index 2468bed4..85b648cd 100644 --- a/test/spoom/deadcode/plugins/ruby_test.rb +++ b/test/spoom/deadcode/plugins/ruby_test.rb @@ -163,6 +163,23 @@ def test_alive_constants_with_const_source_location assert_dead(index, "DEAD") end + def test_alive_methods_with_method + @project.write!("foo.rb", <<~RB) + def alive1; end + def alive2; end + + def dead; end + + method :alive1 + [].map(&method(:alive2)) + RB + + index = index_with_plugins + assert_alive(index, "alive1") + assert_alive(index, "alive2") + assert_dead(index, "dead") + end + private sig { returns(Deadcode::Index) }