Class: RuboCop::Cop::Style::SpecialGlobalVars
- Extended by:
- AutoCorrector
- Includes:
- ConfigurableEnforcedStyle, RangeHelp, RequireLibrary
- 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.
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] }
- 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
- PERL_VARS =
ENGLISH_VARS.flat_map { |k, vs| vs.map { |v| [v, [k]] } }.to_h
- BUILTIN_VARS =
PERL_VARS.merge( NON_ENGLISH_VARS .select { |v| v.to_s.start_with?('$') } .flat_map { |v| [[v, [v]], PERL_VARS[v].flat_map { |a| [a, [v]] }] } .to_h )
- STYLE_VARS_MAP =
{ use_english_names: ENGLISH_VARS, use_perl_names: PERL_VARS, use_builtin_english_names: BUILTIN_VARS }.freeze
- LIBRARY_NAME =
'English'
Constants included from RequireLibrary
RequireLibrary::RESTRICT_ON_SEND
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
- #on_new_investigation ⇒ Object
Methods included from AutoCorrector
Methods included from RequireLibrary
#ensure_required, #on_send, #remove_subsequent_requires
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
#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, #cop_name, cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #offenses, #on_investigation_end, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version
Methods included from ExcludeLimit
Methods included from AutocorrectLogic
#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #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
175 176 177 178 179 180 181 182 183 184 185 |
# 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 |
#message(global_var) ⇒ Object
167 168 169 170 171 172 173 |
# File 'lib/rubocop/cop/style/special_global_vars.rb', line 167 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
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# 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: (global_var)) do |corrector| autocorrect(corrector, node, global_var) end end end |
#on_new_investigation ⇒ Object
146 147 148 149 |
# File 'lib/rubocop/cop/style/special_global_vars.rb', line 146 def on_new_investigation super @required_english = false end |