Skip to content

Commit 80fe9a1

Browse files
Earlopainkddnewton
authored andcommitted
[ruby/prism] Better handle multiline interpolated strings in the parser translator
Much of this logic should be shared between interpolated symbols and regexps. It's also incorrect when the node contains a literal `\\n` (same as for plain string nodes at the moment) ruby/prism@561914f99b
1 parent 9f38ee1 commit 80fe9a1

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

lib/prism/translation/parser/compiler.rb

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,24 +1085,22 @@ def visit_interpolated_string_node(node)
10851085
return visit_heredoc(node) { |children, closing| builder.string_compose(token(node.opening_loc), children, closing) }
10861086
end
10871087

1088-
parts = if node.parts.one? { |part| part.type == :string_node }
1089-
node.parts.flat_map do |node|
1090-
if node.type == :string_node && node.unescaped.lines.count >= 2
1091-
start_offset = node.content_loc.start_offset
1088+
parts = node.parts.flat_map do |node|
1089+
# When the content of a string node is split across multiple lines, the
1090+
# parser gem creates individual string nodes for each line the content is part of.
1091+
if node.type == :string_node && node.content.include?("\n") && node.opening_loc.nil?
1092+
start_offset = node.content_loc.start_offset
10921093

1093-
node.unescaped.lines.map do |line|
1094-
end_offset = start_offset + line.bytesize
1095-
offsets = srange_offsets(start_offset, end_offset)
1096-
start_offset = end_offset
1094+
node.unescaped.lines.map do |line|
1095+
end_offset = start_offset + line.bytesize
1096+
offsets = srange_offsets(start_offset, end_offset)
1097+
start_offset = end_offset
10971098

1098-
builder.string_internal([line, offsets])
1099-
end
1100-
else
1101-
visit(node)
1099+
builder.string_internal([line, offsets])
11021100
end
1101+
else
1102+
visit(node)
11031103
end
1104-
else
1105-
visit_all(node.parts)
11061104
end
11071105

11081106
builder.string_compose(

test/prism/ruby/parser_test.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ class ParserTest < TestCase
7070
"seattlerb/pctW_lineno.txt",
7171
"seattlerb/regexp_esc_C_slash.txt",
7272
"unparser/corpus/literal/literal.txt",
73-
"unparser/corpus/semantic/dstr.txt",
7473
"whitequark/parser_slash_slash_n_escaping_in_literals.txt",
7574
]
7675

0 commit comments

Comments
 (0)