Sha256: 4c190d672b7d0ded2541733cfc29173458c25852a8945fb9989cc556f65594a2

Contents?: true

Size: 1.61 KB

Versions: 5

Compression:

Stored size: 1.61 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?

          arg, 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?

          # Check for suspicious argument names
          return unless name_in_suspicious_param_names?(arg)

          add_offense(last_arg, :expression, MSG)
        end

        private

        def supports_keyword_arguments?
          RUBY_VERSION >= '2.0.0'
        end

        def name_in_suspicious_param_names?(arg_name)
          cop_config.key?('SuspiciousParamNames') &&
            cop_config['SuspiciousParamNames'].include?(arg_name.to_s)
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubocop-0.35.1 lib/rubocop/cop/style/option_hash.rb
rubocop-0.35.0 lib/rubocop/cop/style/option_hash.rb
rubocop-0.34.2 lib/rubocop/cop/style/option_hash.rb
rubocop-0.34.1 lib/rubocop/cop/style/option_hash.rb
rubocop-0.34.0 lib/rubocop/cop/style/option_hash.rb