Sha256: a1aceb14bc847b353b12fa425035bfdfbaa09780a23c823ba7c9b24327cd3398

Contents?: true

Size: 1.53 KB

Versions: 20

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for the use of strings as keys in hashes. The use of
      # symbols is preferred instead.
      #
      # @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)
          return if receive_environments_method?(node)

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

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

Version data entries

20 entries across 20 versions & 3 rubygems

Version Path
rubocop-1.21.0 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.20.0 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.19.1 lib/rubocop/cop/style/string_hash_keys.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/rubocop-1.18.3/lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.19.0 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.18.4 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.18.3 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.18.2 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.18.1 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.18.0 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.17.0 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.16.1 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.16.0 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.15.0 lib/rubocop/cop/style/string_hash_keys.rb
cocRb-0.1.0 .bundle/ruby/3.0.0/gems/rubocop-1.14.0/lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.14.0 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.13.0 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.12.1 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.12.0 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-1.11.0 lib/rubocop/cop/style/string_hash_keys.rb