[ruby/prism] Fix xstring heredoc parser translator

https://github.com/ruby/prism/commit/4e0f703975
This commit is contained in:
Kevin Newton 2024-02-26 09:35:52 -05:00 committed by git
parent 99d0f687fc
commit 1ce3d9acbf
2 changed files with 30 additions and 6 deletions

View File

@ -49,10 +49,34 @@ module Prism
class StringNode < Node class StringNode < Node
include HeredocQuery include HeredocQuery
# Occasionally it's helpful to treat a string as if it were interpolated so
# that there's a consistent interface for working with strings.
def to_interpolated
InterpolatedStringNode.new(
source,
opening_loc,
[copy(opening_loc: nil, closing_loc: nil, location: content_loc)],
closing_loc,
location
)
end
end end
class XStringNode < Node class XStringNode < Node
include HeredocQuery include HeredocQuery
# Occasionally it's helpful to treat a string as if it were interpolated so
# that there's a consistent interface for working with strings.
def to_interpolated
InterpolatedXStringNode.new(
source,
opening_loc,
[StringNode.new(source, 0, nil, content_loc, nil, unescaped, content_loc)],
closing_loc,
location
)
end
end end
private_constant :HeredocQuery private_constant :HeredocQuery

View File

@ -942,7 +942,7 @@ module Prism
# "foo #{bar}" # "foo #{bar}"
# ^^^^^^^^^^^^ # ^^^^^^^^^^^^
def visit_interpolated_string_node(node) def visit_interpolated_string_node(node)
if node.opening&.start_with?("<<") if node.heredoc?
children, closing = visit_heredoc(node) children, closing = visit_heredoc(node)
builder.string_compose(token(node.opening_loc), children, closing) builder.string_compose(token(node.opening_loc), children, closing)
else else
@ -967,7 +967,7 @@ module Prism
# `foo #{bar}` # `foo #{bar}`
# ^^^^^^^^^^^^ # ^^^^^^^^^^^^
def visit_interpolated_x_string_node(node) def visit_interpolated_x_string_node(node)
if node.opening.start_with?("<<") if node.heredoc?
children, closing = visit_heredoc(node) children, closing = visit_heredoc(node)
builder.xstring_compose(token(node.opening_loc), children, closing) builder.xstring_compose(token(node.opening_loc), children, closing)
else else
@ -1485,8 +1485,8 @@ module Prism
# "foo" # "foo"
# ^^^^^ # ^^^^^
def visit_string_node(node) def visit_string_node(node)
if node.opening&.start_with?("<<") if node.heredoc?
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)) children, closing = visit_heredoc(node.to_interpolated)
builder.string_compose(token(node.opening_loc), children, closing) builder.string_compose(token(node.opening_loc), children, closing)
elsif node.opening == "?" elsif node.opening == "?"
builder.character([node.unescaped, srange(node.location)]) builder.character([node.unescaped, srange(node.location)])
@ -1646,8 +1646,8 @@ module Prism
# `foo` # `foo`
# ^^^^^ # ^^^^^
def visit_x_string_node(node) def visit_x_string_node(node)
if node.opening&.start_with?("<<") if node.heredoc?
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)) children, closing = visit_heredoc(node.to_interpolated)
builder.xstring_compose(token(node.opening_loc), children, closing) builder.xstring_compose(token(node.opening_loc), children, closing)
else else
builder.xstring_compose( builder.xstring_compose(