Sha256: 3f96e7f03f61936af76aecead29604111f8998326d6bdabc1cbe50f1dabc9721

Contents?: true

Size: 1.14 KB

Versions: 9

Compression:

Stored size: 1.14 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'
          lambda do |corrector|
            corrector.replace(node.loc.begin, src.sub(/%Q?/, replacement))
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rubocop-0.35.1 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.35.0 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.34.2 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.34.1 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.34.0 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.33.0 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.32.1 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.32.0 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.31.0 lib/rubocop/cop/style/bare_percent_literals.rb