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
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/env_home.rb
rubocop-1.63.1 lib/rubocop/cop/style/env_home.rb
rubocop-1.63.0 lib/rubocop/cop/style/env_home.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/style/env_home.rb
rubocop-1.62.1 lib/rubocop/cop/style/env_home.rb
rubocop-1.62.0 lib/rubocop/cop/style/env_home.rb
rubocop-1.61.0 lib/rubocop/cop/style/env_home.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/style/env_home.rb
rubocop-1.60.2 lib/rubocop/cop/style/env_home.rb
study_line-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/env_home.rb
study_line-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/env_home.rb
study_line-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/env_home.rb
study_line-0.2.4 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/env_home.rb
study_line-0.2.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/env_home.rb
study_line-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/env_home.rb
rubocop-1.60.1 lib/rubocop/cop/style/env_home.rb
study_line-0.2.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/env_home.rb
study_line-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/env_home.rb
rubocop-1.60.0 lib/rubocop/cop/style/env_home.rb
harbr-0.2.10 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/env_home.rb