forked from rubocop/rubocop
/
optional_boolean_parameter.rb
42 lines (39 loc) · 1.06 KB
/
optional_boolean_parameter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# frozen_string_literal: true
module RuboCop
module Cop
module Style
# This cop checks for places where keyword arguments can be used instead of
# boolean arguments when defining methods.
#
# @example
# # bad
# def some_method(bar = false)
# puts bar
# end
#
# # bad - common hack before keyword args were introduced
# def some_method(options = {})
# bar = options.fetch(:bar, false)
# puts bar
# end
#
# # good
# def some_method(bar: false)
# puts bar
# end
#
class OptionalBooleanParameter < Base
MSG = 'Use keyword arguments when defining method with boolean argument.'
BOOLEAN_TYPES = %i[true false].freeze
def on_def(node)
node.arguments.each do |arg|
next unless arg.optarg_type?
_name, value = *arg
add_offense(arg) if BOOLEAN_TYPES.include?(value.type)
end
end
alias on_defs on_def
end
end
end
end