From 2ac03343f0f9382fbe459c43c5feacedf1d8b50e Mon Sep 17 00:00:00 2001 From: QQ Date: Fri, 3 Jun 2022 15:08:22 +1000 Subject: [PATCH] Fix `Performance/MapCompact` autocorrect causing invalid syntax This commit fixes the issue when `Performance/MapCompact` auto-correction fails to detect the argument block of an assignment method so it deletes the block ending (i.e. `end` or `}`) when `map { ... }.compact` is multiline. Before ```ruby object.new_collection = collection.map do |item| end.compact object.new_collection = collection.filter_map do |item| ``` After ```ruby object.new_collection = collection.map do |item| end.compact object.new_collection = collection.filter_map do |item| end ``` --- CHANGELOG.md | 1 + ...mapcompact_autocorrect_causing_invalid_syntax.md | 1 + lib/rubocop/cop/performance/map_compact.rb | 2 +- spec/rubocop/cop/performance/map_compact_spec.rb | 13 +++++++++++++ 4 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_performance_mapcompact_autocorrect_causing_invalid_syntax.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 36551bceef..fd02b1908d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -362,3 +362,4 @@ [@mvz]: https://github.com/mvz [@leoarnold]: https://github.com/leoarnold [@ydah]: https://github.com/ydah +[@QQism]: https://github.com/QQism diff --git a/changelog/fix_performance_mapcompact_autocorrect_causing_invalid_syntax.md b/changelog/fix_performance_mapcompact_autocorrect_causing_invalid_syntax.md new file mode 100644 index 0000000000..763b896833 --- /dev/null +++ b/changelog/fix_performance_mapcompact_autocorrect_causing_invalid_syntax.md @@ -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][]) diff --git a/lib/rubocop/cop/performance/map_compact.rb b/lib/rubocop/cop/performance/map_compact.rb index 3ae66f4b28..b3a168d772 100644 --- a/lib/rubocop/cop/performance/map_compact.rb +++ b/lib/rubocop/cop/performance/map_compact.rb @@ -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) diff --git a/spec/rubocop/cop/performance/map_compact_spec.rb b/spec/rubocop/cop/performance/map_compact_spec.rb index 5326f83b90..28f8d699ba 100644 --- a/spec/rubocop/cop/performance/map_compact_spec.rb +++ b/spec/rubocop/cop/performance/map_compact_spec.rb @@ -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!