Sha256: 1f49ccc4b6d66fc695daf4af235684a4bd97c8d17a0be66a4bb6f8d3e86658db

Contents?: true

Size: 1.4 KB

Versions: 42

Compression:

Stored size: 1.4 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. `respond_to_missing?` method is allowed by default.
      # These are customizable with `AllowedMethods` option.
      #
      # @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
      #
      # @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

        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)
          end
        end
        alias on_defs on_def
      end
    end
  end
end

Version data entries

42 entries across 42 versions & 3 rubygems

Version Path
rubocop-1.21.0 lib/rubocop/cop/style/optional_boolean_parameter.rb
rubocop-1.20.0 lib/rubocop/cop/style/optional_boolean_parameter.rb
rubocop-1.19.1 lib/rubocop/cop/style/optional_boolean_parameter.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/rubocop-1.18.3/lib/rubocop/cop/style/optional_boolean_parameter.rb
rubocop-1.19.0 lib/rubocop/cop/style/optional_boolean_parameter.rb
rubocop-1.18.4 lib/rubocop/cop/style/optional_boolean_parameter.rb
rubocop-1.18.3 lib/rubocop/cop/style/optional_boolean_parameter.rb
rubocop-1.18.2 lib/rubocop/cop/style/optional_boolean_parameter.rb
rubocop-1.18.1 lib/rubocop/cop/style/optional_boolean_parameter.rb
rubocop-1.18.0 lib/rubocop/cop/style/optional_boolean_parameter.rb
rubocop-1.17.0 lib/rubocop/cop/style/optional_boolean_parameter.rb
rubocop-1.16.1 lib/rubocop/cop/style/optional_boolean_parameter.rb
rubocop-1.16.0 lib/rubocop/cop/style/optional_boolean_parameter.rb
rubocop-1.15.0 lib/rubocop/cop/style/optional_boolean_parameter.rb
cocRb-0.1.0 .bundle/ruby/3.0.0/gems/rubocop-1.14.0/lib/rubocop/cop/style/optional_boolean_parameter.rb
rubocop-1.14.0 lib/rubocop/cop/style/optional_boolean_parameter.rb
rubocop-1.13.0 lib/rubocop/cop/style/optional_boolean_parameter.rb
rubocop-1.12.1 lib/rubocop/cop/style/optional_boolean_parameter.rb
rubocop-1.12.0 lib/rubocop/cop/style/optional_boolean_parameter.rb
rubocop-1.11.0 lib/rubocop/cop/style/optional_boolean_parameter.rb