Class: Rubocop::Cop::Style::AvoidPerlisms

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

Overview

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

Constant Summary collapse

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

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

#on_gvar(node) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rubocop/cop/style/avoid_perlisms.rb', line 33

def on_gvar(node)
  global_var, = *node
  global_var = global_var

  if PREFERRED_VARS[global_var]
    add_offence(
      :convention,
      node.loc.expression,
      "Prefer #{PREFERRED_VARS[global_var]} over #{global_var}."
    )
  end
end