Sha256: 5858807d339a8629d600b88284440d9e9b4ac9ba1e8bf281d7975d66861e18b8

Contents?: true

Size: 1.58 KB

Versions: 203

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for optional arguments to methods
      # that do not come at the end of the argument list.
      #
      # @safety
      #   This cop is unsafe because changing a method signature will
      #   implicitly change behavior.
      #
      # @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) { |argument| add_offense(argument) }
        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

203 entries across 196 versions & 20 rubygems

Version Path
rubocop-1.74.0 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.73.2 lib/rubocop/cop/style/optional_arguments.rb
siteimprove_api_client-1.0.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.73.1/lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.73.1 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.73.0 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.72.2 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.72.1 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.72.0 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.71.2 lib/rubocop/cop/style/optional_arguments.rb
tailscale_middleware-0.0.3 vendor/cache/ruby/3.4.0/gems/rubocop-1.71.1/lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.71.1 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.71.0 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.70.0 lib/rubocop/cop/style/optional_arguments.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.69.2 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.69.1 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.69.0 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.68.0 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.67.0 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.66.1 lib/rubocop/cop/style/optional_arguments.rb