Class: RuboCop::Cop::Style::SpecialGlobalVars
- Extended by:
- AutoCorrector
- Includes:
- ConfigurableEnforcedStyle
- Defined in:
- lib/rubocop/cop/style/special_global_vars.rb
Overview
This cop looks for uses of Perl-style global variables.
Constant Summary collapse
- MSG_BOTH =
'Prefer `%<prefer>s` from the stdlib \'English\' ' \ 'module (don\'t forget to require it) or `%<regular>s` over ' \ '`%<global>s`.'
- MSG_ENGLISH =
'Prefer `%<prefer>s` from the stdlib \'English\' ' \ 'module (don\'t forget to require it) over `%<global>s`.'
- MSG_REGULAR =
'Prefer `%<prefer>s` over `%<global>s`.'
- ENGLISH_VARS =
rubocop:disable Style/MutableConstant
{ # rubocop:disable Style/MutableConstant :$: => [:$LOAD_PATH], :$" => [:$LOADED_FEATURES], :$0 => [:$PROGRAM_NAME], :$! => [:$ERROR_INFO], :$@ => [:$ERROR_POSITION], :$; => %i[$FIELD_SEPARATOR $FS], :$, => %i[$OUTPUT_FIELD_SEPARATOR $OFS], :$/ => %i[$INPUT_RECORD_SEPARATOR $RS], :$\ => %i[$OUTPUT_RECORD_SEPARATOR $ORS], :$. => %i[$INPUT_LINE_NUMBER $NR], :$_ => [:$LAST_READ_LINE], :$> => [:$DEFAULT_OUTPUT], :$< => [:$DEFAULT_INPUT], :$$ => %i[$PROCESS_ID $PID], :$? => [:$CHILD_STATUS], :$~ => [:$LAST_MATCH_INFO], :$= => [:$IGNORECASE], :$* => %i[$ARGV ARGV] }
- PERL_VARS =
ENGLISH_VARS.flat_map { |k, vs| vs.map { |v| [v, [k]] } }.to_h
- NON_ENGLISH_VARS =
Anything not in this set is provided by the English library.
Set.new(%i[ $LOAD_PATH $LOADED_FEATURES $PROGRAM_NAME ARGV ]).freeze
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #autocorrect(corrector, node, global_var) ⇒ Object
- #message(global_var) ⇒ Object
- #on_gvar(node) ⇒ Object
Methods included from AutoCorrector
Methods included from ConfigurableEnforcedStyle
#alternative_style, #alternative_styles, #ambiguous_style_detected, #correct_style_detected, #detected_style, #detected_style=, #no_acceptable_style!, #no_acceptable_style?, #opposite_style_detected, #style, #style_configured?, #style_detected, #style_parameter_name, #supported_styles, #unexpected_style_detected
Methods inherited from Base
#add_global_offense, #add_offense, autocorrect_incompatible_with, badge, callbacks_needed, #callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, joining_forces, lint?, match?, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version
Methods included from ExcludeLimit
Methods included from AutocorrectLogic
#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?
Methods included from IgnoredNode
#ignore_node, #ignored_node?, #part_of_ignored_node?
Methods included from Util
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Method Details
#autocorrect(corrector, node, global_var) ⇒ Object
129 130 131 132 133 |
# File 'lib/rubocop/cop/style/special_global_vars.rb', line 129 def autocorrect(corrector, node, global_var) node = node.parent while node.parent&.begin_type? && node.parent.children.one? corrector.replace(node, replacement(node, global_var)) end |
#message(global_var) ⇒ Object
119 120 121 122 123 124 125 126 127 |
# File 'lib/rubocop/cop/style/special_global_vars.rb', line 119 def (global_var) if style == :use_english_names (global_var) else format(MSG_REGULAR, prefer: preferred_names(global_var).first, global: global_var) end end |
#on_gvar(node) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/rubocop/cop/style/special_global_vars.rb', line 103 def on_gvar(node) global_var, = *node return unless (preferred = preferred_names(global_var)) if preferred.include?(global_var) correct_style_detected else opposite_style_detected add_offense(node, message: (global_var)) do |corrector| autocorrect(corrector, node, global_var) end end end |