From 561914f99b7817c3411441731584f8f3e6f92faf Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Tue, 7 Jan 2025 15:56:39 +0100 Subject: [PATCH] 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) --- lib/prism/translation/parser/compiler.rb | 26 +++++++++++------------- test/prism/ruby/parser_test.rb | 1 - 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/lib/prism/translation/parser/compiler.rb b/lib/prism/translation/parser/compiler.rb index 54e08eb991..cff1824681 100644 --- a/lib/prism/translation/parser/compiler.rb +++ b/lib/prism/translation/parser/compiler.rb @@ -1085,24 +1085,22 @@ def visit_interpolated_string_node(node) return visit_heredoc(node) { |children, closing| builder.string_compose(token(node.opening_loc), children, closing) } end - parts = if node.parts.one? { |part| part.type == :string_node } - node.parts.flat_map do |node| - if node.type == :string_node && node.unescaped.lines.count >= 2 - start_offset = node.content_loc.start_offset + parts = node.parts.flat_map do |node| + # When the content of a string node is split across multiple lines, the + # parser gem creates individual string nodes for each line the content is part of. + if node.type == :string_node && node.content.include?("\n") && node.opening_loc.nil? + start_offset = node.content_loc.start_offset - node.unescaped.lines.map do |line| - end_offset = start_offset + line.bytesize - offsets = srange_offsets(start_offset, end_offset) - start_offset = end_offset + node.unescaped.lines.map do |line| + end_offset = start_offset + line.bytesize + offsets = srange_offsets(start_offset, end_offset) + start_offset = end_offset - builder.string_internal([line, offsets]) - end - else - visit(node) + builder.string_internal([line, offsets]) end + else + visit(node) end - else - visit_all(node.parts) end builder.string_compose( diff --git a/test/prism/ruby/parser_test.rb b/test/prism/ruby/parser_test.rb index 55671cc72e..08a5c23a8e 100644 --- a/test/prism/ruby/parser_test.rb +++ b/test/prism/ruby/parser_test.rb @@ -71,7 +71,6 @@ class ParserTest < TestCase "seattlerb/pctW_lineno.txt", "seattlerb/regexp_esc_C_slash.txt", "unparser/corpus/literal/literal.txt", - "unparser/corpus/semantic/dstr.txt", "whitequark/parser_slash_slash_n_escaping_in_literals.txt", ]