Class: RuboCop::Cop::Lint::UnescapedBracketInRegexp
- Extended by:
- AutoCorrector
- Defined in:
- lib/rubocop/cop/lint/unescaped_bracket_in_regexp.rb
Overview
Checks for Regexpes (both literals and via ‘Regexp.new` / `Regexp.compile`) that contain unescaped `]` characters.
It emulates the following Ruby warning:
- source,ruby
$ ruby -e ‘/abc]123/’ -e:1: warning: regular expression has ‘]’ without escape: /abc]123/
Constant Summary collapse
- MSG =
'Regular expression has `]` without escape.'
- RESTRICT_ON_SEND =
%i[new compile].freeze
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
Methods included from AutoCorrector
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, #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
#on_regexp(node) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/rubocop/cop/lint/unescaped_bracket_in_regexp.rb', line 45 def on_regexp(node) RuboCop::Util.silence_warnings do node.parsed_tree&.each_expression do |expr| detect_offenses(node, expr) end end end |
#on_send(node) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/rubocop/cop/lint/unescaped_bracket_in_regexp.rb', line 53 def on_send(node) # Ignore nodes that contain interpolation return if node.each_descendant(:dstr).any? regexp_constructor(node) do |text| Regexp::Parser.parse(text.value)&.each_expression do |expr| detect_offenses(text, expr) end rescue Regexp::Parser::Error # Upon encountering an invalid regular expression, # we aim to proceed and identify any remaining potential offenses. end end |
#regexp_constructor(node) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/rubocop/cop/lint/unescaped_bracket_in_regexp.rb', line 37 def_node_search :regexp_constructor, <<~PATTERN (send (const {nil? cbase} :Regexp) {:new :compile} $str ... ) PATTERN |