diff options
author | Martin Emde <[email protected]> | 2023-11-30 19:53:54 -0800 |
---|---|---|
committer | git <[email protected]> | 2023-12-01 15:17:20 +0000 |
commit | cbe57caa246f57440667a53b4526ddabcea82df9 () | |
tree | 12c1255647a4b7092c2cc80c6fc8399c7f7b019b /lib/prism/lex_compat.rb | |
parent | ffeec108cfccda71ff63167d41f090aa39c2432c (diff) |
[ruby/prism] Fix comments after HEREDOCs again.
The problem was deeper than just looking back a single token. You can push the heredoc_end token way back into the list. We need to save the last location of a heredoc end to see if it's the last token in the file. Fixes https://.com/ruby/prism/pull/1954 https://.com/ruby/prism/commit/91dfd4eecd
-rw-r--r-- | lib/prism/lex_compat.rb | 21 |
1 files changed, 8 insertions, 13 deletions
@@ -610,6 +610,7 @@ module Prism result = Prism.lex(source, **options) result_value = result.value previous_state = nil # In previous versions of Ruby, Ripper wouldn't flush the bom before the # first token, so we had to have a hack in place to account for that. This @@ -664,6 +665,7 @@ module Prism when :on_heredoc_end # Heredoc end tokens can be emitted in an odd order, so we don't # want to bother comparing the state on them. IgnoreStateToken.new([[lineno, column], event, value, lex_state]) when :on_ident if lex_state == Ripper::EXPR_END @@ -730,20 +732,13 @@ module Prism # Ripper will append a on_nl token (even though there isn't # necessarily a newline). We mirror that here. if previous_token.type == :COMMENT - # If the token before the comment was a heredoc end, then - # the comment's end_offset is before the heredoc end token. # This is not the correct offset to use for figuring out if - # there is trailing whitespace after the comment. - # Use the end_offset of the heredoc end instead. - before_comment = result_value[index - 2] - before_comment &&= before_comment[0] - - if before_comment&.type == :HEREDOC_END - start_offset = before_comment.location.end_offset - else - start_offset = previous_token.location.end_offset - end - end_offset = token.location.start_offset if start_offset < end_offset |