From dd24d88473d432f166894bd6d187a6db2c27bfc5 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 8 Mar 2024 00:22:41 +0900 Subject: [PATCH] [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 --- lib/prism/translation/parser/lexer.rb | 6 +++++- test/prism/fixtures/heredoc.txt | 2 ++ test/prism/fixtures/heredoc_with_carriage_returns.txt | 2 ++ test/prism/snapshots/heredoc.txt | 11 +++++++++++ .../prism/snapshots/heredoc_with_carriage_returns.txt | 11 +++++++++++ 5 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 test/prism/fixtures/heredoc.txt create mode 100644 test/prism/fixtures/heredoc_with_carriage_returns.txt create mode 100644 test/prism/snapshots/heredoc.txt create mode 100644 test/prism/snapshots/heredoc_with_carriage_returns.txt diff --git a/lib/prism/translation/parser/lexer.rb b/lib/prism/translation/parser/lexer.rb index 7585aadd7b..b28273b03f 100644 --- a/lib/prism/translation/parser/lexer.rb +++ b/lib/prism/translation/parser/lexer.rb @@ -304,7 +304,11 @@ module Prism when :tSTRING_DVAR value = nil 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] location = Range.new(source_buffer, offset_cache[token.location.start_offset], offset_cache[token.location.start_offset + 1]) end diff --git a/test/prism/fixtures/heredoc.txt b/test/prism/fixtures/heredoc.txt new file mode 100644 index 0000000000..f94fd912df --- /dev/null +++ b/test/prism/fixtures/heredoc.txt @@ -0,0 +1,2 @@ +<