Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #504 from Shopify/action-mailer-preview-plugin
Add plugin to ignore ActionMailer::Preview classes
- Loading branch information
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# 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 MyDirectInheritanceMailerPreview < ActionMailer::Preview; end | ||
class MyIndirectInheritanceMailerPreview < MyDirectInheritanceMailerPreview; 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 | ||
# assert_ignored(index, "MyIndirectInheritanceMailerPreview") ideally we'd want this, but seems tricky to do | ||
refute_ignored(index, "Foo") | ||
refute_ignored(index, "bar") | ||
|
||
# Document current less then ideal behavior: | ||
assert_ignored(index, "some_unused_private_method") | ||
refute_ignored(index, "MyIndirectInheritanceMailerPreview") | ||
end | ||
|
||
private | ||
|
||
sig { returns(Index) } | ||
def index_with_plugins | ||
deadcode_index(plugins: [ActionMailerPreview.new]) | ||
end | ||
end | ||
end | ||
end | ||
end |