From c037f5a28c54b86139ef17db061fdbf7dc82fd32 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] [ruby/prism] Fix parser translator ast for heredoc with written newlines Heredocs that contain "\\n" don't start a new string node. https://github.com/ruby/prism/commit/61d9d3a15e --- lib/prism/translation/parser/compiler.rb | 32 ++++++++++++++++++++---- test/prism/ruby/parser_test.rb | 2 -- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/lib/prism/translation/parser/compiler.rb b/lib/prism/translation/parser/compiler.rb index d66e553fa3..0a95e44f85 100644 --- a/lib/prism/translation/parser/compiler.rb +++ b/lib/prism/translation/parser/compiler.rb @@ -2079,27 +2079,49 @@ module Prism 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?(/(?