diff options
author | Earlopain <[email protected]> | 2025-01-12 15:11:00 +0100 |
---|---|---|
committer | git <[email protected]> | 2025-01-12 18:34:06 +0000 |
commit | d0deec3ef3a439374e77aad6fd90bc8f9c5bcc90 (patch) | |
tree | 1e1c15aaa1fa02434265ffd02cbc9b8cd0943965 /lib | |
parent | f56f3eaae55cc6f8d9e79862ee73a9ffb53d6077 (diff) |
[ruby/prism] Fix parser translator tokens for comment-only file
In https://github.com/ruby/prism/pull/3393 I made a mistake.
When there is no previous token, it wraps around to -1. Oops
Additionally, if a comment has no newline then the offset should be kept as is
https://github.com/ruby/prism/commit/3c266f1de4
Diffstat (limited to 'lib')
-rw-r--r-- | lib/prism/translation/parser/lexer.rb | 10 |
1 files changed, 5 insertions, 5 deletions
diff --git a/lib/prism/translation/parser/lexer.rb b/lib/prism/translation/parser/lexer.rb index 49fdd2aea8..550219fc48 100644 --- a/lib/prism/translation/parser/lexer.rb +++ b/lib/prism/translation/parser/lexer.rb @@ -281,14 +281,14 @@ module Prism location = range(token.location.start_offset, lexed[index][0].location.end_offset) index += 1 else - value.chomp! - location = range(token.location.start_offset, token.location.end_offset - 1) + is_at_eol = value.chomp!.nil? + location = range(token.location.start_offset, token.location.end_offset + (is_at_eol ? 0 : -1)) - prev_token = lexed[index - 2][0] + prev_token = lexed[index - 2][0] if index - 2 >= 0 next_token = lexed[index][0] - is_inline_comment = prev_token.location.start_line == token.location.start_line - if is_inline_comment && !COMMENT_CONTINUATION_TYPES.include?(next_token&.type) + is_inline_comment = prev_token&.location&.start_line == token.location.start_line + if is_inline_comment && !is_at_eol && !COMMENT_CONTINUATION_TYPES.include?(next_token&.type) tokens << [:tCOMMENT, [value, location]] nl_location = range(token.location.end_offset - 1, token.location.end_offset) |