lib/spoom/deadcode/remover.rb in spoom-1.3.1 vs lib/spoom/deadcode/remover.rb in spoom-1.3.2
- old
+ new
@@ -562,11 +562,11 @@
end.join(" ")
raise ParserError, "Error while parsing #{location.file}: #{message}"
end
- visitor = new(location)
+ visitor = new(location, kind)
visitor.visit(result.value)
node = visitor.node
unless node
raise Error, "Can't find node at #{location}"
@@ -615,14 +615,15 @@
attr_reader :node
sig { returns(T::Array[Prism::Node]) }
attr_reader :nodes_nesting
- sig { params(location: Location).void }
- def initialize(location)
+ sig { params(location: Location, kind: T.nilable(Definition::Kind)).void }
+ def initialize(location, kind)
super()
@location = location
+ @kind = kind
@node = T.let(nil, T.nilable(Prism::Node))
@nodes_nesting = T.let([], T::Array[Prism::Node])
end
sig { override.params(node: T.nilable(Prism::Node)).void }
@@ -632,9 +633,12 @@
location = Location.from_prism(@location.file, node.location)
if location == @location
# We found the node we're looking for at `@location`
@node = node
+
+ # The node we found matches the kind we're looking for, we can stop here
+ return if @kind && self.class.node_match_kind?(node, @kind)
# There may be a more precise child inside the node that also matches `@location`, let's visit them
@nodes_nesting << node
super(node)
@nodes_nesting.pop if @nodes_nesting.last == @node