Sha256: d8b45cee740ec4621d8504c1169fbf6750ebe87ce8f50f1d389e52a1da776c11

Contents?: true

Size: 1.35 KB

Versions: 191

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for consistent usage of `ENV['HOME']`. If `nil` is used as
      # the second argument of `ENV.fetch`, it is treated as a bad case like `ENV[]`.
      #
      # @safety
      #   The cop is unsafe because the result when `nil` is assigned to `ENV['HOME']` changes:
      #
      #   [source,ruby]
      #   ----
      #   ENV['HOME'] = nil
      #   ENV['HOME'] # => nil
      #   Dir.home    # => '/home/foo'
      #   ----
      #
      # @example
      #
      #   # bad
      #   ENV['HOME']
      #   ENV.fetch('HOME', nil)
      #
      #   # good
      #   Dir.home
      #
      #   # good
      #   ENV.fetch('HOME', default)
      #
      class EnvHome < Base
        extend AutoCorrector

        MSG = 'Use `Dir.home` instead.'
        RESTRICT_ON_SEND = %i[[] fetch].freeze

        # @!method env_home?(node)
        def_node_matcher :env_home?, <<~PATTERN
          (send
            (const {cbase nil?} :ENV) {:[] :fetch}
            (str "HOME")
            ...)
        PATTERN

        def on_send(node)
          return unless env_home?(node)
          return if node.arguments.count == 2 && !node.arguments[1].nil_type?

          add_offense(node) do |corrector|
            corrector.replace(node, 'Dir.home')
          end
        end
      end
    end
  end
end

Version data entries

191 entries across 184 versions & 18 rubygems

Version Path
rubocop-1.70.0 lib/rubocop/cop/style/env_home.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/env_home.rb
rubocop-1.69.2 lib/rubocop/cop/style/env_home.rb
rubocop-1.69.1 lib/rubocop/cop/style/env_home.rb
rubocop-1.69.0 lib/rubocop/cop/style/env_home.rb
rubocop-1.68.0 lib/rubocop/cop/style/env_home.rb
rubocop-1.67.0 lib/rubocop/cop/style/env_home.rb
rubocop-1.66.1 lib/rubocop/cop/style/env_home.rb
rubocop-1.66.0 lib/rubocop/cop/style/env_home.rb
rubocop-1.65.1 lib/rubocop/cop/style/env_home.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/env_home.rb
rubocop-1.65.0 lib/rubocop/cop/style/env_home.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/env_home.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/env_home.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/env_home.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/env_home.rb
rubocop-1.64.1 lib/rubocop/cop/style/env_home.rb
rubocop-1.63.4 lib/rubocop/cop/style/env_home.rb
rubocop-1.63.3 lib/rubocop/cop/style/env_home.rb
rubocop-1.63.2 lib/rubocop/cop/style/env_home.rb