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.

Instance Attribute Summary

Attributes inherited from Cop

#autocorrect, #corrections, #debug, #disabled_lines, #offences

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, all, #autocorrect_action, cop_name, cop_type, #do_autocorrect, #ignore_node, inherited, #initialize, lint?, #name, rails?, style?

Constructor Details

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

Class Method Details

.max_slashesObject



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

def self.max_slashes
  RegexpLiteral.config['MaxSlashes']
end

Instance Method Details

#on_regexp(node) ⇒ Object



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

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