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.
Constant Summary
-
BUILTIN_VARS =
# File 'lib/rubocop/cop/style/special_global_vars.rb', line 127PERL_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 )
-
ENGLISH_VARS =
# File 'lib/rubocop/cop/style/special_global_vars.rb', line 99{ # 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] }
-
LIBRARY_NAME =
# File 'lib/rubocop/cop/style/special_global_vars.rb', line 144'English'
-
MSG_BOTH =
# File 'lib/rubocop/cop/style/special_global_vars.rb', line 92'Prefer `%<prefer>s` from the stdlib \'English\' ' \ 'module (don\'t forget to require it) or `%<regular>s` over ' \ '`%<global>s`.'
-
MSG_ENGLISH =
# File 'lib/rubocop/cop/style/special_global_vars.rb', line 95'Prefer `%<prefer>s` from the stdlib \'English\' ' \ 'module (don\'t forget to require it) over `%<global>s`.'
-
MSG_REGULAR =
# File 'lib/rubocop/cop/style/special_global_vars.rb', line 97'Prefer `%<prefer>s` over `%<global>s`.'
-
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 =
# File 'lib/rubocop/cop/style/special_global_vars.rb', line 123ENGLISH_VARS.flat_map { |k, vs| vs.map { |v| [v, [k]] } }.to_h
-
STYLE_VARS_MAP =
# File 'lib/rubocop/cop/style/special_global_vars.rb', line 138{ use_english_names: ENGLISH_VARS, use_perl_names: PERL_VARS, use_builtin_english_names: BUILTIN_VARS }.freeze
::RuboCop::Cop::Base
- Inherited
EMPTY_OFFENSES, RESTRICT_ON_SEND
::RuboCop::Cop::ConfigurableEnforcedStyle
- Included
::RuboCop::Cop::RangeHelp
- Included
::RuboCop::Cop::RequireLibrary
- Included
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 |
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
- #add_require_english? ⇒ Boolean readonly private
::RuboCop::Cop::ConfigurableEnforcedStyle
- Included
::RuboCop::Cop::Base
- Inherited
::RuboCop::Cop::AutocorrectLogic
- Included
Instance Method Summary
- #autocorrect(corrector, node, global_var)
- #message(global_var)
- #on_gvar(node)
- #on_new_investigation
- #english_name_replacement(preferred_name, node) private
- #format_english_message(global_var) private
-
#format_list(items)
private
For now, we assume that lists are 2 items or less.
- #format_message(english, regular, global) private
- #matching_styles(global) private
- #preferred_names(global) private
- #replacement(node, global_var) private
- #should_require_english?(global_var) ⇒ Boolean private
::RuboCop::Cop::RequireLibrary
- Included
#ensure_required, #on_send, #remove_subsequent_requires, #on_new_investigation, #require_any_library?, #require_library_name? |
::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 |
#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
#disable_offense, #disable_offense_at_end_of_line, #disable_offense_before_and_after, #disable_offense_with_eol_or_surround_comment, #max_line_length, | |
#range_by_lines | Expand the given range to include all of any lines it covers. |
#range_of_first_line, #range_overlaps_offense?, #string_continuation, #string_continuation?, #surrounding_heredoc, #surrounding_percent_array |
::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 (global_var) regular, english = ENGLISH_VARS[global_var].partition do |var| NON_ENGLISH_VARS.include? var end (english, regular, global_var) end
#format_list(items) (private)
For now, we assume that lists are 2 items or less. Easy grammar!
# 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 (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 (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)
[ 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: (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)
# 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