Sha256: 4ae4609949a48e1f842a5e179017d356049b2acc27afc5371ca63284d4cc836c

Contents?: true

Size: 1.33 KB

Versions: 13

Compression:

Stored size: 1.33 KB

Contents

# encoding: utf-8
# 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.'.freeze

        def on_def(node)
          _method, arguments, = *node
          arguments = *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

          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
            argument = arguments[optarg_position]
            arg, = *argument

            add_offense(argument, :expression, format(MSG, arg))
          end
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/optional_arguments.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/optional_arguments.rb
rubocop-0.42.0 lib/rubocop/cop/style/optional_arguments.rb
rubocop-0.41.2 lib/rubocop/cop/style/optional_arguments.rb
rubocop-0.41.1 lib/rubocop/cop/style/optional_arguments.rb
rubocop-0.41.0 lib/rubocop/cop/style/optional_arguments.rb
rubocop-0.40.0 lib/rubocop/cop/style/optional_arguments.rb
rubocop-0.39.0 lib/rubocop/cop/style/optional_arguments.rb
rubocop-0.38.0 lib/rubocop/cop/style/optional_arguments.rb
rubocop-0.37.2 lib/rubocop/cop/style/optional_arguments.rb
rubocop-0.37.1 lib/rubocop/cop/style/optional_arguments.rb
rubocop-0.37.0 lib/rubocop/cop/style/optional_arguments.rb
rubocop-0.36.0 lib/rubocop/cop/style/optional_arguments.rb