Skip to content

Commit

Permalink
Add plugin to ignore ActionMailer::Preview classes
Browse files Browse the repository at this point in the history
New plugin that ingores all methods inside ActionMailer::Preview classes
as they are all automatically made available by Rails in the previwer,
but never otherwise referenced.
  • Loading branch information
Korri committed Feb 2, 2024
1 parent 93d60b2 commit 7f74d3f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/spoom/deadcode/plugins.rb
Expand Up @@ -5,6 +5,7 @@

require_relative "plugins/actionpack"
require_relative "plugins/active_job"
require_relative "plugins/action_mailer_preview"
require_relative "plugins/action_mailer"
require_relative "plugins/active_model"
require_relative "plugins/active_record"
Expand Down
19 changes: 19 additions & 0 deletions lib/spoom/deadcode/plugins/action_mailer_preview.rb
@@ -0,0 +1,19 @@
# typed: strict
# frozen_string_literal: true

module Spoom
module Deadcode
module Plugins
class ActionMailerPreview < Base
extend T::Sig

ignore_classes_inheriting_from("ActionMailer::Preview")

sig { override.params(indexer: Indexer, definition: Definition).void }
def on_define_method(indexer, definition)
definition.ignored! if indexer.nesting_class_superclass_name == "ActionMailer::Preview"
end
end
end
end
end
48 changes: 48 additions & 0 deletions test/spoom/deadcode/plugins/action_mailer_preview_test.rb
@@ -0,0 +1,48 @@
# typed: true
# frozen_string_literal: true

require "test_with_project"
require "helpers/deadcode_helper"

module Spoom
module Deadcode
module Plugins
class ActionMailerPreviewTest < TestWithProject
include Test::Helpers::DeadcodeHelper

def test_action_mailer_preview_ignore_direct_inheritance
@project.write!("test/mailers/previews/my_mailer_preview.rb", <<~RB)
class MyMailerPreview < ActionMailer::Preview
def my_email_preview_method; end
private
def some_unused_private_method; end
end
class MyOtherMailerPreview < ::ActionMailer::Preview; end
class Foo
def bar; end
end
RB

index = index_with_plugins
assert_ignored(index, "MyMailerPreview")
assert_ignored(index, "MyOtherMailerPreview")
assert_ignored(index, "my_email_preview_method")
# refute_ignored(index, "some_unused_private_method") ideally we'd want this, but seems tricky to do
refute_ignored(index, "Foo")
refute_ignored(index, "bar")
end

private

sig { returns(Index) }
def index_with_plugins
deadcode_index(plugins: [ActionMailerPreview.new])
end
end
end
end
end

0 comments on commit 7f74d3f

Please sign in to comment.