Sha256: 1e866fe80b10d9ad5daa98c4a6168a5ffd067d7f9d5e13e4c9866ca2b7f6c1e3

Contents?: true

Size: 1.18 KB

Versions: 7

Compression:

Stored size: 1.18 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
        METHODS_EXCLUDED = %i[respond_to_missing?].freeze

        def on_def(node)
          return if METHODS_EXCLUDED.include?(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)
          end
        end
        alias on_defs on_def
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/optional_boolean_parameter.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/optional_boolean_parameter.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/optional_boolean_parameter.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/optional_boolean_parameter.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/optional_boolean_parameter.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/optional_boolean_parameter.rb
rubocop-0.91.1 lib/rubocop/cop/style/optional_boolean_parameter.rb