Sha256: fca1652198fd0ff54058df321ce010433c0c0442d7c4cf6796736738a2d97bb7

Contents?: true

Size: 1.15 KB

Versions: 3

Compression:

Stored size: 1.15 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # 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.
      class RegexpLiteral < Cop
        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

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

        private

        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
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-0.12.0 lib/rubocop/cop/style/regexp_literal.rb
rubocop-0.11.1 lib/rubocop/cop/style/regexp_literal.rb
rubocop-0.11.0 lib/rubocop/cop/style/regexp_literal.rb