Skip to content

Fix incorrect autocorrects for Performance/MapCompact around a line break before compact - #555

Open
maoxor wants to merge 2 commits into
rubocop:masterfrom
maoxor:fix_map_compact_line_break_before_compact
Open

maoxor wants to merge 2 commits into
rubocop:masterfrom
maoxor:fix_map_compact_line_break_before_compact

Conversation

@maoxor

@maoxor maoxor commented Oct 29, 2026

Copy link
Copy Markdown

I found these running rubocop -A with every cop enabled over a corpus of real-world Ruby (Homebrew, the default gems shipped with Ruby 4.0 and a few hundred installed gems): stackprof's lib/stackprof/report.rb and rspec-core's lib/rspec/core/backtrace_formatter.rb ended up with syntax errors. There are two separate causes, so there are two commits.

compact on its own line

When compact is called on its own line, the correction removed .compact but kept the line break in front of it, so whatever followed compact on that line got cut off:

# before
values = collection.map { |item| item.do_something }
  .compact if condition

# after (syntax error)
values = collection.filter_map { |item| item.do_something }
   if condition

With an operator it still parses but changes meaning: .compact + other turned into a separate + other statement (a NoMethodError for +@ at runtime in my test). The line break and indentation before compact are now removed along with it, which also covers the trailing-dot style. If a comment sits in between, the previous behavior is kept so the comment isn't deleted.

With Layout/SingleLineBlockChain

When both cops correct collection.map { ... }.compact if condition in the same pass, SingleLineBlockChain inserts a line break before .compact while MapCompact removes it, and the line break survives in front of the modifier. This declares them incompatible. I put the declaration on Layout/SingleLineBlockChain, like the existing ones for Performance/BlockGivenWithExplicitBlock, because RuboCop only started honoring a declaration made by the cop that runs last in 1.92, and the gemspec still allows 1.89.

After these changes, the same run leaves both files valid. bundle exec rake passes locally.

…ct` is on its own line

When `compact` is called on its own line, the autocorrection removed only
`.compact` and kept the line break in front of it, so whatever followed
`compact` on that line was cut off from the expression:

```ruby
# before
values = collection.map { |item| item.do_something }
  .compact if condition

# after (syntax error)
values = collection.filter_map { |item| item.do_something }
   if condition
```

With an operator such as `.compact + other`, the result still parses but
`+ other` becomes a separate statement. The line break and indentation
before `compact` are now removed with it, unless a comment sits there.
…t/SingleLineBlockChain`

`Layout/SingleLineBlockChain` inserts a line break before a `.compact`
chained to a single line `map` block, and `Performance/MapCompact`
removes that `.compact` in the same pass. The inserted line break
survives the removal, so a modifier following `compact` starts a line
of its own:

```ruby
# before
values = collection.map { |item| item.do_something }.compact if condition

# after (syntax error)
values = collection.filter_map { |item| item.do_something }
 if condition
```

The incompatibility is declared on `Layout/SingleLineBlockChain`, like the
existing ones for `Performance/BlockGivenWithExplicitBlock`, so that it is
honored by RuboCop versions that only check the cop that runs first.
@maoxor
maoxor force-pushed the fix_map_compact_line_break_before_compact branch from d93c512 to 623d321 Compare October 29, 2026 12:50
@maoxor
maoxor marked this pull request as ready for review October 29, 2026 12:50
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.

1 participant