Class: RuboCop::Cop::SketchupRequirements::GlobalVariables

Inherits:
SketchUp::Cop
  • Object
show all
Includes:
SketchUp::DynamicComponentGlobals, SketchUp::NoCommentDisable
Defined in:
lib/rubocop/sketchup/cop/requirements/global_variables.rb

Overview

This cops 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.

Constant Summary collapse

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

predefined global variables their English aliases www.zenspider.com/Languages/Ruby/QuickRef.html

%i[
  $: $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
]
SKETCHUP_VARS =
%i[
  $loaded_files
]
READ_ONLY_VARS =

Some globals, like DC’s, are being read from so often that it’s better to ignore these to reduce noise.

DC_GLOBALS
ALLOWED_VARS =
BUILT_IN_VARS | SKETCHUP_VARS

Constants included from SketchUp::DynamicComponentGlobals

SketchUp::DynamicComponentGlobals::DC_GLOBALS

Constants included from SketchUp::Config

SketchUp::Config::DEFAULT_CONFIGURATION

Instance Method Summary collapse

Methods inherited from SketchUp::Cop

inherited, #relevant_file?

Instance Method Details

#allowed_var?(global_var) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/rubocop/sketchup/cop/requirements/global_variables.rb', line 61

def allowed_var?(global_var)
  ALLOWED_VARS.include?(global_var)
end

#check(node) ⇒ Object



78
79
80
81
82
# File 'lib/rubocop/sketchup/cop/requirements/global_variables.rb', line 78

def check(node)
  global_var, = *node

  add_offense(node, location: :name, severity: :error) unless allowed_var?(global_var)
end

#on_gvar(node) ⇒ Object



69
70
71
72
# File 'lib/rubocop/sketchup/cop/requirements/global_variables.rb', line 69

def on_gvar(node)
  global_var, = *node
  check(node) unless read_allowed?(global_var)
end

#on_gvasgn(node) ⇒ Object



74
75
76
# File 'lib/rubocop/sketchup/cop/requirements/global_variables.rb', line 74

def on_gvasgn(node)
  check(node)
end

#read_allowed?(global_var) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/rubocop/sketchup/cop/requirements/global_variables.rb', line 65

def read_allowed?(global_var)
  READ_ONLY_VARS.include?(global_var)
end