123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::Style::SpecialGlobalVars

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ::RuboCop::Cop::AutoCorrector, ::RuboCop::Cop::Base, ::RuboCop::ExcludeLimit, NodePattern::Macros, RuboCop::AST::Sexp
Instance Chain:
Inherits: RuboCop::Cop::Base
Defined in: lib/rubocop/cop/style/special_global_vars.rb

Overview

Looks for uses of Perl-style global variables. Correcting to global variables in the 'English' library will add a require statement to the top of the file if enabled by RequireEnglish config.

Examples:

EnforcedStyle: use_english_names (default)

# good
require 'English' # or this could be in another file.

puts $LOAD_PATH
puts $LOADED_FEATURES
puts $PROGRAM_NAME
puts $ERROR_INFO
puts $ERROR_POSITION
puts $FIELD_SEPARATOR # or $FS
puts $OUTPUT_FIELD_SEPARATOR # or $OFS
puts $INPUT_RECORD_SEPARATOR # or $RS
puts $OUTPUT_RECORD_SEPARATOR # or $ORS
puts $INPUT_LINE_NUMBER # or $NR
puts $LAST_READ_LINE
puts $DEFAULT_OUTPUT
puts $DEFAULT_INPUT
puts $PROCESS_ID # or $PID
puts $CHILD_STATUS
puts $LAST_MATCH_INFO
puts $IGNORECASE
puts $ARGV # or ARGV

EnforcedStyle: use_perl_names

# good
puts $:
puts $"
puts $0
puts $!
puts $@
puts $;
puts $,
puts $/
puts $\
puts $.
puts $_
puts $>
puts $<
puts $$
puts $?
puts $~
puts $=
puts $*

EnforcedStyle: use_builtin_english_names

# good
# Like {use_perl_names} but allows builtin global vars.
puts $LOAD_PATH
puts $LOADED_FEATURES
puts $PROGRAM_NAME
puts ARGV
puts $:
puts $"
puts $0
puts $!
puts $@
puts $;
puts $,
puts $/
puts $\
puts $.
puts $_
puts $>
puts $<
puts $$
puts $?
puts $~
puts $=
puts $*

Cop Safety Information:

  • Autocorrection is marked as unsafe because if RequireEnglish is not true, replacing perl-style variables with english variables will break.

Constant Summary

::RuboCop::Cop::Base - Inherited

EMPTY_OFFENSES, RESTRICT_ON_SEND

::RuboCop::Cop::ConfigurableEnforcedStyle - Included

SYMBOL_TO_STRING_CACHE

::RuboCop::Cop::RangeHelp - Included

BYTE_ORDER_MARK, NOT_GIVEN

::RuboCop::Cop::RequireLibrary - Included

RESTRICT_ON_SEND

Class Attribute Summary

::RuboCop::Cop::AutoCorrector - Extended

::RuboCop::Cop::Base - Inherited

.gem_requirements, .lint?,
.support_autocorrect?

Returns if class supports autocorrect.

.support_multiple_source?

Override if your cop should be called repeatedly for multiple investigations Between calls to #on_new_investigation and on_investigation_end, the result of processed_source will remain constant.

Class Method Summary

::RuboCop::Cop::Base - Inherited

.autocorrect_incompatible_with

List of cops that should not try to autocorrect at the same time as this cop.

.badge

Naming.

.callbacks_needed, .cop_name, .department,
.documentation_url

Returns a url to view this cops documentation online.

.exclude_from_registry

Call for abstract Cop classes.

.inherited,
.joining_forces

Override and return the Force class(es) you need to join.

.match?

Returns true if the cop name or the cop namespace matches any of the given names.

.new,
.requires_gem

Register a version requirement for the given gem name.

.restrict_on_send

::RuboCop::ExcludeLimit - Extended

exclude_limit

Sets up a configuration option to have an exclude limit tracked.

transform

Instance Attribute Summary

Instance Method Summary

::RuboCop::Cop::RequireLibrary - Included

::RuboCop::Cop::RangeHelp - Included

#add_range, #column_offset_between,
#contents_range

