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 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