Skip to content

Commit 5030917

Browse files
committed
Better factoring for heredocs in ripper translation
1 parent 97a031e commit 5030917

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

lib/prism/translation/ripper.rb

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,8 +2056,7 @@ def visit_interpolated_regular_expression_node(node)
20562056
# ^^^^^^^^^^^^
20572057
def visit_interpolated_string_node(node)
20582058
if node.opening&.start_with?("<<~")
2059-
bounds(node.parts.first.location)
2060-
heredoc = visit_heredoc_node(node.parts, on_string_content) { |parts, part| on_string_add(parts, part) }
2059+
heredoc = visit_heredoc_string_node(node)
20612060

20622061
bounds(node.location)
20632062
on_string_literal(heredoc)
@@ -2098,8 +2097,7 @@ def visit_interpolated_symbol_node(node)
20982097
# ^^^^^^^^^^^^
20992098
def visit_interpolated_x_string_node(node)
21002099
if node.opening.start_with?("<<~")
2101-
bounds(node.parts.first.location)
2102-
heredoc = visit_heredoc_node(node.parts, on_xstring_new) { |parts, part| on_xstring_add(parts, part) }
2100+
heredoc = visit_heredoc_x_string_node(node)
21032101

21042102
bounds(node.location)
21052103
on_xstring_literal(heredoc)
@@ -2827,8 +2825,7 @@ def visit_string_node(node)
28272825
bounds(node.location)
28282826
on_CHAR("?#{node.content}")
28292827
elsif opening.start_with?("<<~")
2830-
bounds(node.location)
2831-
heredoc = visit_heredoc_node([node], on_string_content) { |parts, part| on_string_add(parts, part) }
2828+
heredoc = visit_heredoc_string_node(node.to_interpolated)
28322829

28332830
bounds(node.location)
28342831
on_string_literal(heredoc)
@@ -2841,16 +2838,15 @@ def visit_string_node(node)
28412838
end
28422839
end
28432840

2844-
# Visit a string that is expressed using a <<~ heredoc.
2845-
private def visit_heredoc_node(parts, base)
2841+
# Ripper gives back the escaped string content but strips out the common
2842+
# leading whitespace. Prism gives back the unescaped string content and
2843+
# a location for the escaped string content. Unfortunately these don't
2844+
# work well together, so here we need to re-derive the common leading
2845+
# whitespace.
2846+
private def visit_heredoc_node_whitespace(parts)
28462847
common_whitespace = nil
28472848
dedent_next = true
28482849

2849-
# Ripper gives back the escaped string content but strips out the common
2850-
# leading whitespace. Prism gives back the unescaped string content and
2851-
# a location for the escaped string content. Unfortunately these don't
2852-
# work well together, so here we need to re-derive the common leading
2853-
# whitespace.
28542850
parts.each do |part|
28552851
if part.is_a?(StringNode)
28562852
if dedent_next && !(content = part.content).chomp.empty?
@@ -2868,7 +2864,14 @@ def visit_string_node(node)
28682864
end
28692865
end
28702866

2871-
if common_whitespace.nil? || common_whitespace == 0
2867+
common_whitespace || 0
2868+
end
2869+
2870+
# Visit a string that is expressed using a <<~ heredoc.
2871+
private def visit_heredoc_node(parts, base)
2872+
common_whitespace = visit_heredoc_node_whitespace(parts)
2873+
2874+
if common_whitespace == 0
28722875
bounds(parts.first.location)
28732876

28742877
string = []
@@ -2910,6 +2913,20 @@ def visit_string_node(node)
29102913
end
29112914
end
29122915

2916+
private def visit_heredoc_string_node(node)
2917+
bounds(node.location)
2918+
visit_heredoc_node(node.parts, on_string_content) do |parts, part|
2919+
on_string_add(parts, part)
2920+
end
2921+
end
2922+
2923+
private def visit_heredoc_x_string_node(node)
2924+
bounds(node.location)
2925+
visit_heredoc_node(node.parts, on_xstring_new) do |parts, part|
2926+
on_xstring_add(parts, part)
2927+
end
2928+
end
2929+
29132930
# super(foo)
29142931
# ^^^^^^^^^^
29152932
def visit_super_node(node)
@@ -3078,8 +3095,7 @@ def visit_x_string_node(node)
30783095
bounds(node.location)
30793096
on_xstring_literal(on_xstring_new)
30803097
elsif node.opening.start_with?("<<~")
3081-
bounds(node.location)
3082-
heredoc = visit_heredoc_node([node], on_xstring_new) { |parts, part| on_xstring_add(parts, part) }
3098+
heredoc = visit_heredoc_x_string_node(node.to_interpolated)
30833099

30843100
bounds(node.location)
30853101
on_xstring_literal(heredoc)

0 commit comments

Comments
 (0)