Sha256: 2613a377d90088225426951aac1dd295079c44b3423047450957fa9dd4a1e4ec

Contents?: true

Size: 1.02 KB

Versions: 4

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

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

        def_node_matcher :option_hash, <<-PATTERN
          (args ... $(optarg [#suspicious_name? _] (hash)))
        PATTERN

        def on_args(node)
          option_hash(node) do |options|
            add_offense(options)
          end
        end

        private

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-0.54.0 lib/rubocop/cop/style/option_hash.rb
rubocop-0.53.0 lib/rubocop/cop/style/option_hash.rb
rubocop-0.52.1 lib/rubocop/cop/style/option_hash.rb
rubocop-0.52.0 lib/rubocop/cop/style/option_hash.rb