Sha256: 3c38279c64827f71ab3424edf7e4e4ad66c595c5ea598f7c29e503e89602366c

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module Style
      # This cop checks for options hashes and discourages them if the
      # current Ruby version supports keyword arguments.
      #
      # @example
      #   Instead of:
      #
      #   def fry(options = {})
      #     temperature = options.fetch(:temperature, 300)
      #     ...
      #   end
      #
      #   Prefer:
      #
      #   def fry(temperature: 300)
      #     ...
      #   end
      class OptionHash < Cop
        MSG = 'Prefer keyword arguments to options hashes.'

        def on_args(node)
          return unless supports_keyword_arguments?

          *_but_last, last_arg = *node

          # asserting that there was an argument at all
          return unless last_arg

          # asserting last argument is an optional argument
          return unless last_arg.optarg_type?

          _, default_value = *last_arg

          # asserting default value is a hash
          return unless default_value.hash_type?

          # asserting default value is empty hash
          *key_value_pairs = *default_value
          return unless key_value_pairs.empty?

          add_offense(last_arg, :expression, MSG)
        end

        private

        def supports_keyword_arguments?
          RUBY_VERSION >= '2.0.0'
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-0.33.0 lib/rubocop/cop/style/option_hash.rb