Sha256: f212ddda751b58c03fc23ff517fcac251217a02f36b644dd9b23918ff8ff4bf3

Contents?: true

Size: 1.29 KB

Versions: 203

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # 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 < Base
        MSG = 'Prefer keyword arguments to options hashes.'

        # @!method option_hash(node)
        def_node_matcher :option_hash, <<~PATTERN
          (args ... $(optarg [#suspicious_name? _] (hash)))
        PATTERN

        def on_args(node)
          return if super_used?(node)
          return if allowlist.include?(node.parent.method_name.to_s)

          option_hash(node) { |options| add_offense(options) }
        end

        private

        def allowlist
          cop_config['Allowlist'] || []
        end

        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

203 entries across 196 versions & 20 rubygems

Version Path
rubocop-1.74.0 lib/rubocop/cop/style/option_hash.rb
rubocop-1.73.2 lib/rubocop/cop/style/option_hash.rb
siteimprove_api_client-1.0.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.73.1/lib/rubocop/cop/style/option_hash.rb
rubocop-1.73.1 lib/rubocop/cop/style/option_hash.rb
rubocop-1.73.0 lib/rubocop/cop/style/option_hash.rb
rubocop-1.72.2 lib/rubocop/cop/style/option_hash.rb
rubocop-1.72.1 lib/rubocop/cop/style/option_hash.rb
rubocop-1.72.0 lib/rubocop/cop/style/option_hash.rb
rubocop-1.71.2 lib/rubocop/cop/style/option_hash.rb
tailscale_middleware-0.0.3 vendor/cache/ruby/3.4.0/gems/rubocop-1.71.1/lib/rubocop/cop/style/option_hash.rb
rubocop-1.71.1 lib/rubocop/cop/style/option_hash.rb
rubocop-1.71.0 lib/rubocop/cop/style/option_hash.rb
rubocop-1.70.0 lib/rubocop/cop/style/option_hash.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/option_hash.rb
rubocop-1.69.2 lib/rubocop/cop/style/option_hash.rb
rubocop-1.69.1 lib/rubocop/cop/style/option_hash.rb
rubocop-1.69.0 lib/rubocop/cop/style/option_hash.rb
rubocop-1.68.0 lib/rubocop/cop/style/option_hash.rb
rubocop-1.67.0 lib/rubocop/cop/style/option_hash.rb
rubocop-1.66.1 lib/rubocop/cop/style/option_hash.rb