Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make dead code indexing use a Model to index classes #562

Merged
merged 3 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 9 additions & 9 deletions lib/spoom/deadcode/plugins/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,26 +159,26 @@ def internal_on_define_accessor(indexer, definition)
#
# ~~~rb
# class MyPlugin < Spoom::Deadcode::Plugins::Base
# def on_define_class(indexer, definition)
# definition.ignored! if definition.name == "Foo"
# def on_define_class(symbol_def, definition)
# definition.ignored! if symbol_def.name == "Foo"
# end
# end
# ~~~
sig { params(indexer: Indexer, definition: Definition).void }
def on_define_class(indexer, definition)
sig { params(symbol_def: Model::Class, definition: Definition).void }
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be abstract?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We don't want to crash when calling on_define_class to a plugin that do nothing with it, we just want it to no-op.

We _could_refactor this to have multiple interfaces:

module ClassPlugin
  interface!

  sig { abstract.void }
  def on_define_class; end

and then let the plugins cherry pick the interfaces they want to implement.

But in practice I just rely on YJIT to optimize this empty calls away.

def on_define_class(symbol_def, definition)
# no-op
end

# Do not override this method, use `on_define_class` instead.
sig { params(indexer: Indexer, definition: Definition).void }
def internal_on_define_class(indexer, definition)
if ignored_class_name?(definition.name)
sig { params(symbol_def: Model::Class, definition: Definition).void }
def internal_on_define_class(symbol_def, definition)
if ignored_class_name?(symbol_def.name)
definition.ignored!
elsif ignored_subclass?(indexer.nesting_class_superclass_name)
elsif ignored_subclass?(symbol_def.superclass_name&.delete_prefix("::"))
definition.ignored!
end

on_define_class(indexer, definition)
on_define_class(symbol_def, definition)
end

# Called when a constant is defined.
Expand Down
6 changes: 3 additions & 3 deletions lib/spoom/deadcode/plugins/namespaces.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ module Plugins
class Namespaces < Base
extend T::Sig

sig { override.params(indexer: Indexer, definition: Definition).void }
def on_define_class(indexer, definition)
definition.ignored! if used_as_namespace?(indexer)
sig { override.params(symbol_def: Model::Class, definition: Definition).void }
def on_define_class(symbol_def, definition)
definition.ignored! unless symbol_def.children.empty?
end

sig { override.params(indexer: Indexer, definition: Definition).void }
Expand Down
6 changes: 3 additions & 3 deletions lib/spoom/deadcode/plugins/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class Rails < Base

ignore_constants_named("APP_PATH", "ENGINE_PATH", "ENGINE_ROOT")

sig { override.params(indexer: Indexer, definition: Definition).void }
def on_define_class(indexer, definition)
definition.ignored! if file_is_helper?(indexer)
sig { override.params(symbol_def: Model::Class, definition: Definition).void }
def on_define_class(symbol_def, definition)
definition.ignored! if symbol_def.location.file.match?(%r{app/helpers/.*\.rb$})
end

sig { override.params(indexer: Indexer, definition: Definition).void }
Expand Down
4 changes: 2 additions & 2 deletions test/spoom/deadcode/plugins/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def on_define_accessor(indexer, definition)

def test_on_define_class
plugin = Class.new(Base) do
def on_define_class(indexer, definition)
definition.ignored! if definition.name == "Class1"
def on_define_class(symbol_def, definition)
definition.ignored! if symbol_def.name == "Class1"
end
end

Expand Down
Loading