From 73d657a3c9ff2b8f0817091ac7d4865ef9630a3b Mon Sep 17 00:00:00 2001 From: fatkodima Date: Thu, 24 Sep 2020 23:14:59 +0300 Subject: [PATCH] Add `AllowedMethods` option to `Style/OptionalBooleanParameter` cop --- CHANGELOG.md | 4 ++++ config/default.yml | 2 ++ docs/modules/ROOT/pages/cops_style.adoc | 23 ++++++++++++++++++- .../cop/style/optional_boolean_parameter.rb | 14 ++++++++--- .../style/optional_boolean_parameter_spec.rb | 22 ++++++++++++------ 5 files changed, 54 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a810a6a155..125122ef8f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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][]) diff --git a/config/default.yml b/config/default.yml index 595fb33236e..5f3adaee625 100644 --- a/config/default.yml +++ b/config/default.yml @@ -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.' diff --git a/docs/modules/ROOT/pages/cops_style.adoc b/docs/modules/ROOT/pages/cops_style.adoc index f0ab71073ec..a668eaf9d10 100644 --- a/docs/modules/ROOT/pages/cops_style.adoc +++ b/docs/modules/ROOT/pages/cops_style.adoc @@ -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 @@ -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 diff --git a/lib/rubocop/cop/style/optional_boolean_parameter.rb b/lib/rubocop/cop/style/optional_boolean_parameter.rb index 9a31c3cd10d..0919a941dab 100644 --- a/lib/rubocop/cop/style/optional_boolean_parameter.rb +++ b/lib/rubocop/cop/style/optional_boolean_parameter.rb @@ -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 @@ -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? diff --git a/spec/rubocop/cop/style/optional_boolean_parameter_spec.rb b/spec/rubocop/cop/style/optional_boolean_parameter_spec.rb index d207a1e9584..e589b04a3bd 100644 --- a/spec/rubocop/cop/style/optional_boolean_parameter_spec.rb +++ b/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) @@ -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