Skip to content

Commit 0b60feb

Browse files
committed
Guard modifier removal to statement calls
Assisted-By: devx/eb25d669-b4ee-4e28-9b69-129b45e1e178
1 parent 0a390c2 commit 0b60feb

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

lib/spoom/deadcode/remover.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ def apply_edit
7474
#
7575
# Modifiers are matched structurally rather than from a fixed list, so user-defined ones are
7676
# handled too. A call only counts as a modifier when the `def` is its sole argument and it takes
77-
# no block, so we never remove a call that does more than wrap the method (e.g.
78-
# `register(:thing, def foo; end)`).
77+
# no block and is a standalone statement, so we never remove a call that does more than wrap
78+
# the method (e.g. `register(:thing, def foo; end)` or `FOO = register(def foo; end)`).
7979
#: (Prism::DefNode def_node) -> NodeContext?
8080
def modifier_call_context(def_node)
8181
wrapped = def_node #: Prism::Node
@@ -103,6 +103,7 @@ def modifier_call_context(def_node)
103103
end
104104
end
105105
return unless outer_call && outer_nesting
106+
return unless outer_nesting.last.is_a?(Prism::StatementsNode)
106107

107108
NodeContext.new(@old_source, @node_context.comments, outer_call, outer_nesting)
108109
end

test/spoom/deadcode/remover_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,29 @@ def keep; end
11201120
RB
11211121
end
11221122

1123+
def test_keeps_parent_expression_when_sole_argument_call_is_not_a_statement
1124+
res = remove(<<~RB, "bar")
1125+
class Foo
1126+
FOO = register(
1127+
def bar
1128+
x
1129+
end
1130+
)
1131+
1132+
def keep; end
1133+
end
1134+
RB
1135+
1136+
assert_equal(<<~RB, res)
1137+
class Foo
1138+
FOO = register(
1139+
)
1140+
1141+
def keep; end
1142+
end
1143+
RB
1144+
end
1145+
11231146
def test_removes_node_sig_and_comments
11241147
res = remove(<<~RB, "bar")
11251148
class Foo

0 commit comments

Comments
 (0)