Skip to content

Commit 9640f7b

Browse files
n-rodriguezclaude
andcommitted
Stop emitting a modifier if that guards several statements
`inline` records how the node was written, and a rewrite invalidates it. Replacing the single statement of `foo if bar` with several emitted `a\nb if bar`, where only the last one stays guarded — the rest runs unconditionally. Phlex expands an element into three statements, so a component writing `span(class: "x") { plain content } if flag` opened the tag and called `content` even when `flag` was false, leaving the tag unclosed and running a side effect that should not have happened. 41 failures across an application's suite came from this, second only to the buffer being hoisted out of a capture. A modifier only reads as one when it guards exactly one statement, so that is now the condition for emitting it. The test builds the node by hand: Ruby cannot parse a modifier `if` with more than one statement, so the shape only ever comes from a rewrite. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 303cd0c commit 9640f7b

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

‎lib/refract/formatter.rb‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,12 @@ def format_node(node)
583583
end
584584

585585
visit IfNode do |node|
586-
if node.inline
586+
# `inline` describes how the node was written, and a rewrite can
587+
# invalidate it: replacing the single statement of `foo if bar` with
588+
# several would emit `a\nb if bar`, where only `b` stays guarded — the
589+
# rest runs unconditionally, side effects included. A modifier only
590+
# reads as one when it guards exactly one statement.
591+
if node.inline && node.statements&.body&.size == 1
587592
visit node.statements
588593
push " if "
589594
visit node.predicate

‎test/refract.test.rb‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,31 @@ def foo(...)
567567
RUBY
568568
end
569569

570+
# A modifier `if` cannot be parsed with more than one statement, so this one
571+
# is built by hand: it is the shape a rewrite produces when it replaces the
572+
# single statement of `foo if bar` with several. Emitted as a modifier, only
573+
# the last statement would stay guarded and the rest would run unconditionally.
574+
test "modifier if whose statements were replaced by several" do
575+
node = Refract::IfNode.new(
576+
inline: true,
577+
predicate: Refract::LocalVariableReadNode.new(name: :bar),
578+
statements: Refract::StatementsNode.new(
579+
body: [
580+
Refract::LocalVariableReadNode.new(name: :a),
581+
Refract::LocalVariableReadNode.new(name: :b),
582+
],
583+
),
584+
subsequent: nil,
585+
)
586+
587+
assert_equal Refract::Formatter.new.format_node(node).source, <<~RUBY.strip
588+
if bar
589+
a
590+
b
591+
end
592+
RUBY
593+
end
594+
570595
test "single quoted symbol" do
571596
assert_refract <<~RUBY
572597
:'foo'

0 commit comments

Comments
 (0)