Sha256: 8d176d20f5f124d2ba734a8e998ca318df13e5d590237daa261cc17fa520cc3d

Contents?: true

Size: 1.77 KB

Versions: 168

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for the use of strings as keys in hashes. The use of
      # symbols is preferred instead.
      #
      # @safety
      #   This cop is unsafe because while symbols are preferred for hash keys,
      #   there are instances when string keys are required.
      #
      # @example
      #   # bad
      #   { 'one' => 1, 'two' => 2, 'three' => 3 }
      #
      #   # good
      #   { one: 1, two: 2, three: 3 }
      class StringHashKeys < Base
        extend AutoCorrector

        MSG = 'Prefer symbols instead of strings as hash keys.'

        # @!method string_hash_key?(node)
        def_node_matcher :string_hash_key?, <<~PATTERN
          (pair (str _) _)
        PATTERN

        # @!method receive_environments_method?(node)
        def_node_matcher :receive_environments_method?, <<~PATTERN
          {
            ^^(send (const {nil? cbase} :IO) :popen ...)
            ^^(send (const {nil? cbase} :Open3)
                {:capture2 :capture2e :capture3 :popen2 :popen2e :popen3} ...)
            ^^^(send (const {nil? cbase} :Open3)
                {:pipeline :pipeline_r :pipeline_rw :pipeline_start :pipeline_w} ...)
            ^^(send {nil? (const {nil? cbase} :Kernel)} {:spawn :system} ...)
            ^^(send _ {:gsub :gsub!} ...)
          }
        PATTERN

        def on_pair(node)
          return unless string_hash_key?(node)

          key_content = node.key.str_content
          return unless key_content.valid_encoding?
          return if receive_environments_method?(node)

          add_offense(node.key) do |corrector|
            symbol_content = key_content.to_sym.inspect

            corrector.replace(node.key, symbol_content)
          end
        end
      end
    end
  end
end

Version data entries

168 entries across 167 versions & 18 rubygems

Version Path
rubocop-1.72.1 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.72.0 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.71.2 lib/rubocop/cop/style/string_hash_keys.rb
tailscale_middleware-0.0.3 vendor/cache/ruby/3.4.0/gems/rubocop-1.71.1/lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.71.1 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.71.0 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.70.0 lib/rubocop/cop/style/string_hash_keys.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.69.2 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.69.1 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.69.0 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.68.0 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.67.0 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.66.1 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.66.0 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.65.1 lib/rubocop/cop/style/string_hash_keys.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.65.0 lib/rubocop/cop/style/string_hash_keys.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.64.1 lib/rubocop/cop/style/string_hash_keys.rb