Sha256: f02b8cf158d8799d9e0e8819f8d6f8a934855b31d4feb3305d68843a6cd25781

Contents?: true

Size: 1.93 KB

Versions: 74

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop 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

74 entries across 74 versions & 8 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/bare_percent_literals.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.29.1 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.29.0 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.28.2 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.28.1 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.28.0 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.27.0 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.26.1 lib/rubocop/cop/style/bare_percent_literals.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.26.0 lib/rubocop/cop/style/bare_percent_literals.rb
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/bare_percent_literals.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.25.1 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.25.0 lib/rubocop/cop/style/bare_percent_literals.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/bare_percent_literals.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/bare_percent_literals.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.24.1 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.24.0 lib/rubocop/cop/style/bare_percent_literals.rb