Sha256: 5ad4479c02a8b75e8020cf43ad78e7e4c4664d68ca96697cd07dc0a0b4474a8a

Contents?: true

Size: 1.47 KB

Versions: 14

Compression:

Stored size: 1.47 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 < Cop
        MSG = 'Prefer symbols instead of strings as hash keys.'

        def_node_matcher :string_hash_key?, <<-PATTERN
          (pair (str _) _)
        PATTERN

        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)
        end

        def autocorrect(node)
          lambda do |corrector|
            symbol_content = node.str_content.to_sym.inspect
            corrector.replace(node.source_range, symbol_content)
          end
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/string_hash_keys.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/string_hash_keys.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/string_hash_keys.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/string_hash_keys.rb
zuora_connect_ui-0.8.3 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/string_hash_keys.rb
zuora_connect_ui-0.8.2 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/string_hash_keys.rb
zuora_connect_ui-0.8.1 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/string_hash_keys.rb
zuora_connect_ui-0.8.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/string_hash_keys.rb
zuora_connect_ui-0.7.1 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/string_hash_keys.rb
zuora_connect_ui-0.7.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/string_hash_keys.rb
rubocop-0.72.0 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-0.71.0 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-0.70.0 lib/rubocop/cop/style/string_hash_keys.rb
rubocop-0.69.0 lib/rubocop/cop/style/string_hash_keys.rb