class Rubocop::Cop::Style::RegexpLiteral

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.

Public Class Methods

max_slashes() click to toggle source
# File lib/rubocop/cop/style/regexp_literal.rb, line 23
def self.max_slashes
  RegexpLiteral.config['MaxSlashes']
end

Public Instance Methods

on_regexp(node) click to toggle source
Calls superclass method
# File lib/rubocop/cop/style/regexp_literal.rb, line 10
def on_regexp(node)
  slashes = node.loc.expression.source[1...-1].scan(/\//).size
  max = RegexpLiteral.max_slashes
  msg = if node.loc.begin.is?('/')
          error_message('') if slashes > max
        else
          error_message('only ') if slashes <= max
        end
  add_offence(:convention, node.loc.expression, msg) if msg

  super
end

Private Instance Methods

error_message(word) click to toggle source
# File lib/rubocop/cop/style/regexp_literal.rb, line 29
def error_message(word)
  sprintf('Use %%r %sfor regular expressions matching more ' +
          "than %d '/' character%s.",
          word,
          RegexpLiteral.max_slashes,
          RegexpLiteral.max_slashes == 1 ? '' : 's')
end