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 AllowedMethods option to Style/OptionalBooleanParameter cop #8790

Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### New features

* [#8790](https://github.com/rubocop-hq/rubocop/pull/8790): Add `AllowedMethods` option to `Style/OptionalBooleanParameter` cop. ([@fatkodima][])

### Bug fixes

* [#8774](https://github.com/rubocop-hq/rubocop/issues/8774): Fix a false positive for `Layout/ArrayAlignment` with parallel assignment. ([@dvandersluis][])
Expand Down
2 changes: 2 additions & 0 deletions config/default.yml
Expand Up @@ -3725,6 +3725,8 @@ Style/OptionalBooleanParameter:
Enabled: pending
Safe: false
VersionAdded: '0.89'
AllowedMethods:
- respond_to_missing?

Style/OrAssignment:
Description: 'Recommend usage of double pipe equals (||=) where applicable.'
Expand Down
23 changes: 22 additions & 1 deletion docs/modules/ROOT/pages/cops_style.adoc
Expand Up @@ -6888,7 +6888,8 @@ end
|===

This cop checks for places where keyword arguments can be used instead of
boolean arguments when defining methods.
boolean arguments when defining methods. `respond_to_missing?` method is allowed by default.
These are customizable with `AllowedMethods` option.

=== Examples

Expand All @@ -6911,6 +6912,26 @@ def some_method(bar: false)
end
----

==== AllowedMethods: ['some_method']

[source,ruby]
----
# good
def some_method(bar = false)
puts bar
end
----

=== Configurable attributes

|===
| Name | Default value | Configurable values

| AllowedMethods
| `respond_to_missing?`
| Array
|===

=== References

* https://rubystyle.guide#boolean-keyword-arguments
Expand Down
14 changes: 11 additions & 3 deletions lib/rubocop/cop/style/optional_boolean_parameter.rb
Expand Up @@ -4,7 +4,8 @@ module RuboCop
module Cop
module Style
# This cop checks for places where keyword arguments can be used instead of
# boolean arguments when defining methods.
# boolean arguments when defining methods. `respond_to_missing?` method is allowed by default.
# These are customizable with `AllowedMethods` option.
#
# @example
# # bad
Expand All @@ -23,13 +24,20 @@ module Style
# puts bar
# end
#
# @example AllowedMethods: ['some_method']
# # good
# def some_method(bar = false)
# puts bar
# end
#
class OptionalBooleanParameter < Base
include AllowedMethods

MSG = 'Use keyword arguments when defining method with boolean argument.'
BOOLEAN_TYPES = %i[true false].freeze
METHODS_EXCLUDED = %i[respond_to_missing?].freeze

def on_def(node)
return if METHODS_EXCLUDED.include?(node.method_name)
return if allowed_method?(node.method_name)

node.arguments.each do |arg|
next unless arg.optarg_type?
Expand Down
22 changes: 15 additions & 7 deletions spec/rubocop/cop/style/optional_boolean_parameter_spec.rb
@@ -1,7 +1,9 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Style::OptionalBooleanParameter do
subject(:cop) { described_class.new }
RSpec.describe RuboCop::Cop::Style::OptionalBooleanParameter, :config do
let(:cop_config) do
{ 'AllowedMethods' => [] }
end

it 'registers an offense when defining method with optional boolean arg' do
expect_offense(<<~RUBY)
Expand Down Expand Up @@ -49,10 +51,16 @@ def some_method(bar = 'foo')
RUBY
end

it 'does not register an offense when defining respond_to_missing? method with boolean arg' do
expect_no_offenses(<<~RUBY)
def respond_to_missing?(arg, bar = false)
end
RUBY
context 'when AllowedMethods is not empty' do
let(:cop_config) do
{ 'AllowedMethods' => %w[respond_to_missing?] }
end

it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
def respond_to_missing?(method, include_all = false)
end
RUBY
end
end
end