From 6697cb47b24d4d78fa268cd4c46297b820d4700a Mon Sep 17 00:00:00 2001 From: Michael Scrivo <275524+mscrivo@users.noreply.github.com> Date: Wed, 18 Nov 2026 11:25:46 -0500 Subject: [PATCH 1/2] Keep comments above a leading-dot call in a chain Since 6.2.0, a comment on its own line before a leading-dot call in a method chain is attached to the call operator. format_chain then prints the operator trailing, which joins the comment to the previous line and leaves the dot on a line of its own: foo .bar(:a, :b)# comment . baz This is upstream syntax_tree issue 411. The trailing operator is only necessary when the comment follows the operator. The check now looks at the comment location, so a comment that comes before a leading dot stays on its own line, as in 6.1.1. Co-Authored-By: Claude Opus 5.5 --- lib/syntax_tree/node.rb | 14 +++++++++++--- test/fixtures/call.rb | 13 +++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/syntax_tree/node.rb b/lib/syntax_tree/node.rb index 96241bb1..aa9e756c 100644 --- a/lib/syntax_tree/node.rb +++ b/lib/syntax_tree/node.rb @@ -2860,13 +2860,21 @@ def format_chain(q, children) skip_attached: empty_except_last && children.empty? ) - # If the parent call node has a comment on the message then we need - # to print the operator trailing in order to keep it working. + # If the parent call node has a comment on the message, or a comment + # that follows the operator, then we need to print the operator + # trailing in order to keep it working. A comment that comes before + # a leading operator stays on its own line above the call. last_child = children.last if last_child.is_a?(CallNode) && last_child.message != :call && ( (last_child.message.comments.any? && last_child.operator) || - (last_child.operator && last_child.operator.comments.any?) + ( + last_child.operator && + last_child.operator.comments.any? do |comment| + comment.location.start_char >= + last_child.operator.location.end_char + end + ) ) q.format(CallOperatorFormatter.new(last_child.operator)) skip_operator = true diff --git a/test/fixtures/call.rb b/test/fixtures/call.rb index eec717f0..d731fb2c 100644 --- a/test/fixtures/call.rb +++ b/test/fixtures/call.rb @@ -40,6 +40,19 @@ qux .quux % +foo + .bar(:a, :b) + # comment + .baz + .qux +% +foo + .bar + # first comment + # second comment + .baz(:a) + .qux +% { a: 1, b: 2 }.fooooooooooooooooo.barrrrrrrrrrrrrrrrrrr.bazzzzzzzzzzzz.quxxxxxxxxxxxx - { a: 1, b: 2 }.fooooooooooooooooo From 8544253e0cc271353b726bf1b80fd798998fb5b6 Mon Sep 17 00:00:00 2001 From: Michael Scrivo <275524+mscrivo@users.noreply.github.com> Date: Wed, 18 Nov 2026 11:32:11 -0500 Subject: [PATCH 2/2] Add a changelog entry for the chain comment fix Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 489cd466..ec5bac24 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a ## [Unreleased] +### Changed + +- A comment on its own line before a leading-dot call in a method chain stays on its own line. It is no longer joined to the previous call with the dot moved to a separate line. + ## [6.3.0] - 2025-07-16 ### Added