Sha256: 09f0dec30f74ca5bbfa634b7875a4c83c434c713cfed1fa41610b71b396d9d70

Contents?: true

Size: 1.06 KB

Versions: 5

Compression:

Stored size: 1.06 KB

Contents

# 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

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
rubocop-0.91.0 lib/rubocop/cop/style/optional_boolean_parameter.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.90.0/lib/rubocop/cop/style/optional_boolean_parameter.rb
rubocop-0.90.0 lib/rubocop/cop/style/optional_boolean_parameter.rb
rubocop-0.89.1 lib/rubocop/cop/style/optional_boolean_parameter.rb
rubocop-0.89.0 lib/rubocop/cop/style/optional_boolean_parameter.rb