Sha256: 8b2fd6a650c20167fb3406c70305c8db4ee6473515994422a8b265671d5b411c

Contents?: true

Size: 1.46 KB

Versions: 18

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

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`.'.freeze

        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

          source = node.loc.begin.source
          if requires_percent_q?(source)
            add_offense_for_wrong_style(node, 'Q', '')
          elsif requires_bare_percent?(source)
            add_offense_for_wrong_style(node, '', 'Q')
          end
        end

        def requires_percent_q?(source)
          style == :percent_q && source =~ /^%[^\w]/
        end

        def requires_bare_percent?(source)
          style == :bare_percent && source =~ /^%Q/
        end

        def add_offense_for_wrong_style(node, good, bad)
          add_offense(node, :begin, format(MSG, good, bad))
        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

18 entries across 18 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/bare_percent_literals.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/bare_percent_literals.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/bare_percent_literals.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/bare_percent_literals.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/bare_percent_literals.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/bare_percent_literals.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.50.0 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.49.1 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.49.0 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.48.1 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.48.0 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.47.1 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.47.0 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.46.0 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.45.0 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.44.1 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-0.44.0 lib/rubocop/cop/style/bare_percent_literals.rb