Sha256: ebb617c771f488a33f2509aa32dab2b7f7034de20a03ebfaf12d02b64e05863c

Contents?: true

Size: 1.59 KB

Versions: 19

Compression:

Stored size: 1.59 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.
      #
      # @safety
      #   This cop is unsafe because changing a method signature will
      #   implicitly change behaviour.
      #
      # @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

19 entries across 19 versions & 4 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/optional_arguments.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.28.2 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.28.1 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.28.0 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.27.0 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.26.1 lib/rubocop/cop/style/optional_arguments.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.26.0 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.25.1 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.25.0 lib/rubocop/cop/style/optional_arguments.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.24.1 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.24.0 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.23.0 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.22.3 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.22.2 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.22.1 lib/rubocop/cop/style/optional_arguments.rb
rubocop-1.22.0 lib/rubocop/cop/style/optional_arguments.rb