-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #11908. This PR supports `AllowedReceivers` for `Style/CollectionMethods`. For example, by setting `AllowedReceivers: ['params']` in RuboCop Rails or user configuration, the following false positive for `params` can be prevented: ```ruby def selection_params(current, last_category = nil, params = {}) params.merge( category_id: current.is_a?(Category) ? current.to_param : last_category.to_param, site_id: current.is_a?(Site) ? current.to_param : nil, ).delete_if { |_k, v| v.nil? } end ``` The configuration to `params` is not set in the RuboCop core due to Rails specific issues. So if this approach is accepted, I will add it to RuboCop Rails's default configuration.
- Loading branch information
Showing
7 changed files
with
67 additions
and
22 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
changelog/new_support_allowed_receivers_for_style_collection_compact.md
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#11908](/~https://github.com/rubocop/rubocop/issues/11908): Support `AllowedReceivers` for `Style/CollectionMethods`. ([@koic][]) |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
# This module encapsulates the ability to allow certain receivers in a cop. | ||
module AllowedReceivers | ||
def allowed_receiver?(receiver) | ||
receiver_name = receiver_name(receiver) | ||
|
||
allowed_receivers.include?(receiver_name) | ||
end | ||
|
||
def receiver_name(receiver) | ||
if receiver.receiver && !receiver.receiver.const_type? | ||
return receiver_name(receiver.receiver) | ||
end | ||
|
||
if receiver.send_type? | ||
if receiver.receiver | ||
"#{receiver_name(receiver.receiver)}.#{receiver.method_name}" | ||
else | ||
receiver.method_name.to_s | ||
end | ||
else | ||
receiver.source | ||
end | ||
end | ||
|
||
def allowed_receivers | ||
cop_config.fetch('AllowedReceivers', []) | ||
end | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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
This file contains 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