Sha256: 28b09c8edd6a249086a5f0be6b2e13be52a3a5cae235bdae90bc247efaeb874c

Contents?: true

Size: 1.36 KB

Versions: 4

Compression:

Stored size: 1.36 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    class AvoidGlobalVars < Cop
      MSG = 'Do not introduce global variables.'

      # predefined global variables their English aliases
      # http://www.zenspider.com/Languages/Ruby/QuickRef.html
      BUILT_IN_VARS = %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
        $-0 $-a $-d $-F $-i $-I $-l $-p $-v $-w
      )

      def on_gvar(node)
        check(node)

        super
      end

      def on_gvasgn(node)
        check(node)

        super
      end

      def check(node)
        global_var, = *node

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-0.8.3 lib/rubocop/cop/avoid_global_vars.rb
rubocop-0.8.2 lib/rubocop/cop/avoid_global_vars.rb
rubocop-0.8.1 lib/rubocop/cop/avoid_global_vars.rb
rubocop-0.8.0 lib/rubocop/cop/avoid_global_vars.rb