Sha256: 8009c1cb75aa87eb300071c15b6e8f85c65758aceb8f8fba65c0f3489203a7dc

Contents?: true

Size: 1.28 KB

Versions: 32

Compression:

Stored size: 1.28 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 < Base
        MSG = 'Prefer keyword arguments to options hashes.'

        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) do |options|
            add_offense(options)
          end
        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

32 entries across 32 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/option_hash.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/option_hash.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/option_hash.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/option_hash.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/option_hash.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/option_hash.rb
rubocop-1.10.0 lib/rubocop/cop/style/option_hash.rb
rubocop-1.9.1 lib/rubocop/cop/style/option_hash.rb
rubocop-1.9.0 lib/rubocop/cop/style/option_hash.rb
rubocop-1.8.1 lib/rubocop/cop/style/option_hash.rb
rubocop-1.8.0 lib/rubocop/cop/style/option_hash.rb
rubocop-1.7.0 lib/rubocop/cop/style/option_hash.rb
rubocop-1.6.1 lib/rubocop/cop/style/option_hash.rb
rubocop-1.6.0 lib/rubocop/cop/style/option_hash.rb
rubocop-1.5.2 lib/rubocop/cop/style/option_hash.rb
rubocop-1.5.1 lib/rubocop/cop/style/option_hash.rb
rubocop-1.5.0 lib/rubocop/cop/style/option_hash.rb
rubocop-1.4.2 lib/rubocop/cop/style/option_hash.rb
rubocop-1.4.1 lib/rubocop/cop/style/option_hash.rb
rubocop-1.4.0 lib/rubocop/cop/style/option_hash.rb