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

Extensions in SketchUp all share the same Ruby environment on the user’s machine. Because of this it’s important that each extension isolate itself to avoid clashing with other extensions.

Extensions submitted to Extension Warehouse is expected to not define global variables.

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.'
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
].freeze
SKETCHUP_VARS =
%i[
  $loaded_files
].freeze
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 inherited from SketchUp::Cop

SketchUp::Cop::SKETCHUP_DEPARTMENT_SEVERITY

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)


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

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

#check(node) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/rubocop/sketchup/cop/requirements/global_variables.rb', line 85

def check(node)
  global_var, = *node

  return if allowed_var?(global_var)

  add_offense(node, location: :name)
end

#on_gvar(node) ⇒ Object



76
77
78
79
# File 'lib/rubocop/sketchup/cop/requirements/global_variables.rb', line 76

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

#on_gvasgn(node) ⇒ Object



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

def on_gvasgn(node)
  check(node)
end

#read_allowed?(global_var) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/rubocop/sketchup/cop/requirements/global_variables.rb', line 72

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