Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Performance/MapCompact autocorrect causing invalid syntax #291

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,4 @@
[@mvz]: https://github.com/mvz
[@leoarnold]: https://github.com/leoarnold
[@ydah]: https://github.com/ydah
[@QQism]: https://github.com/QQism
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#291](https://github.com/rubocop/rubocop-performance/pull/291): Fix `Performance/MapCompact` autocorrect causing invalid syntax when using multiline `map { ... }.compact` as an argument for an assignment method. ([@QQism][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/performance/map_compact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def map_method_and_compact_method_on_same_line?(map_node, compact_node)
end

def invoke_method_after_map_compact_on_same_line?(compact_node, chained_method)
compact_node.loc.selector.line == chained_method.loc.selector.line
compact_node.loc.selector.line == chained_method.loc.last_line
end

def compact_method_with_final_newline_range(compact_method_range)
Expand Down
13 changes: 13 additions & 0 deletions spec/rubocop/cop/performance/map_compact_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,19 @@
RUBY
end

it 'registers an offense when using multiline `map { ... }.compact` as an argument of an assignment method' do
expect_offense(<<~RUBY)
object.new_collection = collection.map do |item|
^^^^^^^^^^^^^ Use `filter_map` instead.
end.compact
RUBY

expect_correction(<<~RUBY)
object.new_collection = collection.filter_map do |item|
end
RUBY
end

it 'does not register an offense when using `collection.map(&:do_something).compact!`' do
expect_no_offenses(<<~RUBY)
collection.map(&:do_something).compact!
Expand Down