Class: Rubocop::Cop::Style::SpecialGlobalVars

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

Overview

This cop looks for uses of Perl-style global variables.

Constant Summary collapse

MSG =
'Prefer %s over %s.'
PREFERRED_VARS =
{
  '$:' => '$LOAD_PATH',
  '$"' => '$LOADED_FEATURES',
  '$0' => '$PROGRAM_NAME',
  '$!' => '$ERROR_INFO from English library',
  '$@' => '$ERROR_POSITION from English library',
  '$;' => '$FS or $FIELD_SEPARATOR from English library',
  '$,' => '$OFS or $OUTPUT_FIELD_SEPARATOR from English library',
  '$/' => '$RS or $INPUT_RECORD_SEPARATOR from English library',
  '$\\' => '$ORS or $OUTPUT_RECORD_SEPARATOR from English library',
  '$.' => '$NR or $INPUT_LINE_NUMBER from English library',
  '$_' => '$LAST_READ_LINE from English library',
  '$>' => '$DEFAULT_OUTPUT from English library',
  '$<' => '$DEFAULT_INPUT from English library',
  '$$' => '$PID or $PROCESS_ID from English library',
  '$?' => '$CHILD_STATUS from English library',
  '$~' => '$LAST_MATCH_INFO from English library',
  '$=' => '$IGNORECASE from English library',
  '$*' => '$ARGV from English library or ARGV constant',
  '$&' => '$MATCH from English library',
  '$`' => '$PREMATCH from English library',
  '$\'' => '$POSTMATCH from English library',
  '$+' => '$LAST_PAREN_MATCH from English library'
}.symbolize_keys

Constants inherited from Cop

Cop::OPERATOR_METHODS

Instance Attribute Summary

Attributes inherited from Cop

#config, #corrections, #offences, #processed_source

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, all, #autocorrect?, #convention, #cop_config, cop_name, #cop_name, cop_type, #debug?, #ignore_node, inherited, #initialize, lint?, rails?, style?, #warning

Constructor Details

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

Instance Method Details

#autocorrect_action(node) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/rubocop/cop/style/special_global_vars.rb', line 46

def autocorrect_action(node)
  @corrections << lambda do |corrector|
    global_var, = *node

    corrector.replace(node.loc.expression,
                      PREFERRED_VARS[global_var])
  end
end

#message(node) ⇒ Object



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

def message(node)
  global_var, = *node
  MSG.format(PREFERRED_VARS[global_var], global_var)
end

#on_gvar(node) ⇒ Object



35
36
37
38
39
# File 'lib/rubocop/cop/style/special_global_vars.rb', line 35

def on_gvar(node)
  global_var, = *node

  convention(node, :expression) if PREFERRED_VARS[global_var]
end