diff options
| author | Kevin Newton <kddnewton@gmail.com> | 2024-06-07 15:27:21 -0400 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2024-06-07 19:46:27 +0000 |
| commit | 41a36b68531dd1d9fa044c34ac40a5c4abedda2e (patch) | |
| tree | ab3e11aff18caeb115df7576f0720d0c3f712e64 /lib | |
| parent | 94e059797ae89b35a2c72bf1a4307766a1a9e960 (diff) | |
[ruby/prism] Handle chomped bytesize with lines without newlines
https://github.com/ruby/prism/commit/1528d3c019
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/prism/translation/parser/compiler.rb | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/lib/prism/translation/parser/compiler.rb b/lib/prism/translation/parser/compiler.rb index cbec2bb062..48f3d4db5c 100644 --- a/lib/prism/translation/parser/compiler.rb +++ b/lib/prism/translation/parser/compiler.rb @@ -2040,6 +2040,13 @@ module Prism 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 @@ module Prism 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?(/(?<!\\)\\\r?\n$/) } .each do |lines| escaped_lengths << lines.sum(&:bytesize) - normalized_lengths << lines.sum { |line| line.chomp.bytesize + 1 } + normalized_lengths << lines.sum { |line| chomped_bytesize(line) } end end |
