Class: Rubocop::Cop::Style::RegexpLiteral

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/style/regexp_literal.rb

Overview

This cop checks for regexp literals and reports offences based on how many escaped slashes there are in the regexp and on the value of the configuration parameter MaxSlashes.

Constant Summary

Constants inherited from Cop

Cop::OPERATOR_METHODS

Instance Attribute Summary

Attributes inherited from Cop

#config, #corrections, #offences, #processed_source

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, all, #autocorrect?, #convention, #cop_config, cop_name, #cop_name, cop_type, #debug?, #ignore_node, inherited, #initialize, lint?, #message, non_rails, rails?, style?, #support_autocorrect?, #warning

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#max_slashesObject



21
22
23
# File 'lib/rubocop/cop/style/regexp_literal.rb', line 21

def max_slashes
  cop_config['MaxSlashes']
end

#on_regexp(node) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/rubocop/cop/style/regexp_literal.rb', line 10

def on_regexp(node)
  slashes = node.loc.expression.source.count('/')
  msg = if node.loc.begin.is?('/')
          slashes -= 2 # subtract delimiters
          error_message('') if slashes > max_slashes
        else
          error_message('only ') if slashes <= max_slashes
        end
  convention(node, :expression, msg) if msg
end