Sha256: 5e70afb9a2bc1f0955ffcafae7e0d0d5ee1ba5d1b36dac80f0ec24468ad40398

Contents?: true

Size: 1.15 KB

Versions: 24

Compression:

Stored size: 1.15 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)
          return if super_used?(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

        def super_used?(node)
          node.parent.each_node(:zsuper).any?
        end
      end
    end
  end
end

Version data entries

24 entries across 22 versions & 3 rubygems

Version Path
rubocop-0.65.0 lib/rubocop/cop/style/option_hash.rb
rubocop-0.64.0 lib/rubocop/cop/style/option_hash.rb
rubocop-0.63.1 lib/rubocop/cop/style/option_hash.rb
rubocop-0.63.0 lib/rubocop/cop/style/option_hash.rb
rubocop-0.62.0 lib/rubocop/cop/style/option_hash.rb
rubocop-0.61.1 lib/rubocop/cop/style/option_hash.rb
rubocop-0.61.0 lib/rubocop/cop/style/option_hash.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/style/option_hash.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/style/option_hash.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/style/option_hash.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/style/option_hash.rb
rubocop-0.60.0 lib/rubocop/cop/style/option_hash.rb
rubocop-0.59.2 lib/rubocop/cop/style/option_hash.rb
rubocop-0.59.1 lib/rubocop/cop/style/option_hash.rb
rubocop-0.59.0 lib/rubocop/cop/style/option_hash.rb
rubocop-0.58.2 lib/rubocop/cop/style/option_hash.rb
rubocop-0.58.1 lib/rubocop/cop/style/option_hash.rb
rubocop-0.58.0 lib/rubocop/cop/style/option_hash.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/style/option_hash.rb
rubocop-0.57.2 lib/rubocop/cop/style/option_hash.rb