Sha256: 11b4df6fbbeaeb3a91f13dfaf0e717b0c22526c65e59542e8852435786802e50

Contents?: true

Size: 1.5 KB

Versions: 47

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for optional arguments to methods
      # that do not come at the end of the argument list
      #
      # @example
      #   # bad
      #   def foo(a = 1, b, c)
      #   end
      #
      #   # good
      #   def baz(a, b, c = 1)
      #   end
      #
      #   def foobar(a = 1, b = 2, c = 3)
      #   end
      class OptionalArguments < Cop
        MSG = 'Optional arguments should appear at the end ' \
              'of the argument list.'

        def on_def(node)
          each_misplaced_optional_arg(node.arguments) do |argument|
            add_offense(argument)
          end
        end

        private

        def each_misplaced_optional_arg(arguments)
          optarg_positions, arg_positions = argument_positions(arguments)
          return if optarg_positions.empty? || arg_positions.empty?

          optarg_positions.each do |optarg_position|
            # there can only be one group of optional arguments
            break if optarg_position > arg_positions.max

            yield arguments[optarg_position]
          end
        end

        def argument_positions(arguments)
          optarg_positions = []
          arg_positions = []

          arguments.each_with_index do |argument, index|
            optarg_positions << index if argument.optarg_type?
            arg_positions << index if argument.arg_type?
          end

          [optarg_positions, arg_positions]
        end
      end
    end
  end
end

Version data entries

47 entries across 28 versions & 3 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/optional_arguments.rb
rubocop-0.82.0 lib/rubocop/cop/style/optional_arguments.rb
rubocop-0.81.0 lib/rubocop/cop/style/optional_arguments.rb
rubocop-0.80.1 lib/rubocop/cop/style/optional_arguments.rb
rubocop-0.80.0 lib/rubocop/cop/style/optional_arguments.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/optional_arguments.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/optional_arguments.rb
rubocop-0.79.0 lib/rubocop/cop/style/optional_arguments.rb
rubocop-0.78.0 lib/rubocop/cop/style/optional_arguments.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/style/optional_arguments.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.77.0/lib/rubocop/cop/style/optional_arguments.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.1/lib/rubocop/cop/style/optional_arguments.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/style/optional_arguments.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/optional_arguments.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.76.0/lib/rubocop/cop/style/optional_arguments.rb
rubocop-0.77.0 lib/rubocop/cop/style/optional_arguments.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.75.1/lib/rubocop/cop/style/optional_arguments.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.76.0/lib/rubocop/cop/style/optional_arguments.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/style/optional_arguments.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/optional_arguments.rb