Class: RuboCop::Cop::Lint::MixedCaseRange

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/lint/mixed_case_range.rb

Overview

Checks for mixed-case character ranges since they include likely unintended characters.

Offenses are registered for regexp character classes like /[A-z]/ as well as range objects like ('A'..'z').

Note
Range objects cannot be autocorrected.

Examples:


# bad
r = /[A-z]/

# good
r = /[A-Za-z]/

Cop Safety Information:

  • The cop autocorrects regexp character classes by replacing one character range with two: A-z becomes A-Za-z. In most cases this is probably what was originally intended but it changes the regexp to no longer match symbols it used to include. For this reason, this cop’s autocorrect is unsafe (it will change the behavior of the code).

Constant Summary collapse

MSG =
'Ranges from upper to lower case ASCII letters may include unintended ' \
'characters. Instead of `A-z` (which also includes several symbols) ' \
'specify each range individually: `A-Za-z` and individually specify any symbols.'
RANGES =
[('a'..'z').freeze, ('A'..'Z').freeze].freeze

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #begin_investigation, 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, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

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

silence_warnings

Constructor Details

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

Instance Method Details

#each_unsafe_regexp_range(node) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rubocop/cop/lint/mixed_case_range.rb', line 56

def each_unsafe_regexp_range(node)
  node.parsed_tree&.each_expression do |expr|
    next if skip_expression?(expr)

    range_pairs(expr).reject do |range_start, range_end|
      next if skip_range?(range_start, range_end)

      next unless unsafe_range?(range_start.text, range_end.text)

      yield(build_source_range(range_start, range_end))
    end
  end
end

#on_irange(node) ⇒ Object Also known as: on_erange



37
38
39
40
41
42
43
44
45
# File 'lib/rubocop/cop/lint/mixed_case_range.rb', line 37

def on_irange(node)
  return unless node.children.compact.all?(&:str_type?)

  range_start, range_end = node.children

  return if range_start.nil? || range_end.nil?

  add_offense(node) if unsafe_range?(range_start.value, range_end.value)
end

#on_regexp(node) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/rubocop/cop/lint/mixed_case_range.rb', line 48

def on_regexp(node)
  each_unsafe_regexp_range(node) do |loc|
    add_offense(loc) do |corrector|
      corrector.replace(loc, rewrite_regexp_range(loc.source))
    end
  end
end