Skip to content

Commit df68189

Browse files
authored
Merge pull request #543 from Shopify/at-fix-deadcode-removal
Fix removal of def nodes with a rescue
2 parents c5d28a5 + b0b2434 commit df68189

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

lib/spoom/deadcode/remover.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ def find(source, location, kind)
564564
raise ParserError, "Error while parsing #{location.file}: #{message}"
565565
end
566566

567-
visitor = new(location)
567+
visitor = new(location, kind)
568568
visitor.visit(result.value)
569569

570570
node = visitor.node
@@ -617,10 +617,11 @@ def node_match_kind?(node, kind)
617617
sig { returns(T::Array[Prism::Node]) }
618618
attr_reader :nodes_nesting
619619

620-
sig { params(location: Location).void }
621-
def initialize(location)
620+
sig { params(location: Location, kind: T.nilable(Definition::Kind)).void }
621+
def initialize(location, kind)
622622
super()
623623
@location = location
624+
@kind = kind
624625
@node = T.let(nil, T.nilable(Prism::Node))
625626
@nodes_nesting = T.let([], T::Array[Prism::Node])
626627
end
@@ -635,6 +636,9 @@ def visit(node)
635636
# We found the node we're looking for at `@location`
636637
@node = node
637638

639+
# The node we found matches the kind we're looking for, we can stop here
640+
return if @kind && self.class.node_match_kind?(node, @kind)
641+
638642
# There may be a more precise child inside the node that also matches `@location`, let's visit them
639643
@nodes_nesting << node
640644
super(node)

test/spoom/deadcode/remover_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,26 @@ def bar; end
10271027
RB
10281028
end
10291029

1030+
def test_deadcode_remover_removes_method_with_rescue
1031+
res = remove(<<~RB, "foo")
1032+
def bar; end
1033+
1034+
def foo
1035+
puts "foo"
1036+
rescue => error
1037+
puts error
1038+
end
1039+
1040+
def baz; end
1041+
RB
1042+
1043+
assert_equal(<<~RB, res)
1044+
def bar; end
1045+
1046+
def baz; end
1047+
RB
1048+
end
1049+
10301050
def test_deadcode_remover_does_not_remove_singleton_class_if_more_than_one_node
10311051
res = remove(<<~RB, "foo")
10321052
class Foo

0 commit comments

Comments
 (0)