lib/rubocop/cop/style/special_global_vars.rb in rubocop-1.18.4 vs lib/rubocop/cop/style/special_global_vars.rb in rubocop-1.19.0

- old
+ new

@@ -3,13 +3,18 @@ module RuboCop module Cop module Style # # This cop 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. # # @example 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 @@ -48,10 +53,12 @@ # puts $= # puts $* # class SpecialGlobalVars < Base include ConfigurableEnforcedStyle + include RangeHelp + include RequireLibrary extend AutoCorrector MSG_BOTH = 'Prefer `%<prefer>s` from the stdlib \'English\' ' \ 'module (don\'t forget to require it) or `%<regular>s` over ' \ '`%<global>s`.' @@ -88,10 +95,12 @@ PERL_VARS.each_value(&:freeze).freeze # Anything *not* in this set is provided by the English library. NON_ENGLISH_VARS = Set.new(%i[$LOAD_PATH $LOADED_FEATURES $PROGRAM_NAME ARGV]).freeze + LIBRARY_NAME = 'English' + def on_gvar(node) global_var, = *node return unless (preferred = preferred_names(global_var)) @@ -115,10 +124,12 @@ end def autocorrect(corrector, node, global_var) node = node.parent while node.parent&.begin_type? && node.parent.children.one? + ensure_required(corrector, node, LIBRARY_NAME) if should_require_english?(global_var) + corrector.replace(node, replacement(node, global_var)) end private @@ -169,9 +180,19 @@ def english_name_replacement(preferred_name, node) return "\#{#{preferred_name}}" if node.begin_type? "{#{preferred_name}}" + end + + def add_require_english? + cop_config['RequireEnglish'] + end + + def should_require_english?(global_var) + style == :use_english_names && + add_require_english? && + !NON_ENGLISH_VARS.include?(preferred_names(global_var).first) end end end end end