Class: Rubocop::Cop::Style::AvoidGlobalVars

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/style/avoid_global_vars.rb

Overview

This cops looks for uses of global variables. It does not report offences for built-in global variables.

Constant Summary collapse

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

predefined global variables their English aliases http://www.zenspider.com/Languages/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
).map(&:to_sym)

Instance Attribute Summary

Attributes inherited from Cop

#autocorrect, #corrections, #debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, all, #autocorrect_action, cop_name, cop_type, #do_autocorrect, #ignore_node, inherited, #initialize, lint?, #name, rails?, style?

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#check(node) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/rubocop/cop/style/avoid_global_vars.rb', line 49

def check(node)
  global_var, = *node

  unless BUILT_IN_VARS.include?(global_var)
    add_offence(:convention,
                node.loc.name,
                MSG)
  end
end

#on_gvar(node) ⇒ Object



41
42
43
# File 'lib/rubocop/cop/style/avoid_global_vars.rb', line 41

def on_gvar(node)
  check(node)
end

#on_gvasgn(node) ⇒ Object



45
46
47
# File 'lib/rubocop/cop/style/avoid_global_vars.rb', line 45

def on_gvasgn(node)
  check(node)
end