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 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
7 changes: 5 additions & 2 deletions lib/spoom/cli/deadcode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,16 @@ def deadcode(*paths)
$stderr.puts
end

index = Spoom::Deadcode::Index.new
model = Spoom::Model.new
index = Spoom::Deadcode::Index.new(model)

$stderr.puts "Indexing #{blue(files.size.to_s)} files..."
files.each do |file|
content = File.read(file)
content = Spoom::Deadcode::ERB.new(content).src if file.end_with?(".erb")

tree = Spoom.parse_ruby(content, file: file)
Spoom::Model::Builder.new(model, file).visit(tree)
Spoom::Deadcode.index_node(index, tree, content, file: file, plugins: plugins)
rescue ParseError => e
say_error("Error parsing #{file}: #{e.message}")
Expand All @@ -97,6 +99,8 @@ def deadcode(*paths)
next
end

index.finalize!(plugins: plugins)

if options[:show_defs]
$stderr.puts "\nDefinitions:"
index.definitions.each do |name, definitions|
Expand All @@ -123,7 +127,6 @@ def deadcode(*paths)
references_count = index.references.size.to_s
$stderr.puts "Analyzing #{blue(definitions_count)} definitions against #{blue(references_count)} references..."

index.finalize!
dead = index.definitions.values.flatten.select(&:dead?)

if options[:sort] == "name"
Expand Down
1 change: 1 addition & 0 deletions lib/spoom/deadcode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def index_node(index, node, ruby, file:, plugins: [])
sig { params(index: Index, ruby: String, file: String, plugins: T::Array[Deadcode::Plugins::Base]).void }
def index_ruby(index, ruby, file:, plugins: [])
node = Spoom.parse_ruby(ruby, file: file)
Model::Builder.new(index.model, file).visit(node)
index_node(index, node, ruby, file: file, plugins: plugins)
end

Expand Down
27 changes: 23 additions & 4 deletions lib/spoom/deadcode/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ module Deadcode
class Index
extend T::Sig

sig { returns(Model) }
attr_reader :model

sig { returns(T::Hash[String, T::Array[Definition]]) }
attr_reader :definitions

sig { returns(T::Hash[String, T::Array[Reference]]) }
attr_reader :references

sig { void }
def initialize
sig { params(model: Model).void }
def initialize(model)
@model = model
@definitions = T.let({}, T::Hash[String, T::Array[Definition]])
@references = T.let({}, T::Hash[String, T::Array[Reference]])
end
Expand All @@ -33,8 +37,23 @@ def reference(reference)
# Mark all definitions having a reference of the same name as `alive`
#
# To be called once all the files have been indexed and all the definitions and references discovered.
sig { void }
def finalize!
sig { params(plugins: T::Array[Plugins::Base]).void }
def finalize!(plugins: [])
@model.symbols.each do |_full_name, symbol|
symbol.definitions.each do |symbol_def|
case symbol_def
when Model::Class
definition = Definition.new(
kind: Definition::Kind::Class,
name: symbol.name,
full_name: symbol.full_name,
location: symbol_def.location,
)
define(definition)
plugins.each { |plugin| plugin.internal_on_define_class(symbol_def, definition) }
end
end
end
@references.keys.each do |name|
definitions_for_name(name).each(&:alive!)
end
Expand Down
15 changes: 0 additions & 15 deletions lib/spoom/deadcode/indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ def visit_class_node(node)
@names_nesting.clear
@names_nesting << full_name

define_class(T.must(constant_path.split("::").last), full_name, node)

# We do not call `super` here because we don't want to visit the `constant` again
visit(node.superclass) if node.superclass
visit(node.body)
Expand All @@ -123,7 +121,6 @@ def visit_class_node(node)
@names_nesting = old_nesting
else
@names_nesting << constant_path
define_class(T.must(constant_path.split("::").last), @names_nesting.join("::"), node)

# We do not call `super` here because we don't want to visit the `constant` again
visit(node.superclass) if node.superclass
Expand Down Expand Up @@ -351,18 +348,6 @@ def define_attr_writer(name, full_name, node)
@plugins.each { |plugin| plugin.internal_on_define_accessor(self, definition) }
end

sig { params(name: String, full_name: String, node: Prism::Node).void }
def define_class(name, full_name, node)
definition = Definition.new(
kind: Definition::Kind::Class,
name: name,
full_name: full_name,
location: node_location(node),
)
@index.define(definition)
@plugins.each { |plugin| plugin.internal_on_define_class(self, definition) }
end

sig { params(name: String, full_name: String, node: Prism::Node).void }
def define_constant(name, full_name, node)
definition = Definition.new(
Expand Down
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
5 changes: 3 additions & 2 deletions test/helpers/deadcode_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def deadcode_index(plugins: [])
allow_mime_types: ["text/x-ruby", "text/x-ruby-script"],
)

index = Deadcode::Index.new
model = Model.new
index = Deadcode::Index.new(model)

files.each do |file|
content = project.read(file)
Expand All @@ -32,7 +33,7 @@ def deadcode_index(plugins: [])
end
end

index.finalize!
index.finalize!(plugins: plugins)
index
end

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
8 changes: 5 additions & 3 deletions test/spoom/deadcode/remover_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1611,11 +1611,13 @@ def foo

sig { params(ruby_string: String, def_name: String).returns(String) }
def remove(ruby_string, def_name)
file = "file.rb"
context = Context.mktmp!
context.write!("file.rb", ruby_string)
context.write!(file, ruby_string)

index = Index.new
Deadcode.index_ruby(index, ruby_string, file: "file.rb")
model = Model.new
index = Index.new(model)
Deadcode.index_ruby(index, ruby_string, file: file)
index.finalize!

definitions = definitions_for_name(index, def_name)
Expand Down
Loading