Class: RuboCop::Cop::Lint::NoENV

Inherits:
RuboCop::Cop show all
Defined in:
lib/rubocop/cop/lint/no_env.rb

Overview

Examples:

# bad
ENV[]

# bad
ENV.fetch(..)

# good
GetEnv[]

# good
GetEnv.fetch(...)

Constant Summary collapse

MSG =
'Use `GetEnv` instead of `ENV`.'

Instance Method Summary collapse

Instance Method Details

#autocorrect(node) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/rubocop/cop/lint/no_env.rb', line 31

def autocorrect(node)
  lambda do |corrector|
    if Gem::Version.new(RuboCop::Version.version) >= Gem::Version.new('0.82.0')
      corrector.replace(node, 'GetEnv')
    else
      corrector.replace(node.loc.expression, 'GetEnv')
    end
  end
end

#on_const(node) ⇒ Object



24
25
26
27
28
29
# File 'lib/rubocop/cop/lint/no_env.rb', line 24

def on_const(node)
  parent = node.parent
  return unless is_ENV_index?(parent) || is_ENV_fetch?(parent)

  add_offense(node)
end