[ruby/prism] Fix a token incompatibility for Prism::Translation::Parser

Fixes https://github.com/ruby/prism/pull/2512.

This PR fixes a token incompatibility between Parser gem and `Prism::Translation::Parser`
for `HEREDOC_END` with a newline.

https://github.com/ruby/prism/commit/b67d1e0c6f
This commit is contained in:
Koichi ITO 2024-03-08 00:22:41 +09:00 committed by git
parent 609bbad15d
commit dd24d88473
5 changed files with 31 additions and 1 deletions

View File

@ -304,7 +304,11 @@ module Prism
when :tSTRING_DVAR when :tSTRING_DVAR
value = nil value = nil
when :tSTRING_END when :tSTRING_END
if token.type == :REGEXP_END if token.type == :HEREDOC_END && value.end_with?("\n")
newline_length = value.end_with?("\r\n") ? 2 : 1
value = value.sub(/\r?\n\z/, '')
location = Range.new(source_buffer, offset_cache[token.location.start_offset], offset_cache[token.location.end_offset - newline_length])
elsif token.type == :REGEXP_END
value = value[0] value = value[0]
location = Range.new(source_buffer, offset_cache[token.location.start_offset], offset_cache[token.location.start_offset + 1]) location = Range.new(source_buffer, offset_cache[token.location.start_offset], offset_cache[token.location.start_offset + 1])
end end

View File

@ -0,0 +1,2 @@
<<TEXT
TEXT

View File

@ -0,0 +1,2 @@
<<TEXT
TEXT

View File

@ -0,0 +1,11 @@
@ ProgramNode (location: (1,0)-(1,6))
├── locals: []
└── statements:
@ StatementsNode (location: (1,0)-(1,6))
└── body: (length: 1)
└── @ StringNode (location: (1,0)-(1,6))
├── flags: ∅
├── opening_loc: (1,0)-(1,6) = "<<TEXT"
├── content_loc: (2,0)-(2,0) = ""
├── closing_loc: (2,0)-(3,0) = "TEXT\n"
└── unescaped: ""

View File

@ -0,0 +1,11 @@
@ ProgramNode (location: (1,0)-(1,6))
├── locals: []
└── statements:
@ StatementsNode (location: (1,0)-(1,6))
└── body: (length: 1)
└── @ StringNode (location: (1,0)-(1,6))
├── flags: ∅
├── opening_loc: (1,0)-(1,6) = "<<TEXT"
├── content_loc: (2,0)-(2,0) = ""
├── closing_loc: (2,0)-(3,0) = "TEXT\r\n"
└── unescaped: ""