Sha256: fd60235b214a848eead01ce90bdf223461a4f3bc95c42a3ad84a1df8b2dc69c3

Contents?: true

Size: 1.16 KB

Versions: 12

Compression:

Stored size: 1.16 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module Style
      # This cop checks if usage of %() or %Q() matches configuration.
      class BarePercentLiterals < Cop
        include ConfigurableEnforcedStyle

        MSG = 'Use `%%%s` instead of `%%%s`.'

        def on_dstr(node)
          check(node)
        end

        def on_str(node)
          check(node)
        end

        private

        def check(node)
          return if node.loc.respond_to?(:heredoc_body)
          return unless node.loc.respond_to?(:begin)
          return unless node.loc.begin

          msg = case node.loc.begin.source
                when /^%[^\w]/
                  format(MSG, 'Q', '') if style == :percent_q
                when /^%Q/
                  format(MSG, '', 'Q') if style == :bare_percent
                end
          add_offense(node, :begin, msg) if msg
        end

        def autocorrect(node)
          src = node.loc.begin.source
          replacement = src.start_with?('%Q') ? '%' : '%Q'
          @corrections << lambda do |corrector|
            corrector.replace(node.loc.begin, src.sub(/%Q?/, replacement))
          end
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/bare_percent_literals.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.30.1 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.30.0 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.29.1 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.29.0 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.28.0 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.27.1 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.27.0 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.26.1 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.26.0 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.25.0 lib/rubocop/cop/style/bare_percent_literals.rb