Class: RuboCop::Cop::Style::GlobalVars

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/style/global_vars.rb

Overview

Looks for uses of global variables. It does not report offenses for built-in global variables. Built-in global variables are allowed by default. Additionally users can allow additional variables via the AllowedVariables option.

Note that backreferences like $1, $2, etc are not global variables.

Examples:

# bad
$foo = 2
bar = $foo + 5

# good
FOO = 2
foo = 2
$stdin.read

Constant Summary collapse

MSG =
'Do not introduce global variables.'
BUILT_IN_VARS =

built-in global variables and their English aliases www.zenspider.com/ruby/quickref.html

%w[
  $: $LOAD_PATH
  $" $LOADED_FEATURES
  $0 $PROGRAM_NAME
  $! $ERROR_INFO
  $@ $ERROR_POSITION
  $; $FS $FIELD_SEPARATOR
  $, $OFS $OUTPUT_FIELD_SEPARATOR
  $/ $RS $INPUT_RECORD_SEPARATOR
  $\\ $ORS $OUTPUT_RECORD_SEPARATOR
  $. $NR $INPUT_LINE_NUMBER
  $_ $LAST_READ_LINE
  $> $DEFAULT_OUTPUT
  $< $DEFAULT_INPUT
  $$ $PID $PROCESS_ID
  $? $CHILD_STATUS
  $~ $LAST_MATCH_INFO
  $= $IGNORECASE
  $* $ARGV
  $& $MATCH
  $` $PREMATCH
  $' $POSTMATCH
  $+ $LAST_PAREN_MATCH
  $stdin $stdout $stderr
  $DEBUG $FILENAME $VERBOSE $SAFE
  $-0 $-a $-d $-F $-i $-I $-l $-p $-v $-w
  $CLASSPATH $JRUBY_VERSION $JRUBY_REVISION $ENV_JAVA
].map(&:to_sym)

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#allowed_var?(global_var) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/rubocop/cop/style/global_vars.rb', line 60

def allowed_var?(global_var)
  BUILT_IN_VARS.include?(global_var) || user_vars.include?(global_var)
end

#check(node) ⇒ Object



72
73
74
# File 'lib/rubocop/cop/style/global_vars.rb', line 72

def check(node)
  add_offense(node.loc.name) unless allowed_var?(node.name)
end

#on_gvar(node) ⇒ Object



64
65
66
# File 'lib/rubocop/cop/style/global_vars.rb', line 64

def on_gvar(node)
  check(node)
end

#on_gvasgn(node) ⇒ Object



68
69
70
# File 'lib/rubocop/cop/style/global_vars.rb', line 68

def on_gvasgn(node)
  check(node)
end

#user_varsObject



56
57
58
# File 'lib/rubocop/cop/style/global_vars.rb', line 56

def user_vars
  cop_config['AllowedVariables'].map(&:to_sym)
end