Skip to content

Commit 1528d3c

Browse files
committed
Handle chomped bytesize with lines without newlines
1 parent d218e65 commit 1528d3c

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

lib/prism/translation/parser/compiler.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2040,6 +2040,13 @@ def visit_block(call, block)
20402040
end
20412041
end
20422042

2043+
# The parser gem automatically converts \r\n to \n, meaning our offsets
2044+
# need to be adjusted to always subtract 1 from the length.
2045+
def chomped_bytesize(line)
2046+
chomped = line.chomp
2047+
chomped.bytesize + (chomped == line ? 0 : 1)
2048+
end
2049+
20432050
# Visit a heredoc that can be either a string or an xstring.
20442051
def visit_heredoc(node)
20452052
children = Array.new
@@ -2066,14 +2073,14 @@ def visit_heredoc(node)
20662073
if node.opening.end_with?("'")
20672074
escaped.each do |line|
20682075
escaped_lengths << line.bytesize
2069-
normalized_lengths << (line.chomp.bytesize + 1)
2076+
normalized_lengths << chomped_bytesize(line)
20702077
end
20712078
else
20722079
escaped
20732080
.chunk_while { |before, after| before.match?(/(?<!\\)\\\r?\n$/) }
20742081
.each do |lines|
20752082
escaped_lengths << lines.sum(&:bytesize)
2076-
normalized_lengths << lines.sum { |line| line.chomp.bytesize + 1 }
2083+
normalized_lengths << lines.sum { |line| chomped_bytesize(line) }
20772084
end
20782085
end
20792086

test/prism/ruby/parser_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ class ParserTest < TestCase
5959
# These files are either failing to parse or failing to translate, so we'll
6060
# skip them for now.
6161
skip_all = skip_incorrect | [
62-
"dash_heredocs.txt",
6362
"regex.txt",
6463
"regex_char_width.txt",
6564
"unescaping.txt",
@@ -86,6 +85,7 @@ class ParserTest < TestCase
8685
# output expected by the parser gem, so we'll skip them for now.
8786
skip_tokens = [
8887
"comments.txt",
88+
"dash_heredocs.txt",
8989
"dos_endings.txt",
9090
"embdoc_no_newline_at_end.txt",
9191
"heredoc_with_comment.txt",

0 commit comments

Comments
 (0)