Sha256: 79eb7a6cd5f2dca44b5309234aeb0dea1b9b5649b3572da66c7f3d0ae47036ed

Contents?: true

Size: 1.51 KB

Versions: 35

Compression:

Stored size: 1.51 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 < Base
        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

35 entries across 35 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/optional_arguments.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/optional_arguments.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/optional_arguments.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/optional_arguments.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/optional_arguments.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.12.1 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.12.0 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.11.0 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.10.0 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.9.1 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.9.0 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.8.1 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.8.0 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.7.0 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.6.1 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.6.0 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.5.2 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.5.1 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.5.0 lib/rubocop/cop/style/optional_arguments.rb