A range containing only the contents of a literal with delimiters (e.g.

#directions,
#effective_column

Returns the column attribute of the range, except if the range is on the first line and there’s a byte order mark at the beginning of that line, in which case 1 is subtracted from the column value.

#final_pos, #move_pos, #move_pos_str, #range_between, #range_by_whole_lines, #range_with_comments, #range_with_comments_and_lines, #range_with_surrounding_comma, #range_with_surrounding_space, #source_range

::RuboCop::Cop::ConfigurableEnforcedStyle - Included

::RuboCop::Cop::Base - Inherited

#add_global_offense

Adds an offense that has no particular location.

#add_offense

Adds an offense on the specified range (or node with an expression) Unless that offense is disabled for this range, a corrector will be yielded to provide the cop the opportunity to autocorrect the offense.

#begin_investigation

Called before any investigation.

#callbacks_needed,
#cop_config

Configuration Helpers.

#cop_name, #excluded_file?,
#external_dependency_checksum

This method should be overridden when a cop’s behavior depends on state that lives outside of these locations:

#inspect,
#message

Gets called if no message is specified when calling add_offense or add_global_offense Cops are discouraged to override this; instead pass your message directly.

#name

Alias for Base#cop_name.

#offenses,
#on_investigation_end

Called after all on_…​

#on_new_investigation

Called before all on_…​

#on_other_file

Called instead of all on_…​

#parse

There should be very limited reasons for a Cop to do it’s own parsing.

#parser_engine,
#ready

Called between investigations.

#relevant_file?, #target_rails_version, #target_ruby_version, #annotate, #apply_correction, #attempt_correction,
#callback_argument

Reserved for Cop::Cop.

#complete_investigation

Called to complete an investigation.

#correct, #current_corrector,
#current_offense_locations

Reserved for Commissioner:

#current_offenses, #currently_disabled_lines, #custom_severity, #default_severity, #disable_uncorrectable, #enabled_line?, #file_name_matches_any?, #find_message, #find_severity, #range_for_original, #range_from_node_or_range,
#reset_investigation

Actually private methods.

#use_corrector

::RuboCop::Cop::AutocorrectLogic - Included

::RuboCop::Cop::IgnoredNode - Included

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Attribute Details

#add_require_english?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/special_global_vars.rb', line 246

def add_require_english?
  cop_config['RequireEnglish']
end

Instance Method Details

#autocorrect(corrector, node, global_var)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/special_global_vars.rb', line 175

def autocorrect(corrector, node, global_var)
  node = node.parent while node.parent&.begin_type? && node.parent.children.one?

  if should_require_english?(global_var)
    ensure_required(corrector, node, LIBRARY_NAME)

    @required_english = true
  end

  corrector.replace(node, replacement(node, global_var))
end

#english_name_replacement(preferred_name, node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/special_global_vars.rb', line 240

def english_name_replacement(preferred_name, node)
  return "\#{#{preferred_name}}" if node.begin_type?

  "{#{preferred_name}}"
end

#format_english_message(global_var) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/special_global_vars.rb', line 189

def format_english_message(global_var)
  regular, english = ENGLISH_VARS[global_var].partition do |var|
    NON_ENGLISH_VARS.include? var
  end

  format_message(english, regular, global_var)
end

#format_list(items) (private)

For now, we assume that lists are 2 items or less. Easy grammar!

[ GitHub ]

  
# File 'lib/rubocop/cop/style/special_global_vars.rb', line 211

def format_list(items)
  items.join('` or `')
end

#format_message(english, regular, global) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/special_global_vars.rb', line 197

def format_message(english, regular, global)
  if regular.empty?
    format(MSG_ENGLISH, prefer: format_list(english), global: global)
  elsif english.empty?
    format(MSG_REGULAR, prefer: format_list(regular), global: global)
  else
    format(MSG_BOTH,
           prefer: format_list(english),
           regular: format_list(regular),
           global: global)
  end
end

#matching_styles(global) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/special_global_vars.rb', line 234

def matching_styles(global)
  STYLE_VARS_MAP.filter_map do |style, vars|
    style if vars.values.flatten(1).include? global
  end
end

#message(global_var)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/special_global_vars.rb', line 167

def message(global_var)
  if style == :use_english_names
    format_english_message(global_var)
  else
    format(MSG_REGULAR, prefer: preferred_names(global_var).first, global: global_var)
  end
end

#on_gvar(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/special_global_vars.rb', line 151

def on_gvar(node)
  global_var, = *node

  return unless (preferred = preferred_names(global_var))

  if preferred.include?(global_var)
    correct_style_detected
  else
    style_detected(matching_styles(global_var))

    add_offense(node, message: message(global_var)) do |corrector|
      autocorrect(corrector, node, global_var)
    end
  end
end

#on_new_investigation

[ GitHub ]

  
# File 'lib/rubocop/cop/style/special_global_vars.rb', line 146

def on_new_investigation
  super
  @required_english = false
end

#preferred_names(global) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/special_global_vars.rb', line 226

def preferred_names(global)
  vars = STYLE_VARS_MAP.fetch(style) do
    raise ArgumentError, "Invalid style: #{style.inspect}"
  end

  vars[global]
end

#replacement(node, global_var) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/special_global_vars.rb', line 215

def replacement(node, global_var)
  parent_type = node.parent&.type
  preferred_name = preferred_names(global_var).first

  return preferred_name.to_s unless %i[dstr xstr regexp].include?(parent_type)

  return english_name_replacement(preferred_name, node) if style == :use_english_names

  "##{preferred_name}"
end

#should_require_english?(global_var) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/special_global_vars.rb', line 250

def should_require_english?(global_var)
  style == :use_english_names &&
    add_require_english? &&
    !@required_english &&
    !NON_ENGLISH_VARS.include?(preferred_names(global_var).first)
end