lib/spoom/deadcode/indexer.rb in spoom-1.2.3 vs lib/spoom/deadcode/indexer.rb in spoom-1.2.4
- old
+ new
@@ -155,14 +155,14 @@
define_constant(name, full_name, node)
end
sig { override.params(node: SyntaxTree::DefNode).void }
def visit_def(node)
- super
-
name = node_string(node.name)
define_method(name, [*@names_nesting, name].join("::"), node)
+
+ super
end
sig { override.params(node: SyntaxTree::Field).void }
def visit_field(node)
visit(node.parent)
@@ -228,11 +228,11 @@
define_attr_reader(name, full_name, arg)
define_attr_writer("#{name}=", "#{full_name}=", arg)
end
else
@plugins.each do |plugin|
- plugin.on_send(self, send)
+ plugin.internal_on_send(self, send)
end
reference_method(send.name, send.node)
visit_all(send.args)
visit(send.block)
@@ -284,11 +284,11 @@
name: name,
full_name: full_name,
location: node_location(node),
)
@index.define(definition)
- @plugins.each { |plugin| plugin.on_define_accessor(self, definition) }
+ @plugins.each { |plugin| plugin.internal_on_define_accessor(self, definition) }
end
sig { params(name: String, full_name: String, node: SyntaxTree::Node).void }
def define_attr_writer(name, full_name, node)
definition = Definition.new(
@@ -296,11 +296,11 @@
name: name,
full_name: full_name,
location: node_location(node),
)
@index.define(definition)
- @plugins.each { |plugin| plugin.on_define_accessor(self, definition) }
+ @plugins.each { |plugin| plugin.internal_on_define_accessor(self, definition) }
end
sig { params(name: String, full_name: String, node: SyntaxTree::Node).void }
def define_class(name, full_name, node)
definition = Definition.new(
@@ -308,11 +308,11 @@
name: name,
full_name: full_name,
location: node_location(node),
)
@index.define(definition)
- @plugins.each { |plugin| plugin.on_define_class(self, definition) }
+ @plugins.each { |plugin| plugin.internal_on_define_class(self, definition) }
end
sig { params(name: String, full_name: String, node: SyntaxTree::Node).void }
def define_constant(name, full_name, node)
definition = Definition.new(
@@ -320,11 +320,11 @@
name: name,
full_name: full_name,
location: node_location(node),
)
@index.define(definition)
- @plugins.each { |plugin| plugin.on_define_constant(self, definition) }
+ @plugins.each { |plugin| plugin.internal_on_define_constant(self, definition) }
end
sig { params(name: String, full_name: String, node: SyntaxTree::Node).void }
def define_method(name, full_name, node)
definition = Definition.new(
@@ -332,11 +332,11 @@
name: name,
full_name: full_name,
location: node_location(node),
)
@index.define(definition)
- @plugins.each { |plugin| plugin.on_define_method(self, definition) }
+ @plugins.each { |plugin| plugin.internal_on_define_method(self, definition) }
end
sig { params(name: String, full_name: String, node: SyntaxTree::Node).void }
def define_module(name, full_name, node)
definition = Definition.new(
@@ -344,11 +344,11 @@
name: name,
full_name: full_name,
location: node_location(node),
)
@index.define(definition)
- @plugins.each { |plugin| plugin.on_define_module(self, definition) }
+ @plugins.each { |plugin| plugin.internal_on_define_module(self, definition) }
end
# Reference indexing
sig { params(name: String, node: SyntaxTree::Node).void }
@@ -357,9 +357,77 @@
end
sig { params(name: String, node: SyntaxTree::Node).void }
def reference_method(name, node)
@index.reference(Reference.new(name: name, kind: Reference::Kind::Method, location: node_location(node)))
+ end
+
+ # Context
+
+ sig { returns(SyntaxTree::Node) }
+ def current_node
+ T.must(@nodes_nesting.last)
+ end
+
+ sig { type_parameters(:N).params(type: T::Class[T.type_parameter(:N)]).returns(T.nilable(T.type_parameter(:N))) }
+ def nesting_node(type)
+ @nodes_nesting.reverse_each do |node|
+ return T.unsafe(node) if node.is_a?(type)
+ end
+
+ nil
+ end
+
+ sig { returns(T.nilable(SyntaxTree::ClassDeclaration)) }
+ def nesting_class
+ nesting_node(SyntaxTree::ClassDeclaration)
+ end
+
+ sig { returns(T.nilable(SyntaxTree::BlockNode)) }
+ def nesting_block
+ nesting_node(SyntaxTree::BlockNode)
+ end
+
+ sig { returns(T.nilable(SyntaxTree::MethodAddBlock)) }
+ def nesting_block_call
+ nesting_node(SyntaxTree::MethodAddBlock)
+ end
+
+ sig { returns(T.nilable(String)) }
+ def nesting_block_call_name
+ block = nesting_block_call
+ return unless block.is_a?(SyntaxTree::MethodAddBlock)
+
+ call = block.call
+ case call
+ when SyntaxTree::ARef
+ node_string(call.collection)
+ when SyntaxTree::CallNode, SyntaxTree::Command, SyntaxTree::CommandCall
+ node_string(call.message)
+ end
+ end
+
+ sig { returns(T.nilable(String)) }
+ def nesting_class_name
+ nesting_class = self.nesting_class
+ return unless nesting_class
+
+ node_string(nesting_class.constant)
+ end
+
+ sig { returns(T.nilable(String)) }
+ def nesting_class_superclass_name
+ nesting_class_superclass = nesting_class&.superclass
+ return unless nesting_class_superclass
+
+ node_string(nesting_class_superclass).delete_prefix("::")
+ end
+
+ sig { returns(T.nilable(String)) }
+ def last_sig
+ return unless @previous_node.is_a?(SyntaxTree::MethodAddBlock)
+
+ node_string(@previous_node)
end
# Node utils
sig { params(node: T.any(Symbol, SyntaxTree::Node)).returns(String) }