Skip to content

Commit e8f2592

Browse files
authored
Merge pull request #3399 from Earlopain/parser-translator-comment-followup
Fix parser translator tokens for comment-only file
2 parents 01cbec9 + 3c266f1 commit e8f2592

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

lib/prism/translation/parser/lexer.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,14 +281,14 @@ def to_a
281281
location = range(token.location.start_offset, lexed[index][0].location.end_offset)
282282
index += 1
283283
else
284-
value.chomp!
285-
location = range(token.location.start_offset, token.location.end_offset - 1)
284+
is_at_eol = value.chomp!.nil?
285+
location = range(token.location.start_offset, token.location.end_offset + (is_at_eol ? 0 : -1))
286286

287-
prev_token = lexed[index - 2][0]
287+
prev_token = lexed[index - 2][0] if index - 2 >= 0
288288
next_token = lexed[index][0]
289289

290-
is_inline_comment = prev_token.location.start_line == token.location.start_line
291-
if is_inline_comment && !COMMENT_CONTINUATION_TYPES.include?(next_token&.type)
290+
is_inline_comment = prev_token&.location&.start_line == token.location.start_line
291+
if is_inline_comment && !is_at_eol && !COMMENT_CONTINUATION_TYPES.include?(next_token&.type)
292292
tokens << [:tCOMMENT, [value, location]]
293293

294294
nl_location = range(token.location.end_offset - 1, token.location.end_offset)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo # Bar

test/prism/snapshots/comment_single.txt

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)