Sha256: e47d849247d136105cfe0b8c9e72141e69a1b47fa27f9586a9fbe41a5a65e73f

Contents?: true

Size: 1.92 KB

Versions: 192

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

192 entries across 185 versions & 18 rubygems

Version Path
rubocop-1.63.2 lib/rubocop/cop/style/bare_percent_literals.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.63.1 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.63.0 lib/rubocop/cop/style/bare_percent_literals.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.62.1 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.62.0 lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.61.0 lib/rubocop/cop/style/bare_percent_literals.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.60.2 lib/rubocop/cop/style/bare_percent_literals.rb
study_line-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/bare_percent_literals.rb
study_line-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/bare_percent_literals.rb
study_line-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/bare_percent_literals.rb
study_line-0.2.4 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/bare_percent_literals.rb
study_line-0.2.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/bare_percent_literals.rb
study_line-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.60.1 lib/rubocop/cop/style/bare_percent_literals.rb
study_line-0.2.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/bare_percent_literals.rb
study_line-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/bare_percent_literals.rb
rubocop-1.60.0 lib/rubocop/cop/style/bare_percent_literals.rb