From d0deec3ef3a439374e77aad6fd90bc8f9c5bcc90 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Sun, 12 Jan 2025 15:11:00 +0100 Subject: [PATCH] [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 --- lib/prism/translation/parser/lexer.rb | 10 +++++----- test/prism/fixtures/comment_single.txt | 1 + test/prism/snapshots/comment_single.txt | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 test/prism/fixtures/comment_single.txt create mode 100644 test/prism/snapshots/comment_single.txt 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) diff --git a/test/prism/fixtures/comment_single.txt b/test/prism/fixtures/comment_single.txt new file mode 100644 index 0000000000..72037a6ea1 --- /dev/null +++ b/test/prism/fixtures/comment_single.txt @@ -0,0 +1 @@ +foo # Bar \ No newline at end of file diff --git a/test/prism/snapshots/comment_single.txt b/test/prism/snapshots/comment_single.txt new file mode 100644 index 0000000000..3de3483075 --- /dev/null +++ b/test/prism/snapshots/comment_single.txt @@ -0,0 +1,17 @@ +@ ProgramNode (location: (1,0)-(1,3)) +├── flags: ∅ +├── locals: [] +└── statements: + @ StatementsNode (location: (1,0)-(1,3)) + ├── flags: ∅ + └── body: (length: 1) + └── @ CallNode (location: (1,0)-(1,3)) + ├── flags: newline, variable_call, ignore_visibility + ├── receiver: ∅ + ├── call_operator_loc: ∅ + ├── name: :foo + ├── message_loc: (1,0)-(1,3) = "foo" + ├── opening_loc: ∅ + ├── arguments: ∅ + ├── closing_loc: ∅ + └── block: ∅