From 20f8d4549b442e0947205ccf9a88ddcd58a3bd13 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Wed, 2 Sep 2026 11:20:42 +0200 Subject: [PATCH] Fix `Comment#trailing` for syntax invalid edgecase When prism encounter invalid bytes and still switches over to parsing comments, `strip` will raise `syntax_suggest` might run into this via `ruby/spec` --- lib/prism/parse_result.rb | 2 ++ test/prism/result/comments_test.rb | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/prism/parse_result.rb b/lib/prism/parse_result.rb index 92eb56bf36..e9380d0f09 100644 --- a/lib/prism/parse_result.rb +++ b/lib/prism/parse_result.rb @@ -744,6 +744,8 @@ class InlineComment < Comment #: () -> bool def trailing? !location.start_line_slice.strip.empty? + rescue ArgumentError # invalid bytes, never whitespace only + false end # Returns a string representation of this comment. diff --git a/test/prism/result/comments_test.rb b/test/prism/result/comments_test.rb index 178623a75f..140eddd5d1 100644 --- a/test/prism/result/comments_test.rb +++ b/test/prism/result/comments_test.rb @@ -39,6 +39,16 @@ def foo ) end + def test_comment_trailing + comment = Prism.parse_comments("x # trailing")[0] + assert_predicate(comment, :trailing?) + end + + def test_comment_syntax_invalid_bytes_trailing + comment = Prism.parse_comments("\xFF\xFE# not trailing")[0] + refute_predicate(comment, :trailing?) + end + def test___END__ result = Prism.parse(<<~RUBY) __END__