From 721ec6d3f4bc5fe21a39a3cd4b7e10b78a29decc Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Thu, 28 May 2026 20:24:43 +0900 Subject: [PATCH] fix dropped receiver after comment-in-parens (#357) skip_space_or_newline wrote "\n" via `write`, which does not increment @line. A trailing-newline comment inside parens left @line one short of reality, so later `dedent_calls` looked up @line_to_call_info on the wrong line and trimmed leading characters off an unrelated call, deleting the method receiver. Use write_line so newline bookkeeping stays consistent; the manual @column reset becomes unnecessary. Co-Authored-By: Claude Opus 4.7 (1M context) --- lib/rufo/formatter.rb | 3 +-- .../junk_drawer.rb.spec | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/rufo/formatter.rb b/lib/rufo/formatter.rb index 5adbbf39..35305203 100644 --- a/lib/rufo/formatter.rb +++ b/lib/rufo/formatter.rb @@ -3405,9 +3405,8 @@ def skip_space_or_newline(_want_semicolon: false, write_first_semicolon: false) if current_token_value.end_with?("\n") write_space write current_token_value.rstrip - write "\n" + write_line write_indent(next_indent) - @column = next_indent else write current_token_value end diff --git a/spec/lib/rufo/formatter_source_specs/junk_drawer.rb.spec b/spec/lib/rufo/formatter_source_specs/junk_drawer.rb.spec index 94bd42e3..2932b000 100644 --- a/spec/lib/rufo/formatter_source_specs/junk_drawer.rb.spec +++ b/spec/lib/rufo/formatter_source_specs/junk_drawer.rb.spec @@ -51,3 +51,22 @@ end def foo end + +#~# ORIGINAL issue 357 receiver preserved after comment in parens +a=(#X +1) + +[STDOUT].each{ |elem| elem.puts [ + 2 + ] +} + +#~# EXPECTED +a = ( #X + 1) + +[STDOUT].each { |elem| + elem.puts [ + 2, + ] +}