Repository navigation
Conversation
…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
force-pushed
the
fix_map_compact_line_break_before_compact
branch
from
October 29, 2026 12:50
d93c512 to
623d321
Compare
maoxor
marked this pull request as ready for review
October 29, 2026 12:50
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I found these running
rubocop -Awith 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'slib/stackprof/report.rband rspec-core'slib/rspec/core/backtrace_formatter.rbended up with syntax errors. There are two separate causes, so there are two commits.compacton its own lineWhen
compactis called on its own line, the correction removed.compactbut kept the line break in front of it, so whatever followedcompacton that line got cut off:With an operator it still parses but changes meaning:
.compact + otherturned into a separate+ otherstatement (aNoMethodErrorfor+@at runtime in my test). The line break and indentation beforecompactare 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/SingleLineBlockChainWhen both cops correct
collection.map { ... }.compact if conditionin the same pass,SingleLineBlockChaininserts a line break before.compactwhileMapCompactremoves it, and the line break survives in front of the modifier. This declares them incompatible. I put the declaration onLayout/SingleLineBlockChain, like the existing ones forPerformance/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 rakepasses locally.