Skip to content

Commit

Permalink
Pass SymbolDefs to plugin on_constant
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Terrasa <[email protected]>
  • Loading branch information
Morriar committed Jun 20, 2024
1 parent d8af777 commit 074d408
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 41 deletions.
16 changes: 8 additions & 8 deletions lib/spoom/deadcode/plugins/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,22 +189,22 @@ def internal_on_define_class(symbol_def, definition)
#
# ~~~rb
# class MyPlugin < Spoom::Deadcode::Plugins::Base
# def on_define_constant(indexer, definition)
# definition.ignored! if definition.name == "FOO"
# def on_define_constant(symbol_def, definition)
# definition.ignored! if symbol_def.name == "FOO"
# end
# end
# ~~~
sig { params(indexer: Indexer, definition: Definition).void }
def on_define_constant(indexer, definition)
sig { params(symbol_def: Model::Constant, definition: Definition).void }
def on_define_constant(symbol_def, definition)
# no-op
end

# Do not override this method, use `on_define_constant` instead.
sig { params(indexer: Indexer, definition: Definition).void }
def internal_on_define_constant(indexer, definition)
definition.ignored! if ignored_constant_name?(definition.name)
sig { params(symbol_def: Model::Constant, definition: Definition).void }
def internal_on_define_constant(symbol_def, definition)
definition.ignored! if ignored_constant_name?(symbol_def.name)

on_define_constant(indexer, definition)
on_define_constant(symbol_def, definition)
end

# Called when a method is defined.
Expand Down
17 changes: 9 additions & 8 deletions lib/spoom/deadcode/plugins/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ class Rubocop < Base
/^(::)?RuboCop::Cop::Base$/,
)

sig { override.params(indexer: Indexer, definition: Definition).void }
def on_define_constant(indexer, definition)
definition.ignored! if rubocop_constant?(indexer, definition)
sig { override.params(symbol_def: Model::Constant, definition: Definition).void }
def on_define_constant(symbol_def, definition)
owner = symbol_def.owner
return false unless owner.is_a?(Model::Class)

superclass_name = owner.superclass_name
return false unless superclass_name

definition.ignored! if ignored_subclass?(superclass_name) && RUBOCOP_CONSTANTS.include?(symbol_def.name)
end

sig { override.params(indexer: Indexer, definition: Definition).void }
Expand All @@ -26,11 +32,6 @@ def on_define_method(indexer, definition)

private

sig { params(indexer: Indexer, definition: Definition).returns(T::Boolean) }
def rubocop_constant?(indexer, definition)
ignored_subclass?(indexer.nesting_class_superclass_name) && RUBOCOP_CONSTANTS.include?(definition.name)
end

sig { params(indexer: Indexer, definition: Definition).returns(T::Boolean) }
def rubocop_method?(indexer, definition)
ignored_subclass?(indexer.nesting_class_superclass_name) && definition.name == "on_send"
Expand Down
28 changes: 14 additions & 14 deletions lib/spoom/deadcode/plugins/sorbet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ module Plugins
class Sorbet < Base
extend T::Sig

sig { override.params(indexer: Indexer, definition: Definition).void }
def on_define_constant(indexer, definition)
definition.ignored! if sorbet_type_member?(indexer, definition) || sorbet_enum_constant?(indexer, definition)
sig { override.params(symbol_def: Model::Constant, definition: Definition).void }
def on_define_constant(symbol_def, definition)
definition.ignored! if sorbet_type_member?(symbol_def) || sorbet_enum_constant?(symbol_def)
end

sig { override.params(indexer: Indexer, definition: Definition).void }
Expand All @@ -19,20 +19,20 @@ def on_define_method(indexer, definition)

private

sig { params(indexer: Indexer, definition: Definition).returns(T::Boolean) }
def sorbet_type_member?(indexer, definition)
assign = indexer.nesting_node(Prism::ConstantWriteNode)
return false unless assign
sig { params(symbol_def: Model::Constant).returns(T::Boolean) }
def sorbet_type_member?(symbol_def)
symbol_def.value.match?(/^(type_member|type_template)/)
end

value = assign.value
return false unless value.is_a?(Prism::CallNode)
sig { params(symbol_def: Model::Constant).returns(T::Boolean) }
def sorbet_enum_constant?(symbol_def)
owner = symbol_def.owner
return false unless owner.is_a?(Model::Class)

value.name == :type_member || value.name == :type_template
end
superclass_name = owner.superclass_name
return false unless superclass_name

sig { params(indexer: Indexer, definition: Definition).returns(T::Boolean) }
def sorbet_enum_constant?(indexer, definition)
/^(::)?T::Enum$/.match?(indexer.nesting_class_superclass_name) && indexer.nesting_call&.name == :enums
superclass_name.match?(/^(::)?T::Enum$/)
end
end
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 @@ -48,8 +48,8 @@ class Class2; end

def test_on_define_constant
plugin = Class.new(Base) do
def on_define_constant(indexer, definition)
definition.ignored! if definition.name == "CONST1"
def on_define_constant(symbol_def, definition)
definition.ignored! if symbol_def.name == "CONST1"
end
end

Expand Down
18 changes: 9 additions & 9 deletions test/spoom/deadcode/plugins/sorbet_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,17 @@ def test_ignore_sorbet_enum_constants
DEAD1 = new
end
class BadEnum < T::Enum
something do
DEAD2 = new
end
end
class BadEnum < T::Enum
class BadEnum
enums do
DEAD3 = new
DEAD2 = new
end
end
end
class Foo
enums do
DEAD4 = new
DEAD3 = new
end
end
Expand All @@ -66,17 +60,23 @@ class OtherEnum < ::T::Enum
end
end
end
class BadEnum < T::Enum
something do
IGNORED5 = new
end
end
RB

index = index_with_plugins
assert_ignored(index, "IGNORED1")
assert_ignored(index, "IGNORED2")
assert_ignored(index, "IGNORED3")
assert_ignored(index, "IGNORED4")
assert_ignored(index, "IGNORED5")
refute_ignored(index, "DEAD1")
refute_ignored(index, "DEAD2")
refute_ignored(index, "DEAD3")
refute_ignored(index, "DEAD4")
end

def test_ignore_sorbet_overrides
Expand Down

0 comments on commit 074d408

Please sign in to comment.