From af8a4205bfaebac3be8ad260ca5d044052384f4d Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 8 Mar 2024 18:11:34 +0900 Subject: [PATCH] Fix an error for `Prism::Translation::Parser::Lexer` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR fixes the following error for `Prism::Translation::Parser::Lexer` on the main branch: ```console $ cat example.rb 'a' # aあ " #{x} " $ bundle exec rubocop Parser::Source::Range: end_pos must not be less than begin_pos /Users/koic/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/parser-3.3.0.5/lib/parser/source/range.rb:39:in `initialize' /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser/lexer.rb:299:in `new' /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser/lexer.rb:299:in `block in to_a' /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser/lexer.rb:297:in `map' /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser/lexer.rb:297:in `to_a' /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser.rb:263:in `build_tokens' /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser.rb:92:in `tokenize' ``` This change was made in https://github.com/ruby/prism/pull/2557, and it seems there was an inconsistency in Range due to forgetting to apply `offset_cache` to `start_offset`. --- lib/prism/translation/parser/lexer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/prism/translation/parser/lexer.rb b/lib/prism/translation/parser/lexer.rb index 62ce17c7ea..7585aadd7b 100644 --- a/lib/prism/translation/parser/lexer.rb +++ b/lib/prism/translation/parser/lexer.rb @@ -296,7 +296,7 @@ module Prism start_offset = offset_cache[token.location.start_offset] lines.map do |line| end_offset = start_offset + line.length - tokens << [:tSTRING_CONTENT, [line, Range.new(source_buffer, start_offset, offset_cache[end_offset])]] + tokens << [:tSTRING_CONTENT, [line, Range.new(source_buffer, offset_cache[start_offset], offset_cache[end_offset])]] start_offset = end_offset end next