Sha256: e47d849247d136105cfe0b8c9e72141e69a1b47fa27f9586a9fbe41a5a65e73f

Contents?: true

Size: 1.92 KB

Versions: 191

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks if usage of %() or %Q() matches configuration.
      #
      # @example EnforcedStyle: bare_percent (default)
      #   # bad
      #   %Q(He said: "#{greeting}")
      #   %q{She said: 'Hi'}
      #
      #   # good
      #   %(He said: "#{greeting}")
      #   %{She said: 'Hi'}
      #
      # @example EnforcedStyle: percent_q
      #   # bad
      #   %|He said: "#{greeting}"|
      #   %/She said: 'Hi'/
      #
      #   # good
      #   %Q|He said: "#{greeting}"|
      #   %q/She said: 'Hi'/
      #
      class BarePercentLiterals < Base
        include ConfigurableEnforcedStyle
        extend AutoCorrector

        MSG = 'Use `%%%<good>s` instead of `%%%<bad>s`.'

        def on_dstr(node)
          check(node)
        end

        def on_str(node)
          check(node)
        end

        private

        def check(node)
          return if node.heredoc?
          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 && /^%[^\w]/.match?(source)
        end

        def requires_bare_percent?(source)
          style == :bare_percent && source.start_with?('%Q')
        end

        def add_offense_for_wrong_style(node, good, bad)
          location = node.loc.begin

          add_offense(location, message: format(MSG, good: good, bad: bad)) do |corrector|
            source = location.source
            replacement = source.start_with?('%Q') ? '%' : '%Q'

            corrector.replace(location, source.sub(/%Q?/, replacement))
          end
        end
      end
    end
  end
end

Version data entries

191 entries across 184 versions & 18 rubygems

Version Path
rubocop-1.70.0 lib/rubocop/cop/style/bare_percent_literals.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.69.2 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.69.1 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.69.0 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.68.0 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.67.0 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.66.1 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.66.0 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.65.1 lib/rubocop/cop/style/bare_percent_literals.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.65.0 lib/rubocop/cop/style/bare_percent_literals.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/bare_percent_literals.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/bare_percent_literals.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/bare_percent_literals.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.64.1 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.63.4 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.63.3 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.63.2 lib/rubocop/cop/style/bare_percent_literals.rb