Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/spoom/deadcode/plugins/active_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@ module Plugins
class ActiveJob < Base
ignore_classes_named("ApplicationJob")
ignore_methods_named("perform", "build_enumerator", "each_iteration")

CALLBACKS = [
"after_enqueue",
"after_perform",
"around_enqueue",
"around_perform",
"before_enqueue",
"before_perform",
].freeze

# @override
#: (Send send) -> void
def on_send(send)
return unless send.recv.nil? && CALLBACKS.include?(send.name)

send.each_arg(Prism::SymbolNode) do |arg|

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it probably accepts strings too if you want to be exhaustive

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find any occurrence using a String in our monolith. I'll stick to symbols for now, I can add strings support late 👍

@index.reference_method(arg.unescaped, send.location)
end
end
end
end
end
Expand Down
8 changes: 7 additions & 1 deletion rbi/spoom.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,13 @@ class Spoom::Deadcode::Plugins::ActionPack < ::Spoom::Deadcode::Plugins::Base
end

Spoom::Deadcode::Plugins::ActionPack::CALLBACKS = T.let(T.unsafe(nil), Array)
class Spoom::Deadcode::Plugins::ActiveJob < ::Spoom::Deadcode::Plugins::Base; end

class Spoom::Deadcode::Plugins::ActiveJob < ::Spoom::Deadcode::Plugins::Base
sig { override.params(send: ::Spoom::Deadcode::Send).void }
def on_send(send); end
end

Spoom::Deadcode::Plugins::ActiveJob::CALLBACKS = T.let(T.unsafe(nil), Array)

class Spoom::Deadcode::Plugins::ActiveModel < ::Spoom::Deadcode::Plugins::Base
sig { override.params(send: ::Spoom::Deadcode::Send).void }
Expand Down
30 changes: 30 additions & 0 deletions test/spoom/deadcode/plugins/active_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,36 @@ def perform; end
)
end

def test_ignore_active_job_callbacks
@project.write!("app/jobs/foo.rb", <<~RB)
class FooJob
after_enqueue(:ignored1)
after_perform(:ignored2)
around_enqueue(:ignored3)
around_perform(:ignored4)
before_enqueue(:ignored5)
before_perform(:ignored6)

def ignored1; end
def ignored2; end
def ignored3; end
def ignored4; end
def ignored5; end
def ignored6; end
def dead; end
end
RB

index = index_with_plugins
assert_alive(index, "ignored1")
assert_alive(index, "ignored2")
assert_alive(index, "ignored3")
assert_alive(index, "ignored4")
assert_alive(index, "ignored5")
assert_alive(index, "ignored6")
assert_dead(index, "dead")
end

private

#: -> Deadcode::Index
Expand Down