From 1528d3c0196bfc5702d54b86dc1bb5f31033b9d9 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Fri, 7 Jun 2024 15:27:21 -0400 Subject: [PATCH] Handle chomped bytesize with lines without newlines --- lib/prism/translation/parser/compiler.rb | 11 +++++++++-- test/prism/ruby/parser_test.rb | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/prism/translation/parser/compiler.rb b/lib/prism/translation/parser/compiler.rb index cbec2bb062e..48f3d4db5cb 100644 --- a/lib/prism/translation/parser/compiler.rb +++ b/lib/prism/translation/parser/compiler.rb @@ -2040,6 +2040,13 @@ def visit_block(call, block) end end + # The parser gem automatically converts \r\n to \n, meaning our offsets + # need to be adjusted to always subtract 1 from the length. + def chomped_bytesize(line) + chomped = line.chomp + chomped.bytesize + (chomped == line ? 0 : 1) + end + # Visit a heredoc that can be either a string or an xstring. def visit_heredoc(node) children = Array.new @@ -2066,14 +2073,14 @@ def visit_heredoc(node) if node.opening.end_with?("'") escaped.each do |line| escaped_lengths << line.bytesize - normalized_lengths << (line.chomp.bytesize + 1) + normalized_lengths << chomped_bytesize(line) end else escaped .chunk_while { |before, after| before.match?(/(?