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]/

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, #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?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, 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



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

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
55
56
# File 'lib/rubocop/cop/lint/mixed_case_range.rb', line 48

def on_regexp(node)
  each_unsafe_regexp_range(node) do |loc|
    next unless (replacement = regexp_range(loc.source))

    add_offense(loc) do |corrector|
      corrector.replace(loc, replacement)
    end
  end
end