Skip to content

Commit

Permalink
Update the message for Style/OptionalBooleanParameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvandersluis authored and bbatsov committed Sep 28, 2021
1 parent a94b9fa commit 3a694f6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
14 changes: 10 additions & 4 deletions lib/rubocop/cop/style/optional_boolean_parameter.rb
Expand Up @@ -37,20 +37,26 @@ module Style
class OptionalBooleanParameter < Base
include AllowedMethods

MSG = 'Use keyword arguments when defining method with boolean argument.'
BOOLEAN_TYPES = %i[true false].freeze
MSG = 'Prefer keyword arguments for arguments with a boolean default value; ' \
'use `%<replacement>s` instead of `%<original>s`.'

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

node.arguments.each do |arg|
next unless arg.optarg_type?

_name, value = *arg
add_offense(arg) if BOOLEAN_TYPES.include?(value.type)
add_offense(arg, message: format_message(arg)) if arg.default_value.boolean_type?
end
end
alias on_defs on_def

private

def format_message(argument)
source = argument.source
format(MSG, original: source, replacement: source.sub(/\s+=/, ':'))
end
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/rubocop/cop/style/optional_boolean_parameter_spec.rb
Expand Up @@ -6,24 +6,24 @@
it 'registers an offense when defining method with optional boolean arg' do
expect_offense(<<~RUBY)
def some_method(bar = false)
^^^^^^^^^^^ Use keyword arguments when defining method with boolean argument.
^^^^^^^^^^^ Prefer keyword arguments for arguments with a boolean default value; use `bar: false` instead of `bar = false`.
end
RUBY
end

it 'registers an offense when defining class method with optional boolean arg' do
expect_offense(<<~RUBY)
def self.some_method(bar = false)
^^^^^^^^^^^ Use keyword arguments when defining method with boolean argument.
^^^^^^^^^^^ Prefer keyword arguments for arguments with a boolean default value; use `bar: false` instead of `bar = false`.
end
RUBY
end

it 'registers an offense when defining method with multiple optional boolean args' do
expect_offense(<<~RUBY)
def some_method(foo = true, bar = 1, baz = false, quux: true)
^^^^^^^^^^ Use keyword arguments when defining method with boolean argument.
^^^^^^^^^^^ Use keyword arguments when defining method with boolean argument.
^^^^^^^^^^ Prefer keyword arguments for arguments with a boolean default value; use `foo: true` instead of `foo = true`.
^^^^^^^^^^^ Prefer keyword arguments for arguments with a boolean default value; use `baz: false` instead of `baz = false`.
end
RUBY
end
Expand Down

0 comments on commit 3a694f6

Please sign in to comment.