From ea529dd409a7edcaa2c6154a315ec6766b98459d Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Sun, 18 Feb 2024 16:11:18 -0500 Subject: [ruby/prism] Lazy methods for start and end offset on node https://github.com/ruby/prism/commit/4510e2746d --- lib/prism/parse_result/comments.rb | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/prism/parse_result/comments.rb b/lib/prism/parse_result/comments.rb index 26775b7f76..dd8fa8e1c7 100644 --- a/lib/prism/parse_result/comments.rb +++ b/lib/prism/parse_result/comments.rb @@ -27,11 +27,11 @@ module Prism end def start_offset - node.location.start_offset + node.start_offset end def end_offset - node.location.end_offset + node.end_offset end def encloses?(comment) @@ -94,13 +94,20 @@ module Prism preceding, enclosing, following = nearest_targets(parse_result.value, comment) if comment.trailing? - preceding&.trailing_comment(comment) || + if preceding + preceding.trailing_comment(comment) + else (following || enclosing || NodeTarget.new(parse_result.value)).leading_comment(comment) + end else # If a comment exists on its own line, prefer a leading comment. - following&.leading_comment(comment) || - preceding&.trailing_comment(comment) || + if following + following.leading_comment(comment) + elsif preceding + preceding.trailing_comment(comment) + else (enclosing || NodeTarget.new(parse_result.value)).leading_comment(comment) + end end end end -- cgit v1.2.3