Skip to content

Commit 4e0f703

Browse files
committedFeb 26, 2024
Fix xstring heredoc parser translator
1 parent 14a636d commit 4e0f703

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed
 

‎lib/prism/node_ext.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,34 @@ class InterpolatedXStringNode < Node
4949

5050
class StringNode < Node
5151
include HeredocQuery
52+
53+
# Occasionally it's helpful to treat a string as if it were interpolated so
54+
# that there's a consistent interface for working with strings.
55+
def to_interpolated
56+
InterpolatedStringNode.new(
57+
source,
58+
opening_loc,
59+
[copy(opening_loc: nil, closing_loc: nil, location: content_loc)],
60+
closing_loc,
61+
location
62+
)
63+
end
5264
end
5365

5466
class XStringNode < Node
5567
include HeredocQuery
68+
69+
# Occasionally it's helpful to treat a string as if it were interpolated so
70+
# that there's a consistent interface for working with strings.
71+
def to_interpolated
72+
InterpolatedXStringNode.new(
73+
source,
74+
opening_loc,
75+
[StringNode.new(source, 0, nil, content_loc, nil, unescaped, content_loc)],
76+
closing_loc,
77+
location
78+
)
79+
end
5680
end
5781

5882
private_constant :HeredocQuery

‎lib/prism/translation/parser/compiler.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ def visit_interpolated_regular_expression_node(node)
942942
# "foo #{bar}"
943943
# ^^^^^^^^^^^^
944944
def visit_interpolated_string_node(node)
945-
if node.opening&.start_with?("<<")
945+
if node.heredoc?
946946
children, closing = visit_heredoc(node)
947947
builder.string_compose(token(node.opening_loc), children, closing)
948948
else
@@ -967,7 +967,7 @@ def visit_interpolated_symbol_node(node)
967967
# `foo #{bar}`
968968
# ^^^^^^^^^^^^
969969
def visit_interpolated_x_string_node(node)
970-
if node.opening.start_with?("<<")
970+
if node.heredoc?
971971
children, closing = visit_heredoc(node)
972972
builder.xstring_compose(token(node.opening_loc), children, closing)
973973
else
@@ -1485,8 +1485,8 @@ def visit_statements_node(node)
14851485
# "foo"
14861486
# ^^^^^
14871487
def visit_string_node(node)
1488-
if node.opening&.start_with?("<<")
1489-
children, closing = visit_heredoc(InterpolatedStringNode.new(node.send(:source), node.opening_loc, [node.copy(opening_loc: nil, closing_loc: nil, location: node.content_loc)], node.closing_loc, node.location))
1488+
if node.heredoc?
1489+
children, closing = visit_heredoc(node.to_interpolated)
14901490
builder.string_compose(token(node.opening_loc), children, closing)
14911491
elsif node.opening == "?"
14921492
builder.character([node.unescaped, srange(node.location)])
@@ -1646,8 +1646,8 @@ def visit_while_node(node)
16461646
# `foo`
16471647
# ^^^^^
16481648
def visit_x_string_node(node)
1649-
if node.opening&.start_with?("<<")
1650-
children, closing = visit_heredoc(InterpolatedXStringNode.new(node.opening_loc, [StringNode.new(0, nil, node.content_loc, nil, node.unescaped, node.content_loc)], node.closing_loc, node.location))
1649+
if node.heredoc?
1650+
children, closing = visit_heredoc(node.to_interpolated)
16511651
builder.xstring_compose(token(node.opening_loc), children, closing)
16521652
else
16531653
builder.xstring_compose(

0 commit comments

Comments
 (0)