Skip to content

Fix duplicate comment output when method chain has block#89

Merged
fs0414 merged 1 commit intomainfrom
issue85-methodcnain-cmd-dep
Feb 1, 2026
Merged

Fix duplicate comment output when method chain has block#89
fs0414 merged 1 commit intomainfrom
issue85-methodcnain-cmd-dep

Conversation

@fs0414
Copy link
Owner

@fs0414 fs0414 commented Jan 26, 2026

📋 Summary

Fixes a bug where inline comments were duplicated in the output when formatting code with chained method calls that include blocks.

Before (bug):

# Input
some_method # comment
  .method_with_block { do_something }

# Output (incorrect - comment duplicated)
some_method # comment
  .method_with_block { do_something }
# comment  ← duplicated

After (fixed):

# Input = Output (no change, already formatted)
some_method # comment
  .method_with_block { do_something }

🔧 Changes

Core Fix

  • emit_call_without_block(): Added logic to mark comments within the extracted source range as emitted, preventing duplicate output in subsequent processing

Root Cause

When emit_call_without_block() extracts source text from call_node.start_offset to block_node.start_offset, inline comments are included in the extracted text. However, these comments were not being marked in emitted_comment_indices, causing them to be output again by later comment-handling routines.

Test Coverage

  • Added 2 test cases covering:
    • Method chain with brace block ({ })
    • Method chain with do-end block

🗂️ Changed Files

  • Rust emitter: ext/rfmt/src/emitter/mod.rs
  • Tests: spec/rfmt_spec.rb

🧪 Testing

Test Execution

bundle exec rspec

Results

  • 89 examples, 0 failures
  • No regressions

Verification

  • Bug reproduction confirmed before fix
  • New tests pass after fix
  • All existing tests pass (regression check)
  • Manual verification with rfmt --diff

📦 Breaking Changes

None

Related Issue

Closes #85

@fs0414 fs0414 merged commit 8176a8e into main Feb 1, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Duplicate comments generated when chained method takes a block

1 participant