Skip to content

Commit 4a9a7a6

Browse files
kddnewtonjasonkimHParker
committed
Use correct newlines for heredoc inner lines
Co-authored-by: Jason Kim <jasonkim@github.com> Co-authored-by: Adam Hess <HParker@github.com>
1 parent 8b54e16 commit 4a9a7a6

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

lib/prism/translation/parser/compiler.rb

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,23 +2057,31 @@ def visit_heredoc(node)
20572057
node.parts.each do |part|
20582058
pushing =
20592059
if part.is_a?(StringNode) && part.unescaped.include?("\n")
2060-
unescaped = part.unescaped.lines(chomp: true)
2061-
escaped = part.content.lines(chomp: true)
2060+
unescaped = part.unescaped.lines
2061+
escaped = part.content.lines
20622062

2063-
escaped_lengths =
2064-
if node.opening.end_with?("'")
2065-
escaped.map { |line| line.bytesize + 1 }
2066-
else
2067-
escaped.chunk_while { |before, after| before.match?(/(?<!\\)\\$/) }.map { |line| line.join.bytesize + line.length }
2063+
escaped_lengths = []
2064+
normalized_lengths = []
2065+
2066+
if node.opening.end_with?("'")
2067+
escaped.each do |line|
2068+
escaped_lengths << line.bytesize
2069+
normalized_lengths << (line.chomp.bytesize + 1)
20682070
end
2071+
else
2072+
escaped
2073+
.chunk_while { |before, after| before.match?(/(?<!\\)\\\r?\n$/) }
2074+
.each do |lines|
2075+
escaped_lengths << lines.sum(&:bytesize)
2076+
normalized_lengths << lines.sum { |line| line.chomp.bytesize + 1 }
2077+
end
2078+
end
20692079

20702080
start_offset = part.location.start_offset
2071-
end_offset = nil
20722081

2073-
unescaped.zip(escaped_lengths).map do |unescaped_line, escaped_length|
2074-
end_offset = start_offset + (escaped_length || 0)
2075-
inner_part = builder.string_internal(["#{unescaped_line}\n", srange_offsets(start_offset, end_offset)])
2076-
start_offset = end_offset
2082+
unescaped.map.with_index do |unescaped_line, index|
2083+
inner_part = builder.string_internal([unescaped_line, srange_offsets(start_offset, start_offset + normalized_lengths.fetch(index, 0))])
2084+
start_offset += escaped_lengths.fetch(index, 0)
20772085
inner_part
20782086
end
20792087
else

test/prism/ruby/parser_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,12 @@ class ParserTest < TestCase
5757
# skip them for now.
5858
skip_all = skip_incorrect | [
5959
"dash_heredocs.txt",
60-
"heredocs_with_ignored_newlines.txt",
60+
# "heredocs_with_ignored_newlines.txt",
6161
"regex.txt",
6262
"regex_char_width.txt",
6363
"unescaping.txt",
6464
"seattlerb/bug190.txt",
6565
"seattlerb/heredoc_nested.txt",
66-
"seattlerb/heredoc_with_carriage_return_escapes_windows.txt",
67-
"seattlerb/heredoc_with_carriage_return_escapes.txt",
6866
"seattlerb/heredoc_with_extra_carriage_returns_windows.txt",
6967
"seattlerb/heredoc_with_only_carriage_returns_windows.txt",
7068
"seattlerb/heredoc_with_only_carriage_returns.txt",
@@ -116,6 +114,8 @@ class ParserTest < TestCase
116114
"seattlerb/heredoc_squiggly_visually_blank_lines.txt",
117115
"seattlerb/heredoc_squiggly.txt",
118116
"seattlerb/heredoc_unicode.txt",
117+
"seattlerb/heredoc_with_carriage_return_escapes_windows.txt",
118+
"seattlerb/heredoc_with_carriage_return_escapes.txt",
119119
"seattlerb/heredoc_with_interpolation_and_carriage_return_escapes_windows.txt",
120120
"seattlerb/heredoc_with_interpolation_and_carriage_return_escapes.txt",
121121
"seattlerb/interpolated_symbol_array_line_breaks.txt",

0 commit comments

Comments
 (0)