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

Add Performance/MapCompact cop #229

Merged
merged 1 commit into from
Apr 14, 2021
Merged

Conversation

koic
Copy link
Member

@koic koic commented Mar 29, 2021

Follow #211 (comment)

This PR adds Performance/MapCompact cop

In Ruby 2.7, Enumerable#filter_map has been added.

This cop identifies places where map { ... }.compact can be replaced by filter_map.
It is marked as unsafe auto-correction by default because map { ... }.compact that is not compatible with filter_map.

The following is good and bad cases.

# bad
ary.map(&:foo).compact
ary.collect(&:foo).compact

# good
ary.filter_map(&:foo)
ary.map(&:foo).compact!

Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@koic koic force-pushed the add_new_map_compact_cop branch 2 times, most recently from 963ca2b to 970d0fb Compare March 29, 2021 23:37
Copy link
Contributor

@Drenmi Drenmi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 🎉

lib/rubocop/cop/performance/map_compact.rb Outdated Show resolved Hide resolved
Follow rubocop#211 (comment)

This PR adds `Performance/MapCompact` cop

In Ruby 2.7, `Enumerable#filter_map` has been added.

This cop identifies places where `map { ... }.compact` can be replaced by `filter_map`.
It is marked as unsafe auto-correction by default because `map { ... }.compact`
that is not compatible with `filter_map`.

The following is good and bad cases.

```ruby
# bad
ary.map(&:foo).compact
ary.collect(&:foo).compact

# good
ary.filter_map(&:foo)
ary.map(&:foo).compact!
```
@koic koic merged commit d08aa05 into rubocop:master Apr 14, 2021
@koic koic deleted the add_new_map_compact_cop branch April 14, 2021 15:15
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.

2 participants