From 61d9d3a15e9b022c7a61d7871f05ad80cf82383c Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Fri, 3 Jan 2025 19:14:15 +0100 Subject: [PATCH] Fix parser translator ast for heredoc with written newlines Heredocs that contain "\\n" don't start a new string node. --- lib/prism/translation/parser/compiler.rb | 34 +++++++++++++++++++----- test/prism/ruby/parser_test.rb | 2 -- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/lib/prism/translation/parser/compiler.rb b/lib/prism/translation/parser/compiler.rb index 4a3cba3de0..3f0294bf62 100644 --- a/lib/prism/translation/parser/compiler.rb +++ b/lib/prism/translation/parser/compiler.rb @@ -2079,27 +2079,49 @@ def visit_heredoc(node) escaped_lengths = [] normalized_lengths = [] + # Keeps track of where an unescaped line should start a new token. An unescaped + # \n would otherwise be indistinguishable from the actual newline at the end of + # of the line. The parser gem only emits a new string node at "real" newlines, + # line continuations don't start a new node as well. + do_next_tokens = [] if node.opening.end_with?("'") escaped.each do |line| escaped_lengths << line.bytesize normalized_lengths << chomped_bytesize(line) + do_next_tokens << true end else escaped - .chunk_while { |before, after| before.match?(/(?